clang 23.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.
59 enum class SuppressInlineNamespaceMode : uint8_t { None, Redundant, All };
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.
98
99 /// Adjust this printing policy for cases where it's known that we're
100 /// printing C++ code (for instance, if AST dumping reaches a C++-only
101 /// construct). This should not be used if a real LangOptions object is
102 /// available.
104 SuppressTagKeyword = true;
105 Bool = true;
106 UseVoidForZeroParams = false;
107 }
108
109 /// The number of spaces to use to indent each line.
110 unsigned Indentation : 8;
111
112 /// Whether we should suppress printing of the actual specifiers for
113 /// the given type or declaration.
114 ///
115 /// This flag is only used when we are printing declarators beyond
116 /// the first declarator within a declaration group. For example, given:
117 ///
118 /// \code
119 /// const int *x, *y;
120 /// \endcode
121 ///
122 /// SuppressSpecifiers will be false when printing the
123 /// declaration for "x", so that we will print "int *x"; it will be
124 /// \c true when we print "y", so that we suppress printing the
125 /// "const int" type specifier and instead only print the "*y".
126 LLVM_PREFERRED_TYPE(bool)
128
129 /// Whether type printing should skip printing the tag keyword.
130 ///
131 /// This is used when printing the inner type of elaborated types,
132 /// (as the tag keyword is part of the elaborated type):
133 ///
134 /// \code
135 /// struct Geometry::Point;
136 /// \endcode
137 LLVM_PREFERRED_TYPE(bool)
138 unsigned SuppressTagKeyword : 1;
139
140 /// Whether type printing should skip printing the tag keyword
141 /// of anonymous entities. E.g.,
142 ///
143 /// * \c (anonymous) as opopsed to (anonymous struct)
144 /// * \c (unnamed) as opposed to (unnamed enum)
145 ///
146 LLVM_PREFERRED_TYPE(bool)
148
149 /// When true, include the body of a tag definition.
150 ///
151 /// This is used to place the definition of a struct
152 /// in the middle of another declaration as with:
153 ///
154 /// \code
155 /// typedef struct { int x, y; } Point;
156 /// \endcode
157 LLVM_PREFERRED_TYPE(bool)
159
160 /// Suppresses printing of scope specifiers.
161 LLVM_PREFERRED_TYPE(bool)
162 unsigned SuppressScope : 1;
163
164 /// Suppress printing parts of scope specifiers that are never
165 /// written, e.g., for anonymous namespaces.
166 LLVM_PREFERRED_TYPE(bool)
168
169 /// Suppress printing parts of scope specifiers that correspond
170 /// to inline namespaces.
171 /// If Redundant, where the name is unambiguous with the specifier removed.
172 /// If All, even if the name is ambiguous with the specifier
173 /// removed.
174 LLVM_PREFERRED_TYPE(SuppressInlineNamespaceMode)
176
177 /// Suppress printing of variable initializers.
178 ///
179 /// This flag is used when printing the loop variable in a for-range
180 /// statement. For example, given:
181 ///
182 /// \code
183 /// for (auto x : coll)
184 /// \endcode
185 ///
186 /// SuppressInitializers will be true when printing "auto x", so that the
187 /// internal initializer constructed for x will not be printed.
188 LLVM_PREFERRED_TYPE(bool)
190
191 /// Whether we should print the sizes of constant array expressions as written
192 /// in the sources.
193 ///
194 /// This flag determines whether array types declared as
195 ///
196 /// \code
197 /// int a[4+10*10];
198 /// char a[] = "A string";
199 /// \endcode
200 ///
201 /// will be printed as written or as follows:
202 ///
203 /// \code
204 /// int a[104];
205 /// char a[9] = "A string";
206 /// \endcode
207 LLVM_PREFERRED_TYPE(bool)
209
210 LLVM_PREFERRED_TYPE(AnonymousTagMode)
212
213 /// When true, suppress printing of the __strong lifetime qualifier in ARC.
214 LLVM_PREFERRED_TYPE(bool)
216
217 /// When true, suppress printing of lifetime qualifier in ARC.
218 LLVM_PREFERRED_TYPE(bool)
220
221 /// When true, suppresses printing template arguments in names of C++
222 /// constructors.
223 LLVM_PREFERRED_TYPE(bool)
225
226 /// When true, attempt to suppress template arguments that match the default
227 /// argument for the parameter.
228 LLVM_PREFERRED_TYPE(bool)
230
231 /// Whether we can use 'bool' rather than '_Bool' (even if the language
232 /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
233 LLVM_PREFERRED_TYPE(bool)
234 unsigned Bool : 1;
235
236 /// Whether we should use 'nullptr' rather than '0' as a null pointer
237 /// constant.
238 LLVM_PREFERRED_TYPE(bool)
239 unsigned Nullptr : 1;
240
241 /// Whether 'nullptr_t' is in namespace 'std' or not.
242 LLVM_PREFERRED_TYPE(bool)
244
245 /// Whether we can use 'restrict' rather than '__restrict'.
246 LLVM_PREFERRED_TYPE(bool)
247 unsigned Restrict : 1;
248
249 /// Whether we can use 'alignof' rather than '__alignof'.
250 LLVM_PREFERRED_TYPE(bool)
251 unsigned Alignof : 1;
252
253 /// Whether we can use '_Alignof' rather than '__alignof'.
254 LLVM_PREFERRED_TYPE(bool)
255 unsigned UnderscoreAlignof : 1;
256
257 /// Whether we should use '(void)' rather than '()' for a function prototype
258 /// with zero parameters.
259 LLVM_PREFERRED_TYPE(bool)
261
262 /// Whether nested templates must be closed like 'a<b<c> >' rather than
263 /// 'a<b<c>>'.
264 LLVM_PREFERRED_TYPE(bool)
266
267 /// Provide a 'terse' output.
268 ///
269 /// For example, in this mode we don't print function bodies, class members,
270 /// declarations inside namespaces etc. Effectively, this should print
271 /// only the requested declaration.
272 LLVM_PREFERRED_TYPE(bool)
273 unsigned TerseOutput : 1;
274
275 /// When true, do certain refinement needed for producing proper declaration
276 /// tag; such as, do not print attributes attached to the declaration.
277 ///
278 LLVM_PREFERRED_TYPE(bool)
280
281 /// When true, print the half-precision floating-point type as 'half'
282 /// instead of '__fp16'
283 LLVM_PREFERRED_TYPE(bool)
284 unsigned Half : 1;
285
286 /// When true, print the built-in wchar_t type as __wchar_t. For use in
287 /// Microsoft mode when wchar_t is not available.
288 LLVM_PREFERRED_TYPE(bool)
289 unsigned MSWChar : 1;
290
291 /// When true, include newlines after statements like "break", etc.
292 LLVM_PREFERRED_TYPE(bool)
293 unsigned IncludeNewlines : 1;
294
295 /// Use whitespace and punctuation like MSVC does. In particular, this prints
296 /// anonymous namespaces as `anonymous namespace' and does not insert spaces
297 /// after template arguments.
298 LLVM_PREFERRED_TYPE(bool)
299 unsigned MSVCFormatting : 1;
300
301 /// Whether we should print the constant expressions as written in the
302 /// sources.
303 ///
304 /// This flag determines whether constants expressions like
305 ///
306 /// \code
307 /// 0x10
308 /// 2.5e3
309 /// \endcode
310 ///
311 /// will be printed as written or as follows:
312 ///
313 /// \code
314 /// 0x10
315 /// 2.5e3
316 /// \endcode
317 LLVM_PREFERRED_TYPE(bool)
318 unsigned ConstantsAsWritten : 1;
319
320 /// When true, don't print the implicit 'self' or 'this' expressions.
321 LLVM_PREFERRED_TYPE(bool)
323
324 /// When true, print the fully qualified name of function declarations.
325 /// This is the opposite of SuppressScope and thus overrules it.
326 LLVM_PREFERRED_TYPE(bool)
327 unsigned FullyQualifiedName : 1;
328
329 /// Whether to print entities as written or canonically.
330 LLVM_PREFERRED_TYPE(bool)
331 unsigned PrintAsCanonical : 1;
332
333 /// Whether to print an InjectedClassNameType with template arguments or as
334 /// written. When a template argument is unnamed, printing it results in
335 /// invalid C++ code.
336 LLVM_PREFERRED_TYPE(bool)
338
339 /// Whether to use C++ template preferred_name attributes when printing
340 /// templates.
341 LLVM_PREFERRED_TYPE(bool)
342 unsigned UsePreferredNames : 1;
343
344 /// Whether to use type suffixes (eg: 1U) on integral non-type template
345 /// parameters.
346 LLVM_PREFERRED_TYPE(bool)
348
349 /// Whether to strip underscores when printing reserved parameter names.
350 /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
351 /// This only affects parameter names, and so describes a compatible API.
352 LLVM_PREFERRED_TYPE(bool)
354
355 /// Whether to print the entire array initializers, especially on non-type
356 /// template parameters, no matter how many elements there are.
357 LLVM_PREFERRED_TYPE(bool)
359
360 /// Whether to print enumerator non-type template parameters with a matching
361 /// enumerator name or via cast of an integer.
362 LLVM_PREFERRED_TYPE(bool)
363 unsigned UseEnumerators : 1;
364
365 /// Whether or not we're printing known HLSL code and should print HLSL
366 /// sugared types when possible.
367 LLVM_PREFERRED_TYPE(bool)
368 unsigned UseHLSLTypes : 1;
369
370 /// Whether to suppress attributes in decl printing.
371 LLVM_PREFERRED_TYPE(bool)
373
374 /// Callbacks to use to allow the behavior of printing to be customized.
375 const PrintingCallbacks *Callbacks = nullptr;
376};
377
378} // end namespace clang
379
380#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:1449
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:86
The JSON file list parser is used to communicate input to InstallAPI.
@ CPlusPlus
@ CPlusPlus11
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
#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 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 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,...
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 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.
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.
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 PrintAsCanonical
Whether to print entities as written or canonically.
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.