1 //===--- PrettyPrinter.h - Classes for aiding with AST printing -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines helper types for AST pretty-printing.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_PRETTYPRINTER_H
14 #define LLVM_CLANG_AST_PRETTYPRINTER_H
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/LangOptions.h"
27 virtual ~PrinterHelper();
28 virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
31 /// Callbacks to use to customize the behavior of the pretty-printer.
32 class PrintingCallbacks {
34 ~PrintingCallbacks() = default;
37 /// Remap a path to a form suitable for printing.
38 virtual std::string remapPath(StringRef Path) const {
39 return std::string(Path);
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; }
52 /// Describes how types, statements, expressions, and declarations should be
55 /// This type is intended to be small and suitable for passing by value.
56 /// It is very frequently copied.
57 struct PrintingPolicy {
58 /// Create a default printing policy for the specified language.
59 PrintingPolicy(const LangOptions &LO)
60 : Indentation(2), SuppressSpecifiers(false),
61 SuppressTagKeyword(LO.CPlusPlus), IncludeTagDefinition(false),
62 SuppressScope(false), SuppressUnwrittenScope(false),
63 SuppressInlineNamespace(true), SuppressInitializers(false),
64 ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
65 SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false),
66 SuppressTemplateArgsInCXXConstructors(false),
67 SuppressDefaultTemplateArgs(true), Bool(LO.Bool),
68 Nullptr(LO.CPlusPlus11), Restrict(LO.C99), Alignof(LO.CPlusPlus11),
69 UnderscoreAlignof(LO.C11), UseVoidForZeroParams(!LO.CPlusPlus),
70 SplitTemplateClosers(!LO.CPlusPlus11), TerseOutput(false),
71 PolishForDeclaration(false), Half(LO.Half),
72 MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
73 MSVCFormatting(false), ConstantsAsWritten(false),
74 SuppressImplicitBase(false), FullyQualifiedName(false),
75 PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
76 UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
77 CleanUglifiedParameters(false) {}
79 /// Adjust this printing policy for cases where it's known that we're
80 /// printing C++ code (for instance, if AST dumping reaches a C++-only
81 /// construct). This should not be used if a real LangOptions object is
83 void adjustForCPlusPlus() {
84 SuppressTagKeyword = true;
86 UseVoidForZeroParams = false;
89 /// The number of spaces to use to indent each line.
90 unsigned Indentation : 8;
92 /// Whether we should suppress printing of the actual specifiers for
93 /// the given type or declaration.
95 /// This flag is only used when we are printing declarators beyond
96 /// the first declarator within a declaration group. For example, given:
102 /// SuppressSpecifiers will be false when printing the
103 /// declaration for "x", so that we will print "int *x"; it will be
104 /// \c true when we print "y", so that we suppress printing the
105 /// "const int" type specifier and instead only print the "*y".
106 unsigned SuppressSpecifiers : 1;
108 /// Whether type printing should skip printing the tag keyword.
110 /// This is used when printing the inner type of elaborated types,
111 /// (as the tag keyword is part of the elaborated type):
114 /// struct Geometry::Point;
116 unsigned SuppressTagKeyword : 1;
118 /// When true, include the body of a tag definition.
120 /// This is used to place the definition of a struct
121 /// in the middle of another declaration as with:
124 /// typedef struct { int x, y; } Point;
126 unsigned IncludeTagDefinition : 1;
128 /// Suppresses printing of scope specifiers.
129 unsigned SuppressScope : 1;
131 /// Suppress printing parts of scope specifiers that are never
132 /// written, e.g., for anonymous namespaces.
133 unsigned SuppressUnwrittenScope : 1;
135 /// Suppress printing parts of scope specifiers that correspond
136 /// to inline namespaces, where the name is unambiguous with the specifier
138 unsigned SuppressInlineNamespace : 1;
140 /// Suppress printing of variable initializers.
142 /// This flag is used when printing the loop variable in a for-range
143 /// statement. For example, given:
146 /// for (auto x : coll)
149 /// SuppressInitializers will be true when printing "auto x", so that the
150 /// internal initializer constructed for x will not be printed.
151 unsigned SuppressInitializers : 1;
153 /// Whether we should print the sizes of constant array expressions as written
156 /// This flag determines whether array types declared as
160 /// char a[] = "A string";
163 /// will be printed as written or as follows:
167 /// char a[9] = "A string";
169 unsigned ConstantArraySizeAsWritten : 1;
171 /// When printing an anonymous tag name, also print the location of that
172 /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
173 /// "(anonymous)" for the name.
174 unsigned AnonymousTagLocations : 1;
176 /// When true, suppress printing of the __strong lifetime qualifier in ARC.
177 unsigned SuppressStrongLifetime : 1;
179 /// When true, suppress printing of lifetime qualifier in ARC.
180 unsigned SuppressLifetimeQualifiers : 1;
182 /// When true, suppresses printing template arguments in names of C++
184 unsigned SuppressTemplateArgsInCXXConstructors : 1;
186 /// When true, attempt to suppress template arguments that match the default
187 /// argument for the parameter.
188 unsigned SuppressDefaultTemplateArgs : 1;
190 /// Whether we can use 'bool' rather than '_Bool' (even if the language
191 /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
194 /// Whether we should use 'nullptr' rather than '0' as a null pointer
196 unsigned Nullptr : 1;
198 /// Whether we can use 'restrict' rather than '__restrict'.
199 unsigned Restrict : 1;
201 /// Whether we can use 'alignof' rather than '__alignof'.
202 unsigned Alignof : 1;
204 /// Whether we can use '_Alignof' rather than '__alignof'.
205 unsigned UnderscoreAlignof : 1;
207 /// Whether we should use '(void)' rather than '()' for a function prototype
208 /// with zero parameters.
209 unsigned UseVoidForZeroParams : 1;
211 /// Whether nested templates must be closed like 'a\<b\<c\> \>' rather than
213 unsigned SplitTemplateClosers : 1;
215 /// Provide a 'terse' output.
217 /// For example, in this mode we don't print function bodies, class members,
218 /// declarations inside namespaces etc. Effectively, this should print
219 /// only the requested declaration.
220 unsigned TerseOutput : 1;
222 /// When true, do certain refinement needed for producing proper declaration
223 /// tag; such as, do not print attributes attached to the declaration.
225 unsigned PolishForDeclaration : 1;
227 /// When true, print the half-precision floating-point type as 'half'
228 /// instead of '__fp16'
231 /// When true, print the built-in wchar_t type as __wchar_t. For use in
232 /// Microsoft mode when wchar_t is not available.
233 unsigned MSWChar : 1;
235 /// When true, include newlines after statements like "break", etc.
236 unsigned IncludeNewlines : 1;
238 /// Use whitespace and punctuation like MSVC does. In particular, this prints
239 /// anonymous namespaces as `anonymous namespace' and does not insert spaces
240 /// after template arguments.
241 unsigned MSVCFormatting : 1;
243 /// Whether we should print the constant expressions as written in the
246 /// This flag determines whether constants expressions like
253 /// will be printed as written or as follows:
259 unsigned ConstantsAsWritten : 1;
261 /// When true, don't print the implicit 'self' or 'this' expressions.
262 unsigned SuppressImplicitBase : 1;
264 /// When true, print the fully qualified name of function declarations.
265 /// This is the opposite of SuppressScope and thus overrules it.
266 unsigned FullyQualifiedName : 1;
268 /// Whether to print types as written or canonically.
269 unsigned PrintCanonicalTypes : 1;
271 /// Whether to print an InjectedClassNameType with template arguments or as
272 /// written. When a template argument is unnamed, printing it results in
273 /// invalid C++ code.
274 unsigned PrintInjectedClassNameWithArguments : 1;
276 /// Whether to use C++ template preferred_name attributes when printing
278 unsigned UsePreferredNames : 1;
280 /// Whether to use type suffixes (eg: 1U) on integral non-type template
282 unsigned AlwaysIncludeTypeForTemplateArgument : 1;
284 /// Whether to strip underscores when printing reserved parameter names.
285 /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
286 /// This only affects parameter names, and so describes a compatible API.
287 unsigned CleanUglifiedParameters : 1;
289 /// Callbacks to use to allow the behavior of printing to be customized.
290 const PrintingCallbacks *Callbacks = nullptr;
293 } // end namespace clang