Coverage Report

Created: 2022-01-18 06:27

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