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