1 //===--- PrettyPrinter.h - Classes for aiding with AST printing -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PrinterHelper interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_PRETTYPRINTER_H
15 #define LLVM_CLANG_AST_PRETTYPRINTER_H
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/LangOptions.h"
29 virtual ~PrinterHelper();
30 virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
33 /// Describes how types, statements, expressions, and declarations should be
36 /// This type is intended to be small and suitable for passing by value.
37 /// It is very frequently copied.
38 struct PrintingPolicy {
39 /// \brief Create a default printing policy for the specified language.
40 PrintingPolicy(const LangOptions &LO)
41 : Indentation(2), SuppressSpecifiers(false),
42 SuppressTagKeyword(LO.CPlusPlus),
43 IncludeTagDefinition(false), SuppressScope(false),
44 SuppressUnwrittenScope(false), SuppressInitializers(false),
45 ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
46 SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false),
47 SuppressTemplateArgsInCXXConstructors(false),
48 Bool(LO.Bool), Restrict(LO.C99),
49 Alignof(LO.CPlusPlus11), UnderscoreAlignof(LO.C11),
50 UseVoidForZeroParams(!LO.CPlusPlus),
51 TerseOutput(false), PolishForDeclaration(false),
52 Half(LO.Half), MSWChar(LO.MicrosoftExt && !LO.WChar),
53 IncludeNewlines(true), MSVCFormatting(false),
54 ConstantsAsWritten(false), SuppressImplicitBase(false),
55 FullyQualifiedName(false) { }
57 /// Adjust this printing policy for cases where it's known that we're
58 /// printing C++ code (for instance, if AST dumping reaches a C++-only
59 /// construct). This should not be used if a real LangOptions object is
61 void adjustForCPlusPlus() {
62 SuppressTagKeyword = true;
64 UseVoidForZeroParams = false;
67 /// The number of spaces to use to indent each line.
68 unsigned Indentation : 8;
70 /// Whether we should suppress printing of the actual specifiers for
71 /// the given type or declaration.
73 /// This flag is only used when we are printing declarators beyond
74 /// the first declarator within a declaration group. For example, given:
80 /// SuppressSpecifiers will be false when printing the
81 /// declaration for "x", so that we will print "int *x"; it will be
82 /// \c true when we print "y", so that we suppress printing the
83 /// "const int" type specifier and instead only print the "*y".
84 bool SuppressSpecifiers : 1;
86 /// Whether type printing should skip printing the tag keyword.
88 /// This is used when printing the inner type of elaborated types,
89 /// (as the tag keyword is part of the elaborated type):
92 /// struct Geometry::Point;
94 bool SuppressTagKeyword : 1;
96 /// When true, include the body of a tag definition.
98 /// This is used to place the definition of a struct
99 /// in the middle of another declaration as with:
102 /// typedef struct { int x, y; } Point;
104 bool IncludeTagDefinition : 1;
106 /// Suppresses printing of scope specifiers.
107 bool SuppressScope : 1;
109 /// Suppress printing parts of scope specifiers that don't need
110 /// to be written, e.g., for inline or anonymous namespaces.
111 bool SuppressUnwrittenScope : 1;
113 /// Suppress printing of variable initializers.
115 /// This flag is used when printing the loop variable in a for-range
116 /// statement. For example, given:
119 /// for (auto x : coll)
122 /// SuppressInitializers will be true when printing "auto x", so that the
123 /// internal initializer constructed for x will not be printed.
124 bool SuppressInitializers : 1;
126 /// Whether we should print the sizes of constant array expressions as written
129 /// This flag determines whether array types declared as
133 /// char a[] = "A string";
136 /// will be printed as written or as follows:
140 /// char a[9] = "A string";
142 bool ConstantArraySizeAsWritten : 1;
144 /// When printing an anonymous tag name, also print the location of that
145 /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
146 /// "(anonymous)" for the name.
147 bool AnonymousTagLocations : 1;
149 /// When true, suppress printing of the __strong lifetime qualifier in ARC.
150 unsigned SuppressStrongLifetime : 1;
152 /// When true, suppress printing of lifetime qualifier in ARC.
153 unsigned SuppressLifetimeQualifiers : 1;
155 /// When true, suppresses printing template arguments in names of C++
157 unsigned SuppressTemplateArgsInCXXConstructors : 1;
159 /// Whether we can use 'bool' rather than '_Bool' (even if the language
160 /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
163 /// Whether we can use 'restrict' rather than '__restrict'.
164 unsigned Restrict : 1;
166 /// Whether we can use 'alignof' rather than '__alignof'.
167 unsigned Alignof : 1;
169 /// Whether we can use '_Alignof' rather than '__alignof'.
170 unsigned UnderscoreAlignof : 1;
172 /// Whether we should use '(void)' rather than '()' for a function prototype
173 /// with zero parameters.
174 unsigned UseVoidForZeroParams : 1;
176 /// Provide a 'terse' output.
178 /// For example, in this mode we don't print function bodies, class members,
179 /// declarations inside namespaces etc. Effectively, this should print
180 /// only the requested declaration.
181 unsigned TerseOutput : 1;
183 /// When true, do certain refinement needed for producing proper declaration
184 /// tag; such as, do not print attributes attached to the declaration.
186 unsigned PolishForDeclaration : 1;
188 /// When true, print the half-precision floating-point type as 'half'
189 /// instead of '__fp16'
192 /// When true, print the built-in wchar_t type as __wchar_t. For use in
193 /// Microsoft mode when wchar_t is not available.
194 unsigned MSWChar : 1;
196 /// When true, include newlines after statements like "break", etc.
197 unsigned IncludeNewlines : 1;
199 /// Use whitespace and punctuation like MSVC does. In particular, this prints
200 /// anonymous namespaces as `anonymous namespace' and does not insert spaces
201 /// after template arguments.
202 bool MSVCFormatting : 1;
204 /// Whether we should print the constant expressions as written in the
207 /// This flag determines whether constants expressions like
214 /// will be printed as written or as follows:
220 bool ConstantsAsWritten : 1;
222 /// When true, don't print the implicit 'self' or 'this' expressions.
223 bool SuppressImplicitBase : 1;
225 /// When true, print the fully qualified name of function declarations.
226 /// This is the opposite of SuppressScope and thus overrules it.
227 bool FullyQualifiedName : 1;
230 } // end namespace clang