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