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