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