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