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 LangOptions;
22 class SourceManager;
23 class Stmt;
24 class TagDecl;
25 
26 class PrinterHelper {
27 public:
28   virtual ~PrinterHelper();
29   virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
30 };
31 
32 /// Callbacks to use to customize the behavior of the pretty-printer.
33 class PrintingCallbacks {
34 protected:
35   ~PrintingCallbacks() = default;
36 
37 public:
38   /// Remap a path to a form suitable for printing.
39   virtual std::string remapPath(StringRef Path) const {
40     return std::string(Path);
41   }
42 };
43 
44 /// Describes how types, statements, expressions, and declarations should be
45 /// printed.
46 ///
47 /// This type is intended to be small and suitable for passing by value.
48 /// It is very frequently copied.
49 struct PrintingPolicy {
50   /// Create a default printing policy for the specified language.
51   PrintingPolicy(const LangOptions &LO)
52       : Indentation(2), SuppressSpecifiers(false),
53         SuppressTagKeyword(LO.CPlusPlus), IncludeTagDefinition(false),
54         SuppressScope(false), SuppressUnwrittenScope(false),
55         SuppressInitializers(false), ConstantArraySizeAsWritten(false),
56         AnonymousTagLocations(true), SuppressStrongLifetime(false),
57         SuppressLifetimeQualifiers(false),
58         SuppressTemplateArgsInCXXConstructors(false), Bool(LO.Bool),
59         Restrict(LO.C99), Alignof(LO.CPlusPlus11), UnderscoreAlignof(LO.C11),
60         UseVoidForZeroParams(!LO.CPlusPlus),
61         SplitTemplateClosers(!LO.CPlusPlus11), TerseOutput(false),
62         PolishForDeclaration(false), Half(LO.Half),
63         MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
64         MSVCFormatting(false), ConstantsAsWritten(false),
65         SuppressImplicitBase(false), FullyQualifiedName(false),
66         PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true) {}
67 
68   /// Adjust this printing policy for cases where it's known that we're
69   /// printing C++ code (for instance, if AST dumping reaches a C++-only
70   /// construct). This should not be used if a real LangOptions object is
71   /// available.
72   void adjustForCPlusPlus() {
73     SuppressTagKeyword = true;
74     Bool = true;
75     UseVoidForZeroParams = false;
76   }
77 
78   /// The number of spaces to use to indent each line.
79   unsigned Indentation : 8;
80 
81   /// Whether we should suppress printing of the actual specifiers for
82   /// the given type or declaration.
83   ///
84   /// This flag is only used when we are printing declarators beyond
85   /// the first declarator within a declaration group. For example, given:
86   ///
87   /// \code
88   /// const int *x, *y;
89   /// \endcode
90   ///
91   /// SuppressSpecifiers will be false when printing the
92   /// declaration for "x", so that we will print "int *x"; it will be
93   /// \c true when we print "y", so that we suppress printing the
94   /// "const int" type specifier and instead only print the "*y".
95   unsigned SuppressSpecifiers : 1;
96 
97   /// Whether type printing should skip printing the tag keyword.
98   ///
99   /// This is used when printing the inner type of elaborated types,
100   /// (as the tag keyword is part of the elaborated type):
101   ///
102   /// \code
103   /// struct Geometry::Point;
104   /// \endcode
105   unsigned SuppressTagKeyword : 1;
106 
107   /// When true, include the body of a tag definition.
108   ///
109   /// This is used to place the definition of a struct
110   /// in the middle of another declaration as with:
111   ///
112   /// \code
113   /// typedef struct { int x, y; } Point;
114   /// \endcode
115   unsigned IncludeTagDefinition : 1;
116 
117   /// Suppresses printing of scope specifiers.
118   unsigned SuppressScope : 1;
119 
120   /// Suppress printing parts of scope specifiers that don't need
121   /// to be written, e.g., for inline or anonymous namespaces.
122   unsigned SuppressUnwrittenScope : 1;
123 
124   /// Suppress printing of variable initializers.
125   ///
126   /// This flag is used when printing the loop variable in a for-range
127   /// statement. For example, given:
128   ///
129   /// \code
130   /// for (auto x : coll)
131   /// \endcode
132   ///
133   /// SuppressInitializers will be true when printing "auto x", so that the
134   /// internal initializer constructed for x will not be printed.
135   unsigned SuppressInitializers : 1;
136 
137   /// Whether we should print the sizes of constant array expressions as written
138   /// in the sources.
139   ///
140   /// This flag determines whether array types declared as
141   ///
142   /// \code
143   /// int a[4+10*10];
144   /// char a[] = "A string";
145   /// \endcode
146   ///
147   /// will be printed as written or as follows:
148   ///
149   /// \code
150   /// int a[104];
151   /// char a[9] = "A string";
152   /// \endcode
153   unsigned ConstantArraySizeAsWritten : 1;
154 
155   /// When printing an anonymous tag name, also print the location of that
156   /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
157   /// "(anonymous)" for the name.
158   unsigned AnonymousTagLocations : 1;
159 
160   /// When true, suppress printing of the __strong lifetime qualifier in ARC.
161   unsigned SuppressStrongLifetime : 1;
162 
163   /// When true, suppress printing of lifetime qualifier in ARC.
164   unsigned SuppressLifetimeQualifiers : 1;
165 
166   /// When true, suppresses printing template arguments in names of C++
167   /// constructors.
168   unsigned SuppressTemplateArgsInCXXConstructors : 1;
169 
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 
174   /// Whether we can use 'restrict' rather than '__restrict'.
175   unsigned Restrict : 1;
176 
177   /// Whether we can use 'alignof' rather than '__alignof'.
178   unsigned Alignof : 1;
179 
180   /// Whether we can use '_Alignof' rather than '__alignof'.
181   unsigned UnderscoreAlignof : 1;
182 
183   /// Whether we should use '(void)' rather than '()' for a function prototype
184   /// with zero parameters.
185   unsigned UseVoidForZeroParams : 1;
186 
187   /// Whether nested templates must be closed like 'a\<b\<c\> \>' rather than
188   /// 'a\<b\<c\>\>'.
189   unsigned SplitTemplateClosers : 1;
190 
191   /// Provide a 'terse' output.
192   ///
193   /// For example, in this mode we don't print function bodies, class members,
194   /// declarations inside namespaces etc.  Effectively, this should print
195   /// only the requested declaration.
196   unsigned TerseOutput : 1;
197 
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 
203   /// When true, print the half-precision floating-point type as 'half'
204   /// instead of '__fp16'
205   unsigned Half : 1;
206 
207   /// When true, print the built-in wchar_t type as __wchar_t. For use in
208   /// Microsoft mode when wchar_t is not available.
209   unsigned MSWChar : 1;
210 
211   /// When true, include newlines after statements like "break", etc.
212   unsigned IncludeNewlines : 1;
213 
214   /// Use whitespace and punctuation like MSVC does. In particular, this prints
215   /// anonymous namespaces as `anonymous namespace' and does not insert spaces
216   /// after template arguments.
217   unsigned MSVCFormatting : 1;
218 
219   /// Whether we should print the constant expressions as written in the
220   /// sources.
221   ///
222   /// This flag determines whether constants expressions like
223   ///
224   /// \code
225   /// 0x10
226   /// 2.5e3
227   /// \endcode
228   ///
229   /// will be printed as written or as follows:
230   ///
231   /// \code
232   /// 0x10
233   /// 2.5e3
234   /// \endcode
235   unsigned ConstantsAsWritten : 1;
236 
237   /// When true, don't print the implicit 'self' or 'this' expressions.
238   unsigned SuppressImplicitBase : 1;
239 
240   /// When true, print the fully qualified name of function declarations.
241   /// This is the opposite of SuppressScope and thus overrules it.
242   unsigned FullyQualifiedName : 1;
243 
244   /// Whether to print types as written or canonically.
245   unsigned PrintCanonicalTypes : 1;
246 
247   /// Whether to print an InjectedClassNameType with template arguments or as
248   /// written. When a template argument is unnamed, printing it results in
249   /// invalid C++ code.
250   unsigned PrintInjectedClassNameWithArguments : 1;
251 
252   /// Callbacks to use to allow the behavior of printing to be customized.
253   const PrintingCallbacks *Callbacks = nullptr;
254 };
255 
256 } // end namespace clang
257 
258 #endif
259