clang 20.0.0git
PrettyPrinter.h
Go to the documentation of this file.
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"
18
19namespace clang {
20
21class DeclContext;
22class LangOptions;
23class Stmt;
24
26public:
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.
33protected:
34 ~PrintingCallbacks() = default;
35
36public:
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.
58 /// Create a default printing policy for the specified language.
74 MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(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.
87 SuppressTagKeyword = true;
88 Bool = true;
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 LLVM_PREFERRED_TYPE(bool)
111
112 /// Whether type printing should skip printing the tag keyword.
113 ///
114 /// This is used when printing the inner type of elaborated types,
115 /// (as the tag keyword is part of the elaborated type):
116 ///
117 /// \code
118 /// struct Geometry::Point;
119 /// \endcode
120 LLVM_PREFERRED_TYPE(bool)
121 unsigned SuppressTagKeyword : 1;
122
123 /// When true, include the body of a tag definition.
124 ///
125 /// This is used to place the definition of a struct
126 /// in the middle of another declaration as with:
127 ///
128 /// \code
129 /// typedef struct { int x, y; } Point;
130 /// \endcode
131 LLVM_PREFERRED_TYPE(bool)
133
134 /// Suppresses printing of scope specifiers.
135 LLVM_PREFERRED_TYPE(bool)
136 unsigned SuppressScope : 1;
137
138 /// Suppress printing parts of scope specifiers that are never
139 /// written, e.g., for anonymous namespaces.
140 LLVM_PREFERRED_TYPE(bool)
142
143 /// Suppress printing parts of scope specifiers that correspond
144 /// to inline namespaces, where the name is unambiguous with the specifier
145 /// removed.
146 LLVM_PREFERRED_TYPE(bool)
148
149 /// Ignore qualifiers and tag keywords as specified by elaborated type sugar,
150 /// instead letting the underlying type print as normal.
151 LLVM_PREFERRED_TYPE(bool)
153
154 /// Suppress printing of variable initializers.
155 ///
156 /// This flag is used when printing the loop variable in a for-range
157 /// statement. For example, given:
158 ///
159 /// \code
160 /// for (auto x : coll)
161 /// \endcode
162 ///
163 /// SuppressInitializers will be true when printing "auto x", so that the
164 /// internal initializer constructed for x will not be printed.
165 LLVM_PREFERRED_TYPE(bool)
167
168 /// Whether we should print the sizes of constant array expressions as written
169 /// in the sources.
170 ///
171 /// This flag determines whether array types declared as
172 ///
173 /// \code
174 /// int a[4+10*10];
175 /// char a[] = "A string";
176 /// \endcode
177 ///
178 /// will be printed as written or as follows:
179 ///
180 /// \code
181 /// int a[104];
182 /// char a[9] = "A string";
183 /// \endcode
184 LLVM_PREFERRED_TYPE(bool)
186
187 /// When printing an anonymous tag name, also print the location of that
188 /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
189 /// "(anonymous)" for the name.
190 LLVM_PREFERRED_TYPE(bool)
192
193 /// When true, suppress printing of the __strong lifetime qualifier in ARC.
194 LLVM_PREFERRED_TYPE(bool)
196
197 /// When true, suppress printing of lifetime qualifier in ARC.
198 LLVM_PREFERRED_TYPE(bool)
200
201 /// When true, suppresses printing template arguments in names of C++
202 /// constructors.
203 LLVM_PREFERRED_TYPE(bool)
205
206 /// When true, attempt to suppress template arguments that match the default
207 /// argument for the parameter.
208 LLVM_PREFERRED_TYPE(bool)
210
211 /// Whether we can use 'bool' rather than '_Bool' (even if the language
212 /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
213 LLVM_PREFERRED_TYPE(bool)
214 unsigned Bool : 1;
215
216 /// Whether we should use 'nullptr' rather than '0' as a null pointer
217 /// constant.
218 LLVM_PREFERRED_TYPE(bool)
219 unsigned Nullptr : 1;
220
221 /// Whether 'nullptr_t' is in namespace 'std' or not.
222 LLVM_PREFERRED_TYPE(bool)
224
225 /// Whether we can use 'restrict' rather than '__restrict'.
226 LLVM_PREFERRED_TYPE(bool)
227 unsigned Restrict : 1;
228
229 /// Whether we can use 'alignof' rather than '__alignof'.
230 LLVM_PREFERRED_TYPE(bool)
231 unsigned Alignof : 1;
232
233 /// Whether we can use '_Alignof' rather than '__alignof'.
234 LLVM_PREFERRED_TYPE(bool)
235 unsigned UnderscoreAlignof : 1;
236
237 /// Whether we should use '(void)' rather than '()' for a function prototype
238 /// with zero parameters.
239 LLVM_PREFERRED_TYPE(bool)
241
242 /// Whether nested templates must be closed like 'a<b<c> >' rather than
243 /// 'a<b<c>>'.
244 LLVM_PREFERRED_TYPE(bool)
246
247 /// Provide a 'terse' output.
248 ///
249 /// For example, in this mode we don't print function bodies, class members,
250 /// declarations inside namespaces etc. Effectively, this should print
251 /// only the requested declaration.
252 LLVM_PREFERRED_TYPE(bool)
253 unsigned TerseOutput : 1;
254
255 /// When true, do certain refinement needed for producing proper declaration
256 /// tag; such as, do not print attributes attached to the declaration.
257 ///
258 LLVM_PREFERRED_TYPE(bool)
260
261 /// When true, print the half-precision floating-point type as 'half'
262 /// instead of '__fp16'
263 LLVM_PREFERRED_TYPE(bool)
264 unsigned Half : 1;
265
266 /// When true, print the built-in wchar_t type as __wchar_t. For use in
267 /// Microsoft mode when wchar_t is not available.
268 LLVM_PREFERRED_TYPE(bool)
269 unsigned MSWChar : 1;
270
271 /// When true, include newlines after statements like "break", etc.
272 LLVM_PREFERRED_TYPE(bool)
273 unsigned IncludeNewlines : 1;
274
275 /// Use whitespace and punctuation like MSVC does. In particular, this prints
276 /// anonymous namespaces as `anonymous namespace' and does not insert spaces
277 /// after template arguments.
278 LLVM_PREFERRED_TYPE(bool)
279 unsigned MSVCFormatting : 1;
280
281 /// Whether we should print the constant expressions as written in the
282 /// sources.
283 ///
284 /// This flag determines whether constants expressions like
285 ///
286 /// \code
287 /// 0x10
288 /// 2.5e3
289 /// \endcode
290 ///
291 /// will be printed as written or as follows:
292 ///
293 /// \code
294 /// 0x10
295 /// 2.5e3
296 /// \endcode
297 LLVM_PREFERRED_TYPE(bool)
298 unsigned ConstantsAsWritten : 1;
299
300 /// When true, don't print the implicit 'self' or 'this' expressions.
301 LLVM_PREFERRED_TYPE(bool)
303
304 /// When true, print the fully qualified name of function declarations.
305 /// This is the opposite of SuppressScope and thus overrules it.
306 LLVM_PREFERRED_TYPE(bool)
307 unsigned FullyQualifiedName : 1;
308
309 /// Whether to print types as written or canonically.
310 LLVM_PREFERRED_TYPE(bool)
312
313 /// Whether to print an InjectedClassNameType with template arguments or as
314 /// written. When a template argument is unnamed, printing it results in
315 /// invalid C++ code.
316 LLVM_PREFERRED_TYPE(bool)
318
319 /// Whether to use C++ template preferred_name attributes when printing
320 /// templates.
321 LLVM_PREFERRED_TYPE(bool)
322 unsigned UsePreferredNames : 1;
323
324 /// Whether to use type suffixes (eg: 1U) on integral non-type template
325 /// parameters.
326 LLVM_PREFERRED_TYPE(bool)
328
329 /// Whether to strip underscores when printing reserved parameter names.
330 /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
331 /// This only affects parameter names, and so describes a compatible API.
332 LLVM_PREFERRED_TYPE(bool)
334
335 /// Whether to print the entire array initializers, especially on non-type
336 /// template parameters, no matter how many elements there are.
337 LLVM_PREFERRED_TYPE(bool)
339
340 /// Whether to print enumerator non-type template parameters with a matching
341 /// enumerator name or via cast of an integer.
342 LLVM_PREFERRED_TYPE(bool)
343 unsigned UseEnumerators : 1;
344
345 /// Whether or not we're printing known HLSL code and should print HLSL
346 /// sugared types when possible.
347 LLVM_PREFERRED_TYPE(bool)
348 unsigned UseHLSLTypes : 1;
349
350 /// Callbacks to use to allow the behavior of printing to be customized.
351 const PrintingCallbacks *Callbacks = nullptr;
352};
353
354} // end namespace clang
355
356#endif
IndirectLocalPath & Path
Expr * E
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1436
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:476
virtual bool handledStmt(Stmt *E, raw_ostream &OS)=0
virtual ~PrinterHelper()
Callbacks to use to customize the behavior of the pretty-printer.
Definition: PrettyPrinter.h:32
virtual std::string remapPath(StringRef Path) const
Remap a path to a form suitable for printing.
Definition: PrettyPrinter.h:38
virtual bool isScopeVisible(const DeclContext *DC) const
When printing type to be inserted into code in specific context, this callback can be used to avoid p...
Definition: PrettyPrinter.h:49
Stmt - This represents one statement.
Definition: Stmt.h:84
The JSON file list parser is used to communicate input to InstallAPI.
@ CPlusPlus
Definition: LangStandard.h:56
@ CPlusPlus11
Definition: LangStandard.h:57
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
unsigned SuppressUnwrittenScope
Suppress printing parts of scope specifiers that are never written, e.g., for anonymous namespaces.
unsigned FullyQualifiedName
When true, print the fully qualified name of function declarations.
unsigned MSVCFormatting
Use whitespace and punctuation like MSVC does.
unsigned SuppressDefaultTemplateArgs
When true, attempt to suppress template arguments that match the default argument for the parameter.
unsigned SplitTemplateClosers
Whether nested templates must be closed like 'a<b<c> >' rather than 'a<b<c>>'.
unsigned PrintCanonicalTypes
Whether to print types as written or canonically.
unsigned PolishForDeclaration
When true, do certain refinement needed for producing proper declaration tag; such as,...
unsigned PrintInjectedClassNameWithArguments
Whether to print an InjectedClassNameType with template arguments or as written.
void adjustForCPlusPlus()
Adjust this printing policy for cases where it's known that we're printing C++ code (for instance,...
Definition: PrettyPrinter.h:86
unsigned UseVoidForZeroParams
Whether we should use '(void)' rather than '()' for a function prototype with zero parameters.
unsigned Alignof
Whether we can use 'alignof' rather than '__alignof'.
unsigned AnonymousTagLocations
When printing an anonymous tag name, also print the location of that entity (e.g.,...
unsigned SuppressElaboration
Ignore qualifiers and tag keywords as specified by elaborated type sugar, instead letting the underly...
unsigned CleanUglifiedParameters
Whether to strip underscores when printing reserved parameter names.
unsigned SuppressSpecifiers
Whether we should suppress printing of the actual specifiers for the given type or declaration.
unsigned Nullptr
Whether we should use 'nullptr' rather than '0' as a null pointer constant.
unsigned ConstantArraySizeAsWritten
Whether we should print the sizes of constant array expressions as written in the sources.
unsigned SuppressTagKeyword
Whether type printing should skip printing the tag keyword.
unsigned AlwaysIncludeTypeForTemplateArgument
Whether to use type suffixes (eg: 1U) on integral non-type template parameters.
unsigned Bool
Whether we can use 'bool' rather than '_Bool' (even if the language doesn't actually have 'bool',...
unsigned UsePreferredNames
Whether to use C++ template preferred_name attributes when printing templates.
unsigned SuppressStrongLifetime
When true, suppress printing of the __strong lifetime qualifier in ARC.
unsigned Restrict
Whether we can use 'restrict' rather than '__restrict'.
unsigned UseEnumerators
Whether to print enumerator non-type template parameters with a matching enumerator name or via cast ...
unsigned UseHLSLTypes
Whether or not we're printing known HLSL code and should print HLSL sugared types when possible.
unsigned NullptrTypeInNamespace
Whether 'nullptr_t' is in namespace 'std' or not.
unsigned SuppressInlineNamespace
Suppress printing parts of scope specifiers that correspond to inline namespaces, where the name is u...
unsigned SuppressScope
Suppresses printing of scope specifiers.
unsigned EntireContentsOfLargeArray
Whether to print the entire array initializers, especially on non-type template parameters,...
const PrintingCallbacks * Callbacks
Callbacks to use to allow the behavior of printing to be customized.
unsigned ConstantsAsWritten
Whether we should print the constant expressions as written in the sources.
unsigned IncludeNewlines
When true, include newlines after statements like "break", etc.
unsigned Indentation
The number of spaces to use to indent each line.
Definition: PrettyPrinter.h:93
unsigned Half
When true, print the half-precision floating-point type as 'half' instead of '__fp16'.
PrintingPolicy(const LangOptions &LO)
Create a default printing policy for the specified language.
Definition: PrettyPrinter.h:59
unsigned SuppressInitializers
Suppress printing of variable initializers.
unsigned IncludeTagDefinition
When true, include the body of a tag definition.
unsigned TerseOutput
Provide a 'terse' output.
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
unsigned UnderscoreAlignof
Whether we can use '_Alignof' rather than '__alignof'.
unsigned MSWChar
When true, print the built-in wchar_t type as __wchar_t.
unsigned SuppressImplicitBase
When true, don't print the implicit 'self' or 'this' expressions.
unsigned SuppressLifetimeQualifiers
When true, suppress printing of lifetime qualifier in ARC.