Coverage Report

Created: 2020-02-25 14:32

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