PrettyPrinter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- PrettyPrinter.h - Classes for aiding with AST printing -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines helper types for AST pretty-printing.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_PRETTYPRINTER_H
  18. #define LLVM_CLANG_AST_PRETTYPRINTER_H
  19. #include "clang/Basic/LLVM.h"
  20. #include "clang/Basic/LangOptions.h"
  21. namespace clang {
  22. class DeclContext;
  23. class LangOptions;
  24. class Stmt;
  25. class PrinterHelper {
  26. public:
  27. virtual ~PrinterHelper();
  28. virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
  29. };
  30. /// Callbacks to use to customize the behavior of the pretty-printer.
  31. class PrintingCallbacks {
  32. protected:
  33. ~PrintingCallbacks() = default;
  34. public:
  35. /// Remap a path to a form suitable for printing.
  36. virtual std::string remapPath(StringRef Path) const {
  37. return std::string(Path);
  38. }
  39. /// When printing type to be inserted into code in specific context, this
  40. /// callback can be used to avoid printing the redundant part of the
  41. /// qualifier. For example, when inserting code inside namespace foo, we
  42. /// should print bar::SomeType instead of foo::bar::SomeType.
  43. /// To do this, shouldPrintScope should return true on "foo" NamespaceDecl.
  44. /// The printing stops at the first isScopeVisible() == true, so there will
  45. /// be no calls with outer scopes.
  46. virtual bool isScopeVisible(const DeclContext *DC) const { return false; }
  47. };
  48. /// Describes how types, statements, expressions, and declarations should be
  49. /// printed.
  50. ///
  51. /// This type is intended to be small and suitable for passing by value.
  52. /// It is very frequently copied.
  53. struct PrintingPolicy {
  54. /// Create a default printing policy for the specified language.
  55. PrintingPolicy(const LangOptions &LO)
  56. : Indentation(2), SuppressSpecifiers(false),
  57. SuppressTagKeyword(LO.CPlusPlus), IncludeTagDefinition(false),
  58. SuppressScope(false), SuppressUnwrittenScope(false),
  59. SuppressInlineNamespace(true), SuppressInitializers(false),
  60. ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
  61. SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false),
  62. SuppressTemplateArgsInCXXConstructors(false),
  63. SuppressDefaultTemplateArgs(true), Bool(LO.Bool),
  64. Nullptr(LO.CPlusPlus11), Restrict(LO.C99), Alignof(LO.CPlusPlus11),
  65. UnderscoreAlignof(LO.C11), UseVoidForZeroParams(!LO.CPlusPlus),
  66. SplitTemplateClosers(!LO.CPlusPlus11), TerseOutput(false),
  67. PolishForDeclaration(false), Half(LO.Half),
  68. MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
  69. MSVCFormatting(false), ConstantsAsWritten(false),
  70. SuppressImplicitBase(false), FullyQualifiedName(false),
  71. PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
  72. UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
  73. CleanUglifiedParameters(false) {}
  74. /// Adjust this printing policy for cases where it's known that we're
  75. /// printing C++ code (for instance, if AST dumping reaches a C++-only
  76. /// construct). This should not be used if a real LangOptions object is
  77. /// available.
  78. void adjustForCPlusPlus() {
  79. SuppressTagKeyword = true;
  80. Bool = true;
  81. UseVoidForZeroParams = false;
  82. }
  83. /// The number of spaces to use to indent each line.
  84. unsigned Indentation : 8;
  85. /// Whether we should suppress printing of the actual specifiers for
  86. /// the given type or declaration.
  87. ///
  88. /// This flag is only used when we are printing declarators beyond
  89. /// the first declarator within a declaration group. For example, given:
  90. ///
  91. /// \code
  92. /// const int *x, *y;
  93. /// \endcode
  94. ///
  95. /// SuppressSpecifiers will be false when printing the
  96. /// declaration for "x", so that we will print "int *x"; it will be
  97. /// \c true when we print "y", so that we suppress printing the
  98. /// "const int" type specifier and instead only print the "*y".
  99. unsigned SuppressSpecifiers : 1;
  100. /// Whether type printing should skip printing the tag keyword.
  101. ///
  102. /// This is used when printing the inner type of elaborated types,
  103. /// (as the tag keyword is part of the elaborated type):
  104. ///
  105. /// \code
  106. /// struct Geometry::Point;
  107. /// \endcode
  108. unsigned SuppressTagKeyword : 1;
  109. /// When true, include the body of a tag definition.
  110. ///
  111. /// This is used to place the definition of a struct
  112. /// in the middle of another declaration as with:
  113. ///
  114. /// \code
  115. /// typedef struct { int x, y; } Point;
  116. /// \endcode
  117. unsigned IncludeTagDefinition : 1;
  118. /// Suppresses printing of scope specifiers.
  119. unsigned SuppressScope : 1;
  120. /// Suppress printing parts of scope specifiers that are never
  121. /// written, e.g., for anonymous namespaces.
  122. unsigned SuppressUnwrittenScope : 1;
  123. /// Suppress printing parts of scope specifiers that correspond
  124. /// to inline namespaces, where the name is unambiguous with the specifier
  125. /// removed.
  126. unsigned SuppressInlineNamespace : 1;
  127. /// Suppress printing of variable initializers.
  128. ///
  129. /// This flag is used when printing the loop variable in a for-range
  130. /// statement. For example, given:
  131. ///
  132. /// \code
  133. /// for (auto x : coll)
  134. /// \endcode
  135. ///
  136. /// SuppressInitializers will be true when printing "auto x", so that the
  137. /// internal initializer constructed for x will not be printed.
  138. unsigned SuppressInitializers : 1;
  139. /// Whether we should print the sizes of constant array expressions as written
  140. /// in the sources.
  141. ///
  142. /// This flag determines whether array types declared as
  143. ///
  144. /// \code
  145. /// int a[4+10*10];
  146. /// char a[] = "A string";
  147. /// \endcode
  148. ///
  149. /// will be printed as written or as follows:
  150. ///
  151. /// \code
  152. /// int a[104];
  153. /// char a[9] = "A string";
  154. /// \endcode
  155. unsigned ConstantArraySizeAsWritten : 1;
  156. /// When printing an anonymous tag name, also print the location of that
  157. /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
  158. /// "(anonymous)" for the name.
  159. unsigned AnonymousTagLocations : 1;
  160. /// When true, suppress printing of the __strong lifetime qualifier in ARC.
  161. unsigned SuppressStrongLifetime : 1;
  162. /// When true, suppress printing of lifetime qualifier in ARC.
  163. unsigned SuppressLifetimeQualifiers : 1;
  164. /// When true, suppresses printing template arguments in names of C++
  165. /// constructors.
  166. unsigned SuppressTemplateArgsInCXXConstructors : 1;
  167. /// When true, attempt to suppress template arguments that match the default
  168. /// argument for the parameter.
  169. unsigned SuppressDefaultTemplateArgs : 1;
  170. /// Whether we can use 'bool' rather than '_Bool' (even if the language
  171. /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
  172. unsigned Bool : 1;
  173. /// Whether we should use 'nullptr' rather than '0' as a null pointer
  174. /// constant.
  175. unsigned Nullptr : 1;
  176. /// Whether we can use 'restrict' rather than '__restrict'.
  177. unsigned Restrict : 1;
  178. /// Whether we can use 'alignof' rather than '__alignof'.
  179. unsigned Alignof : 1;
  180. /// Whether we can use '_Alignof' rather than '__alignof'.
  181. unsigned UnderscoreAlignof : 1;
  182. /// Whether we should use '(void)' rather than '()' for a function prototype
  183. /// with zero parameters.
  184. unsigned UseVoidForZeroParams : 1;
  185. /// Whether nested templates must be closed like 'a\<b\<c\> \>' rather than
  186. /// 'a\<b\<c\>\>'.
  187. unsigned SplitTemplateClosers : 1;
  188. /// Provide a 'terse' output.
  189. ///
  190. /// For example, in this mode we don't print function bodies, class members,
  191. /// declarations inside namespaces etc. Effectively, this should print
  192. /// only the requested declaration.
  193. unsigned TerseOutput : 1;
  194. /// When true, do certain refinement needed for producing proper declaration
  195. /// tag; such as, do not print attributes attached to the declaration.
  196. ///
  197. unsigned PolishForDeclaration : 1;
  198. /// When true, print the half-precision floating-point type as 'half'
  199. /// instead of '__fp16'
  200. unsigned Half : 1;
  201. /// When true, print the built-in wchar_t type as __wchar_t. For use in
  202. /// Microsoft mode when wchar_t is not available.
  203. unsigned MSWChar : 1;
  204. /// When true, include newlines after statements like "break", etc.
  205. unsigned IncludeNewlines : 1;
  206. /// Use whitespace and punctuation like MSVC does. In particular, this prints
  207. /// anonymous namespaces as `anonymous namespace' and does not insert spaces
  208. /// after template arguments.
  209. unsigned MSVCFormatting : 1;
  210. /// Whether we should print the constant expressions as written in the
  211. /// sources.
  212. ///
  213. /// This flag determines whether constants expressions like
  214. ///
  215. /// \code
  216. /// 0x10
  217. /// 2.5e3
  218. /// \endcode
  219. ///
  220. /// will be printed as written or as follows:
  221. ///
  222. /// \code
  223. /// 0x10
  224. /// 2.5e3
  225. /// \endcode
  226. unsigned ConstantsAsWritten : 1;
  227. /// When true, don't print the implicit 'self' or 'this' expressions.
  228. unsigned SuppressImplicitBase : 1;
  229. /// When true, print the fully qualified name of function declarations.
  230. /// This is the opposite of SuppressScope and thus overrules it.
  231. unsigned FullyQualifiedName : 1;
  232. /// Whether to print types as written or canonically.
  233. unsigned PrintCanonicalTypes : 1;
  234. /// Whether to print an InjectedClassNameType with template arguments or as
  235. /// written. When a template argument is unnamed, printing it results in
  236. /// invalid C++ code.
  237. unsigned PrintInjectedClassNameWithArguments : 1;
  238. /// Whether to use C++ template preferred_name attributes when printing
  239. /// templates.
  240. unsigned UsePreferredNames : 1;
  241. /// Whether to use type suffixes (eg: 1U) on integral non-type template
  242. /// parameters.
  243. unsigned AlwaysIncludeTypeForTemplateArgument : 1;
  244. /// Whether to strip underscores when printing reserved parameter names.
  245. /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
  246. /// This only affects parameter names, and so describes a compatible API.
  247. unsigned CleanUglifiedParameters : 1;
  248. /// Callbacks to use to allow the behavior of printing to be customized.
  249. const PrintingCallbacks *Callbacks = nullptr;
  250. };
  251. } // end namespace clang
  252. #endif
  253. #ifdef __GNUC__
  254. #pragma GCC diagnostic pop
  255. #endif