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