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