PrettyPrinter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 || LO.C2x), NullptrTypeInNamespace(LO.CPlusPlus),
  65. Restrict(LO.C99), Alignof(LO.CPlusPlus11),
  66. UnderscoreAlignof(LO.C11), UseVoidForZeroParams(!LO.CPlusPlus),
  67. SplitTemplateClosers(!LO.CPlusPlus11), TerseOutput(false),
  68. PolishForDeclaration(false), Half(LO.Half),
  69. MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
  70. MSVCFormatting(false), ConstantsAsWritten(false),
  71. SuppressImplicitBase(false), FullyQualifiedName(false),
  72. PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
  73. UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
  74. CleanUglifiedParameters(false), EntireContentsOfLargeArray(true),
  75. UseEnumerators(true) {}
  76. /// Adjust this printing policy for cases where it's known that we're
  77. /// printing C++ code (for instance, if AST dumping reaches a C++-only
  78. /// construct). This should not be used if a real LangOptions object is
  79. /// available.
  80. void adjustForCPlusPlus() {
  81. SuppressTagKeyword = true;
  82. Bool = true;
  83. UseVoidForZeroParams = false;
  84. }
  85. /// The number of spaces to use to indent each line.
  86. unsigned Indentation : 8;
  87. /// Whether we should suppress printing of the actual specifiers for
  88. /// the given type or declaration.
  89. ///
  90. /// This flag is only used when we are printing declarators beyond
  91. /// the first declarator within a declaration group. For example, given:
  92. ///
  93. /// \code
  94. /// const int *x, *y;
  95. /// \endcode
  96. ///
  97. /// SuppressSpecifiers will be false when printing the
  98. /// declaration for "x", so that we will print "int *x"; it will be
  99. /// \c true when we print "y", so that we suppress printing the
  100. /// "const int" type specifier and instead only print the "*y".
  101. unsigned SuppressSpecifiers : 1;
  102. /// Whether type printing should skip printing the tag keyword.
  103. ///
  104. /// This is used when printing the inner type of elaborated types,
  105. /// (as the tag keyword is part of the elaborated type):
  106. ///
  107. /// \code
  108. /// struct Geometry::Point;
  109. /// \endcode
  110. unsigned SuppressTagKeyword : 1;
  111. /// When true, include the body of a tag definition.
  112. ///
  113. /// This is used to place the definition of a struct
  114. /// in the middle of another declaration as with:
  115. ///
  116. /// \code
  117. /// typedef struct { int x, y; } Point;
  118. /// \endcode
  119. unsigned IncludeTagDefinition : 1;
  120. /// Suppresses printing of scope specifiers.
  121. unsigned SuppressScope : 1;
  122. /// Suppress printing parts of scope specifiers that are never
  123. /// written, e.g., for anonymous namespaces.
  124. unsigned SuppressUnwrittenScope : 1;
  125. /// Suppress printing parts of scope specifiers that correspond
  126. /// to inline namespaces, where the name is unambiguous with the specifier
  127. /// removed.
  128. unsigned SuppressInlineNamespace : 1;
  129. /// Suppress printing of variable initializers.
  130. ///
  131. /// This flag is used when printing the loop variable in a for-range
  132. /// statement. For example, given:
  133. ///
  134. /// \code
  135. /// for (auto x : coll)
  136. /// \endcode
  137. ///
  138. /// SuppressInitializers will be true when printing "auto x", so that the
  139. /// internal initializer constructed for x will not be printed.
  140. unsigned SuppressInitializers : 1;
  141. /// Whether we should print the sizes of constant array expressions as written
  142. /// in the sources.
  143. ///
  144. /// This flag determines whether array types declared as
  145. ///
  146. /// \code
  147. /// int a[4+10*10];
  148. /// char a[] = "A string";
  149. /// \endcode
  150. ///
  151. /// will be printed as written or as follows:
  152. ///
  153. /// \code
  154. /// int a[104];
  155. /// char a[9] = "A string";
  156. /// \endcode
  157. unsigned ConstantArraySizeAsWritten : 1;
  158. /// When printing an anonymous tag name, also print the location of that
  159. /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
  160. /// "(anonymous)" for the name.
  161. unsigned AnonymousTagLocations : 1;
  162. /// When true, suppress printing of the __strong lifetime qualifier in ARC.
  163. unsigned SuppressStrongLifetime : 1;
  164. /// When true, suppress printing of lifetime qualifier in ARC.
  165. unsigned SuppressLifetimeQualifiers : 1;
  166. /// When true, suppresses printing template arguments in names of C++
  167. /// constructors.
  168. unsigned SuppressTemplateArgsInCXXConstructors : 1;
  169. /// When true, attempt to suppress template arguments that match the default
  170. /// argument for the parameter.
  171. unsigned SuppressDefaultTemplateArgs : 1;
  172. /// Whether we can use 'bool' rather than '_Bool' (even if the language
  173. /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
  174. unsigned Bool : 1;
  175. /// Whether we should use 'nullptr' rather than '0' as a null pointer
  176. /// constant.
  177. unsigned Nullptr : 1;
  178. /// Whether 'nullptr_t' is in namespace 'std' or not.
  179. unsigned NullptrTypeInNamespace : 1;
  180. /// Whether we can use 'restrict' rather than '__restrict'.
  181. unsigned Restrict : 1;
  182. /// Whether we can use 'alignof' rather than '__alignof'.
  183. unsigned Alignof : 1;
  184. /// Whether we can use '_Alignof' rather than '__alignof'.
  185. unsigned UnderscoreAlignof : 1;
  186. /// Whether we should use '(void)' rather than '()' for a function prototype
  187. /// with zero parameters.
  188. unsigned UseVoidForZeroParams : 1;
  189. /// Whether nested templates must be closed like 'a\<b\<c\> \>' rather than
  190. /// 'a\<b\<c\>\>'.
  191. unsigned SplitTemplateClosers : 1;
  192. /// Provide a 'terse' output.
  193. ///
  194. /// For example, in this mode we don't print function bodies, class members,
  195. /// declarations inside namespaces etc. Effectively, this should print
  196. /// only the requested declaration.
  197. unsigned TerseOutput : 1;
  198. /// When true, do certain refinement needed for producing proper declaration
  199. /// tag; such as, do not print attributes attached to the declaration.
  200. ///
  201. unsigned PolishForDeclaration : 1;
  202. /// When true, print the half-precision floating-point type as 'half'
  203. /// instead of '__fp16'
  204. unsigned Half : 1;
  205. /// When true, print the built-in wchar_t type as __wchar_t. For use in
  206. /// Microsoft mode when wchar_t is not available.
  207. unsigned MSWChar : 1;
  208. /// When true, include newlines after statements like "break", etc.
  209. unsigned IncludeNewlines : 1;
  210. /// Use whitespace and punctuation like MSVC does. In particular, this prints
  211. /// anonymous namespaces as `anonymous namespace' and does not insert spaces
  212. /// after template arguments.
  213. unsigned MSVCFormatting : 1;
  214. /// Whether we should print the constant expressions as written in the
  215. /// sources.
  216. ///
  217. /// This flag determines whether constants expressions like
  218. ///
  219. /// \code
  220. /// 0x10
  221. /// 2.5e3
  222. /// \endcode
  223. ///
  224. /// will be printed as written or as follows:
  225. ///
  226. /// \code
  227. /// 0x10
  228. /// 2.5e3
  229. /// \endcode
  230. unsigned ConstantsAsWritten : 1;
  231. /// When true, don't print the implicit 'self' or 'this' expressions.
  232. unsigned SuppressImplicitBase : 1;
  233. /// When true, print the fully qualified name of function declarations.
  234. /// This is the opposite of SuppressScope and thus overrules it.
  235. unsigned FullyQualifiedName : 1;
  236. /// Whether to print types as written or canonically.
  237. unsigned PrintCanonicalTypes : 1;
  238. /// Whether to print an InjectedClassNameType with template arguments or as
  239. /// written. When a template argument is unnamed, printing it results in
  240. /// invalid C++ code.
  241. unsigned PrintInjectedClassNameWithArguments : 1;
  242. /// Whether to use C++ template preferred_name attributes when printing
  243. /// templates.
  244. unsigned UsePreferredNames : 1;
  245. /// Whether to use type suffixes (eg: 1U) on integral non-type template
  246. /// parameters.
  247. unsigned AlwaysIncludeTypeForTemplateArgument : 1;
  248. /// Whether to strip underscores when printing reserved parameter names.
  249. /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
  250. /// This only affects parameter names, and so describes a compatible API.
  251. unsigned CleanUglifiedParameters : 1;
  252. /// Whether to print the entire array initializers, especially on non-type
  253. /// template parameters, no matter how many elements there are.
  254. unsigned EntireContentsOfLargeArray : 1;
  255. /// Whether to print enumerator non-type template parameters with a matching
  256. /// enumerator name or via cast of an integer.
  257. unsigned UseEnumerators : 1;
  258. /// Callbacks to use to allow the behavior of printing to be customized.
  259. const PrintingCallbacks *Callbacks = nullptr;
  260. };
  261. } // end namespace clang
  262. #endif
  263. #ifdef __GNUC__
  264. #pragma GCC diagnostic pop
  265. #endif