clang 24.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#include "llvm/ADT/STLForwardCompat.h"
19
20namespace clang {
21
22class DeclContext;
23class LangOptions;
24class Stmt;
25
27public:
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.
34protected:
35 ~PrintingCallbacks() = default;
36
37public:
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 /// When printing type to be inserted into code in specific context, this
44 /// callback can be used to avoid printing the redundant part of the
45 /// qualifier. For example, when inserting code inside namespace foo, we
46 /// should print bar::SomeType instead of foo::bar::SomeType.
47 /// To do this, shouldPrintScope should return true on "foo" NamespaceDecl.
48 /// The printing stops at the first isScopeVisible() == true, so there will
49 /// be no calls with outer scopes.
50 virtual bool isScopeVisible(const DeclContext *DC) const { return false; }
51};
52
53/// Describes how types, statements, expressions, and declarations should be
54/// printed.
55///
56/// This type is intended to be small and suitable for passing by value.
57/// It is very frequently copied.
60
61 /// Dictates how anonymous/unnamed entities are printed.
62 enum class AnonymousTagMode {
63 /// E.g., (anonymous enum)/(unnamed struct)/etc.
65
66 /// When printing an anonymous tag name, also print the location of that
67 /// entity (e.g., "enum <anonymous at t.h:10:5>").
69 };
70
71 /// Create a default printing policy for the specified language.
81 llvm::to_underlying(AnonymousTagMode::SourceLocation)),
90 MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
91 MSVCFormatting(false), ConstantsAsWritten(false),
92 SuppressImplicitBase(false), FullyQualifiedName(false),
93 PrintAsCanonical(false), PrintInjectedClassNameWithArguments(true),
94 UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
95 CleanUglifiedParameters(false), ResolveDecltype(false),
99
100 /// Adjust this printing policy for cases where it's known that we're
101 /// printing C++ code (for instance, if AST dumping reaches a C++-only
102 /// construct). This should not be used if a real LangOptions object is
103 /// available.
105 SuppressTagKeyword = true;
106 Bool = true;
107 UseVoidForZeroParams = false;
108 }
109
110 /// The number of spaces to use to indent each line.
111 unsigned Indentation : 8;
112
113 /// Whether we should suppress printing of the actual specifiers for
114 /// the given type or declaration.
115 ///
116 /// This flag is only used when we are printing declarators beyond
117 /// the first declarator within a declaration group. For example, given:
118 ///
119 /// \code
120 /// const int *x, *y;
121 /// \endcode
122 ///
123 /// SuppressSpecifiers will be false when printing the
124 /// declaration for "x", so that we will print "int *x"; it will be
125 /// \c true when we print "y", so that we suppress printing the
126 /// "const int" type specifier and instead only print the "*y".
127 LLVM_PREFERRED_TYPE(bool)
129
130 /// Whether type printing should skip printing the tag keyword.
131 ///
132 /// This is used when printing the inner type of elaborated types,
133 /// (as the tag keyword is part of the elaborated type):
134 ///
135 /// \code
136 /// struct Geometry::Point;
137 /// \endcode
138 LLVM_PREFERRED_TYPE(bool)
139 unsigned SuppressTagKeyword : 1;
140
141 /// Whether type printing should skip printing the tag keyword
142 /// of anonymous entities. E.g.,
143 ///
144 /// * \c (anonymous) as opopsed to (anonymous struct)
145 /// * \c (unnamed) as opposed to (unnamed enum)
146 ///
147 LLVM_PREFERRED_TYPE(bool)
149
150 /// When true, include the body of a tag definition.
151 ///
152 /// This is used to place the definition of a struct
153 /// in the middle of another declaration as with:
154 ///
155 /// \code
156 /// typedef struct { int x, y; } Point;
157 /// \endcode
158 LLVM_PREFERRED_TYPE(bool)
160
161 /// Suppresses printing of scope specifiers.
162 LLVM_PREFERRED_TYPE(bool)
163 unsigned SuppressScope : 1;
164
165 /// Suppress printing parts of scope specifiers that are never
166 /// written, e.g., for anonymous namespaces.
167 LLVM_PREFERRED_TYPE(bool)
169
170 /// Suppress printing parts of scope specifiers that correspond
171 /// to inline namespaces.
172 /// If Redundant, where the name is unambiguous with the specifier removed.
173 /// If All, even if the name is ambiguous with the specifier
174 /// removed.
175 LLVM_PREFERRED_TYPE(SuppressInlineNamespaceMode)
177
178 /// Suppress printing of variable initializers.
179 ///
180 /// This flag is used when printing the loop variable in a for-range
181 /// statement. For example, given:
182 ///
183 /// \code
184 /// for (auto x : coll)
185 /// \endcode
186 ///
187 /// SuppressInitializers will be true when printing "auto x", so that the
188 /// internal initializer constructed for x will not be printed.
189 LLVM_PREFERRED_TYPE(bool)
191
192 /// Whether we should print the sizes of constant array expressions as written
193 /// in the sources.
194 ///
195 /// This flag determines whether array types declared as
196 ///
197 /// \code
198 /// int a[4+10*10];
199 /// char a[] = "A string";
200 /// \endcode
201 ///
202 /// will be printed as written or as follows:
203 ///
204 /// \code
205 /// int a[104];
206 /// char a[9] = "A string";
207 /// \endcode
208 LLVM_PREFERRED_TYPE(bool)
210
211 LLVM_PREFERRED_TYPE(AnonymousTagMode)
213
214 /// When true, suppress printing of the __strong lifetime qualifier in ARC.
215 LLVM_PREFERRED_TYPE(bool)
217
218 /// When true, suppress printing of lifetime qualifier in ARC.
219 LLVM_PREFERRED_TYPE(bool)
221
222 /// When true, suppresses printing template arguments in names of C++
223 /// constructors.
224 LLVM_PREFERRED_TYPE(bool)
226
227 /// When true, attempt to suppress template arguments that match the default
228 /// argument for the parameter.
229 LLVM_PREFERRED_TYPE(bool)
231
232 /// Whether we can use 'bool' rather than '_Bool' (even if the language
233 /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
234 LLVM_PREFERRED_TYPE(bool)
235 unsigned Bool : 1;
236
237 /// Whether we should use 'nullptr' rather than '0' as a null pointer
238 /// constant.
239 LLVM_PREFERRED_TYPE(bool)
240 unsigned Nullptr : 1;
241
242 /// Whether 'nullptr_t' is in namespace 'std' or not.
243 LLVM_PREFERRED_TYPE(bool)
245
246 /// Whether we can use 'restrict' rather than '__restrict'.
247 LLVM_PREFERRED_TYPE(bool)
248 unsigned Restrict : 1;
249
250 /// Whether we can use 'alignof' rather than '__alignof'.
251 LLVM_PREFERRED_TYPE(bool)
252 unsigned Alignof : 1;
253
254 /// Whether we can use '_Alignof' rather than '__alignof'.
255 LLVM_PREFERRED_TYPE(bool)
256 unsigned UnderscoreAlignof : 1;
257
258 /// Whether we should use '(void)' rather than '()' for a function prototype
259 /// with zero parameters.
260 LLVM_PREFERRED_TYPE(bool)
262
263 /// Whether nested templates must be closed like 'a<b<c> >' rather than
264 /// 'a<b<c>>'.
265 LLVM_PREFERRED_TYPE(bool)
267
268 /// Provide a 'terse' output.
269 ///
270 /// For example, in this mode we don't print function bodies, class members,
271 /// declarations inside namespaces etc. Effectively, this should print
272 /// only the requested declaration.
273 LLVM_PREFERRED_TYPE(bool)
274 unsigned TerseOutput : 1;
275
276 /// When true, do certain refinement needed for producing proper declaration
277 /// tag; such as, do not print attributes attached to the declaration.
278 ///
279 LLVM_PREFERRED_TYPE(bool)
281
282 /// When true, print the half-precision floating-point type as 'half'
283 /// instead of '__fp16'
284 LLVM_PREFERRED_TYPE(bool)
285 unsigned Half : 1;
286
287 /// When true, print the built-in wchar_t type as __wchar_t. For use in
288 /// Microsoft mode when wchar_t is not available.
289 LLVM_PREFERRED_TYPE(bool)
290 unsigned MSWChar : 1;
291
292 /// When true, include newlines after statements like "break", etc.
293 LLVM_PREFERRED_TYPE(bool)
294 unsigned IncludeNewlines : 1;
295
296 /// Use whitespace and punctuation like MSVC does. In particular, this prints
297 /// anonymous namespaces as `anonymous namespace' and does not insert spaces
298 /// after template arguments.
299 LLVM_PREFERRED_TYPE(bool)
300 unsigned MSVCFormatting : 1;
301
302 /// Whether we should print the constant expressions as written in the
303 /// sources.
304 ///
305 /// This flag determines whether constants expressions like
306 ///
307 /// \code
308 /// 0x10
309 /// 2.5e3
310 /// \endcode
311 ///
312 /// will be printed as written or as follows:
313 ///
314 /// \code
315 /// 0x10
316 /// 2.5e3
317 /// \endcode
318 LLVM_PREFERRED_TYPE(bool)
319 unsigned ConstantsAsWritten : 1;
320
321 /// When true, don't print the implicit 'self' or 'this' expressions.
322 LLVM_PREFERRED_TYPE(bool)
323 unsigned SuppressImplicitBase : 1;
324
325 /// When true, print the fully qualified name of function declarations.
326 /// This is the opposite of SuppressScope and thus overrules it.
327 LLVM_PREFERRED_TYPE(bool)
328 unsigned FullyQualifiedName : 1;
329
330 /// Whether to print entities as written or canonically.
331 LLVM_PREFERRED_TYPE(bool)
332 unsigned PrintAsCanonical : 1;
333
334 /// Whether to print an InjectedClassNameType with template arguments or as
335 /// written. When a template argument is unnamed, printing it results in
336 /// invalid C++ code.
337 LLVM_PREFERRED_TYPE(bool)
338 unsigned PrintInjectedClassNameWithArguments : 1;
339
340 /// Whether to use C++ template preferred_name attributes when printing
341 /// templates.
342 LLVM_PREFERRED_TYPE(bool)
343 unsigned UsePreferredNames : 1;
344
345 /// Whether to use type suffixes (eg: 1U) on integral non-type template
346 /// parameters.
347 LLVM_PREFERRED_TYPE(bool)
348 unsigned AlwaysIncludeTypeForTemplateArgument : 1;
349
350 /// Whether to strip underscores when printing reserved parameter names.
351 /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
352 /// This only affects parameter names, and so describes a compatible API.
353 LLVM_PREFERRED_TYPE(bool)
354 unsigned CleanUglifiedParameters : 1;
355
356 /// Whether to print the type a non-dependent `decltype(expr)` resolves to,
357 /// rather than the `decltype` specifier itself. Intended for display
358 /// contexts such as code completion, where `int` is more informative than
359 /// `decltype(x)`; it does not describe how the type was spelled.
360 LLVM_PREFERRED_TYPE(bool)
361 unsigned ResolveDecltype : 1;
362
363 /// Whether to print the entire array initializers, especially on non-type
364 /// template parameters, no matter how many elements there are.
365 LLVM_PREFERRED_TYPE(bool)
367
368 /// Whether to print enumerators with a matching enumerator name or via cast
369 // of an integer.
370 LLVM_PREFERRED_TYPE(bool)
371 unsigned PrettyEnums : 1;
372
373 /// Whether to print enumerator non-type template parameters with a matching
374 /// enumerator name or via cast of an integer.
375 LLVM_PREFERRED_TYPE(bool)
376 unsigned UseEnumerators : 1;
377
378 /// Whether or not we're printing known HLSL code and should print HLSL
379 /// sugared types when possible.
380 LLVM_PREFERRED_TYPE(bool)
381 unsigned UseHLSLTypes : 1;
382
383 /// Whether to suppress attributes in decl printing.
384 LLVM_PREFERRED_TYPE(bool)
386
387 /// Whether to suppress printing the body of a lambda.
388 LLVM_PREFERRED_TYPE(bool)
389 unsigned SuppressLambdaBody : 1;
390
391 /// Callbacks to use to allow the behavior of printing to be customized.
392 const PrintingCallbacks *Callbacks = nullptr;
393};
394
395} // end namespace clang
396
397#endif
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:1466
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
virtual bool handledStmt(Stmt *E, raw_ostream &OS)=0
virtual ~PrinterHelper()
Callbacks to use to customize the behavior of the pretty-printer.
virtual std::string remapPath(StringRef Path) const
Remap a path to a form suitable for printing.
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...
Stmt - This represents one statement.
Definition Stmt.h:85
Top level wrappers for InstallAPI frontend operations.
@ CPlusPlus
@ CPlusPlus11
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
unsigned SuppressUnwrittenScope
Suppress printing parts of scope specifiers that are never written, e.g., for anonymous namespaces.
unsigned SuppressDeclAttributes
Whether to suppress attributes in decl printing.
unsigned PrettyEnums
Whether to print enumerators with a matching enumerator name or via cast.
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 PolishForDeclaration
When true, do certain refinement needed for producing proper declaration tag; such as,...
void adjustForCPlusPlus()
Adjust this printing policy for cases where it's known that we're printing C++ code (for instance,...
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 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 Bool
Whether we can use 'bool' rather than '_Bool' (even if the language doesn't actually have 'bool',...
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.
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 IncludeNewlines
When true, include newlines after statements like "break", etc.
AnonymousTagMode
Dictates how anonymous/unnamed entities are printed.
@ Plain
E.g., (anonymous enum)/(unnamed struct)/etc.
@ SourceLocation
When printing an anonymous tag name, also print the location of that entity (e.g.,...
unsigned Indentation
The number of spaces to use to indent each line.
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.
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 SuppressTagKeywordInAnonNames
Whether type printing should skip printing the tag keyword of anonymous entities.
unsigned SuppressLambdaBody
Whether to suppress printing the body of a lambda.
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 SuppressLifetimeQualifiers
When true, suppress printing of lifetime qualifier in ARC.
unsigned ResolveDecltype
Use whitespace and punctuation like MSVC does.