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