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