Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Serialization/ASTRecordWriter.h
Line
Count
Source (jump to first uncovered line)
1
//===- ASTRecordWriter.h - Helper classes for writing AST -------*- 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 ASTRecordWriter class, a helper class useful
10
//  when serializing AST.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDWRITER_H
15
#define LLVM_CLANG_SERIALIZATION_ASTRECORDWRITER_H
16
17
#include "clang/AST/AbstractBasicWriter.h"
18
#include "clang/AST/OpenMPClause.h"
19
#include "clang/Serialization/ASTWriter.h"
20
21
namespace clang {
22
23
class TypeLoc;
24
25
/// An object for streaming information to a record.
26
class ASTRecordWriter
27
    : public serialization::DataStreamBasicWriter<ASTRecordWriter> {
28
  ASTWriter *Writer;
29
  ASTWriter::RecordDataImpl *Record;
30
31
  /// Statements that we've encountered while serializing a
32
  /// declaration or type.
33
  SmallVector<Stmt *, 16> StmtsToEmit;
34
35
  /// Indices of record elements that describe offsets within the
36
  /// bitcode. These will be converted to offsets relative to the current
37
  /// record when emitted.
38
  SmallVector<unsigned, 8> OffsetIndices;
39
40
  /// Flush all of the statements and expressions that have
41
  /// been added to the queue via AddStmt().
42
  void FlushStmts();
43
  void FlushSubStmts();
44
45
10.1M
  void PrepareToEmit(uint64_t MyOffset) {
46
    // Convert offsets into relative form.
47
1.86M
    for (unsigned I : OffsetIndices) {
48
1.86M
      auto &StoredOffset = (*Record)[I];
49
1.86M
      assert(StoredOffset < MyOffset && "invalid offset");
50
1.86M
      if (StoredOffset)
51
590k
        StoredOffset = MyOffset - StoredOffset;
52
1.86M
    }
53
10.1M
    OffsetIndices.clear();
54
10.1M
  }
55
56
public:
57
  /// Construct a ASTRecordWriter that uses the default encoding scheme.
58
  ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
59
10.7M
      : Writer(&Writer), Record(&Record) {}
60
61
  /// Construct a ASTRecordWriter that uses the same encoding scheme as another
62
  /// ASTRecordWriter.
63
  ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
64
84.1k
      : Writer(Parent.Writer), Record(&Record) {}
65
66
  /// Copying an ASTRecordWriter is almost certainly a bug.
67
  ASTRecordWriter(const ASTRecordWriter &) = delete;
68
  ASTRecordWriter &operator=(const ASTRecordWriter &) = delete;
69
70
  /// Extract the underlying record storage.
71
0
  ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
72
73
  /// Minimal vector-like interface.
74
  /// @{
75
144M
  void push_back(uint64_t N) { Record->push_back(N); }
76
  template<typename InputIterator>
77
55.7k
  void append(InputIterator begin, InputIterator end) {
78
55.7k
    Record->append(begin, end);
79
55.7k
  }
80
0
  bool empty() const { return Record->empty(); }
81
279k
  size_t size() const { return Record->size(); }
82
139k
  uint64_t &operator[](size_t N) { return (*Record)[N]; }
83
  /// @}
84
85
  /// Emit the record to the stream, followed by its substatements, and
86
  /// return its offset.
87
  // FIXME: Allow record producers to suggest Abbrevs.
88
5.05M
  uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
89
5.05M
    uint64_t Offset = Writer->Stream.GetCurrentBitNo();
90
5.05M
    PrepareToEmit(Offset);
91
5.05M
    Writer->Stream.EmitRecord(Code, *Record, Abbrev);
92
5.05M
    FlushStmts();
93
5.05M
    return Offset;
94
5.05M
  }
95
96
  /// Emit the record to the stream, preceded by its substatements.
97
5.05M
  uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
98
5.05M
    FlushSubStmts();
99
5.05M
    PrepareToEmit(Writer->Stream.GetCurrentBitNo());
100
5.05M
    Writer->Stream.EmitRecord(Code, *Record, Abbrev);
101
5.05M
    return Writer->Stream.GetCurrentBitNo();
102
5.05M
  }
103
104
  /// Add a bit offset into the record. This will be converted into an
105
  /// offset relative to the current record when emitted.
106
1.86M
  void AddOffset(uint64_t BitOffset) {
107
1.86M
    OffsetIndices.push_back(Record->size());
108
1.86M
    Record->push_back(BitOffset);
109
1.86M
  }
110
111
  /// Add the given statement or expression to the queue of
112
  /// statements to emit.
113
  ///
114
  /// This routine should be used when emitting types and declarations
115
  /// that have expressions as part of their formulation. Once the
116
  /// type or declaration has been written, Emit() will write
117
  /// the corresponding statements just after the record.
118
5.72M
  void AddStmt(Stmt *S) {
119
5.72M
    StmtsToEmit.push_back(S);
120
5.72M
  }
121
167k
  void writeStmtRef(const Stmt *S) {
122
167k
    AddStmt(const_cast<Stmt*>(S));
123
167k
  }
124
125
  /// Add a definition for the given function to the queue of statements
126
  /// to emit.
127
  void AddFunctionDefinition(const FunctionDecl *FD);
128
129
  /// Emit a source location.
130
18.1M
  void AddSourceLocation(SourceLocation Loc) {
131
18.1M
    return Writer->AddSourceLocation(Loc, *Record);
132
18.1M
  }
133
5.72k
  void writeSourceLocation(SourceLocation Loc) {
134
5.72k
    AddSourceLocation(Loc);
135
5.72k
  }
136
137
  /// Emit a source range.
138
2.27M
  void AddSourceRange(SourceRange Range) {
139
2.27M
    return Writer->AddSourceRange(Range, *Record);
140
2.27M
  }
141
142
3.28M
  void writeBool(bool Value) {
143
3.28M
    Record->push_back(Value);
144
3.28M
  }
145
146
8.60M
  void writeUInt32(uint32_t Value) {
147
8.60M
    Record->push_back(Value);
148
8.60M
  }
149
150
644k
  void writeUInt64(uint64_t Value) {
151
644k
    Record->push_back(Value);
152
644k
  }
153
154
  /// Emit an integral value.
155
425k
  void AddAPInt(const llvm::APInt &Value) {
156
425k
    writeAPInt(Value);
157
425k
  }
158
159
  /// Emit a signed integral value.
160
175k
  void AddAPSInt(const llvm::APSInt &Value) {
161
175k
    writeAPSInt(Value);
162
175k
  }
163
164
  /// Emit a floating-point value.
165
  void AddAPFloat(const llvm::APFloat &Value);
166
167
  /// Emit an APvalue.
168
  void AddAPValue(const APValue &Value);
169
170
  /// Emit a reference to an identifier.
171
6.16M
  void AddIdentifierRef(const IdentifierInfo *II) {
172
6.16M
    return Writer->AddIdentifierRef(II, *Record);
173
6.16M
  }
174
3.27M
  void writeIdentifier(const IdentifierInfo *II) {
175
3.27M
    AddIdentifierRef(II);
176
3.27M
  }
177
178
  /// Emit a Selector (which is a smart pointer reference).
179
  void AddSelectorRef(Selector S);
180
91.9k
  void writeSelector(Selector sel) {
181
91.9k
    AddSelectorRef(sel);
182
91.9k
  }
183
184
  /// Emit a CXXTemporary.
185
  void AddCXXTemporary(const CXXTemporary *Temp);
186
187
  /// Emit a C++ base specifier.
188
  void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
189
190
  /// Emit a set of C++ base specifiers.
191
  void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
192
193
  /// Emit a reference to a type.
194
14.1M
  void AddTypeRef(QualType T) {
195
14.1M
    return Writer->AddTypeRef(T, *Record);
196
14.1M
  }
197
2.79M
  void writeQualType(QualType T) {
198
2.79M
    AddTypeRef(T);
199
2.79M
  }
200
201
  /// Emits a reference to a declarator info.
202
  void AddTypeSourceInfo(TypeSourceInfo *TInfo);
203
204
  /// Emits source location information for a type. Does not emit the type.
205
  void AddTypeLoc(TypeLoc TL);
206
207
  /// Emits a template argument location info.
208
  void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
209
                                  const TemplateArgumentLocInfo &Arg);
210
211
  /// Emits a template argument location.
212
  void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
213
214
  /// Emits an AST template argument list info.
215
  void AddASTTemplateArgumentListInfo(
216
      const ASTTemplateArgumentListInfo *ASTTemplArgList);
217
218
  /// Emit a reference to a declaration.
219
10.9M
  void AddDeclRef(const Decl *D) {
220
10.9M
    return Writer->AddDeclRef(D, *Record);
221
10.9M
  }
222
940k
  void writeDeclRef(const Decl *D) {
223
940k
    AddDeclRef(D);
224
940k
  }
225
226
  /// Emit a declaration name.
227
3.48M
  void AddDeclarationName(DeclarationName Name) {
228
3.48M
    writeDeclarationName(Name);
229
3.48M
  }
230
231
  void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
232
                             DeclarationName Name);
233
  void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
234
235
  void AddQualifierInfo(const QualifierInfo &Info);
236
237
  /// Emit a nested name specifier.
238
0
  void AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
239
0
    writeNestedNameSpecifier(NNS);
240
0
  }
241
242
  /// Emit a nested name specifier with source-location information.
243
  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
244
245
  /// Emit a template name.
246
0
  void AddTemplateName(TemplateName Name) {
247
0
    writeTemplateName(Name);
248
0
  }
249
250
  /// Emit a template argument.
251
153k
  void AddTemplateArgument(const TemplateArgument &Arg) {
252
153k
    writeTemplateArgument(Arg);
253
153k
  }
254
255
  /// Emit a template parameter list.
256
  void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
257
258
  /// Emit a template argument list.
259
  void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
260
261
  /// Emit a UnresolvedSet structure.
262
  void AddUnresolvedSet(const ASTUnresolvedSet &Set);
263
264
  /// Emit a CXXCtorInitializer array.
265
  void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer *> CtorInits);
266
267
  void AddCXXDefinitionData(const CXXRecordDecl *D);
268
269
  /// Write an OMPTraitInfo object.
270
  void writeOMPTraitInfo(const OMPTraitInfo *TI);
271
272
  void writeOMPClause(OMPClause *C);
273
274
  /// Writes data related to the OpenMP directives.
275
  void writeOMPChildren(OMPChildren *Data);
276
277
  /// Emit a string.
278
709k
  void AddString(StringRef Str) {
279
709k
    return Writer->AddString(Str, *Record);
280
709k
  }
281
282
  /// Emit a path.
283
0
  void AddPath(StringRef Path) {
284
0
    return Writer->AddPath(Path, *Record);
285
0
  }
286
287
  /// Emit a version tuple.
288
967k
  void AddVersionTuple(const VersionTuple &Version) {
289
967k
    return Writer->AddVersionTuple(Version, *Record);
290
967k
  }
291
292
  // Emit an attribute.
293
  void AddAttr(const Attr *A);
294
295
  /// Emit a list of attributes.
296
  void AddAttributes(ArrayRef<const Attr*> Attrs);
297
};
298
299
} // end namespace clang
300
301
#endif