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 
19 namespace clang {
20 
21 class DeclContext;
22 class LangOptions;
23 class Stmt;
24 
26 public:
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.
33 protected:
34  ~PrintingCallbacks() = default;
35 
36 public:
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.
75  MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
85 
86  /// Adjust this printing policy for cases where it's known that we're
87  /// printing C++ code (for instance, if AST dumping reaches a C++-only
88  /// construct). This should not be used if a real LangOptions object is
89  /// available.
91  SuppressTagKeyword = true;
92  Bool = true;
93  UseVoidForZeroParams = false;
94  }
95 
96  /// Adjust this printing policy to print C++ forward declaration for a given
97  /// Decl.
99  PolishForDeclaration = true;
100  SuppressDefinition = true;
102  }
103 
104  /// The number of spaces to use to indent each line.
105  unsigned Indentation : 8;
106 
107  /// Whether we should suppress printing of the actual specifiers for
108  /// the given type or declaration.
109  ///
110  /// This flag is only used when we are printing declarators beyond
111  /// the first declarator within a declaration group. For example, given:
112  ///
113  /// \code
114  /// const int *x, *y;
115  /// \endcode
116  ///
117  /// SuppressSpecifiers will be false when printing the
118  /// declaration for "x", so that we will print "int *x"; it will be
119  /// \c true when we print "y", so that we suppress printing the
120  /// "const int" type specifier and instead only print the "*y".
121  LLVM_PREFERRED_TYPE(bool)
123 
124  /// Whether type printing should skip printing the tag keyword.
125  ///
126  /// This is used when printing the inner type of elaborated types,
127  /// (as the tag keyword is part of the elaborated type):
128  ///
129  /// \code
130  /// struct Geometry::Point;
131  /// \endcode
132  LLVM_PREFERRED_TYPE(bool)
133  unsigned SuppressTagKeyword : 1;
134 
135  /// When true, include the body of a tag definition.
136  ///
137  /// This is used to place the definition of a struct
138  /// in the middle of another declaration as with:
139  ///
140  /// \code
141  /// typedef struct { int x, y; } Point;
142  /// \endcode
143  LLVM_PREFERRED_TYPE(bool)
144  unsigned IncludeTagDefinition : 1;
145 
146  /// Suppresses printing of scope specifiers.
147  LLVM_PREFERRED_TYPE(bool)
148  unsigned SuppressScope : 1;
149 
150  /// Suppress printing parts of scope specifiers that are never
151  /// written, e.g., for anonymous namespaces.
152  LLVM_PREFERRED_TYPE(bool)
154 
155  /// Suppress printing parts of scope specifiers that correspond
156  /// to inline namespaces, where the name is unambiguous with the specifier
157  /// removed.
158  LLVM_PREFERRED_TYPE(bool)
160 
161  /// Ignore qualifiers and tag keywords as specified by elaborated type sugar,
162  /// instead letting the underlying type print as normal.
163  LLVM_PREFERRED_TYPE(bool)
164  unsigned SuppressElaboration : 1;
165 
166  /// Suppress printing of variable initializers.
167  ///
168  /// This flag is used when printing the loop variable in a for-range
169  /// statement. For example, given:
170  ///
171  /// \code
172  /// for (auto x : coll)
173  /// \endcode
174  ///
175  /// SuppressInitializers will be true when printing "auto x", so that the
176  /// internal initializer constructed for x will not be printed.
177  LLVM_PREFERRED_TYPE(bool)
178  unsigned SuppressInitializers : 1;
179 
180  /// Whether we should print the sizes of constant array expressions as written
181  /// in the sources.
182  ///
183  /// This flag determines whether array types declared as
184  ///
185  /// \code
186  /// int a[4+10*10];
187  /// char a[] = "A string";
188  /// \endcode
189  ///
190  /// will be printed as written or as follows:
191  ///
192  /// \code
193  /// int a[104];
194  /// char a[9] = "A string";
195  /// \endcode
196  LLVM_PREFERRED_TYPE(bool)
198 
199  /// When printing an anonymous tag name, also print the location of that
200  /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
201  /// "(anonymous)" for the name.
202  LLVM_PREFERRED_TYPE(bool)
203  unsigned AnonymousTagLocations : 1;
204 
205  /// When true, suppress printing of the __strong lifetime qualifier in ARC.
206  LLVM_PREFERRED_TYPE(bool)
208 
209  /// When true, suppress printing of lifetime qualifier in ARC.
210  LLVM_PREFERRED_TYPE(bool)
212 
213  /// When true prints a canonical type instead of an alias.
214  /// Also removes preceeding keywords if there is one. E.g.
215  /// \code
216  /// namespace NS {
217  /// using SizeT = int;
218  /// }
219  /// template<typename NS::SizeT N> class C;
220  /// \endcode
221  /// will be printed as
222  /// \code
223  /// template<int N> class C;
224  /// \endcode
225  unsigned SuppressTypedefs : 1;
226 
227  /// When true, suppress printing final specifier.
229 
230  /// When true, suppresses printing template arguments in names of C++
231  /// constructors.
232  LLVM_PREFERRED_TYPE(bool)
234 
235  /// When true, attempt to suppress template arguments that match the default
236  /// argument for the parameter.
237  LLVM_PREFERRED_TYPE(bool)
239 
240  /// Whether we can use 'bool' rather than '_Bool' (even if the language
241  /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
242  LLVM_PREFERRED_TYPE(bool)
243  unsigned Bool : 1;
244 
245  /// Whether we should use 'nullptr' rather than '0' as a null pointer
246  /// constant.
247  LLVM_PREFERRED_TYPE(bool)
248  unsigned Nullptr : 1;
249 
250  /// Whether 'nullptr_t' is in namespace 'std' or not.
251  LLVM_PREFERRED_TYPE(bool)
253 
254  /// Whether we can use 'restrict' rather than '__restrict'.
255  LLVM_PREFERRED_TYPE(bool)
256  unsigned Restrict : 1;
257 
258  /// Whether we can use 'alignof' rather than '__alignof'.
259  LLVM_PREFERRED_TYPE(bool)
260  unsigned Alignof : 1;
261 
262  /// Whether we can use '_Alignof' rather than '__alignof'.
263  LLVM_PREFERRED_TYPE(bool)
264  unsigned UnderscoreAlignof : 1;
265 
266  /// Whether we should use '(void)' rather than '()' for a function prototype
267  /// with zero parameters.
268  LLVM_PREFERRED_TYPE(bool)
269  unsigned UseVoidForZeroParams : 1;
270 
271  /// Whether nested templates must be closed like 'a<b<c> >' rather than
272  /// 'a<b<c>>'.
273  LLVM_PREFERRED_TYPE(bool)
274  unsigned SplitTemplateClosers : 1;
275 
276  /// Provide a 'terse' output.
277  ///
278  /// For example, in this mode we don't print function bodies, class members,
279  /// declarations inside namespaces etc. Effectively, this should print
280  /// only the requested declaration.
281  LLVM_PREFERRED_TYPE(bool)
282  unsigned TerseOutput : 1;
283 
284  /// When true, do certain refinement needed for producing proper declaration
285  /// tag; such as, do not print attributes attached to the declaration.
286  ///
287  LLVM_PREFERRED_TYPE(bool)
288  unsigned PolishForDeclaration : 1;
289 
290  /// When true, print the half-precision floating-point type as 'half'
291  /// instead of '__fp16'
292  LLVM_PREFERRED_TYPE(bool)
293  unsigned Half : 1;
294 
295  /// When true, print the built-in wchar_t type as __wchar_t. For use in
296  /// Microsoft mode when wchar_t is not available.
297  LLVM_PREFERRED_TYPE(bool)
298  unsigned MSWChar : 1;
299 
300  /// When true, include newlines after statements like "break", etc.
301  LLVM_PREFERRED_TYPE(bool)
302  unsigned IncludeNewlines : 1;
303 
304  /// Use whitespace and punctuation like MSVC does. In particular, this prints
305  /// anonymous namespaces as `anonymous namespace' and does not insert spaces
306  /// after template arguments.
307  LLVM_PREFERRED_TYPE(bool)
308  unsigned MSVCFormatting : 1;
309 
310  /// Whether we should print the constant expressions as written in the
311  /// sources.
312  ///
313  /// This flag determines whether constants expressions like
314  ///
315  /// \code
316  /// 0x10
317  /// 2.5e3
318  /// \endcode
319  ///
320  /// will be printed as written or as follows:
321  ///
322  /// \code
323  /// 0x10
324  /// 2.5e3
325  /// \endcode
326  LLVM_PREFERRED_TYPE(bool)
327  unsigned ConstantsAsWritten : 1;
328 
329  /// When true, don't print the implicit 'self' or 'this' expressions.
330  LLVM_PREFERRED_TYPE(bool)
331  unsigned SuppressImplicitBase : 1;
332 
333  /// When true, print the fully qualified name of function declarations.
334  /// This is the opposite of SuppressScope and thus overrules it.
335  LLVM_PREFERRED_TYPE(bool)
336  unsigned FullyQualifiedName : 1;
337 
338  /// When true does not print definition of a type. E.g.
339  /// \code
340  /// template<typename T> class C0 : public C1 {...}
341  /// \endcode
342  /// will be printed as
343  /// \code
344  /// template<typename T> class C0
345  /// \endcode
346  unsigned SuppressDefinition : 1;
347 
348  /// When true, suppresses printing default template arguments of a type. E.g.
349  /// \code
350  /// template<typename T = void> class A
351  /// \endcode
352  /// will be printed as
353  /// \code
354  /// template<typename T> class A
355  /// \endcode
357 
358  /// Whether to print types as written or canonically.
359  LLVM_PREFERRED_TYPE(bool)
360  unsigned PrintCanonicalTypes : 1;
361 
362  /// Whether to skip the canonicalization (when PrintCanonicalTypes is set) for
363  /// TemplateTypeParmTypes. This has no effect if PrintCanonicalTypes isn't
364  /// set. This is useful for non-type-template-parameters, since the canonical
365  /// version of:
366  /// \code
367  /// TemplateTypeParmType '_Tp'
368  /// TemplateTypeParm '_Tp'
369  /// \endcode
370  /// is:
371  /// \code
372  /// TemplateTypeParmType 'type-parameter-0-0'
373  /// \endcode
375 
376  /// Whether to print an InjectedClassNameType with template arguments or as
377  /// written. When a template argument is unnamed, printing it results in
378  /// invalid C++ code.
379  LLVM_PREFERRED_TYPE(bool)
381 
382  /// Whether to use C++ template preferred_name attributes when printing
383  /// templates.
384  LLVM_PREFERRED_TYPE(bool)
385  unsigned UsePreferredNames : 1;
386 
387  /// Whether to use type suffixes (eg: 1U) on integral non-type template
388  /// parameters.
389  LLVM_PREFERRED_TYPE(bool)
391 
392  /// Whether to strip underscores when printing reserved parameter names.
393  /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
394  /// This only affects parameter names, and so describes a compatible API.
395  LLVM_PREFERRED_TYPE(bool)
397 
398  /// Whether to print the entire array initializers, especially on non-type
399  /// template parameters, no matter how many elements there are.
400  LLVM_PREFERRED_TYPE(bool)
402 
403  /// Whether to print enumerator non-type template parameters with a matching
404  /// enumerator name or via cast of an integer.
405  LLVM_PREFERRED_TYPE(bool)
406  unsigned UseEnumerators : 1;
407 
408  /// Whether or not we're printing known HLSL code and should print HLSL
409  /// sugared types when possible.
410  LLVM_PREFERRED_TYPE(bool)
411  unsigned UseHLSLTypes : 1;
412 
413  /// Callbacks to use to allow the behavior of printing to be customized.
414  const PrintingCallbacks *Callbacks = nullptr;
415 };
416 
417 } // end namespace clang
418 
419 #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:480
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.
void adjustForCPlusPlusFwdDecl()
Adjust this printing policy to print C++ forward declaration for a given Decl.
Definition: PrettyPrinter.h:98
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:90
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 SkipCanonicalizationOfTemplateTypeParms
Whether to skip the canonicalization (when PrintCanonicalTypes is set) for TemplateTypeParmTypes.
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 SuppressTypedefs
When true prints a canonical type instead of an alias.
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 SuppressFinalSpecifier
When true, suppress printing final specifier.
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 SuppressDefinition
When true does not print definition of a type.
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,...
unsigned SuppressDefaultTemplateArguments
When true, suppresses printing default template arguments of a type.
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.
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.