clang 10.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"
17 #include "clang/Basic/LangOptions.h"
18
19 namespace clang {
20
21 class LangOptions;
22 class SourceManager;
23 class Stmt;
24 class TagDecl;
25
26 class PrinterHelper {
27 public:
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.
33 class PrintingCallbacks {
34 protected:
35 ~PrintingCallbacks() = default;
36
37 public:
38 /// Remap a path to a form suitable for printing.
39 virtual std::string remapPath(StringRef Path) const { return Path; }
40 };
41
42 /// Describes how types, statements, expressions, and declarations should be
43 /// printed.
44 ///
45 /// This type is intended to be small and suitable for passing by value.
46 /// It is very frequently copied.
47 struct PrintingPolicy {
48 /// Create a default printing policy for the specified language.
49 PrintingPolicy(const LangOptions &LO)
50 : Indentation(2), SuppressSpecifiers(false),
51 SuppressTagKeyword(LO.CPlusPlus), IncludeTagDefinition(false),
52 SuppressScope(false), SuppressUnwrittenScope(false),
53 SuppressInitializers(false), ConstantArraySizeAsWritten(false),
54 AnonymousTagLocations(true), SuppressStrongLifetime(false),
55 SuppressLifetimeQualifiers(false),
56 SuppressTemplateArgsInCXXConstructors(false), Bool(LO.Bool),
57 Restrict(LO.C99), Alignof(LO.CPlusPlus11), UnderscoreAlignof(LO.C11),
58 UseVoidForZeroParams(!LO.CPlusPlus), TerseOutput(false),
59 PolishForDeclaration(false), Half(LO.Half),
60 MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
61 MSVCFormatting(false), ConstantsAsWritten(false),
62 SuppressImplicitBase(false), FullyQualifiedName(false),
63 PrintCanonicalTypes(false) {}
64
65 /// Adjust this printing policy for cases where it's known that we're
66 /// printing C++ code (for instance, if AST dumping reaches a C++-only
67 /// construct). This should not be used if a real LangOptions object is
68 /// available.
69 void adjustForCPlusPlus() {
70 SuppressTagKeyword = true;
71 Bool = true;
72 UseVoidForZeroParams = false;
73 }
74
75 /// The number of spaces to use to indent each line.
76 unsigned Indentation : 8;
77
78 /// Whether we should suppress printing of the actual specifiers for
79 /// the given type or declaration.
80 ///
81 /// This flag is only used when we are printing declarators beyond
82 /// the first declarator within a declaration group. For example, given:
83 ///
84 /// \code
85 /// const int *x, *y;
86 /// \endcode
87 ///
88 /// SuppressSpecifiers will be false when printing the
89 /// declaration for "x", so that we will print "int *x"; it will be
90 /// \c true when we print "y", so that we suppress printing the
91 /// "const int" type specifier and instead only print the "*y".
92 unsigned SuppressSpecifiers : 1;
93
94 /// Whether type printing should skip printing the tag keyword.
95 ///
96 /// This is used when printing the inner type of elaborated types,
97 /// (as the tag keyword is part of the elaborated type):
98 ///
99 /// \code
100 /// struct Geometry::Point;
101 /// \endcode
102 unsigned SuppressTagKeyword : 1;
103
104 /// When true, include the body of a tag definition.
105 ///
106 /// This is used to place the definition of a struct
107 /// in the middle of another declaration as with:
108 ///
109 /// \code
110 /// typedef struct { int x, y; } Point;
111 /// \endcode
112 unsigned IncludeTagDefinition : 1;
113
114 /// Suppresses printing of scope specifiers.
115 unsigned SuppressScope : 1;
116
117 /// Suppress printing parts of scope specifiers that don't need
118 /// to be written, e.g., for inline or anonymous namespaces.
119 unsigned SuppressUnwrittenScope : 1;
120
121 /// Suppress printing of variable initializers.
122 ///
123 /// This flag is used when printing the loop variable in a for-range
124 /// statement. For example, given:
125 ///
126 /// \code
127 /// for (auto x : coll)
128 /// \endcode
129 ///
130 /// SuppressInitializers will be true when printing "auto x", so that the
131 /// internal initializer constructed for x will not be printed.
132 unsigned SuppressInitializers : 1;
133
134 /// Whether we should print the sizes of constant array expressions as written
135 /// in the sources.
136 ///
137 /// This flag determines whether array types declared as
138 ///
139 /// \code
140 /// int a[4+10*10];
141 /// char a[] = "A string";
142 /// \endcode
143 ///
144 /// will be printed as written or as follows:
145 ///
146 /// \code
147 /// int a[104];
148 /// char a[9] = "A string";
149 /// \endcode
150 unsigned ConstantArraySizeAsWritten : 1;
151
152 /// When printing an anonymous tag name, also print the location of that
153 /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
154 /// "(anonymous)" for the name.
155 unsigned AnonymousTagLocations : 1;
156
157 /// When true, suppress printing of the __strong lifetime qualifier in ARC.
158 unsigned SuppressStrongLifetime : 1;
159
160 /// When true, suppress printing of lifetime qualifier in ARC.
161 unsigned SuppressLifetimeQualifiers : 1;
162
163 /// When true, suppresses printing template arguments in names of C++
164 /// constructors.
165 unsigned SuppressTemplateArgsInCXXConstructors : 1;
166
167 /// Whether we can use 'bool' rather than '_Bool' (even if the language
168 /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
169 unsigned Bool : 1;
170
171 /// Whether we can use 'restrict' rather than '__restrict'.
172 unsigned Restrict : 1;
173
174 /// Whether we can use 'alignof' rather than '__alignof'.
175 unsigned Alignof : 1;
176
177 /// Whether we can use '_Alignof' rather than '__alignof'.
178 unsigned UnderscoreAlignof : 1;
179
180 /// Whether we should use '(void)' rather than '()' for a function prototype
181 /// with zero parameters.
182 unsigned UseVoidForZeroParams : 1;
183
184 /// Provide a 'terse' output.
185 ///
186 /// For example, in this mode we don't print function bodies, class members,
187 /// declarations inside namespaces etc. Effectively, this should print
188 /// only the requested declaration.
189 unsigned TerseOutput : 1;
190
191 /// When true, do certain refinement needed for producing proper declaration
192 /// tag; such as, do not print attributes attached to the declaration.
193 ///
194 unsigned PolishForDeclaration : 1;
195
196 /// When true, print the half-precision floating-point type as 'half'
197 /// instead of '__fp16'
198 unsigned Half : 1;
199
200 /// When true, print the built-in wchar_t type as __wchar_t. For use in
201 /// Microsoft mode when wchar_t is not available.
202 unsigned MSWChar : 1;
203
204 /// When true, include newlines after statements like "break", etc.
205 unsigned IncludeNewlines : 1;
206
207 /// Use whitespace and punctuation like MSVC does. In particular, this prints
208 /// anonymous namespaces as `anonymous namespace' and does not insert spaces
209 /// after template arguments.
210 unsigned MSVCFormatting : 1;
211
212 /// Whether we should print the constant expressions as written in the
213 /// sources.
214 ///
215 /// This flag determines whether constants expressions like
216 ///
217 /// \code
218 /// 0x10
219 /// 2.5e3
220 /// \endcode
221 ///
222 /// will be printed as written or as follows:
223 ///
224 /// \code
225 /// 0x10
226 /// 2.5e3
227 /// \endcode
228 unsigned ConstantsAsWritten : 1;
229
230 /// When true, don't print the implicit 'self' or 'this' expressions.
231 unsigned SuppressImplicitBase : 1;
232
233 /// When true, print the fully qualified name of function declarations.
234 /// This is the opposite of SuppressScope and thus overrules it.
235 unsigned FullyQualifiedName : 1;
236
237 /// Whether to print types as written or canonically.
238 unsigned PrintCanonicalTypes : 1;
239
240 /// Callbacks to use to allow the behavior of printing to be customized.
241 const PrintingCallbacks *Callbacks = nullptr;
242 };
243
244 } // end namespace clang
245
246 #endif
clang::PrintingPolicy::PrintingPolicy
PrintingPolicy(const LangOptions &LO)
Create a default printing policy for the specified language.
Definition: PrettyPrinter.h:49
clang::PrintingPolicy::SuppressInitializers
unsigned SuppressInitializers
Suppress printing of variable initializers.
Definition: PrettyPrinter.h:132
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::PrintingPolicy::MSWChar
unsigned MSWChar
When true, print the built-in wchar_t type as __wchar_t.
Definition: PrettyPrinter.h:202
clang::PrintingPolicy
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:47
clang::CPlusPlus11
Definition: LangStandard.h:48
clang::PrintingPolicy::TerseOutput
unsigned TerseOutput
Provide a &#39;terse&#39; output.
Definition: PrettyPrinter.h:189
GCCTypeClass::Bool
clang::PrinterHelper::~PrinterHelper
virtual ~PrinterHelper()
clang::PrintingCallbacks::remapPath
virtual std::string remapPath(StringRef Path) const
Remap a path to a form suitable for printing.
Definition: PrettyPrinter.h:39
clang::PrintingPolicy::SuppressLifetimeQualifiers
unsigned SuppressLifetimeQualifiers
When true, suppress printing of lifetime qualifier in ARC.
Definition: PrettyPrinter.h:161
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:53
clang::PrintingPolicy::SuppressStrongLifetime
unsigned SuppressStrongLifetime
When true, suppress printing of the __strong lifetime qualifier in ARC.
Definition: PrettyPrinter.h:158
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::PrintingPolicy::UseVoidForZeroParams
unsigned UseVoidForZeroParams
Whether we should use &#39;(void)&#39; rather than &#39;()&#39; for a function prototype with zero parameters...
Definition: PrettyPrinter.h:182
clang::PrintingPolicy::Half
unsigned Half
When true, print the half-precision floating-point type as &#39;half&#39; instead of &#39;__fp16&#39;.
Definition: PrettyPrinter.h:198
clang::PrintingPolicy::PrintCanonicalTypes
unsigned PrintCanonicalTypes
Whether to print types as written or canonically.
Definition: PrettyPrinter.h:238
clang::PrintingPolicy::IncludeTagDefinition
unsigned IncludeTagDefinition
When true, include the body of a tag definition.
Definition: PrettyPrinter.h:112
clang::PrintingPolicy::SuppressTemplateArgsInCXXConstructors
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
Definition: PrettyPrinter.h:165
clang::PrintingPolicy::MSVCFormatting
unsigned MSVCFormatting
Use whitespace and punctuation like MSVC does.
Definition: PrettyPrinter.h:210
clang::PrintingPolicy::SuppressImplicitBase
unsigned SuppressImplicitBase
When true, don&#39;t print the implicit &#39;self&#39; or &#39;this&#39; expressions.
Definition: PrettyPrinter.h:231
clang::PrintingPolicy::SuppressTagKeyword
unsigned SuppressTagKeyword
Whether type printing should skip printing the tag keyword.
Definition: PrettyPrinter.h:102
LangOptions.h
Defines the clang::LangOptions interface.
clang::CPlusPlus
Definition: LangStandard.h:47
clang::PrinterHelper::handledStmt
virtual bool handledStmt(Stmt *E, raw_ostream &OS)=0
clang::PrintingPolicy::ConstantArraySizeAsWritten
unsigned ConstantArraySizeAsWritten
Whether we should print the sizes of constant array expressions as written in the sources...
Definition: PrettyPrinter.h:150
clang::PrintingPolicy::SuppressSpecifiers
unsigned SuppressSpecifiers
Whether we should suppress printing of the actual specifiers for the given type or declaration...
Definition: PrettyPrinter.h:92
clang::PrintingPolicy::Restrict
unsigned Restrict
Whether we can use &#39;restrict&#39; rather than &#39;__restrict&#39;.
Definition: PrettyPrinter.h:172
clang::PrintingPolicy::AnonymousTagLocations
unsigned AnonymousTagLocations
When printing an anonymous tag name, also print the location of that entity (e.g., "enum <anonymous at t.h:10:5>").
Definition: PrettyPrinter.h:155
clang::PrintingPolicy::Bool
unsigned Bool
Whether we can use &#39;bool&#39; rather than &#39;_Bool&#39; (even if the language doesn&#39;t actually have &#39;bool&#39;...
Definition: PrettyPrinter.h:169
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:76
clang::PrintingPolicy::ConstantsAsWritten
unsigned ConstantsAsWritten
Whether we should print the constant expressions as written in the sources.
Definition: PrettyPrinter.h:228
clang::PrintingPolicy::UnderscoreAlignof
unsigned UnderscoreAlignof
Whether we can use &#39;_Alignof&#39; rather than &#39;__alignof&#39;.
Definition: PrettyPrinter.h:178
clang::PrintingPolicy::SuppressScope
unsigned SuppressScope
Suppresses printing of scope specifiers.
Definition: PrettyPrinter.h:115
clang::PrintingPolicy::SuppressUnwrittenScope
unsigned SuppressUnwrittenScope
Suppress printing parts of scope specifiers that don&#39;t need to be written, e.g., for inline or anonym...
Definition: PrettyPrinter.h:119
clang::PrinterHelper
Definition: PrettyPrinter.h:26
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::C99
Definition: LangStandard.h:43
clang::PrintingPolicy::adjustForCPlusPlus
void adjustForCPlusPlus()
Adjust this printing policy for cases where it&#39;s known that we&#39;re printing C++ code (for instance...
Definition: PrettyPrinter.h:69
clang::C11
Definition: LangStandard.h:44
clang::PrintingCallbacks
Callbacks to use to customize the behavior of the pretty-printer.
Definition: PrettyPrinter.h:33
clang::PrintingPolicy::Alignof
unsigned Alignof
Whether we can use &#39;alignof&#39; rather than &#39;__alignof&#39;.
Definition: PrettyPrinter.h:175
clang::PrintingPolicy::FullyQualifiedName
unsigned FullyQualifiedName
When true, print the fully qualified name of function declarations.
Definition: PrettyPrinter.h:235
clang::PrintingPolicy::IncludeNewlines
unsigned IncludeNewlines
When true, include newlines after statements like "break", etc.
Definition: PrettyPrinter.h:205
true
#define true
Definition: stdbool.h:16
clang::PrintingPolicy::PolishForDeclaration
unsigned PolishForDeclaration
When true, do certain refinement needed for producing proper declaration tag; such as...
Definition: PrettyPrinter.h:194