Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Serialization/ASTWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ASTWriter.cpp - AST File Writer ------------------------------------===//
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 ASTWriter class, which writes AST files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "ASTCommon.h"
14
#include "ASTReaderInternals.h"
15
#include "MultiOnDiskHashTable.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/AST/ASTUnresolvedSet.h"
18
#include "clang/AST/AbstractTypeWriter.h"
19
#include "clang/AST/Attr.h"
20
#include "clang/AST/Decl.h"
21
#include "clang/AST/DeclBase.h"
22
#include "clang/AST/DeclCXX.h"
23
#include "clang/AST/DeclContextInternals.h"
24
#include "clang/AST/DeclFriend.h"
25
#include "clang/AST/DeclObjC.h"
26
#include "clang/AST/DeclTemplate.h"
27
#include "clang/AST/DeclarationName.h"
28
#include "clang/AST/Expr.h"
29
#include "clang/AST/ExprCXX.h"
30
#include "clang/AST/LambdaCapture.h"
31
#include "clang/AST/NestedNameSpecifier.h"
32
#include "clang/AST/OpenMPClause.h"
33
#include "clang/AST/RawCommentList.h"
34
#include "clang/AST/TemplateName.h"
35
#include "clang/AST/Type.h"
36
#include "clang/AST/TypeLocVisitor.h"
37
#include "clang/Basic/Diagnostic.h"
38
#include "clang/Basic/DiagnosticOptions.h"
39
#include "clang/Basic/FileManager.h"
40
#include "clang/Basic/FileSystemOptions.h"
41
#include "clang/Basic/IdentifierTable.h"
42
#include "clang/Basic/LLVM.h"
43
#include "clang/Basic/Lambda.h"
44
#include "clang/Basic/LangOptions.h"
45
#include "clang/Basic/Module.h"
46
#include "clang/Basic/ObjCRuntime.h"
47
#include "clang/Basic/OpenCLOptions.h"
48
#include "clang/Basic/SourceLocation.h"
49
#include "clang/Basic/SourceManager.h"
50
#include "clang/Basic/SourceManagerInternals.h"
51
#include "clang/Basic/Specifiers.h"
52
#include "clang/Basic/TargetInfo.h"
53
#include "clang/Basic/TargetOptions.h"
54
#include "clang/Basic/Version.h"
55
#include "clang/Lex/HeaderSearch.h"
56
#include "clang/Lex/HeaderSearchOptions.h"
57
#include "clang/Lex/MacroInfo.h"
58
#include "clang/Lex/ModuleMap.h"
59
#include "clang/Lex/PreprocessingRecord.h"
60
#include "clang/Lex/Preprocessor.h"
61
#include "clang/Lex/PreprocessorOptions.h"
62
#include "clang/Lex/Token.h"
63
#include "clang/Sema/IdentifierResolver.h"
64
#include "clang/Sema/ObjCMethodList.h"
65
#include "clang/Sema/Sema.h"
66
#include "clang/Sema/Weak.h"
67
#include "clang/Serialization/ASTBitCodes.h"
68
#include "clang/Serialization/ASTReader.h"
69
#include "clang/Serialization/ASTRecordWriter.h"
70
#include "clang/Serialization/InMemoryModuleCache.h"
71
#include "clang/Serialization/ModuleFile.h"
72
#include "clang/Serialization/ModuleFileExtension.h"
73
#include "clang/Serialization/SerializationDiagnostic.h"
74
#include "llvm/ADT/APFloat.h"
75
#include "llvm/ADT/APInt.h"
76
#include "llvm/ADT/APSInt.h"
77
#include "llvm/ADT/ArrayRef.h"
78
#include "llvm/ADT/DenseMap.h"
79
#include "llvm/ADT/Hashing.h"
80
#include "llvm/ADT/PointerIntPair.h"
81
#include "llvm/ADT/STLExtras.h"
82
#include "llvm/ADT/ScopeExit.h"
83
#include "llvm/ADT/SmallPtrSet.h"
84
#include "llvm/ADT/SmallString.h"
85
#include "llvm/ADT/SmallVector.h"
86
#include "llvm/ADT/StringMap.h"
87
#include "llvm/ADT/StringRef.h"
88
#include "llvm/Bitstream/BitCodes.h"
89
#include "llvm/Bitstream/BitstreamWriter.h"
90
#include "llvm/Support/Casting.h"
91
#include "llvm/Support/Compression.h"
92
#include "llvm/Support/DJB.h"
93
#include "llvm/Support/Endian.h"
94
#include "llvm/Support/EndianStream.h"
95
#include "llvm/Support/Error.h"
96
#include "llvm/Support/ErrorHandling.h"
97
#include "llvm/Support/LEB128.h"
98
#include "llvm/Support/MemoryBuffer.h"
99
#include "llvm/Support/OnDiskHashTable.h"
100
#include "llvm/Support/Path.h"
101
#include "llvm/Support/SHA1.h"
102
#include "llvm/Support/TimeProfiler.h"
103
#include "llvm/Support/VersionTuple.h"
104
#include "llvm/Support/raw_ostream.h"
105
#include <algorithm>
106
#include <cassert>
107
#include <cstdint>
108
#include <cstdlib>
109
#include <cstring>
110
#include <ctime>
111
#include <limits>
112
#include <memory>
113
#include <optional>
114
#include <queue>
115
#include <tuple>
116
#include <utility>
117
#include <vector>
118
119
using namespace clang;
120
using namespace clang::serialization;
121
122
template <typename T, typename Allocator>
123
43.7k
static StringRef bytes(const std::vector<T, Allocator> &v) {
124
43.7k
  if (v.empty()) 
return StringRef()2.37k
;
125
41.3k
  return StringRef(reinterpret_cast<const char*>(&v[0]),
126
41.3k
                         sizeof(T) * v.size());
127
43.7k
}
ASTWriter.cpp:llvm::StringRef bytes<unsigned long long, std::__1::allocator<unsigned long long> >(std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> > const&)
Line
Count
Source
123
7.24k
static StringRef bytes(const std::vector<T, Allocator> &v) {
124
7.24k
  if (v.empty()) 
return StringRef()0
;
125
7.24k
  return StringRef(reinterpret_cast<const char*>(&v[0]),
126
7.24k
                         sizeof(T) * v.size());
127
7.24k
}
ASTWriter.cpp:llvm::StringRef bytes<unsigned int, std::__1::allocator<unsigned int> >(std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&)
Line
Count
Source
123
21.9k
static StringRef bytes(const std::vector<T, Allocator> &v) {
124
21.9k
  if (v.empty()) 
return StringRef()2.06k
;
125
19.9k
  return StringRef(reinterpret_cast<const char*>(&v[0]),
126
19.9k
                         sizeof(T) * v.size());
127
21.9k
}
ASTWriter.cpp:llvm::StringRef bytes<clang::serialization::PPSkippedRange, std::__1::allocator<clang::serialization::PPSkippedRange> >(std::__1::vector<clang::serialization::PPSkippedRange, std::__1::allocator<clang::serialization::PPSkippedRange> > const&)
Line
Count
Source
123
34
static StringRef bytes(const std::vector<T, Allocator> &v) {
124
34
  if (v.empty()) 
return StringRef()0
;
125
34
  return StringRef(reinterpret_cast<const char*>(&v[0]),
126
34
                         sizeof(T) * v.size());
127
34
}
ASTWriter.cpp:llvm::StringRef bytes<clang::serialization::UnderalignedInt64, std::__1::allocator<clang::serialization::UnderalignedInt64> >(std::__1::vector<clang::serialization::UnderalignedInt64, std::__1::allocator<clang::serialization::UnderalignedInt64> > const&)
Line
Count
Source
123
7.24k
static StringRef bytes(const std::vector<T, Allocator> &v) {
124
7.24k
  if (v.empty()) 
return StringRef()56
;
125
7.18k
  return StringRef(reinterpret_cast<const char*>(&v[0]),
126
7.18k
                         sizeof(T) * v.size());
127
7.24k
}
ASTWriter.cpp:llvm::StringRef bytes<clang::serialization::DeclOffset, std::__1::allocator<clang::serialization::DeclOffset> >(std::__1::vector<clang::serialization::DeclOffset, std::__1::allocator<clang::serialization::DeclOffset> > const&)
Line
Count
Source
123
7.24k
static StringRef bytes(const std::vector<T, Allocator> &v) {
124
7.24k
  if (v.empty()) 
return StringRef()253
;
125
6.98k
  return StringRef(reinterpret_cast<const char*>(&v[0]),
126
6.98k
                         sizeof(T) * v.size());
127
7.24k
}
128
129
template <typename T>
130
221k
static StringRef bytes(const SmallVectorImpl<T> &v) {
131
221k
  return StringRef(reinterpret_cast<const char*>(v.data()),
132
221k
                         sizeof(T) * v.size());
133
221k
}
ASTWriter.cpp:llvm::StringRef bytes<clang::serialization::PPEntityOffset>(llvm::SmallVectorImpl<clang::serialization::PPEntityOffset> const&)
Line
Count
Source
130
217
static StringRef bytes(const SmallVectorImpl<T> &v) {
131
217
  return StringRef(reinterpret_cast<const char*>(v.data()),
132
217
                         sizeof(T) * v.size());
133
217
}
ASTWriter.cpp:llvm::StringRef bytes<unsigned int>(llvm::SmallVectorImpl<unsigned int> const&)
Line
Count
Source
130
220k
static StringRef bytes(const SmallVectorImpl<T> &v) {
131
220k
  return StringRef(reinterpret_cast<const char*>(v.data()),
132
220k
                         sizeof(T) * v.size());
133
220k
}
134
135
7.24k
static std::string bytes(const std::vector<bool> &V) {
136
7.24k
  std::string Str;
137
7.24k
  Str.reserve(V.size() / 8);
138
14.4k
  for (unsigned I = 0, E = V.size(); I < E;) {
139
7.23k
    char Byte = 0;
140
21.9k
    for (unsigned Bit = 0; Bit < 8 && 
I < E21.9k
;
++Bit, ++I14.7k
)
141
14.7k
      Byte |= V[I] << Bit;
142
7.23k
    Str += Byte;
143
7.23k
  }
144
7.24k
  return Str;
145
7.24k
}
146
147
//===----------------------------------------------------------------------===//
148
// Type serialization
149
//===----------------------------------------------------------------------===//
150
151
799k
static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
152
799k
  switch (id) {
153
0
#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
154
799k
  case Type::CLASS_ID: return TYPE_##CODE_ID;
155
0
#include "clang/Serialization/TypeBitCodes.def"
156
0
  case Type::Builtin:
157
0
    llvm_unreachable("shouldn't be serializing a builtin type this way");
158
799k
  }
159
0
  llvm_unreachable("bad type kind");
160
0
}
161
162
namespace {
163
164
std::set<const FileEntry *> GetAffectingModuleMaps(const Preprocessor &PP,
165
3.51k
                                                   Module *RootModule) {
166
3.51k
  SmallVector<const Module *> ModulesToProcess{RootModule};
167
168
3.51k
  const HeaderSearch &HS = PP.getHeaderSearchInfo();
169
170
3.51k
  SmallVector<OptionalFileEntryRef, 16> FilesByUID;
171
3.51k
  HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
172
173
3.51k
  if (FilesByUID.size() > HS.header_file_size())
174
2.15k
    FilesByUID.resize(HS.header_file_size());
175
176
1.73M
  for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; 
++UID1.73M
) {
177
1.73M
    OptionalFileEntryRef File = FilesByUID[UID];
178
1.73M
    if (!File)
179
0
      continue;
180
181
1.73M
    const HeaderFileInfo *HFI =
182
1.73M
        HS.getExistingFileInfo(*File, /*WantExternal*/ false);
183
1.73M
    if (!HFI || 
(1.42M
HFI->isModuleHeader1.42M
&&
!HFI->isCompilingModuleHeader1.41M
))
184
1.71M
      continue;
185
186
15.9k
    for (const auto &KH : HS.findResolvedModulesForHeader(*File)) {
187
13.8k
      if (!KH.getModule())
188
0
        continue;
189
13.8k
      ModulesToProcess.push_back(KH.getModule());
190
13.8k
    }
191
15.9k
  }
192
193
3.51k
  const ModuleMap &MM = HS.getModuleMap();
194
3.51k
  SourceManager &SourceMgr = PP.getSourceManager();
195
196
3.51k
  std::set<const FileEntry *> ModuleMaps{};
197
45.5k
  auto CollectIncludingModuleMaps = [&](FileEntryRef F) {
198
45.5k
    if (!ModuleMaps.insert(F).second)
199
41.1k
      return;
200
4.36k
    FileID FID = SourceMgr.translateFile(F);
201
4.36k
    SourceLocation Loc = SourceMgr.getIncludeLoc(FID);
202
    // The include location of inferred module maps can point into the header
203
    // file that triggered the inferring. Cut off the walk if that's the case.
204
4.36k
    while (Loc.isValid() && 
isModuleMap(SourceMgr.getFileCharacteristic(Loc))166
) {
205
38
      FID = SourceMgr.getFileID(Loc);
206
38
      if (!ModuleMaps.insert(*SourceMgr.getFileEntryRefForID(FID)).second)
207
32
        break;
208
6
      Loc = SourceMgr.getIncludeLoc(FID);
209
6
    }
210
4.36k
  };
211
212
3.51k
  std::set<const Module *> ProcessedModules;
213
73.5k
  auto CollectIncludingMapsFromAncestors = [&](const Module *M) {
214
99.0k
    for (const Module *Mod = M; Mod; 
Mod = Mod->Parent25.4k
) {
215
84.1k
      if (!ProcessedModules.insert(Mod).second)
216
58.7k
        break;
217
      // The containing module map is affecting, because it's being pointed
218
      // into by Module::DefinitionLoc.
219
25.4k
      if (auto ModuleMapFile = MM.getContainingModuleMapFile(Mod))
220
20.7k
        CollectIncludingModuleMaps(*ModuleMapFile);
221
      // For inferred modules, the module map that allowed inferring is not in
222
      // the include chain of the virtual containing module map file. It did
223
      // affect the compilation, though.
224
25.4k
      if (auto ModuleMapFile = MM.getModuleMapFileForUniquing(Mod))
225
24.7k
        CollectIncludingModuleMaps(*ModuleMapFile);
226
25.4k
    }
227
73.5k
  };
228
229
17.3k
  for (const Module *CurrentModule : ModulesToProcess) {
230
17.3k
    CollectIncludingMapsFromAncestors(CurrentModule);
231
17.3k
    for (const Module *ImportedModule : CurrentModule->Imports)
232
55.7k
      CollectIncludingMapsFromAncestors(ImportedModule);
233
17.3k
    for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
234
481
      CollectIncludingMapsFromAncestors(UndeclaredModule);
235
17.3k
  }
236
237
3.51k
  return ModuleMaps;
238
3.51k
}
239
240
class ASTTypeWriter {
241
  ASTWriter &Writer;
242
  ASTWriter::RecordData Record;
243
  ASTRecordWriter BasicWriter;
244
245
public:
246
  ASTTypeWriter(ASTWriter &Writer)
247
805k
    : Writer(Writer), BasicWriter(Writer, Record) {}
248
249
805k
  uint64_t write(QualType T) {
250
805k
    if (T.hasLocalNonFastQualifiers()) {
251
6.36k
      Qualifiers Qs = T.getLocalQualifiers();
252
6.36k
      BasicWriter.writeQualType(T.getLocalUnqualifiedType());
253
6.36k
      BasicWriter.writeQualifiers(Qs);
254
6.36k
      return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
255
6.36k
    }
256
257
799k
    const Type *typePtr = T.getTypePtr();
258
799k
    serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
259
799k
    atw.write(typePtr);
260
799k
    return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
261
799k
                            /*abbrev*/ 0);
262
805k
  }
263
};
264
265
class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
266
  using LocSeq = SourceLocationSequence;
267
268
  ASTRecordWriter &Record;
269
  LocSeq *Seq;
270
271
4.07M
  void addSourceLocation(SourceLocation Loc) {
272
4.07M
    Record.AddSourceLocation(Loc, Seq);
273
4.07M
  }
274
248k
  void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range, Seq); }
275
276
public:
277
  TypeLocWriter(ASTRecordWriter &Record, LocSeq *Seq)
278
1.51M
      : Record(Record), Seq(Seq) {}
279
280
#define ABSTRACT_TYPELOC(CLASS, PARENT)
281
#define TYPELOC(CLASS, PARENT) \
282
    void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
283
#include "clang/AST/TypeLocNodes.def"
284
285
  void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
286
  void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
287
};
288
289
} // namespace
290
291
85.7k
void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
292
  // nothing to do
293
85.7k
}
294
295
371k
void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
296
371k
  addSourceLocation(TL.getBuiltinLoc());
297
371k
  if (TL.needsExtraLocalData()) {
298
237k
    Record.push_back(TL.getWrittenTypeSpec());
299
237k
    Record.push_back(static_cast<uint64_t>(TL.getWrittenSignSpec()));
300
237k
    Record.push_back(static_cast<uint64_t>(TL.getWrittenWidthSpec()));
301
237k
    Record.push_back(TL.hasModeAttr());
302
237k
  }
303
371k
}
304
305
1.59k
void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
306
1.59k
  addSourceLocation(TL.getNameLoc());
307
1.59k
}
308
309
133k
void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
310
133k
  addSourceLocation(TL.getStarLoc());
311
133k
}
312
313
158
void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
314
  // nothing to do
315
158
}
316
317
0
void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
318
  // nothing to do
319
0
}
320
321
1.28k
void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
322
1.28k
  addSourceLocation(TL.getCaretLoc());
323
1.28k
}
324
325
59.6k
void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
326
59.6k
  addSourceLocation(TL.getAmpLoc());
327
59.6k
}
328
329
6.50k
void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
330
6.50k
  addSourceLocation(TL.getAmpAmpLoc());
331
6.50k
}
332
333
430
void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
334
430
  addSourceLocation(TL.getStarLoc());
335
430
  Record.AddTypeSourceInfo(TL.getClassTInfo());
336
430
}
337
338
23.7k
void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
339
23.7k
  addSourceLocation(TL.getLBracketLoc());
340
23.7k
  addSourceLocation(TL.getRBracketLoc());
341
23.7k
  Record.push_back(TL.getSizeExpr() ? 
111.5k
:
012.2k
);
342
23.7k
  if (TL.getSizeExpr())
343
11.5k
    Record.AddStmt(TL.getSizeExpr());
344
23.7k
}
345
346
14.6k
void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
347
14.6k
  VisitArrayTypeLoc(TL);
348
14.6k
}
349
350
2.59k
void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
351
2.59k
  VisitArrayTypeLoc(TL);
352
2.59k
}
353
354
5.57k
void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
355
5.57k
  VisitArrayTypeLoc(TL);
356
5.57k
}
357
358
void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
359
915
                                            DependentSizedArrayTypeLoc TL) {
360
915
  VisitArrayTypeLoc(TL);
361
915
}
362
363
void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
364
11
    DependentAddressSpaceTypeLoc TL) {
365
11
  addSourceLocation(TL.getAttrNameLoc());
366
11
  SourceRange range = TL.getAttrOperandParensRange();
367
11
  addSourceLocation(range.getBegin());
368
11
  addSourceLocation(range.getEnd());
369
11
  Record.AddStmt(TL.getAttrExprOperand());
370
11
}
371
372
void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
373
11
                                        DependentSizedExtVectorTypeLoc TL) {
374
11
  addSourceLocation(TL.getNameLoc());
375
11
}
376
377
1.31k
void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
378
1.31k
  addSourceLocation(TL.getNameLoc());
379
1.31k
}
380
381
void TypeLocWriter::VisitDependentVectorTypeLoc(
382
0
    DependentVectorTypeLoc TL) {
383
0
  addSourceLocation(TL.getNameLoc());
384
0
}
385
386
352
void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
387
352
  addSourceLocation(TL.getNameLoc());
388
352
}
389
390
0
void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
391
0
  addSourceLocation(TL.getAttrNameLoc());
392
0
  SourceRange range = TL.getAttrOperandParensRange();
393
0
  addSourceLocation(range.getBegin());
394
0
  addSourceLocation(range.getEnd());
395
0
  Record.AddStmt(TL.getAttrRowOperand());
396
0
  Record.AddStmt(TL.getAttrColumnOperand());
397
0
}
398
399
void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
400
0
    DependentSizedMatrixTypeLoc TL) {
401
0
  addSourceLocation(TL.getAttrNameLoc());
402
0
  SourceRange range = TL.getAttrOperandParensRange();
403
0
  addSourceLocation(range.getBegin());
404
0
  addSourceLocation(range.getEnd());
405
0
  Record.AddStmt(TL.getAttrRowOperand());
406
0
  Record.AddStmt(TL.getAttrColumnOperand());
407
0
}
408
409
248k
void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
410
248k
  addSourceLocation(TL.getLocalRangeBegin());
411
248k
  addSourceLocation(TL.getLParenLoc());
412
248k
  addSourceLocation(TL.getRParenLoc());
413
248k
  addSourceRange(TL.getExceptionSpecRange());
414
248k
  addSourceLocation(TL.getLocalRangeEnd());
415
684k
  for (unsigned i = 0, e = TL.getNumParams(); i != e; 
++i435k
)
416
435k
    Record.AddDeclRef(TL.getParam(i));
417
248k
}
418
419
248k
void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
420
248k
  VisitFunctionTypeLoc(TL);
421
248k
}
422
423
83
void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
424
83
  VisitFunctionTypeLoc(TL);
425
83
}
426
427
32
void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
428
32
  addSourceLocation(TL.getNameLoc());
429
32
}
430
431
9.31k
void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
432
9.31k
  addSourceLocation(TL.getNameLoc());
433
9.31k
}
434
435
738k
void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
436
738k
  addSourceLocation(TL.getNameLoc());
437
738k
}
438
439
1.26k
void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
440
1.26k
  if (TL.getNumProtocols()) {
441
30
    addSourceLocation(TL.getProtocolLAngleLoc());
442
30
    addSourceLocation(TL.getProtocolRAngleLoc());
443
30
  }
444
1.30k
  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; 
++i31
)
445
31
    addSourceLocation(TL.getProtocolLoc(i));
446
1.26k
}
447
448
61
void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
449
61
  addSourceLocation(TL.getTypeofLoc());
450
61
  addSourceLocation(TL.getLParenLoc());
451
61
  addSourceLocation(TL.getRParenLoc());
452
61
}
453
454
18
void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
455
18
  addSourceLocation(TL.getTypeofLoc());
456
18
  addSourceLocation(TL.getLParenLoc());
457
18
  addSourceLocation(TL.getRParenLoc());
458
18
  Record.AddTypeSourceInfo(TL.getUnmodifiedTInfo());
459
18
}
460
461
1.38k
void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
462
1.38k
  addSourceLocation(TL.getDecltypeLoc());
463
1.38k
  addSourceLocation(TL.getRParenLoc());
464
1.38k
}
465
466
365
void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
467
365
  addSourceLocation(TL.getKWLoc());
468
365
  addSourceLocation(TL.getLParenLoc());
469
365
  addSourceLocation(TL.getRParenLoc());
470
365
  Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
471
365
}
472
473
117
void ASTRecordWriter::AddConceptReference(const ConceptReference *CR) {
474
117
  assert(CR);
475
117
  AddNestedNameSpecifierLoc(CR->getNestedNameSpecifierLoc());
476
117
  AddSourceLocation(CR->getTemplateKWLoc());
477
117
  AddDeclarationNameInfo(CR->getConceptNameInfo());
478
117
  AddDeclRef(CR->getFoundDecl());
479
117
  AddDeclRef(CR->getNamedConcept());
480
117
  push_back(CR->getTemplateArgsAsWritten() != nullptr);
481
117
  if (CR->getTemplateArgsAsWritten())
482
98
    AddASTTemplateArgumentListInfo(CR->getTemplateArgsAsWritten());
483
117
}
484
485
1.43k
void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
486
1.43k
  addSourceLocation(TL.getNameLoc());
487
1.43k
  auto *CR = TL.getConceptReference();
488
1.43k
  Record.push_back(TL.isConstrained() && 
CR1
);
489
1.43k
  if (TL.isConstrained() && 
CR1
)
490
1
    Record.AddConceptReference(CR);
491
1.43k
  Record.push_back(TL.isDecltypeAuto());
492
1.43k
  if (TL.isDecltypeAuto())
493
29
    addSourceLocation(TL.getRParenLoc());
494
1.43k
}
495
496
void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
497
15
    DeducedTemplateSpecializationTypeLoc TL) {
498
15
  addSourceLocation(TL.getTemplateNameLoc());
499
15
}
500
501
60.0k
void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
502
60.0k
  addSourceLocation(TL.getNameLoc());
503
60.0k
}
504
505
4.83k
void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
506
4.83k
  addSourceLocation(TL.getNameLoc());
507
4.83k
}
508
509
72.7k
void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
510
72.7k
  Record.AddAttr(TL.getAttr());
511
72.7k
}
512
513
2
void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
514
  // Nothing to do.
515
2
}
516
517
116k
void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
518
116k
  addSourceLocation(TL.getNameLoc());
519
116k
}
520
521
void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
522
45.7k
                                            SubstTemplateTypeParmTypeLoc TL) {
523
45.7k
  addSourceLocation(TL.getNameLoc());
524
45.7k
}
525
526
void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
527
1
                                          SubstTemplateTypeParmPackTypeLoc TL) {
528
1
  addSourceLocation(TL.getNameLoc());
529
1
}
530
531
void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
532
107k
                                           TemplateSpecializationTypeLoc TL) {
533
107k
  addSourceLocation(TL.getTemplateKeywordLoc());
534
107k
  addSourceLocation(TL.getTemplateNameLoc());
535
107k
  addSourceLocation(TL.getLAngleLoc());
536
107k
  addSourceLocation(TL.getRAngleLoc());
537
273k
  for (unsigned i = 0, e = TL.getNumArgs(); i != e; 
++i166k
)
538
166k
    Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
539
166k
                                      TL.getArgLoc(i).getLocInfo());
540
107k
}
541
542
15.4k
void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
543
15.4k
  addSourceLocation(TL.getLParenLoc());
544
15.4k
  addSourceLocation(TL.getRParenLoc());
545
15.4k
}
546
547
5.63k
void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
548
5.63k
  addSourceLocation(TL.getExpansionLoc());
549
5.63k
}
550
551
845k
void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
552
845k
  addSourceLocation(TL.getElaboratedKeywordLoc());
553
845k
  Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
554
845k
}
555
556
10.2k
void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
557
10.2k
  addSourceLocation(TL.getNameLoc());
558
10.2k
}
559
560
11.1k
void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
561
11.1k
  addSourceLocation(TL.getElaboratedKeywordLoc());
562
11.1k
  Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
563
11.1k
  addSourceLocation(TL.getNameLoc());
564
11.1k
}
565
566
void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
567
105
       DependentTemplateSpecializationTypeLoc TL) {
568
105
  addSourceLocation(TL.getElaboratedKeywordLoc());
569
105
  Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
570
105
  addSourceLocation(TL.getTemplateKeywordLoc());
571
105
  addSourceLocation(TL.getTemplateNameLoc());
572
105
  addSourceLocation(TL.getLAngleLoc());
573
105
  addSourceLocation(TL.getRAngleLoc());
574
227
  for (unsigned I = 0, E = TL.getNumArgs(); I != E; 
++I122
)
575
122
    Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
576
122
                                      TL.getArgLoc(I).getLocInfo());
577
105
}
578
579
4.26k
void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
580
4.26k
  addSourceLocation(TL.getEllipsisLoc());
581
4.26k
}
582
583
33.7k
void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
584
33.7k
  addSourceLocation(TL.getNameLoc());
585
33.7k
  addSourceLocation(TL.getNameEndLoc());
586
33.7k
}
587
588
5.52k
void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
589
5.52k
  Record.push_back(TL.hasBaseTypeAsWritten());
590
5.52k
  addSourceLocation(TL.getTypeArgsLAngleLoc());
591
5.52k
  addSourceLocation(TL.getTypeArgsRAngleLoc());
592
10.1k
  for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; 
++i4.63k
)
593
4.63k
    Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
594
5.52k
  addSourceLocation(TL.getProtocolLAngleLoc());
595
5.52k
  addSourceLocation(TL.getProtocolRAngleLoc());
596
7.37k
  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; 
++i1.85k
)
597
1.85k
    addSourceLocation(TL.getProtocolLoc(i));
598
5.52k
}
599
600
32.9k
void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
601
32.9k
  addSourceLocation(TL.getStarLoc());
602
32.9k
}
603
604
513
void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
605
513
  addSourceLocation(TL.getKWLoc());
606
513
  addSourceLocation(TL.getLParenLoc());
607
513
  addSourceLocation(TL.getRParenLoc());
608
513
}
609
610
15
void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
611
15
  addSourceLocation(TL.getKWLoc());
612
15
}
613
614
18
void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
615
18
  addSourceLocation(TL.getNameLoc());
616
18
}
617
void TypeLocWriter::VisitDependentBitIntTypeLoc(
618
0
    clang::DependentBitIntTypeLoc TL) {
619
0
  addSourceLocation(TL.getNameLoc());
620
0
}
621
622
7.24k
void ASTWriter::WriteTypeAbbrevs() {
623
7.24k
  using namespace llvm;
624
625
7.24k
  std::shared_ptr<BitCodeAbbrev> Abv;
626
627
  // Abbreviation for TYPE_EXT_QUAL
628
7.24k
  Abv = std::make_shared<BitCodeAbbrev>();
629
7.24k
  Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
630
7.24k
  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Type
631
7.24k
  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3));   // Quals
632
7.24k
  TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
633
7.24k
}
634
635
//===----------------------------------------------------------------------===//
636
// ASTWriter Implementation
637
//===----------------------------------------------------------------------===//
638
639
static void EmitBlockID(unsigned ID, const char *Name,
640
                        llvm::BitstreamWriter &Stream,
641
86.9k
                        ASTWriter::RecordDataImpl &Record) {
642
86.9k
  Record.clear();
643
86.9k
  Record.push_back(ID);
644
86.9k
  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
645
646
  // Emit the block name if present.
647
86.9k
  if (!Name || Name[0] == 0)
648
0
    return;
649
86.9k
  Record.clear();
650
1.50M
  while (*Name)
651
1.41M
    Record.push_back(*Name++);
652
86.9k
  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
653
86.9k
}
654
655
static void EmitRecordID(unsigned ID, const char *Name,
656
                         llvm::BitstreamWriter &Stream,
657
2.54M
                         ASTWriter::RecordDataImpl &Record) {
658
2.54M
  Record.clear();
659
2.54M
  Record.push_back(ID);
660
51.2M
  while (*Name)
661
48.7M
    Record.push_back(*Name++);
662
2.54M
  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
663
2.54M
}
664
665
static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
666
7.24k
                          ASTWriter::RecordDataImpl &Record) {
667
919k
#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
668
7.24k
  RECORD(STMT_STOP);
669
7.24k
  RECORD(STMT_NULL_PTR);
670
7.24k
  RECORD(STMT_REF_PTR);
671
7.24k
  RECORD(STMT_NULL);
672
7.24k
  RECORD(STMT_COMPOUND);
673
7.24k
  RECORD(STMT_CASE);
674
7.24k
  RECORD(STMT_DEFAULT);
675
7.24k
  RECORD(STMT_LABEL);
676
7.24k
  RECORD(STMT_ATTRIBUTED);
677
7.24k
  RECORD(STMT_IF);
678
7.24k
  RECORD(STMT_SWITCH);
679
7.24k
  RECORD(STMT_WHILE);
680
7.24k
  RECORD(STMT_DO);
681
7.24k
  RECORD(STMT_FOR);
682
7.24k
  RECORD(STMT_GOTO);
683
7.24k
  RECORD(STMT_INDIRECT_GOTO);
684
7.24k
  RECORD(STMT_CONTINUE);
685
7.24k
  RECORD(STMT_BREAK);
686
7.24k
  RECORD(STMT_RETURN);
687
7.24k
  RECORD(STMT_DECL);
688
7.24k
  RECORD(STMT_GCCASM);
689
7.24k
  RECORD(STMT_MSASM);
690
7.24k
  RECORD(EXPR_PREDEFINED);
691
7.24k
  RECORD(EXPR_DECL_REF);
692
7.24k
  RECORD(EXPR_INTEGER_LITERAL);
693
7.24k
  RECORD(EXPR_FIXEDPOINT_LITERAL);
694
7.24k
  RECORD(EXPR_FLOATING_LITERAL);
695
7.24k
  RECORD(EXPR_IMAGINARY_LITERAL);
696
7.24k
  RECORD(EXPR_STRING_LITERAL);
697
7.24k
  RECORD(EXPR_CHARACTER_LITERAL);
698
7.24k
  RECORD(EXPR_PAREN);
699
7.24k
  RECORD(EXPR_PAREN_LIST);
700
7.24k
  RECORD(EXPR_UNARY_OPERATOR);
701
7.24k
  RECORD(EXPR_SIZEOF_ALIGN_OF);
702
7.24k
  RECORD(EXPR_ARRAY_SUBSCRIPT);
703
7.24k
  RECORD(EXPR_CALL);
704
7.24k
  RECORD(EXPR_MEMBER);
705
7.24k
  RECORD(EXPR_BINARY_OPERATOR);
706
7.24k
  RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
707
7.24k
  RECORD(EXPR_CONDITIONAL_OPERATOR);
708
7.24k
  RECORD(EXPR_IMPLICIT_CAST);
709
7.24k
  RECORD(EXPR_CSTYLE_CAST);
710
7.24k
  RECORD(EXPR_COMPOUND_LITERAL);
711
7.24k
  RECORD(EXPR_EXT_VECTOR_ELEMENT);
712
7.24k
  RECORD(EXPR_INIT_LIST);
713
7.24k
  RECORD(EXPR_DESIGNATED_INIT);
714
7.24k
  RECORD(EXPR_DESIGNATED_INIT_UPDATE);
715
7.24k
  RECORD(EXPR_IMPLICIT_VALUE_INIT);
716
7.24k
  RECORD(EXPR_NO_INIT);
717
7.24k
  RECORD(EXPR_VA_ARG);
718
7.24k
  RECORD(EXPR_ADDR_LABEL);
719
7.24k
  RECORD(EXPR_STMT);
720
7.24k
  RECORD(EXPR_CHOOSE);
721
7.24k
  RECORD(EXPR_GNU_NULL);
722
7.24k
  RECORD(EXPR_SHUFFLE_VECTOR);
723
7.24k
  RECORD(EXPR_BLOCK);
724
7.24k
  RECORD(EXPR_GENERIC_SELECTION);
725
7.24k
  RECORD(EXPR_OBJC_STRING_LITERAL);
726
7.24k
  RECORD(EXPR_OBJC_BOXED_EXPRESSION);
727
7.24k
  RECORD(EXPR_OBJC_ARRAY_LITERAL);
728
7.24k
  RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
729
7.24k
  RECORD(EXPR_OBJC_ENCODE);
730
7.24k
  RECORD(EXPR_OBJC_SELECTOR_EXPR);
731
7.24k
  RECORD(EXPR_OBJC_PROTOCOL_EXPR);
732
7.24k
  RECORD(EXPR_OBJC_IVAR_REF_EXPR);
733
7.24k
  RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
734
7.24k
  RECORD(EXPR_OBJC_KVC_REF_EXPR);
735
7.24k
  RECORD(EXPR_OBJC_MESSAGE_EXPR);
736
7.24k
  RECORD(STMT_OBJC_FOR_COLLECTION);
737
7.24k
  RECORD(STMT_OBJC_CATCH);
738
7.24k
  RECORD(STMT_OBJC_FINALLY);
739
7.24k
  RECORD(STMT_OBJC_AT_TRY);
740
7.24k
  RECORD(STMT_OBJC_AT_SYNCHRONIZED);
741
7.24k
  RECORD(STMT_OBJC_AT_THROW);
742
7.24k
  RECORD(EXPR_OBJC_BOOL_LITERAL);
743
7.24k
  RECORD(STMT_CXX_CATCH);
744
7.24k
  RECORD(STMT_CXX_TRY);
745
7.24k
  RECORD(STMT_CXX_FOR_RANGE);
746
7.24k
  RECORD(EXPR_CXX_OPERATOR_CALL);
747
7.24k
  RECORD(EXPR_CXX_MEMBER_CALL);
748
7.24k
  RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
749
7.24k
  RECORD(EXPR_CXX_CONSTRUCT);
750
7.24k
  RECORD(EXPR_CXX_TEMPORARY_OBJECT);
751
7.24k
  RECORD(EXPR_CXX_STATIC_CAST);
752
7.24k
  RECORD(EXPR_CXX_DYNAMIC_CAST);
753
7.24k
  RECORD(EXPR_CXX_REINTERPRET_CAST);
754
7.24k
  RECORD(EXPR_CXX_CONST_CAST);
755
7.24k
  RECORD(EXPR_CXX_ADDRSPACE_CAST);
756
7.24k
  RECORD(EXPR_CXX_FUNCTIONAL_CAST);
757
7.24k
  RECORD(EXPR_USER_DEFINED_LITERAL);
758
7.24k
  RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
759
7.24k
  RECORD(EXPR_CXX_BOOL_LITERAL);
760
7.24k
  RECORD(EXPR_CXX_PAREN_LIST_INIT);
761
7.24k
  RECORD(EXPR_CXX_NULL_PTR_LITERAL);
762
7.24k
  RECORD(EXPR_CXX_TYPEID_EXPR);
763
7.24k
  RECORD(EXPR_CXX_TYPEID_TYPE);
764
7.24k
  RECORD(EXPR_CXX_THIS);
765
7.24k
  RECORD(EXPR_CXX_THROW);
766
7.24k
  RECORD(EXPR_CXX_DEFAULT_ARG);
767
7.24k
  RECORD(EXPR_CXX_DEFAULT_INIT);
768
7.24k
  RECORD(EXPR_CXX_BIND_TEMPORARY);
769
7.24k
  RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
770
7.24k
  RECORD(EXPR_CXX_NEW);
771
7.24k
  RECORD(EXPR_CXX_DELETE);
772
7.24k
  RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
773
7.24k
  RECORD(EXPR_EXPR_WITH_CLEANUPS);
774
7.24k
  RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
775
7.24k
  RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
776
7.24k
  RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
777
7.24k
  RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
778
7.24k
  RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
779
7.24k
  RECORD(EXPR_CXX_EXPRESSION_TRAIT);
780
7.24k
  RECORD(EXPR_CXX_NOEXCEPT);
781
7.24k
  RECORD(EXPR_OPAQUE_VALUE);
782
7.24k
  RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
783
7.24k
  RECORD(EXPR_TYPE_TRAIT);
784
7.24k
  RECORD(EXPR_ARRAY_TYPE_TRAIT);
785
7.24k
  RECORD(EXPR_PACK_EXPANSION);
786
7.24k
  RECORD(EXPR_SIZEOF_PACK);
787
7.24k
  RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
788
7.24k
  RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
789
7.24k
  RECORD(EXPR_FUNCTION_PARM_PACK);
790
7.24k
  RECORD(EXPR_MATERIALIZE_TEMPORARY);
791
7.24k
  RECORD(EXPR_CUDA_KERNEL_CALL);
792
7.24k
  RECORD(EXPR_CXX_UUIDOF_EXPR);
793
7.24k
  RECORD(EXPR_CXX_UUIDOF_TYPE);
794
7.24k
  RECORD(EXPR_LAMBDA);
795
7.24k
#undef RECORD
796
7.24k
}
797
798
7.24k
void ASTWriter::WriteBlockInfoBlock() {
799
7.24k
  RecordData Record;
800
7.24k
  Stream.EnterBlockInfoBlock();
801
802
86.9k
#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
803
1.62M
#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
804
805
  // Control Block.
806
7.24k
  BLOCK(CONTROL_BLOCK);
807
7.24k
  RECORD(METADATA);
808
7.24k
  RECORD(MODULE_NAME);
809
7.24k
  RECORD(MODULE_DIRECTORY);
810
7.24k
  RECORD(MODULE_MAP_FILE);
811
7.24k
  RECORD(IMPORTS);
812
7.24k
  RECORD(ORIGINAL_FILE);
813
7.24k
  RECORD(ORIGINAL_FILE_ID);
814
7.24k
  RECORD(INPUT_FILE_OFFSETS);
815
816
7.24k
  BLOCK(OPTIONS_BLOCK);
817
7.24k
  RECORD(LANGUAGE_OPTIONS);
818
7.24k
  RECORD(TARGET_OPTIONS);
819
7.24k
  RECORD(FILE_SYSTEM_OPTIONS);
820
7.24k
  RECORD(HEADER_SEARCH_OPTIONS);
821
7.24k
  RECORD(PREPROCESSOR_OPTIONS);
822
823
7.24k
  BLOCK(INPUT_FILES_BLOCK);
824
7.24k
  RECORD(INPUT_FILE);
825
7.24k
  RECORD(INPUT_FILE_HASH);
826
827
  // AST Top-Level Block.
828
7.24k
  BLOCK(AST_BLOCK);
829
7.24k
  RECORD(TYPE_OFFSET);
830
7.24k
  RECORD(DECL_OFFSET);
831
7.24k
  RECORD(IDENTIFIER_OFFSET);
832
7.24k
  RECORD(IDENTIFIER_TABLE);
833
7.24k
  RECORD(EAGERLY_DESERIALIZED_DECLS);
834
7.24k
  RECORD(MODULAR_CODEGEN_DECLS);
835
7.24k
  RECORD(SPECIAL_TYPES);
836
7.24k
  RECORD(STATISTICS);
837
7.24k
  RECORD(TENTATIVE_DEFINITIONS);
838
7.24k
  RECORD(SELECTOR_OFFSETS);
839
7.24k
  RECORD(METHOD_POOL);
840
7.24k
  RECORD(PP_COUNTER_VALUE);
841
7.24k
  RECORD(SOURCE_LOCATION_OFFSETS);
842
7.24k
  RECORD(SOURCE_LOCATION_PRELOADS);
843
7.24k
  RECORD(EXT_VECTOR_DECLS);
844
7.24k
  RECORD(UNUSED_FILESCOPED_DECLS);
845
7.24k
  RECORD(PPD_ENTITIES_OFFSETS);
846
7.24k
  RECORD(VTABLE_USES);
847
7.24k
  RECORD(PPD_SKIPPED_RANGES);
848
7.24k
  RECORD(REFERENCED_SELECTOR_POOL);
849
7.24k
  RECORD(TU_UPDATE_LEXICAL);
850
7.24k
  RECORD(SEMA_DECL_REFS);
851
7.24k
  RECORD(WEAK_UNDECLARED_IDENTIFIERS);
852
7.24k
  RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
853
7.24k
  RECORD(UPDATE_VISIBLE);
854
7.24k
  RECORD(DECL_UPDATE_OFFSETS);
855
7.24k
  RECORD(DECL_UPDATES);
856
7.24k
  RECORD(CUDA_SPECIAL_DECL_REFS);
857
7.24k
  RECORD(HEADER_SEARCH_TABLE);
858
7.24k
  RECORD(FP_PRAGMA_OPTIONS);
859
7.24k
  RECORD(OPENCL_EXTENSIONS);
860
7.24k
  RECORD(OPENCL_EXTENSION_TYPES);
861
7.24k
  RECORD(OPENCL_EXTENSION_DECLS);
862
7.24k
  RECORD(DELEGATING_CTORS);
863
7.24k
  RECORD(KNOWN_NAMESPACES);
864
7.24k
  RECORD(MODULE_OFFSET_MAP);
865
7.24k
  RECORD(SOURCE_MANAGER_LINE_TABLE);
866
7.24k
  RECORD(OBJC_CATEGORIES_MAP);
867
7.24k
  RECORD(FILE_SORTED_DECLS);
868
7.24k
  RECORD(IMPORTED_MODULES);
869
7.24k
  RECORD(OBJC_CATEGORIES);
870
7.24k
  RECORD(MACRO_OFFSET);
871
7.24k
  RECORD(INTERESTING_IDENTIFIERS);
872
7.24k
  RECORD(UNDEFINED_BUT_USED);
873
7.24k
  RECORD(LATE_PARSED_TEMPLATE);
874
7.24k
  RECORD(OPTIMIZE_PRAGMA_OPTIONS);
875
7.24k
  RECORD(MSSTRUCT_PRAGMA_OPTIONS);
876
7.24k
  RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
877
7.24k
  RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
878
7.24k
  RECORD(DELETE_EXPRS_TO_ANALYZE);
879
7.24k
  RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
880
7.24k
  RECORD(PP_CONDITIONAL_STACK);
881
7.24k
  RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
882
7.24k
  RECORD(PP_ASSUME_NONNULL_LOC);
883
884
  // SourceManager Block.
885
7.24k
  BLOCK(SOURCE_MANAGER_BLOCK);
886
7.24k
  RECORD(SM_SLOC_FILE_ENTRY);
887
7.24k
  RECORD(SM_SLOC_BUFFER_ENTRY);
888
7.24k
  RECORD(SM_SLOC_BUFFER_BLOB);
889
7.24k
  RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
890
7.24k
  RECORD(SM_SLOC_EXPANSION_ENTRY);
891
892
  // Preprocessor Block.
893
7.24k
  BLOCK(PREPROCESSOR_BLOCK);
894
7.24k
  RECORD(PP_MACRO_DIRECTIVE_HISTORY);
895
7.24k
  RECORD(PP_MACRO_FUNCTION_LIKE);
896
7.24k
  RECORD(PP_MACRO_OBJECT_LIKE);
897
7.24k
  RECORD(PP_MODULE_MACRO);
898
7.24k
  RECORD(PP_TOKEN);
899
900
  // Submodule Block.
901
7.24k
  BLOCK(SUBMODULE_BLOCK);
902
7.24k
  RECORD(SUBMODULE_METADATA);
903
7.24k
  RECORD(SUBMODULE_DEFINITION);
904
7.24k
  RECORD(SUBMODULE_UMBRELLA_HEADER);
905
7.24k
  RECORD(SUBMODULE_HEADER);
906
7.24k
  RECORD(SUBMODULE_TOPHEADER);
907
7.24k
  RECORD(SUBMODULE_UMBRELLA_DIR);
908
7.24k
  RECORD(SUBMODULE_IMPORTS);
909
7.24k
  RECORD(SUBMODULE_AFFECTING_MODULES);
910
7.24k
  RECORD(SUBMODULE_EXPORTS);
911
7.24k
  RECORD(SUBMODULE_REQUIRES);
912
7.24k
  RECORD(SUBMODULE_EXCLUDED_HEADER);
913
7.24k
  RECORD(SUBMODULE_LINK_LIBRARY);
914
7.24k
  RECORD(SUBMODULE_CONFIG_MACRO);
915
7.24k
  RECORD(SUBMODULE_CONFLICT);
916
7.24k
  RECORD(SUBMODULE_PRIVATE_HEADER);
917
7.24k
  RECORD(SUBMODULE_TEXTUAL_HEADER);
918
7.24k
  RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
919
7.24k
  RECORD(SUBMODULE_INITIALIZERS);
920
7.24k
  RECORD(SUBMODULE_EXPORT_AS);
921
922
  // Comments Block.
923
7.24k
  BLOCK(COMMENTS_BLOCK);
924
7.24k
  RECORD(COMMENTS_RAW_COMMENT);
925
926
  // Decls and Types block.
927
7.24k
  BLOCK(DECLTYPES_BLOCK);
928
7.24k
  RECORD(TYPE_EXT_QUAL);
929
7.24k
  RECORD(TYPE_COMPLEX);
930
7.24k
  RECORD(TYPE_POINTER);
931
7.24k
  RECORD(TYPE_BLOCK_POINTER);
932
7.24k
  RECORD(TYPE_LVALUE_REFERENCE);
933
7.24k
  RECORD(TYPE_RVALUE_REFERENCE);
934
7.24k
  RECORD(TYPE_MEMBER_POINTER);
935
7.24k
  RECORD(TYPE_CONSTANT_ARRAY);
936
7.24k
  RECORD(TYPE_INCOMPLETE_ARRAY);
937
7.24k
  RECORD(TYPE_VARIABLE_ARRAY);
938
7.24k
  RECORD(TYPE_VECTOR);
939
7.24k
  RECORD(TYPE_EXT_VECTOR);
940
7.24k
  RECORD(TYPE_FUNCTION_NO_PROTO);
941
7.24k
  RECORD(TYPE_FUNCTION_PROTO);
942
7.24k
  RECORD(TYPE_TYPEDEF);
943
7.24k
  RECORD(TYPE_TYPEOF_EXPR);
944
7.24k
  RECORD(TYPE_TYPEOF);
945
7.24k
  RECORD(TYPE_RECORD);
946
7.24k
  RECORD(TYPE_ENUM);
947
7.24k
  RECORD(TYPE_OBJC_INTERFACE);
948
7.24k
  RECORD(TYPE_OBJC_OBJECT_POINTER);
949
7.24k
  RECORD(TYPE_DECLTYPE);
950
7.24k
  RECORD(TYPE_ELABORATED);
951
7.24k
  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
952
7.24k
  RECORD(TYPE_UNRESOLVED_USING);
953
7.24k
  RECORD(TYPE_INJECTED_CLASS_NAME);
954
7.24k
  RECORD(TYPE_OBJC_OBJECT);
955
7.24k
  RECORD(TYPE_TEMPLATE_TYPE_PARM);
956
7.24k
  RECORD(TYPE_TEMPLATE_SPECIALIZATION);
957
7.24k
  RECORD(TYPE_DEPENDENT_NAME);
958
7.24k
  RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
959
7.24k
  RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
960
7.24k
  RECORD(TYPE_PAREN);
961
7.24k
  RECORD(TYPE_MACRO_QUALIFIED);
962
7.24k
  RECORD(TYPE_PACK_EXPANSION);
963
7.24k
  RECORD(TYPE_ATTRIBUTED);
964
7.24k
  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
965
7.24k
  RECORD(TYPE_AUTO);
966
7.24k
  RECORD(TYPE_UNARY_TRANSFORM);
967
7.24k
  RECORD(TYPE_ATOMIC);
968
7.24k
  RECORD(TYPE_DECAYED);
969
7.24k
  RECORD(TYPE_ADJUSTED);
970
7.24k
  RECORD(TYPE_OBJC_TYPE_PARAM);
971
7.24k
  RECORD(LOCAL_REDECLARATIONS);
972
7.24k
  RECORD(DECL_TYPEDEF);
973
7.24k
  RECORD(DECL_TYPEALIAS);
974
7.24k
  RECORD(DECL_ENUM);
975
7.24k
  RECORD(DECL_RECORD);
976
7.24k
  RECORD(DECL_ENUM_CONSTANT);
977
7.24k
  RECORD(DECL_FUNCTION);
978
7.24k
  RECORD(DECL_OBJC_METHOD);
979
7.24k
  RECORD(DECL_OBJC_INTERFACE);
980
7.24k
  RECORD(DECL_OBJC_PROTOCOL);
981
7.24k
  RECORD(DECL_OBJC_IVAR);
982
7.24k
  RECORD(DECL_OBJC_AT_DEFS_FIELD);
983
7.24k
  RECORD(DECL_OBJC_CATEGORY);
984
7.24k
  RECORD(DECL_OBJC_CATEGORY_IMPL);
985
7.24k
  RECORD(DECL_OBJC_IMPLEMENTATION);
986
7.24k
  RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
987
7.24k
  RECORD(DECL_OBJC_PROPERTY);
988
7.24k
  RECORD(DECL_OBJC_PROPERTY_IMPL);
989
7.24k
  RECORD(DECL_FIELD);
990
7.24k
  RECORD(DECL_MS_PROPERTY);
991
7.24k
  RECORD(DECL_VAR);
992
7.24k
  RECORD(DECL_IMPLICIT_PARAM);
993
7.24k
  RECORD(DECL_PARM_VAR);
994
7.24k
  RECORD(DECL_FILE_SCOPE_ASM);
995
7.24k
  RECORD(DECL_BLOCK);
996
7.24k
  RECORD(DECL_CONTEXT_LEXICAL);
997
7.24k
  RECORD(DECL_CONTEXT_VISIBLE);
998
7.24k
  RECORD(DECL_NAMESPACE);
999
7.24k
  RECORD(DECL_NAMESPACE_ALIAS);
1000
7.24k
  RECORD(DECL_USING);
1001
7.24k
  RECORD(DECL_USING_SHADOW);
1002
7.24k
  RECORD(DECL_USING_DIRECTIVE);
1003
7.24k
  RECORD(DECL_UNRESOLVED_USING_VALUE);
1004
7.24k
  RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1005
7.24k
  RECORD(DECL_LINKAGE_SPEC);
1006
7.24k
  RECORD(DECL_CXX_RECORD);
1007
7.24k
  RECORD(DECL_CXX_METHOD);
1008
7.24k
  RECORD(DECL_CXX_CONSTRUCTOR);
1009
7.24k
  RECORD(DECL_CXX_DESTRUCTOR);
1010
7.24k
  RECORD(DECL_CXX_CONVERSION);
1011
7.24k
  RECORD(DECL_ACCESS_SPEC);
1012
7.24k
  RECORD(DECL_FRIEND);
1013
7.24k
  RECORD(DECL_FRIEND_TEMPLATE);
1014
7.24k
  RECORD(DECL_CLASS_TEMPLATE);
1015
7.24k
  RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1016
7.24k
  RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1017
7.24k
  RECORD(DECL_VAR_TEMPLATE);
1018
7.24k
  RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1019
7.24k
  RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1020
7.24k
  RECORD(DECL_FUNCTION_TEMPLATE);
1021
7.24k
  RECORD(DECL_TEMPLATE_TYPE_PARM);
1022
7.24k
  RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1023
7.24k
  RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1024
7.24k
  RECORD(DECL_CONCEPT);
1025
7.24k
  RECORD(DECL_REQUIRES_EXPR_BODY);
1026
7.24k
  RECORD(DECL_TYPE_ALIAS_TEMPLATE);
1027
7.24k
  RECORD(DECL_STATIC_ASSERT);
1028
7.24k
  RECORD(DECL_CXX_BASE_SPECIFIERS);
1029
7.24k
  RECORD(DECL_CXX_CTOR_INITIALIZERS);
1030
7.24k
  RECORD(DECL_INDIRECTFIELD);
1031
7.24k
  RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1032
7.24k
  RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1033
7.24k
  RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1034
7.24k
  RECORD(DECL_IMPORT);
1035
7.24k
  RECORD(DECL_OMP_THREADPRIVATE);
1036
7.24k
  RECORD(DECL_EMPTY);
1037
7.24k
  RECORD(DECL_OBJC_TYPE_PARAM);
1038
7.24k
  RECORD(DECL_OMP_CAPTUREDEXPR);
1039
7.24k
  RECORD(DECL_PRAGMA_COMMENT);
1040
7.24k
  RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1041
7.24k
  RECORD(DECL_OMP_DECLARE_REDUCTION);
1042
7.24k
  RECORD(DECL_OMP_ALLOCATE);
1043
7.24k
  RECORD(DECL_HLSL_BUFFER);
1044
1045
  // Statements and Exprs can occur in the Decls and Types block.
1046
7.24k
  AddStmtsExprs(Stream, Record);
1047
1048
7.24k
  BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1049
7.24k
  RECORD(PPD_MACRO_EXPANSION);
1050
7.24k
  RECORD(PPD_MACRO_DEFINITION);
1051
7.24k
  RECORD(PPD_INCLUSION_DIRECTIVE);
1052
1053
  // Decls and Types block.
1054
7.24k
  BLOCK(EXTENSION_BLOCK);
1055
7.24k
  RECORD(EXTENSION_METADATA);
1056
1057
7.24k
  BLOCK(UNHASHED_CONTROL_BLOCK);
1058
7.24k
  RECORD(SIGNATURE);
1059
7.24k
  RECORD(AST_BLOCK_HASH);
1060
7.24k
  RECORD(DIAGNOSTIC_OPTIONS);
1061
7.24k
  RECORD(HEADER_SEARCH_PATHS);
1062
7.24k
  RECORD(DIAG_PRAGMA_MAPPINGS);
1063
1064
7.24k
#undef RECORD
1065
7.24k
#undef BLOCK
1066
7.24k
  Stream.ExitBlock();
1067
7.24k
}
1068
1069
/// Prepares a path for being written to an AST file by converting it
1070
/// to an absolute path and removing nested './'s.
1071
///
1072
/// \return \c true if the path was changed.
1073
static bool cleanPathForOutput(FileManager &FileMgr,
1074
102k
                               SmallVectorImpl<char> &Path) {
1075
102k
  bool Changed = FileMgr.makeAbsolutePath(Path);
1076
102k
  return Changed | llvm::sys::path::remove_dots(Path);
1077
102k
}
1078
1079
/// Adjusts the given filename to only write out the portion of the
1080
/// filename that is not part of the system root directory.
1081
///
1082
/// \param Filename the file name to adjust.
1083
///
1084
/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1085
/// the returned filename will be adjusted by this root directory.
1086
///
1087
/// \returns either the original filename (if it needs no adjustment) or the
1088
/// adjusted filename (which points into the @p Filename parameter).
1089
static const char *
1090
99.3k
adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1091
99.3k
  assert(Filename && "No file name to adjust?");
1092
1093
99.3k
  if (BaseDir.empty())
1094
18.6k
    return Filename;
1095
1096
  // Verify that the filename and the system root have the same prefix.
1097
80.7k
  unsigned Pos = 0;
1098
7.85M
  for (; Filename[Pos] && Pos < BaseDir.size(); 
++Pos7.77M
)
1099
7.78M
    if (Filename[Pos] != BaseDir[Pos])
1100
12.6k
      return Filename; // Prefixes don't match.
1101
1102
  // We hit the end of the filename before we hit the end of the system root.
1103
68.0k
  if (!Filename[Pos])
1104
0
    return Filename;
1105
1106
  // If there's not a path separator at the end of the base directory nor
1107
  // immediately after it, then this isn't within the base directory.
1108
68.0k
  if (!llvm::sys::path::is_separator(Filename[Pos])) {
1109
3
    if (!llvm::sys::path::is_separator(BaseDir.back()))
1110
3
      return Filename;
1111
68.0k
  } else {
1112
    // If the file name has a '/' at the current position, skip over the '/'.
1113
    // We distinguish relative paths from absolute paths by the
1114
    // absence of '/' at the beginning of relative paths.
1115
    //
1116
    // FIXME: This is wrong. We distinguish them by asking if the path is
1117
    // absolute, which isn't the same thing. And there might be multiple '/'s
1118
    // in a row. Use a better mechanism to indicate whether we have emitted an
1119
    // absolute or relative path.
1120
68.0k
    ++Pos;
1121
68.0k
  }
1122
1123
68.0k
  return Filename + Pos;
1124
68.0k
}
1125
1126
std::pair<ASTFileSignature, ASTFileSignature>
1127
2.88k
ASTWriter::createSignature() const {
1128
2.88k
  StringRef AllBytes(Buffer.data(), Buffer.size());
1129
1130
2.88k
  llvm::SHA1 Hasher;
1131
2.88k
  Hasher.update(AllBytes.slice(ASTBlockRange.first, ASTBlockRange.second));
1132
2.88k
  ASTFileSignature ASTBlockHash = ASTFileSignature::create(Hasher.result());
1133
1134
  // Add the remaining bytes:
1135
  //  1. Before the unhashed control block.
1136
2.88k
  Hasher.update(AllBytes.slice(0, UnhashedControlBlockRange.first));
1137
  //  2. Between the unhashed control block and the AST block.
1138
2.88k
  Hasher.update(
1139
2.88k
      AllBytes.slice(UnhashedControlBlockRange.second, ASTBlockRange.first));
1140
  //  3. After the AST block.
1141
2.88k
  Hasher.update(AllBytes.slice(ASTBlockRange.second, StringRef::npos));
1142
2.88k
  ASTFileSignature Signature = ASTFileSignature::create(Hasher.result());
1143
1144
2.88k
  return std::make_pair(ASTBlockHash, Signature);
1145
2.88k
}
1146
1147
7.24k
ASTFileSignature ASTWriter::backpatchSignature() {
1148
7.24k
  if (!WritingModule ||
1149
7.24k
      
!PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent3.51k
)
1150
4.35k
    return {};
1151
1152
  // For implicit modules, write the hash of the PCM as its signature.
1153
1154
5.77k
  
auto BackpatchSignatureAt = [&](const ASTFileSignature &S, uint64_t BitNo) 2.88k
{
1155
115k
    for (uint8_t Byte : S) {
1156
115k
      Stream.BackpatchByte(BitNo, Byte);
1157
115k
      BitNo += 8;
1158
115k
    }
1159
5.77k
  };
1160
1161
2.88k
  ASTFileSignature ASTBlockHash;
1162
2.88k
  ASTFileSignature Signature;
1163
2.88k
  std::tie(ASTBlockHash, Signature) = createSignature();
1164
1165
2.88k
  BackpatchSignatureAt(ASTBlockHash, ASTBlockHashOffset);
1166
2.88k
  BackpatchSignatureAt(Signature, SignatureOffset);
1167
1168
2.88k
  return Signature;
1169
7.24k
}
1170
1171
void ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1172
7.24k
                                          ASTContext &Context) {
1173
7.24k
  using namespace llvm;
1174
1175
  // Flush first to prepare the PCM hash (signature).
1176
7.24k
  Stream.FlushToWord();
1177
7.24k
  UnhashedControlBlockRange.first = Stream.GetCurrentBitNo() >> 3;
1178
1179
  // Enter the block and prepare to write records.
1180
7.24k
  RecordData Record;
1181
7.24k
  Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1182
1183
  // For implicit modules, write the hash of the PCM as its signature.
1184
7.24k
  if (WritingModule &&
1185
7.24k
      
PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent3.51k
) {
1186
    // At this point, we don't know the actual signature of the file or the AST
1187
    // block - we're only able to compute those at the end of the serialization
1188
    // process. Let's store dummy signatures for now, and replace them with the
1189
    // real ones later on.
1190
    // The bitstream VBR-encodes record elements, which makes backpatching them
1191
    // really difficult. Let's store the signatures as blobs instead - they are
1192
    // guaranteed to be word-aligned, and we control their format/encoding.
1193
2.88k
    auto Dummy = ASTFileSignature::createDummy();
1194
2.88k
    SmallString<128> Blob{Dummy.begin(), Dummy.end()};
1195
1196
2.88k
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
1197
2.88k
    Abbrev->Add(BitCodeAbbrevOp(AST_BLOCK_HASH));
1198
2.88k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1199
2.88k
    unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1200
1201
2.88k
    Abbrev = std::make_shared<BitCodeAbbrev>();
1202
2.88k
    Abbrev->Add(BitCodeAbbrevOp(SIGNATURE));
1203
2.88k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1204
2.88k
    unsigned SignatureAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1205
1206
2.88k
    Record.push_back(AST_BLOCK_HASH);
1207
2.88k
    Stream.EmitRecordWithBlob(ASTBlockHashAbbrev, Record, Blob);
1208
2.88k
    ASTBlockHashOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1209
2.88k
    Record.clear();
1210
1211
2.88k
    Record.push_back(SIGNATURE);
1212
2.88k
    Stream.EmitRecordWithBlob(SignatureAbbrev, Record, Blob);
1213
2.88k
    SignatureOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1214
2.88k
    Record.clear();
1215
2.88k
  }
1216
1217
  // Diagnostic options.
1218
7.24k
  const auto &Diags = Context.getDiagnostics();
1219
7.24k
  const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1220
231k
#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1221
7.24k
#define ENUM_DIAGOPT(Name, Type, Bits, Default)                                \
1222
21.7k
  Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1223
7.24k
#include "clang/Basic/DiagnosticOptions.def"
1224
7.24k
  Record.push_back(DiagOpts.Warnings.size());
1225
10.3k
  for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; 
++I3.07k
)
1226
3.07k
    AddString(DiagOpts.Warnings[I], Record);
1227
7.24k
  Record.push_back(DiagOpts.Remarks.size());
1228
7.46k
  for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; 
++I223
)
1229
223
    AddString(DiagOpts.Remarks[I], Record);
1230
  // Note: we don't serialize the log or serialization file names, because they
1231
  // are generally transient files and will almost always be overridden.
1232
7.24k
  Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1233
7.24k
  Record.clear();
1234
1235
  // Header search paths.
1236
7.24k
  Record.clear();
1237
7.24k
  const HeaderSearchOptions &HSOpts =
1238
7.24k
      PP.getHeaderSearchInfo().getHeaderSearchOpts();
1239
1240
  // Include entries.
1241
7.24k
  Record.push_back(HSOpts.UserEntries.size());
1242
21.9k
  for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; 
++I14.7k
) {
1243
14.7k
    const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1244
14.7k
    AddString(Entry.Path, Record);
1245
14.7k
    Record.push_back(static_cast<unsigned>(Entry.Group));
1246
14.7k
    Record.push_back(Entry.IsFramework);
1247
14.7k
    Record.push_back(Entry.IgnoreSysRoot);
1248
14.7k
  }
1249
1250
  // System header prefixes.
1251
7.24k
  Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1252
7.24k
  for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; 
++I0
) {
1253
0
    AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1254
0
    Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1255
0
  }
1256
1257
  // VFS overlay files.
1258
7.24k
  Record.push_back(HSOpts.VFSOverlayFiles.size());
1259
7.24k
  for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1260
71
    AddString(VFSOverlayFile, Record);
1261
1262
7.24k
  Stream.EmitRecord(HEADER_SEARCH_PATHS, Record);
1263
1264
  // Write out the diagnostic/pragma mappings.
1265
7.24k
  WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1266
1267
  // Header search entry usage.
1268
7.24k
  auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1269
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1270
7.24k
  Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1271
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1272
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));      // Bit vector.
1273
7.24k
  unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1274
7.24k
  {
1275
7.24k
    RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1276
7.24k
                                       HSEntryUsage.size()};
1277
7.24k
    Stream.EmitRecordWithBlob(HSUsageAbbrevCode, Record, bytes(HSEntryUsage));
1278
7.24k
  }
1279
1280
  // Leave the options block.
1281
7.24k
  Stream.ExitBlock();
1282
7.24k
  UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1283
7.24k
}
1284
1285
/// Write the control block.
1286
void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1287
7.24k
                                  StringRef isysroot) {
1288
7.24k
  using namespace llvm;
1289
1290
7.24k
  Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1291
7.24k
  RecordData Record;
1292
1293
  // Metadata
1294
7.24k
  auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1295
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1296
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1297
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1298
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1299
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1300
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1301
  // Standard C++ module
1302
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1303
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1304
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1305
7.24k
  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1306
7.24k
  unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1307
7.24k
  assert((!WritingModule || isysroot.empty()) &&
1308
7.24k
         "writing module as a relocatable PCH?");
1309
7.24k
  {
1310
7.24k
    RecordData::value_type Record[] = {METADATA,
1311
7.24k
                                       VERSION_MAJOR,
1312
7.24k
                                       VERSION_MINOR,
1313
7.24k
                                       CLANG_VERSION_MAJOR,
1314
7.24k
                                       CLANG_VERSION_MINOR,
1315
7.24k
                                       !isysroot.empty(),
1316
7.24k
                                       isWritingStdCXXNamedModules(),
1317
7.24k
                                       IncludeTimestamps,
1318
7.24k
                                       ASTHasCompilerErrors};
1319
7.24k
    Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1320
7.24k
                              getClangFullRepositoryVersion());
1321
7.24k
  }
1322
1323
7.24k
  if (WritingModule) {
1324
    // Module name
1325
3.51k
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
1326
3.51k
    Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1327
3.51k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1328
3.51k
    unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1329
3.51k
    RecordData::value_type Record[] = {MODULE_NAME};
1330
3.51k
    Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1331
3.51k
  }
1332
1333
7.24k
  if (WritingModule && 
WritingModule->Directory3.51k
) {
1334
3.16k
    SmallString<128> BaseDir;
1335
3.16k
    if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1336
      // Use the current working directory as the base path for all inputs.
1337
1
      auto CWD =
1338
1
          Context.getSourceManager().getFileManager().getOptionalDirectoryRef(
1339
1
              ".");
1340
1
      BaseDir.assign(CWD->getName());
1341
3.16k
    } else {
1342
3.16k
      BaseDir.assign(WritingModule->Directory->getName());
1343
3.16k
    }
1344
3.16k
    cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1345
1346
    // If the home of the module is the current working directory, then we
1347
    // want to pick up the cwd of the build process loading the module, not
1348
    // our cwd, when we load this module.
1349
3.16k
    if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1350
3.16k
        
(3.16k
!PP.getHeaderSearchInfo()
1351
3.16k
              .getHeaderSearchOpts()
1352
3.16k
              .ModuleMapFileHomeIsCwd ||
1353
3.16k
         
WritingModule->Directory->getName() != StringRef(".")27
)) {
1354
      // Module directory.
1355
3.14k
      auto Abbrev = std::make_shared<BitCodeAbbrev>();
1356
3.14k
      Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1357
3.14k
      Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1358
3.14k
      unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1359
1360
3.14k
      RecordData::value_type Record[] = {MODULE_DIRECTORY};
1361
3.14k
      Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1362
3.14k
    }
1363
1364
    // Write out all other paths relative to the base directory if possible.
1365
3.16k
    BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1366
4.07k
  } else if (!isysroot.empty()) {
1367
    // Write out paths relative to the sysroot if possible.
1368
1
    BaseDirectory = std::string(isysroot);
1369
1
  }
1370
1371
  // Module map file
1372
7.24k
  if (WritingModule && 
WritingModule->Kind == Module::ModuleMapModule3.51k
) {
1373
3.16k
    Record.clear();
1374
1375
3.16k
    auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1376
3.16k
    AddPath(WritingModule->PresumedModuleMapFile.empty()
1377
3.16k
                ? Map.getModuleMapFileForUniquing(WritingModule)
1378
3.15k
                      ->getNameAsRequested()
1379
3.16k
                : 
StringRef(WritingModule->PresumedModuleMapFile)16
,
1380
3.16k
            Record);
1381
1382
    // Additional module map files.
1383
3.16k
    if (auto *AdditionalModMaps =
1384
3.16k
            Map.getAdditionalModuleMapFiles(WritingModule)) {
1385
52
      Record.push_back(AdditionalModMaps->size());
1386
52
      SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1387
52
                                           AdditionalModMaps->end());
1388
52
      llvm::sort(ModMaps, [](FileEntryRef A, FileEntryRef B) {
1389
4
        return A.getName() < B.getName();
1390
4
      });
1391
52
      for (FileEntryRef F : ModMaps)
1392
56
        AddPath(F.getName(), Record);
1393
3.11k
    } else {
1394
3.11k
      Record.push_back(0);
1395
3.11k
    }
1396
1397
3.16k
    Stream.EmitRecord(MODULE_MAP_FILE, Record);
1398
3.16k
  }
1399
1400
  // Imports
1401
7.24k
  if (Chain) {
1402
3.70k
    serialization::ModuleManager &Mgr = Chain->getModuleManager();
1403
3.70k
    Record.clear();
1404
1405
173k
    for (ModuleFile &M : Mgr) {
1406
      // Skip modules that weren't directly imported.
1407
173k
      if (!M.isDirectlyImported())
1408
163k
        continue;
1409
1410
10.0k
      Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1411
10.0k
      Record.push_back(M.StandardCXXModule);
1412
10.0k
      AddSourceLocation(M.ImportLoc, Record);
1413
1414
      // If we have calculated signature, there is no need to store
1415
      // the size or timestamp.
1416
10.0k
      Record.push_back(M.Signature ? 
09.61k
:
M.File->getSize()390
);
1417
10.0k
      Record.push_back(M.Signature ? 
09.61k
:
getTimestampForOutput(M.File)390
);
1418
1419
10.0k
      llvm::append_range(Record, M.Signature);
1420
1421
10.0k
      AddString(M.ModuleName, Record);
1422
10.0k
      AddPath(M.FileName, Record);
1423
10.0k
    }
1424
3.70k
    Stream.EmitRecord(IMPORTS, Record);
1425
3.70k
  }
1426
1427
  // Write the options block.
1428
7.24k
  Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1429
1430
  // Language options.
1431
7.24k
  Record.clear();
1432
7.24k
  const LangOptions &LangOpts = Context.getLangOpts();
1433
7.24k
#define LANGOPT(Name, Bits, Default, Description) \
1434
1.83M
  Record.push_back(LangOpts.Name);
1435
7.24k
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1436
231k
  Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1437
7.24k
#include "clang/Basic/LangOptions.def"
1438
7.24k
#define SANITIZER(NAME, ID)                                                    \
1439
420k
  Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1440
7.24k
#include "clang/Basic/Sanitizers.def"
1441
1442
7.24k
  Record.push_back(LangOpts.ModuleFeatures.size());
1443
7.24k
  for (StringRef Feature : LangOpts.ModuleFeatures)
1444
17
    AddString(Feature, Record);
1445
1446
7.24k
  Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1447
7.24k
  AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1448
1449
7.24k
  AddString(LangOpts.CurrentModule, Record);
1450
1451
  // Comment options.
1452
7.24k
  Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1453
7.24k
  for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1454
4
    AddString(I, Record);
1455
4
  }
1456
7.24k
  Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1457
1458
  // OpenMP offloading options.
1459
7.24k
  Record.push_back(LangOpts.OMPTargetTriples.size());
1460
7.24k
  for (auto &T : LangOpts.OMPTargetTriples)
1461
2.11k
    AddString(T.getTriple(), Record);
1462
1463
7.24k
  AddString(LangOpts.OMPHostIRFile, Record);
1464
1465
7.24k
  Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1466
1467
  // Target options.
1468
7.24k
  Record.clear();
1469
7.24k
  const TargetInfo &Target = Context.getTargetInfo();
1470
7.24k
  const TargetOptions &TargetOpts = Target.getTargetOpts();
1471
7.24k
  AddString(TargetOpts.Triple, Record);
1472
7.24k
  AddString(TargetOpts.CPU, Record);
1473
7.24k
  AddString(TargetOpts.TuneCPU, Record);
1474
7.24k
  AddString(TargetOpts.ABI, Record);
1475
7.24k
  Record.push_back(TargetOpts.FeaturesAsWritten.size());
1476
7.24k
  for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; 
++I6
) {
1477
6
    AddString(TargetOpts.FeaturesAsWritten[I], Record);
1478
6
  }
1479
7.24k
  Record.push_back(TargetOpts.Features.size());
1480
59.9k
  for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; 
++I52.7k
) {
1481
52.7k
    AddString(TargetOpts.Features[I], Record);
1482
52.7k
  }
1483
7.24k
  Stream.EmitRecord(TARGET_OPTIONS, Record);
1484
1485
  // File system options.
1486
7.24k
  Record.clear();
1487
7.24k
  const FileSystemOptions &FSOpts =
1488
7.24k
      Context.getSourceManager().getFileManager().getFileSystemOpts();
1489
7.24k
  AddString(FSOpts.WorkingDir, Record);
1490
7.24k
  Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1491
1492
  // Header search options.
1493
7.24k
  Record.clear();
1494
7.24k
  const HeaderSearchOptions &HSOpts =
1495
7.24k
      PP.getHeaderSearchInfo().getHeaderSearchOpts();
1496
1497
7.24k
  AddString(HSOpts.Sysroot, Record);
1498
7.24k
  AddString(HSOpts.ResourceDir, Record);
1499
7.24k
  AddString(HSOpts.ModuleCachePath, Record);
1500
7.24k
  AddString(HSOpts.ModuleUserBuildPath, Record);
1501
7.24k
  Record.push_back(HSOpts.DisableModuleHash);
1502
7.24k
  Record.push_back(HSOpts.ImplicitModuleMaps);
1503
7.24k
  Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1504
7.24k
  Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1505
7.24k
  Record.push_back(HSOpts.UseBuiltinIncludes);
1506
7.24k
  Record.push_back(HSOpts.UseStandardSystemIncludes);
1507
7.24k
  Record.push_back(HSOpts.UseStandardCXXIncludes);
1508
7.24k
  Record.push_back(HSOpts.UseLibcxx);
1509
  // Write out the specific module cache path that contains the module files.
1510
7.24k
  AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1511
7.24k
  Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1512
1513
  // Preprocessor options.
1514
7.24k
  Record.clear();
1515
7.24k
  const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1516
1517
  // If we're building an implicit module with a context hash, the importer is
1518
  // guaranteed to have the same macros defined on the command line. Skip
1519
  // writing them.
1520
7.24k
  bool SkipMacros = BuildingImplicitModule && 
!HSOpts.DisableModuleHash2.88k
;
1521
7.24k
  bool WriteMacros = !SkipMacros;
1522
7.24k
  Record.push_back(WriteMacros);
1523
7.24k
  if (WriteMacros) {
1524
    // Macro definitions.
1525
4.50k
    Record.push_back(PPOpts.Macros.size());
1526
6.42k
    for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; 
++I1.92k
) {
1527
1.92k
      AddString(PPOpts.Macros[I].first, Record);
1528
1.92k
      Record.push_back(PPOpts.Macros[I].second);
1529
1.92k
    }
1530
4.50k
  }
1531
1532
  // Includes
1533
7.24k
  Record.push_back(PPOpts.Includes.size());
1534
7.25k
  for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; 
++I16
)
1535
16
    AddString(PPOpts.Includes[I], Record);
1536
1537
  // Macro includes
1538
7.24k
  Record.push_back(PPOpts.MacroIncludes.size());
1539
7.24k
  for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; 
++I0
)
1540
0
    AddString(PPOpts.MacroIncludes[I], Record);
1541
1542
7.24k
  Record.push_back(PPOpts.UsePredefines);
1543
  // Detailed record is important since it is used for the module cache hash.
1544
7.24k
  Record.push_back(PPOpts.DetailedRecord);
1545
7.24k
  AddString(PPOpts.ImplicitPCHInclude, Record);
1546
7.24k
  Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1547
7.24k
  Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1548
1549
  // Leave the options block.
1550
7.24k
  Stream.ExitBlock();
1551
1552
  // Original file name and file ID
1553
7.24k
  SourceManager &SM = Context.getSourceManager();
1554
7.24k
  if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID())) {
1555
4.16k
    auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1556
4.16k
    FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1557
4.16k
    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1558
4.16k
    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1559
4.16k
    unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1560
1561
4.16k
    Record.clear();
1562
4.16k
    Record.push_back(ORIGINAL_FILE);
1563
4.16k
    AddFileID(SM.getMainFileID(), Record);
1564
4.16k
    EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1565
4.16k
  }
1566
1567
7.24k
  Record.clear();
1568
7.24k
  AddFileID(SM.getMainFileID(), Record);
1569
7.24k
  Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1570
1571
7.24k
  WriteInputFiles(Context.SourceMgr,
1572
7.24k
                  PP.getHeaderSearchInfo().getHeaderSearchOpts());
1573
7.24k
  Stream.ExitBlock();
1574
7.24k
}
1575
1576
namespace  {
1577
1578
/// An input file.
1579
struct InputFileEntry {
1580
  FileEntryRef File;
1581
  bool IsSystemFile;
1582
  bool IsTransient;
1583
  bool BufferOverridden;
1584
  bool IsTopLevel;
1585
  bool IsModuleMap;
1586
  uint32_t ContentHash[2];
1587
1588
36.3k
  InputFileEntry(FileEntryRef File) : File(File) {}
1589
};
1590
1591
} // namespace
1592
1593
void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1594
7.24k
                                HeaderSearchOptions &HSOpts) {
1595
7.24k
  using namespace llvm;
1596
1597
7.24k
  Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1598
1599
  // Create input-file abbreviation.
1600
7.24k
  auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1601
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1602
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1603
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1604
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1605
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1606
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1607
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1608
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1609
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1610
7.24k
  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1611
7.24k
  unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1612
1613
  // Create input file hash abbreviation.
1614
7.24k
  auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1615
7.24k
  IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1616
7.24k
  IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1617
7.24k
  IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1618
7.24k
  unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1619
1620
7.24k
  uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1621
1622
  // Get all ContentCache objects for files.
1623
7.24k
  std::vector<InputFileEntry> UserFiles;
1624
7.24k
  std::vector<InputFileEntry> SystemFiles;
1625
2.06M
  for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; 
++I2.05M
) {
1626
    // Get this source location entry.
1627
2.05M
    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1628
2.05M
    assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1629
1630
    // We only care about file entries that were not overridden.
1631
2.05M
    if (!SLoc->isFile())
1632
2.00M
      continue;
1633
51.8k
    const SrcMgr::FileInfo &File = SLoc->getFile();
1634
51.8k
    const SrcMgr::ContentCache *Cache = &File.getContentCache();
1635
51.8k
    if (!Cache->OrigEntry)
1636
12.5k
      continue;
1637
1638
    // Do not emit input files that do not affect current module.
1639
39.3k
    if (!IsSLocAffecting[I])
1640
2.98k
      continue;
1641
1642
36.3k
    InputFileEntry Entry(*Cache->OrigEntry);
1643
36.3k
    Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1644
36.3k
    Entry.IsTransient = Cache->IsTransient;
1645
36.3k
    Entry.BufferOverridden = Cache->BufferOverridden;
1646
36.3k
    Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1647
36.3k
    Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
1648
1649
36.3k
    auto ContentHash = hash_code(-1);
1650
36.3k
    if (PP->getHeaderSearchInfo()
1651
36.3k
            .getHeaderSearchOpts()
1652
36.3k
            .ValidateASTInputFilesContent) {
1653
13
      auto MemBuff = Cache->getBufferIfLoaded();
1654
13
      if (MemBuff)
1655
13
        ContentHash = hash_value(MemBuff->getBuffer());
1656
0
      else
1657
0
        PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1658
0
            << Entry.File.getName();
1659
13
    }
1660
36.3k
    auto CH = llvm::APInt(64, ContentHash);
1661
36.3k
    Entry.ContentHash[0] =
1662
36.3k
        static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1663
36.3k
    Entry.ContentHash[1] =
1664
36.3k
        static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1665
1666
36.3k
    if (Entry.IsSystemFile)
1667
26.3k
      SystemFiles.push_back(Entry);
1668
9.94k
    else
1669
9.94k
      UserFiles.push_back(Entry);
1670
36.3k
  }
1671
1672
  // User files go at the front, system files at the back.
1673
7.24k
  auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
1674
7.24k
                                                  std::move(SystemFiles));
1675
1676
7.24k
  unsigned UserFilesNum = 0;
1677
  // Write out all of the input files.
1678
7.24k
  std::vector<uint64_t> InputFileOffsets;
1679
36.3k
  for (const auto &Entry : SortedFiles) {
1680
36.3k
    uint32_t &InputFileID = InputFileIDs[Entry.File];
1681
36.3k
    if (InputFileID != 0)
1682
12.7k
      continue; // already recorded this file.
1683
1684
    // Record this entry's offset.
1685
23.5k
    InputFileOffsets.push_back(Stream.GetCurrentBitNo() - InputFilesOffsetBase);
1686
1687
23.5k
    InputFileID = InputFileOffsets.size();
1688
1689
23.5k
    if (!Entry.IsSystemFile)
1690
9.57k
      ++UserFilesNum;
1691
1692
    // Emit size/modification time for this file.
1693
    // And whether this file was overridden.
1694
23.5k
    {
1695
23.5k
      SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1696
23.5k
      SmallString<128> Name = Entry.File.getName();
1697
1698
23.5k
      PreparePathForOutput(NameAsRequested);
1699
23.5k
      PreparePathForOutput(Name);
1700
1701
23.5k
      if (Name == NameAsRequested)
1702
23.4k
        Name.clear();
1703
1704
23.5k
      RecordData::value_type Record[] = {
1705
23.5k
          INPUT_FILE,
1706
23.5k
          InputFileOffsets.size(),
1707
23.5k
          (uint64_t)Entry.File.getSize(),
1708
23.5k
          (uint64_t)getTimestampForOutput(Entry.File),
1709
23.5k
          Entry.BufferOverridden,
1710
23.5k
          Entry.IsTransient,
1711
23.5k
          Entry.IsTopLevel,
1712
23.5k
          Entry.IsModuleMap,
1713
23.5k
          NameAsRequested.size()};
1714
1715
23.5k
      Stream.EmitRecordWithBlob(IFAbbrevCode, Record,
1716
23.5k
                                (NameAsRequested + Name).str());
1717
23.5k
    }
1718
1719
    // Emit content hash for this file.
1720
23.5k
    {
1721
23.5k
      RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1722
23.5k
                                         Entry.ContentHash[1]};
1723
23.5k
      Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1724
23.5k
    }
1725
23.5k
  }
1726
1727
7.24k
  Stream.ExitBlock();
1728
1729
  // Create input file offsets abbreviation.
1730
7.24k
  auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1731
7.24k
  OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1732
7.24k
  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1733
7.24k
  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1734
                                                                //   input files
1735
7.24k
  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));   // Array
1736
7.24k
  unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1737
1738
  // Write input file offsets.
1739
7.24k
  RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1740
7.24k
                                     InputFileOffsets.size(), UserFilesNum};
1741
7.24k
  Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1742
7.24k
}
1743
1744
//===----------------------------------------------------------------------===//
1745
// Source Manager Serialization
1746
//===----------------------------------------------------------------------===//
1747
1748
/// Create an abbreviation for the SLocEntry that refers to a
1749
/// file.
1750
7.24k
static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1751
7.24k
  using namespace llvm;
1752
1753
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1754
7.24k
  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1755
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1756
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1757
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1758
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1759
  // FileEntry fields.
1760
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1761
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1762
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1763
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1764
7.24k
  return Stream.EmitAbbrev(std::move(Abbrev));
1765
7.24k
}
1766
1767
/// Create an abbreviation for the SLocEntry that refers to a
1768
/// buffer.
1769
7.24k
static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1770
7.24k
  using namespace llvm;
1771
1772
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1773
7.24k
  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1774
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1775
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1776
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1777
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1778
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1779
7.24k
  return Stream.EmitAbbrev(std::move(Abbrev));
1780
7.24k
}
1781
1782
/// Create an abbreviation for the SLocEntry that refers to a
1783
/// buffer's blob.
1784
static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1785
14.4k
                                           bool Compressed) {
1786
14.4k
  using namespace llvm;
1787
1788
14.4k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1789
14.4k
  Abbrev->Add(BitCodeAbbrevOp(Compressed ? 
SM_SLOC_BUFFER_BLOB_COMPRESSED7.24k
1790
14.4k
                                         : 
SM_SLOC_BUFFER_BLOB7.24k
));
1791
14.4k
  if (Compressed)
1792
7.24k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1793
14.4k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1794
14.4k
  return Stream.EmitAbbrev(std::move(Abbrev));
1795
14.4k
}
1796
1797
/// Create an abbreviation for the SLocEntry that refers to a macro
1798
/// expansion.
1799
7.24k
static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1800
7.24k
  using namespace llvm;
1801
1802
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1803
7.24k
  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1804
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1805
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1806
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1807
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1808
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1809
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1810
7.24k
  return Stream.EmitAbbrev(std::move(Abbrev));
1811
7.24k
}
1812
1813
/// Emit key length and data length as ULEB-encoded data, and return them as a
1814
/// pair.
1815
static std::pair<unsigned, unsigned>
1816
15.3M
emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1817
15.3M
  llvm::encodeULEB128(KeyLen, Out);
1818
15.3M
  llvm::encodeULEB128(DataLen, Out);
1819
15.3M
  return std::make_pair(KeyLen, DataLen);
1820
15.3M
}
1821
1822
namespace {
1823
1824
  // Trait used for the on-disk hash table of header search information.
1825
  class HeaderFileInfoTrait {
1826
    ASTWriter &Writer;
1827
1828
    // Keep track of the framework names we've used during serialization.
1829
    SmallString<128> FrameworkStringData;
1830
    llvm::StringMap<unsigned> FrameworkNameOffset;
1831
1832
  public:
1833
7.24k
    HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1834
1835
    struct key_type {
1836
      StringRef Filename;
1837
      off_t Size;
1838
      time_t ModTime;
1839
    };
1840
    using key_type_ref = const key_type &;
1841
1842
    using UnresolvedModule =
1843
        llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1844
1845
    struct data_type {
1846
      const HeaderFileInfo &HFI;
1847
      bool AlreadyIncluded;
1848
      ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1849
      UnresolvedModule Unresolved;
1850
    };
1851
    using data_type_ref = const data_type &;
1852
1853
    using hash_value_type = unsigned;
1854
    using offset_type = unsigned;
1855
1856
20.2k
    hash_value_type ComputeHash(key_type_ref key) {
1857
      // The hash is based only on size/time of the file, so that the reader can
1858
      // match even when symlinking or excess path elements ("foo/../", "../")
1859
      // change the form of the name. However, complete path is still the key.
1860
20.2k
      return llvm::hash_combine(key.Size, key.ModTime);
1861
20.2k
    }
1862
1863
    std::pair<unsigned, unsigned>
1864
20.2k
    EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1865
20.2k
      unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1866
20.2k
      unsigned DataLen = 1 + 4 + 4;
1867
20.2k
      for (auto ModInfo : Data.KnownHeaders)
1868
13.9k
        if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1869
12.3k
          DataLen += 4;
1870
20.2k
      if (Data.Unresolved.getPointer())
1871
12
        DataLen += 4;
1872
20.2k
      return emitULEBKeyDataLength(KeyLen, DataLen, Out);
1873
20.2k
    }
1874
1875
20.2k
    void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1876
20.2k
      using namespace llvm::support;
1877
1878
20.2k
      endian::Writer LE(Out, little);
1879
20.2k
      LE.write<uint64_t>(key.Size);
1880
20.2k
      KeyLen -= 8;
1881
20.2k
      LE.write<uint64_t>(key.ModTime);
1882
20.2k
      KeyLen -= 8;
1883
20.2k
      Out.write(key.Filename.data(), KeyLen);
1884
20.2k
    }
1885
1886
    void EmitData(raw_ostream &Out, key_type_ref key,
1887
20.2k
                  data_type_ref Data, unsigned DataLen) {
1888
20.2k
      using namespace llvm::support;
1889
1890
20.2k
      endian::Writer LE(Out, little);
1891
20.2k
      uint64_t Start = Out.tell(); (void)Start;
1892
1893
20.2k
      unsigned char Flags = (Data.AlreadyIncluded << 6)
1894
20.2k
                          | (Data.HFI.isImport << 5)
1895
20.2k
                          | (Data.HFI.isPragmaOnce << 4)
1896
20.2k
                          | (Data.HFI.DirInfo << 1)
1897
20.2k
                          | Data.HFI.IndexHeaderMapHeader;
1898
20.2k
      LE.write<uint8_t>(Flags);
1899
1900
20.2k
      if (!Data.HFI.ControllingMacro)
1901
7.88k
        LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1902
12.3k
      else
1903
12.3k
        LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1904
1905
20.2k
      unsigned Offset = 0;
1906
20.2k
      if (!Data.HFI.Framework.empty()) {
1907
        // If this header refers into a framework, save the framework name.
1908
2.53k
        llvm::StringMap<unsigned>::iterator Pos
1909
2.53k
          = FrameworkNameOffset.find(Data.HFI.Framework);
1910
2.53k
        if (Pos == FrameworkNameOffset.end()) {
1911
157
          Offset = FrameworkStringData.size() + 1;
1912
157
          FrameworkStringData.append(Data.HFI.Framework);
1913
157
          FrameworkStringData.push_back(0);
1914
1915
157
          FrameworkNameOffset[Data.HFI.Framework] = Offset;
1916
157
        } else
1917
2.38k
          Offset = Pos->second;
1918
2.53k
      }
1919
20.2k
      LE.write<uint32_t>(Offset);
1920
1921
20.2k
      auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1922
13.9k
        if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1923
12.3k
          uint32_t Value = (ModID << 3) | (unsigned)Role;
1924
12.3k
          assert((Value >> 3) == ModID && "overflow in header module info");
1925
12.3k
          LE.write<uint32_t>(Value);
1926
12.3k
        }
1927
13.9k
      };
1928
1929
20.2k
      for (auto ModInfo : Data.KnownHeaders)
1930
13.9k
        EmitModule(ModInfo.getModule(), ModInfo.getRole());
1931
20.2k
      if (Data.Unresolved.getPointer())
1932
12
        EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1933
1934
20.2k
      assert(Out.tell() - Start == DataLen && "Wrong data length");
1935
20.2k
    }
1936
1937
7.24k
    const char *strings_begin() const { return FrameworkStringData.begin(); }
1938
7.24k
    const char *strings_end() const { return FrameworkStringData.end(); }
1939
  };
1940
1941
} // namespace
1942
1943
/// Write the header search block for the list of files that
1944
///
1945
/// \param HS The header search structure to save.
1946
7.24k
void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1947
7.24k
  HeaderFileInfoTrait GeneratorTrait(*this);
1948
7.24k
  llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1949
7.24k
  SmallVector<const char *, 4> SavedStrings;
1950
7.24k
  unsigned NumHeaderSearchEntries = 0;
1951
1952
  // Find all unresolved headers for the current module. We generally will
1953
  // have resolved them before we get here, but not necessarily: we might be
1954
  // compiling a preprocessed module, where there is no requirement for the
1955
  // original files to exist any more.
1956
7.24k
  const HeaderFileInfo Empty; // So we can take a reference.
1957
7.24k
  if (WritingModule) {
1958
3.51k
    llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1959
16.0k
    while (!Worklist.empty()) {
1960
12.5k
      Module *M = Worklist.pop_back_val();
1961
      // We don't care about headers in unimportable submodules.
1962
12.5k
      if (M->isUnimportable())
1963
438
        continue;
1964
1965
      // Map to disk files where possible, to pick up any missing stat
1966
      // information. This also means we don't need to check the unresolved
1967
      // headers list when emitting resolved headers in the first loop below.
1968
      // FIXME: It'd be preferable to avoid doing this if we were given
1969
      // sufficient stat information in the module map.
1970
12.0k
      HS.getModuleMap().resolveHeaderDirectives(M, /*File=*/std::nullopt);
1971
1972
      // If the file didn't exist, we can still create a module if we were given
1973
      // enough information in the module map.
1974
12.0k
      for (const auto &U : M->MissingHeaders) {
1975
        // Check that we were given enough information to build a module
1976
        // without this file existing on disk.
1977
13
        if (!U.Size || 
(12
!U.ModTime12
&&
IncludeTimestamps1
)) {
1978
1
          PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1979
1
              << WritingModule->getFullModuleName() << U.Size.has_value()
1980
1
              << U.FileName;
1981
1
          continue;
1982
1
        }
1983
1984
        // Form the effective relative pathname for the file.
1985
12
        SmallString<128> Filename(M->Directory->getName());
1986
12
        llvm::sys::path::append(Filename, U.FileName);
1987
12
        PreparePathForOutput(Filename);
1988
1989
12
        StringRef FilenameDup = strdup(Filename.c_str());
1990
12
        SavedStrings.push_back(FilenameDup.data());
1991
1992
12
        HeaderFileInfoTrait::key_type Key = {
1993
12
            FilenameDup, *U.Size, IncludeTimestamps ? 
*U.ModTime8
:
04
};
1994
12
        HeaderFileInfoTrait::data_type Data = {
1995
12
            Empty, false, {}, {M, ModuleMap::headerKindToRole(U.Kind)}};
1996
        // FIXME: Deal with cases where there are multiple unresolved header
1997
        // directives in different submodules for the same header.
1998
12
        Generator.insert(Key, Data, GeneratorTrait);
1999
12
        ++NumHeaderSearchEntries;
2000
12
      }
2001
12.0k
      auto SubmodulesRange = M->submodules();
2002
12.0k
      Worklist.append(SubmodulesRange.begin(), SubmodulesRange.end());
2003
12.0k
    }
2004
3.51k
  }
2005
2006
7.24k
  SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2007
7.24k
  HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2008
2009
7.24k
  if (FilesByUID.size() > HS.header_file_size())
2010
2.23k
    FilesByUID.resize(HS.header_file_size());
2011
2012
1.74M
  for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; 
++UID1.74M
) {
2013
1.74M
    OptionalFileEntryRef File = FilesByUID[UID];
2014
1.74M
    if (!File)
2015
0
      continue;
2016
2017
    // Get the file info. This will load info from the external source if
2018
    // necessary. Skip emitting this file if we have no information on it
2019
    // as a header file (in which case HFI will be null) or if it hasn't
2020
    // changed since it was loaded. Also skip it if it's for a modular header
2021
    // from a different module; in that case, we rely on the module(s)
2022
    // containing the header to provide this information.
2023
1.74M
    const HeaderFileInfo *HFI =
2024
1.74M
        HS.getExistingFileInfo(*File, /*WantExternal*/!Chain);
2025
1.74M
    if (!HFI || 
(1.42M
HFI->isModuleHeader1.42M
&&
!HFI->isCompilingModuleHeader1.41M
))
2026
1.72M
      continue;
2027
2028
    // Massage the file path into an appropriate form.
2029
20.2k
    StringRef Filename = File->getName();
2030
20.2k
    SmallString<128> FilenameTmp(Filename);
2031
20.2k
    if (PreparePathForOutput(FilenameTmp)) {
2032
      // If we performed any translation on the file name at all, we need to
2033
      // save this string, since the generator will refer to it later.
2034
15.3k
      Filename = StringRef(strdup(FilenameTmp.c_str()));
2035
15.3k
      SavedStrings.push_back(Filename.data());
2036
15.3k
    }
2037
2038
20.2k
    bool Included = PP->alreadyIncluded(*File);
2039
2040
20.2k
    HeaderFileInfoTrait::key_type Key = {
2041
20.2k
      Filename, File->getSize(), getTimestampForOutput(*File)
2042
20.2k
    };
2043
20.2k
    HeaderFileInfoTrait::data_type Data = {
2044
20.2k
      *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(*File), {}
2045
20.2k
    };
2046
20.2k
    Generator.insert(Key, Data, GeneratorTrait);
2047
20.2k
    ++NumHeaderSearchEntries;
2048
20.2k
  }
2049
2050
  // Create the on-disk hash table in a buffer.
2051
7.24k
  SmallString<4096> TableData;
2052
7.24k
  uint32_t BucketOffset;
2053
7.24k
  {
2054
7.24k
    using namespace llvm::support;
2055
2056
7.24k
    llvm::raw_svector_ostream Out(TableData);
2057
    // Make sure that no bucket is at offset 0
2058
7.24k
    endian::write<uint32_t>(Out, 0, little);
2059
7.24k
    BucketOffset = Generator.Emit(Out, GeneratorTrait);
2060
7.24k
  }
2061
2062
  // Create a blob abbreviation
2063
7.24k
  using namespace llvm;
2064
2065
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2066
7.24k
  Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2067
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2068
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2069
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2070
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2071
7.24k
  unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2072
2073
  // Write the header search table
2074
7.24k
  RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2075
7.24k
                                     NumHeaderSearchEntries, TableData.size()};
2076
7.24k
  TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
2077
7.24k
  Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
2078
2079
  // Free all of the strings we had to duplicate.
2080
22.6k
  for (unsigned I = 0, N = SavedStrings.size(); I != N; 
++I15.3k
)
2081
15.3k
    free(const_cast<char *>(SavedStrings[I]));
2082
7.24k
}
2083
2084
static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2085
                     unsigned SLocBufferBlobCompressedAbbrv,
2086
12.8k
                     unsigned SLocBufferBlobAbbrv) {
2087
12.8k
  using RecordDataType = ASTWriter::RecordData::value_type;
2088
2089
  // Compress the buffer if possible. We expect that almost all PCM
2090
  // consumers will not want its contents.
2091
12.8k
  SmallVector<uint8_t, 0> CompressedBuffer;
2092
12.8k
  if (llvm::compression::zstd::isAvailable()) {
2093
0
    llvm::compression::zstd::compress(
2094
0
        llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer, 9);
2095
0
    RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2096
0
    Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2097
0
                              llvm::toStringRef(CompressedBuffer));
2098
0
    return;
2099
0
  }
2100
12.8k
  if (llvm::compression::zlib::isAvailable()) {
2101
12.8k
    llvm::compression::zlib::compress(
2102
12.8k
        llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
2103
12.8k
    RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2104
12.8k
    Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2105
12.8k
                              llvm::toStringRef(CompressedBuffer));
2106
12.8k
    return;
2107
12.8k
  }
2108
2109
0
  RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2110
0
  Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2111
0
}
2112
2113
/// Writes the block containing the serialized form of the
2114
/// source manager.
2115
///
2116
/// TODO: We should probably use an on-disk hash table (stored in a
2117
/// blob), indexed based on the file name, so that we only create
2118
/// entries for files that we actually need. In the common case (no
2119
/// errors), we probably won't have to create file entries for any of
2120
/// the files in the AST.
2121
void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2122
7.24k
                                        const Preprocessor &PP) {
2123
7.24k
  RecordData Record;
2124
2125
  // Enter the source manager block.
2126
7.24k
  Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
2127
7.24k
  const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2128
2129
  // Abbreviations for the various kinds of source-location entries.
2130
7.24k
  unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2131
7.24k
  unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2132
7.24k
  unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2133
7.24k
  unsigned SLocBufferBlobCompressedAbbrv =
2134
7.24k
      CreateSLocBufferBlobAbbrev(Stream, true);
2135
7.24k
  unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2136
2137
  // Write out the source location entry table. We skip the first
2138
  // entry, which is always the same dummy entry.
2139
7.24k
  std::vector<uint32_t> SLocEntryOffsets;
2140
7.24k
  uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2141
7.24k
  RecordData PreloadSLocs;
2142
7.24k
  SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2143
7.24k
  for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2144
2.06M
       I != N; 
++I2.05M
) {
2145
    // Get this source location entry.
2146
2.05M
    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
2147
2.05M
    FileID FID = FileID::get(I);
2148
2.05M
    assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2149
2150
    // Record the offset of this source-location entry.
2151
2.05M
    uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2152
2.05M
    assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2153
2154
    // Figure out which record code to use.
2155
2.05M
    unsigned Code;
2156
2.05M
    if (SLoc->isFile()) {
2157
51.8k
      const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2158
51.8k
      if (Cache->OrigEntry) {
2159
39.3k
        Code = SM_SLOC_FILE_ENTRY;
2160
39.3k
      } else
2161
12.5k
        Code = SM_SLOC_BUFFER_ENTRY;
2162
51.8k
    } else
2163
2.00M
      Code = SM_SLOC_EXPANSION_ENTRY;
2164
2.05M
    Record.clear();
2165
2.05M
    Record.push_back(Code);
2166
2167
2.05M
    if (SLoc->isFile()) {
2168
51.8k
      const SrcMgr::FileInfo &File = SLoc->getFile();
2169
51.8k
      const SrcMgr::ContentCache *Content = &File.getContentCache();
2170
      // Do not emit files that were not listed as inputs.
2171
51.8k
      if (!IsSLocAffecting[I])
2172
2.98k
        continue;
2173
48.8k
      SLocEntryOffsets.push_back(Offset);
2174
      // Starting offset of this entry within this module, so skip the dummy.
2175
48.8k
      Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2176
48.8k
      AddSourceLocation(File.getIncludeLoc(), Record);
2177
48.8k
      Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2178
48.8k
      Record.push_back(File.hasLineDirectives());
2179
2180
48.8k
      bool EmitBlob = false;
2181
48.8k
      if (Content->OrigEntry) {
2182
36.3k
        assert(Content->OrigEntry == Content->ContentsEntry &&
2183
36.3k
               "Writing to AST an overridden file is not supported");
2184
2185
        // The source location entry is a file. Emit input file ID.
2186
36.3k
        assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2187
36.3k
        Record.push_back(InputFileIDs[Content->OrigEntry]);
2188
2189
36.3k
        Record.push_back(getAdjustedNumCreatedFIDs(FID));
2190
2191
36.3k
        FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
2192
36.3k
        if (FDI != FileDeclIDs.end()) {
2193
28.9k
          Record.push_back(FDI->second->FirstDeclIndex);
2194
28.9k
          Record.push_back(FDI->second->DeclIDs.size());
2195
28.9k
        } else {
2196
7.36k
          Record.push_back(0);
2197
7.36k
          Record.push_back(0);
2198
7.36k
        }
2199
2200
36.3k
        Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2201
2202
36.3k
        if (Content->BufferOverridden || 
Content->IsTransient36.0k
)
2203
308
          EmitBlob = true;
2204
36.3k
      } else {
2205
        // The source location entry is a buffer. The blob associated
2206
        // with this entry contains the contents of the buffer.
2207
2208
        // We add one to the size so that we capture the trailing NULL
2209
        // that is required by llvm::MemoryBuffer::getMemBuffer (on
2210
        // the reader side).
2211
12.5k
        std::optional<llvm::MemoryBufferRef> Buffer =
2212
12.5k
            Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2213
12.5k
        StringRef Name = Buffer ? Buffer->getBufferIdentifier() : 
""0
;
2214
12.5k
        Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2215
12.5k
                                  StringRef(Name.data(), Name.size() + 1));
2216
12.5k
        EmitBlob = true;
2217
2218
12.5k
        if (Name == "<built-in>")
2219
7.24k
          PreloadSLocs.push_back(SLocEntryOffsets.size());
2220
12.5k
      }
2221
2222
48.8k
      if (EmitBlob) {
2223
        // Include the implicit terminating null character in the on-disk buffer
2224
        // if we're writing it uncompressed.
2225
12.8k
        std::optional<llvm::MemoryBufferRef> Buffer =
2226
12.8k
            Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2227
12.8k
        if (!Buffer)
2228
0
          Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2229
12.8k
        StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2230
12.8k
        emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2231
12.8k
                 SLocBufferBlobAbbrv);
2232
12.8k
      }
2233
2.00M
    } else {
2234
      // The source location entry is a macro expansion.
2235
2.00M
      const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2236
2.00M
      SLocEntryOffsets.push_back(Offset);
2237
      // Starting offset of this entry within this module, so skip the dummy.
2238
2.00M
      Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2239
2.00M
      LocSeq::State Seq;
2240
2.00M
      AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq);
2241
2.00M
      AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq);
2242
2.00M
      AddSourceLocation(Expansion.isMacroArgExpansion()
2243
2.00M
                            ? 
SourceLocation()857k
2244
2.00M
                            : 
Expansion.getExpansionLocEnd()1.14M
,
2245
2.00M
                        Record, Seq);
2246
2.00M
      Record.push_back(Expansion.isExpansionTokenRange());
2247
2248
      // Compute the token length for this macro expansion.
2249
2.00M
      SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2250
2.00M
      if (I + 1 != N)
2251
2.00M
        NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2252
2.00M
      Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
2253
2.00M
      Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2254
2.00M
    }
2255
2.05M
  }
2256
2257
7.24k
  Stream.ExitBlock();
2258
2259
7.24k
  if (SLocEntryOffsets.empty())
2260
0
    return;
2261
2262
  // Write the source-location offsets table into the AST block. This
2263
  // table is used for lazily loading source-location information.
2264
7.24k
  using namespace llvm;
2265
2266
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2267
7.24k
  Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2268
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2269
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2270
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2271
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2272
7.24k
  unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2273
7.24k
  {
2274
7.24k
    RecordData::value_type Record[] = {
2275
7.24k
        SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2276
7.24k
        getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2277
7.24k
        SLocEntryOffsetsBase - SourceManagerBlockOffset};
2278
7.24k
    Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2279
7.24k
                              bytes(SLocEntryOffsets));
2280
7.24k
  }
2281
  // Write the source location entry preloads array, telling the AST
2282
  // reader which source locations entries it should load eagerly.
2283
7.24k
  Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
2284
2285
  // Write the line table. It depends on remapping working, so it must come
2286
  // after the source location offsets.
2287
7.24k
  if (SourceMgr.hasLineTable()) {
2288
7.24k
    LineTableInfo &LineTable = SourceMgr.getLineTable();
2289
2290
7.24k
    Record.clear();
2291
2292
    // Emit the needed file names.
2293
7.24k
    llvm::DenseMap<int, int> FilenameMap;
2294
7.24k
    FilenameMap[-1] = -1; // For unspecified filenames.
2295
477k
    for (const auto &L : LineTable) {
2296
477k
      if (L.first.ID < 0)
2297
468k
        continue;
2298
24.1k
      
for (auto &LE : L.second)9.45k
{
2299
24.1k
        if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2300
24.1k
                                              FilenameMap.size() - 1)).second)
2301
16.7k
          AddPath(LineTable.getFilename(LE.FilenameID), Record);
2302
24.1k
      }
2303
9.45k
    }
2304
7.24k
    Record.push_back(0);
2305
2306
    // Emit the line entries
2307
477k
    for (const auto &L : LineTable) {
2308
      // Only emit entries for local files.
2309
477k
      if (L.first.ID < 0)
2310
468k
        continue;
2311
2312
9.45k
      AddFileID(L.first, Record);
2313
2314
      // Emit the line entries
2315
9.45k
      Record.push_back(L.second.size());
2316
24.1k
      for (const auto &LE : L.second) {
2317
24.1k
        Record.push_back(LE.FileOffset);
2318
24.1k
        Record.push_back(LE.LineNo);
2319
24.1k
        Record.push_back(FilenameMap[LE.FilenameID]);
2320
24.1k
        Record.push_back((unsigned)LE.FileKind);
2321
24.1k
        Record.push_back(LE.IncludeOffset);
2322
24.1k
      }
2323
9.45k
    }
2324
2325
7.24k
    Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2326
7.24k
  }
2327
7.24k
}
2328
2329
//===----------------------------------------------------------------------===//
2330
// Preprocessor Serialization
2331
//===----------------------------------------------------------------------===//
2332
2333
static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2334
2.89M
                              const Preprocessor &PP) {
2335
2.89M
  if (MacroInfo *MI = MD->getMacroInfo())
2336
2.89M
    if (MI->isBuiltinMacro())
2337
203k
      return true;
2338
2339
2.69M
  if (IsModule) {
2340
1.30M
    SourceLocation Loc = MD->getLocation();
2341
1.30M
    if (Loc.isInvalid())
2342
0
      return true;
2343
1.30M
    if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2344
1.30M
      return true;
2345
1.30M
  }
2346
2347
1.38M
  return false;
2348
2.69M
}
2349
2350
/// Writes the block containing the serialized form of the
2351
/// preprocessor.
2352
7.24k
void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2353
7.24k
  uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2354
2355
7.24k
  PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2356
7.24k
  if (PPRec)
2357
218
    WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2358
2359
7.24k
  RecordData Record;
2360
7.24k
  RecordData ModuleMacroRecord;
2361
2362
  // If the preprocessor __COUNTER__ value has been bumped, remember it.
2363
7.24k
  if (PP.getCounterValue() != 0) {
2364
4
    RecordData::value_type Record[] = {PP.getCounterValue()};
2365
4
    Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2366
4
  }
2367
2368
  // If we have a recorded #pragma assume_nonnull, remember it so it can be
2369
  // replayed when the preamble terminates into the main file.
2370
7.24k
  SourceLocation AssumeNonNullLoc =
2371
7.24k
      PP.getPreambleRecordedPragmaAssumeNonNullLoc();
2372
7.24k
  if (AssumeNonNullLoc.isValid()) {
2373
1
    assert(PP.isRecordingPreamble());
2374
1
    AddSourceLocation(AssumeNonNullLoc, Record);
2375
1
    Stream.EmitRecord(PP_ASSUME_NONNULL_LOC, Record);
2376
1
    Record.clear();
2377
1
  }
2378
2379
7.24k
  if (PP.isRecordingPreamble() && 
PP.hasRecordedPreamble()100
) {
2380
7
    assert(!IsModule);
2381
7
    auto SkipInfo = PP.getPreambleSkipInfo();
2382
7
    if (SkipInfo) {
2383
5
      Record.push_back(true);
2384
5
      AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2385
5
      AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2386
5
      Record.push_back(SkipInfo->FoundNonSkipPortion);
2387
5
      Record.push_back(SkipInfo->FoundElse);
2388
5
      AddSourceLocation(SkipInfo->ElseLoc, Record);
2389
5
    } else {
2390
2
      Record.push_back(false);
2391
2
    }
2392
7
    for (const auto &Cond : PP.getPreambleConditionalStack()) {
2393
7
      AddSourceLocation(Cond.IfLoc, Record);
2394
7
      Record.push_back(Cond.WasSkipping);
2395
7
      Record.push_back(Cond.FoundNonSkip);
2396
7
      Record.push_back(Cond.FoundElse);
2397
7
    }
2398
7
    Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2399
7
    Record.clear();
2400
7
  }
2401
2402
  // Enter the preprocessor block.
2403
7.24k
  Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2404
2405
  // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2406
  // FIXME: Include a location for the use, and say which one was used.
2407
7.24k
  if (PP.SawDateOrTime())
2408
1
    PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2409
2410
  // Loop over all the macro directives that are live at the end of the file,
2411
  // emitting each to the PP section.
2412
2413
  // Construct the list of identifiers with macro directives that need to be
2414
  // serialized.
2415
7.24k
  SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2416
  // It is meaningless to emit macros for named modules. It only wastes times
2417
  // and spaces.
2418
7.24k
  if (!isWritingStdCXXNamedModules())
2419
6.92k
    for (auto &Id : PP.getIdentifierTable())
2420
34.1M
      if (Id.second->hadMacroDefinition() &&
2421
34.1M
          
(3.13M
!Id.second->isFromAST()3.13M
||
2422
3.13M
          
Id.second->hasChangedSinceDeserialization()94.7k
))
2423
3.12M
        MacroIdentifiers.push_back(Id.second);
2424
  // Sort the set of macro definitions that need to be serialized by the
2425
  // name of the macro, to provide a stable ordering.
2426
7.24k
  llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2427
2428
  // Emit the macro directives as a list and associate the offset with the
2429
  // identifier they belong to.
2430
3.12M
  for (const IdentifierInfo *Name : MacroIdentifiers) {
2431
3.12M
    MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2432
3.12M
    uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2433
3.12M
    assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2434
2435
    // Write out any exported module macros.
2436
3.12M
    bool EmittedModuleMacros = false;
2437
    // C+=20 Header Units are compiled module interfaces, but they preserve
2438
    // macros that are live (i.e. have a defined value) at the end of the
2439
    // compilation.  So when writing a header unit, we preserve only the final
2440
    // value of each macro (and discard any that are undefined).  Header units
2441
    // do not have sub-modules (although they might import other header units).
2442
    // PCH files, conversely, retain the history of each macro's define/undef
2443
    // and of leaf macros in sub modules.
2444
3.12M
    if (IsModule && 
WritingModule->isHeaderUnit()1.63M
) {
2445
      // This is for the main TU when it is a C++20 header unit.
2446
      // We preserve the final state of defined macros, and we do not emit ones
2447
      // that are undefined.
2448
13.3k
      if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2449
13.3k
          
MD->getKind() == MacroDirective::MD_Undefine30
)
2450
13.2k
        continue;
2451
24
      AddSourceLocation(MD->getLocation(), Record);
2452
24
      Record.push_back(MD->getKind());
2453
24
      if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2454
24
        Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2455
24
      } else 
if (auto *0
VisMD0
= dyn_cast<VisibilityMacroDirective>(MD)) {
2456
0
        Record.push_back(VisMD->isPublic());
2457
0
      }
2458
24
      ModuleMacroRecord.push_back(getSubmoduleID(WritingModule));
2459
24
      ModuleMacroRecord.push_back(getMacroRef(MD->getMacroInfo(), Name));
2460
24
      Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2461
24
      ModuleMacroRecord.clear();
2462
24
      EmittedModuleMacros = true;
2463
3.11M
    } else {
2464
      // Emit the macro directives in reverse source order.
2465
4.49M
      for (; MD; 
MD = MD->getPrevious()1.38M
) {
2466
        // Once we hit an ignored macro, we're done: the rest of the chain
2467
        // will all be ignored macros.
2468
2.88M
        if (shouldIgnoreMacro(MD, IsModule, PP))
2469
1.49M
          break;
2470
1.38M
        AddSourceLocation(MD->getLocation(), Record);
2471
1.38M
        Record.push_back(MD->getKind());
2472
1.38M
        if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2473
1.38M
          Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2474
1.38M
        } else 
if (auto *257
VisMD257
= dyn_cast<VisibilityMacroDirective>(MD)) {
2475
158
          Record.push_back(VisMD->isPublic());
2476
158
        }
2477
1.38M
      }
2478
2479
      // We write out exported module macros for PCH as well.
2480
3.11M
      auto Leafs = PP.getLeafModuleMacros(Name);
2481
3.11M
      SmallVector<ModuleMacro *, 8> Worklist(Leafs.begin(), Leafs.end());
2482
3.11M
      llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2483
7.79M
      while (!Worklist.empty()) {
2484
4.68M
        auto *Macro = Worklist.pop_back_val();
2485
2486
        // Emit a record indicating this submodule exports this macro.
2487
4.68M
        ModuleMacroRecord.push_back(getSubmoduleID(Macro->getOwningModule()));
2488
4.68M
        ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2489
4.68M
        for (auto *M : Macro->overrides())
2490
2.19k
          ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2491
2492
4.68M
        Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2493
4.68M
        ModuleMacroRecord.clear();
2494
2495
        // Enqueue overridden macros once we've visited all their ancestors.
2496
4.68M
        for (auto *M : Macro->overrides())
2497
2.19k
          if (++Visits[M] == M->getNumOverridingMacros())
2498
2.09k
            Worklist.push_back(M);
2499
2500
4.68M
        EmittedModuleMacros = true;
2501
4.68M
      }
2502
3.11M
    }
2503
3.11M
    if (Record.empty() && 
!EmittedModuleMacros1.72M
)
2504
1.49M
      continue;
2505
2506
1.61M
    IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2507
1.61M
    Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2508
1.61M
    Record.clear();
2509
1.61M
  }
2510
2511
  /// Offsets of each of the macros into the bitstream, indexed by
2512
  /// the local macro ID
2513
  ///
2514
  /// For each identifier that is associated with a macro, this map
2515
  /// provides the offset into the bitstream where that macro is
2516
  /// defined.
2517
7.24k
  std::vector<uint32_t> MacroOffsets;
2518
2519
1.62M
  for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; 
++I1.61M
) {
2520
1.61M
    const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2521
1.61M
    MacroInfo *MI = MacroInfosToEmit[I].MI;
2522
1.61M
    MacroID ID = MacroInfosToEmit[I].ID;
2523
2524
1.61M
    if (ID < FirstMacroID) {
2525
0
      assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2526
0
      continue;
2527
0
    }
2528
2529
    // Record the local offset of this macro.
2530
1.61M
    unsigned Index = ID - FirstMacroID;
2531
1.61M
    if (Index >= MacroOffsets.size())
2532
1.61M
      MacroOffsets.resize(Index + 1);
2533
2534
1.61M
    uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2535
1.61M
    assert((Offset >> 32) == 0 && "Macro offset too large");
2536
1.61M
    MacroOffsets[Index] = Offset;
2537
2538
1.61M
    AddIdentifierRef(Name, Record);
2539
1.61M
    AddSourceLocation(MI->getDefinitionLoc(), Record);
2540
1.61M
    AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2541
1.61M
    Record.push_back(MI->isUsed());
2542
1.61M
    Record.push_back(MI->isUsedForHeaderGuard());
2543
1.61M
    Record.push_back(MI->getNumTokens());
2544
1.61M
    unsigned Code;
2545
1.61M
    if (MI->isObjectLike()) {
2546
1.56M
      Code = PP_MACRO_OBJECT_LIKE;
2547
1.56M
    } else {
2548
57.2k
      Code = PP_MACRO_FUNCTION_LIKE;
2549
2550
57.2k
      Record.push_back(MI->isC99Varargs());
2551
57.2k
      Record.push_back(MI->isGNUVarargs());
2552
57.2k
      Record.push_back(MI->hasCommaPasting());
2553
57.2k
      Record.push_back(MI->getNumParams());
2554
57.2k
      for (const IdentifierInfo *Param : MI->params())
2555
118k
        AddIdentifierRef(Param, Record);
2556
57.2k
    }
2557
2558
    // If we have a detailed preprocessing record, record the macro definition
2559
    // ID that corresponds to this macro.
2560
1.61M
    if (PPRec)
2561
54.5k
      Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2562
2563
1.61M
    Stream.EmitRecord(Code, Record);
2564
1.61M
    Record.clear();
2565
2566
    // Emit the tokens array.
2567
4.72M
    for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; 
++TokNo3.10M
) {
2568
      // Note that we know that the preprocessor does not have any annotation
2569
      // tokens in it because they are created by the parser, and thus can't
2570
      // be in a macro definition.
2571
3.10M
      const Token &Tok = MI->getReplacementToken(TokNo);
2572
3.10M
      AddToken(Tok, Record);
2573
3.10M
      Stream.EmitRecord(PP_TOKEN, Record);
2574
3.10M
      Record.clear();
2575
3.10M
    }
2576
1.61M
    ++NumMacros;
2577
1.61M
  }
2578
2579
7.24k
  Stream.ExitBlock();
2580
2581
  // Write the offsets table for macro IDs.
2582
7.24k
  using namespace llvm;
2583
2584
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2585
7.24k
  Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2586
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2587
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2588
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32));   // base offset
2589
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2590
2591
7.24k
  unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2592
7.24k
  {
2593
7.24k
    RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2594
7.24k
                                       FirstMacroID - NUM_PREDEF_MACRO_IDS,
2595
7.24k
                                       MacroOffsetsBase - ASTBlockStartOffset};
2596
7.24k
    Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2597
7.24k
  }
2598
7.24k
}
2599
2600
void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2601
218
                                        uint64_t MacroOffsetsBase) {
2602
218
  if (PPRec.local_begin() == PPRec.local_end())
2603
1
    return;
2604
2605
217
  SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2606
2607
  // Enter the preprocessor block.
2608
217
  Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2609
2610
  // If the preprocessor has a preprocessing record, emit it.
2611
217
  unsigned NumPreprocessingRecords = 0;
2612
217
  using namespace llvm;
2613
2614
  // Set up the abbreviation for
2615
217
  unsigned InclusionAbbrev = 0;
2616
217
  {
2617
217
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
2618
217
    Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2619
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2620
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2621
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2622
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2623
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2624
217
    InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2625
217
  }
2626
2627
217
  unsigned FirstPreprocessorEntityID
2628
217
    = (Chain ? 
PPRec.getNumLoadedPreprocessedEntities()96
:
0121
)
2629
217
    + NUM_PREDEF_PP_ENTITY_IDS;
2630
217
  unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2631
217
  RecordData Record;
2632
217
  for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2633
217
                                  EEnd = PPRec.local_end();
2634
79.6k
       E != EEnd;
2635
79.4k
       (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2636
79.4k
    Record.clear();
2637
2638
79.4k
    uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2639
79.4k
    assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2640
79.4k
    PreprocessedEntityOffsets.push_back(
2641
79.4k
        PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
2642
2643
79.4k
    if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2644
      // Record this macro definition's ID.
2645
79.1k
      MacroDefinitions[MD] = NextPreprocessorEntityID;
2646
2647
79.1k
      AddIdentifierRef(MD->getName(), Record);
2648
79.1k
      Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2649
79.1k
      continue;
2650
79.1k
    }
2651
2652
297
    if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2653
39
      Record.push_back(ME->isBuiltinMacro());
2654
39
      if (ME->isBuiltinMacro())
2655
6
        AddIdentifierRef(ME->getName(), Record);
2656
33
      else
2657
33
        Record.push_back(MacroDefinitions[ME->getDefinition()]);
2658
39
      Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2659
39
      continue;
2660
39
    }
2661
2662
258
    if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2663
258
      Record.push_back(PPD_INCLUSION_DIRECTIVE);
2664
258
      Record.push_back(ID->getFileName().size());
2665
258
      Record.push_back(ID->wasInQuotes());
2666
258
      Record.push_back(static_cast<unsigned>(ID->getKind()));
2667
258
      Record.push_back(ID->importedModule());
2668
258
      SmallString<64> Buffer;
2669
258
      Buffer += ID->getFileName();
2670
      // Check that the FileEntry is not null because it was not resolved and
2671
      // we create a PCH even with compiler errors.
2672
258
      if (ID->getFile())
2673
255
        Buffer += ID->getFile()->getName();
2674
258
      Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2675
258
      continue;
2676
258
    }
2677
2678
0
    llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2679
0
  }
2680
217
  Stream.ExitBlock();
2681
2682
  // Write the offsets table for the preprocessing record.
2683
217
  if (NumPreprocessingRecords > 0) {
2684
217
    assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2685
2686
    // Write the offsets table for identifier IDs.
2687
217
    using namespace llvm;
2688
2689
217
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
2690
217
    Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2691
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2692
217
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2693
217
    unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2694
2695
217
    RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2696
217
                                       FirstPreprocessorEntityID -
2697
217
                                           NUM_PREDEF_PP_ENTITY_IDS};
2698
217
    Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2699
217
                              bytes(PreprocessedEntityOffsets));
2700
217
  }
2701
2702
  // Write the skipped region table for the preprocessing record.
2703
217
  ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2704
217
  if (SkippedRanges.size() > 0) {
2705
34
    std::vector<PPSkippedRange> SerializedSkippedRanges;
2706
34
    SerializedSkippedRanges.reserve(SkippedRanges.size());
2707
34
    for (auto const& Range : SkippedRanges)
2708
49
      SerializedSkippedRanges.emplace_back(Range);
2709
2710
34
    using namespace llvm;
2711
34
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
2712
34
    Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2713
34
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2714
34
    unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2715
2716
34
    Record.clear();
2717
34
    Record.push_back(PPD_SKIPPED_RANGES);
2718
34
    Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2719
34
                              bytes(SerializedSkippedRanges));
2720
34
  }
2721
217
}
2722
2723
6.62M
unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2724
6.62M
  if (!Mod)
2725
489k
    return 0;
2726
2727
6.13M
  auto Known = SubmoduleIDs.find(Mod);
2728
6.13M
  if (Known != SubmoduleIDs.end())
2729
6.11M
    return Known->second;
2730
2731
15.7k
  auto *Top = Mod->getTopLevelModule();
2732
15.7k
  if (Top != WritingModule &&
2733
15.7k
      
(3.10k
getLangOpts().CompilingPCH3.10k
||
2734
3.10k
       
!Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))3.08k
))
2735
3.10k
    return 0;
2736
2737
12.5k
  return SubmoduleIDs[Mod] = NextSubmoduleID++;
2738
15.7k
}
2739
2740
6.59M
unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2741
6.59M
  unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2742
  // FIXME: This can easily happen, if we have a reference to a submodule that
2743
  // did not result in us loading a module file for that submodule. For
2744
  // instance, a cross-top-level-module 'conflict' declaration will hit this.
2745
  // assert((ID || !Mod) &&
2746
  //        "asked for module ID for non-local, non-imported module");
2747
6.59M
  return ID;
2748
6.59M
}
2749
2750
/// Compute the number of modules within the given tree (including the
2751
/// given module).
2752
25.1k
static unsigned getNumberOfModules(Module *Mod) {
2753
25.1k
  unsigned ChildModules = 0;
2754
25.1k
  for (auto *Submodule : Mod->submodules())
2755
18.1k
    ChildModules += getNumberOfModules(Submodule);
2756
2757
25.1k
  return ChildModules + 1;
2758
25.1k
}
2759
2760
3.51k
void ASTWriter::WriteSubmodules(Module *WritingModule) {
2761
  // Enter the submodule description block.
2762
3.51k
  Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2763
2764
  // Write the abbreviations needed for the submodules block.
2765
3.51k
  using namespace llvm;
2766
2767
3.51k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2768
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2769
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2770
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2771
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2772
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2773
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2774
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2775
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2776
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2777
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2778
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2779
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2780
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2781
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2782
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2783
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2784
3.51k
  unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2785
2786
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2787
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2788
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2789
3.51k
  unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2790
2791
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2792
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2793
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2794
3.51k
  unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2795
2796
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2797
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2798
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2799
3.51k
  unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2800
2801
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2802
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2803
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2804
3.51k
  unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2805
2806
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2807
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2808
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2809
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Feature
2810
3.51k
  unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2811
2812
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2813
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2814
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2815
3.51k
  unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2816
2817
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2818
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2819
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2820
3.51k
  unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2821
2822
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2823
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2824
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2825
3.51k
  unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2826
2827
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2828
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2829
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2830
3.51k
  unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2831
2832
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2833
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2834
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2835
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Name
2836
3.51k
  unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2837
2838
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2839
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2840
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2841
3.51k
  unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2842
2843
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2844
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2845
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));  // Other module
2846
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Message
2847
3.51k
  unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2848
2849
3.51k
  Abbrev = std::make_shared<BitCodeAbbrev>();
2850
3.51k
  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2851
3.51k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2852
3.51k
  unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2853
2854
  // Write the submodule metadata block.
2855
3.51k
  RecordData::value_type Record[] = {
2856
3.51k
      getNumberOfModules(WritingModule),
2857
3.51k
      FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2858
3.51k
  Stream.EmitRecord(SUBMODULE_METADATA, Record);
2859
2860
  // Write all of the submodules.
2861
3.51k
  std::queue<Module *> Q;
2862
3.51k
  Q.push(WritingModule);
2863
16.1k
  while (!Q.empty()) {
2864
12.5k
    Module *Mod = Q.front();
2865
12.5k
    Q.pop();
2866
12.5k
    unsigned ID = getSubmoduleID(Mod);
2867
2868
12.5k
    uint64_t ParentID = 0;
2869
12.5k
    if (Mod->Parent) {
2870
9.07k
      assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2871
9.07k
      ParentID = SubmoduleIDs[Mod->Parent];
2872
9.07k
    }
2873
2874
12.5k
    uint64_t DefinitionLoc =
2875
12.5k
        SourceLocationEncoding::encode(getAdjustedLocation(Mod->DefinitionLoc));
2876
2877
    // Emit the definition of the block.
2878
12.5k
    {
2879
12.5k
      RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2880
12.5k
                                         ID,
2881
12.5k
                                         ParentID,
2882
12.5k
                                         (RecordData::value_type)Mod->Kind,
2883
12.5k
                                         DefinitionLoc,
2884
12.5k
                                         Mod->IsFramework,
2885
12.5k
                                         Mod->IsExplicit,
2886
12.5k
                                         Mod->IsSystem,
2887
12.5k
                                         Mod->IsExternC,
2888
12.5k
                                         Mod->InferSubmodules,
2889
12.5k
                                         Mod->InferExplicitSubmodules,
2890
12.5k
                                         Mod->InferExportWildcard,
2891
12.5k
                                         Mod->ConfigMacrosExhaustive,
2892
12.5k
                                         Mod->ModuleMapIsPrivate,
2893
12.5k
                                         Mod->NamedModuleHasInit};
2894
12.5k
      Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2895
12.5k
    }
2896
2897
    // Emit the requirements.
2898
12.5k
    for (const auto &R : Mod->Requirements) {
2899
706
      RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2900
706
      Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2901
706
    }
2902
2903
    // Emit the umbrella header, if there is one.
2904
12.5k
    if (std::optional<Module::Header> UmbrellaHeader =
2905
12.5k
            Mod->getUmbrellaHeaderAsWritten()) {
2906
403
      RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2907
403
      Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2908
403
                                UmbrellaHeader->NameAsWritten);
2909
12.1k
    } else if (std::optional<Module::DirectoryName> UmbrellaDir =
2910
12.1k
                   Mod->getUmbrellaDirAsWritten()) {
2911
93
      RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2912
93
      Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2913
93
                                UmbrellaDir->NameAsWritten);
2914
93
    }
2915
2916
    // Emit the headers.
2917
12.5k
    struct {
2918
12.5k
      unsigned RecordKind;
2919
12.5k
      unsigned Abbrev;
2920
12.5k
      Module::HeaderKind HeaderKind;
2921
12.5k
    } HeaderLists[] = {
2922
12.5k
      {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2923
12.5k
      {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2924
12.5k
      {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2925
12.5k
      {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2926
12.5k
        Module::HK_PrivateTextual},
2927
12.5k
      {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2928
12.5k
    };
2929
62.9k
    for (auto &HL : HeaderLists) {
2930
62.9k
      RecordData::value_type Record[] = {HL.RecordKind};
2931
62.9k
      for (auto &H : Mod->Headers[HL.HeaderKind])
2932
7.58k
        Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2933
62.9k
    }
2934
2935
    // Emit the top headers.
2936
12.5k
    {
2937
12.5k
      RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2938
12.5k
      for (FileEntryRef H : Mod->getTopHeaders(PP->getFileManager())) {
2939
12.2k
        SmallString<128> HeaderName(H.getName());
2940
12.2k
        PreparePathForOutput(HeaderName);
2941
12.2k
        Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
2942
12.2k
      }
2943
12.5k
    }
2944
2945
    // Emit the imports.
2946
12.5k
    if (!Mod->Imports.empty()) {
2947
7.90k
      RecordData Record;
2948
7.90k
      for (auto *I : Mod->Imports)
2949
33.6k
        Record.push_back(getSubmoduleID(I));
2950
7.90k
      Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2951
7.90k
    }
2952
2953
    // Emit the modules affecting compilation that were not imported.
2954
12.5k
    if (!Mod->AffectingClangModules.empty()) {
2955
3
      RecordData Record;
2956
3
      for (auto *I : Mod->AffectingClangModules)
2957
3
        Record.push_back(getSubmoduleID(I));
2958
3
      Stream.EmitRecord(SUBMODULE_AFFECTING_MODULES, Record);
2959
3
    }
2960
2961
    // Emit the exports.
2962
12.5k
    if (!Mod->Exports.empty()) {
2963
8.63k
      RecordData Record;
2964
8.85k
      for (const auto &E : Mod->Exports) {
2965
        // FIXME: This may fail; we don't require that all exported modules
2966
        // are local or imported.
2967
8.85k
        Record.push_back(getSubmoduleID(E.getPointer()));
2968
8.85k
        Record.push_back(E.getInt());
2969
8.85k
      }
2970
8.63k
      Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2971
8.63k
    }
2972
2973
    //FIXME: How do we emit the 'use'd modules?  They may not be submodules.
2974
    // Might be unnecessary as use declarations are only used to build the
2975
    // module itself.
2976
2977
    // TODO: Consider serializing undeclared uses of modules.
2978
2979
    // Emit the link libraries.
2980
12.5k
    for (const auto &LL : Mod->LinkLibraries) {
2981
356
      RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2982
356
                                         LL.IsFramework};
2983
356
      Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2984
356
    }
2985
2986
    // Emit the conflicts.
2987
12.5k
    for (const auto &C : Mod->Conflicts) {
2988
      // FIXME: This may fail; we don't require that all conflicting modules
2989
      // are local or imported.
2990
1
      RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2991
1
                                         getSubmoduleID(C.Other)};
2992
1
      Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2993
1
    }
2994
2995
    // Emit the configuration macros.
2996
12.5k
    for (const auto &CM : Mod->ConfigMacros) {
2997
2
      RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2998
2
      Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2999
2
    }
3000
3001
    // Emit the initializers, if any.
3002
12.5k
    RecordData Inits;
3003
12.5k
    for (Decl *D : Context->getModuleInitializers(Mod))
3004
384
      Inits.push_back(GetDeclRef(D));
3005
12.5k
    if (!Inits.empty())
3006
248
      Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3007
3008
    // Emit the name of the re-exported module, if any.
3009
12.5k
    if (!Mod->ExportAsModule.empty()) {
3010
6
      RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3011
6
      Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3012
6
    }
3013
3014
    // Queue up the submodules of this module.
3015
12.5k
    for (auto *M : Mod->submodules())
3016
9.07k
      Q.push(M);
3017
12.5k
  }
3018
3019
3.51k
  Stream.ExitBlock();
3020
3021
3.51k
  assert((NextSubmoduleID - FirstSubmoduleID ==
3022
3.51k
          getNumberOfModules(WritingModule)) &&
3023
3.51k
         "Wrong # of submodules; found a reference to a non-local, "
3024
3.51k
         "non-imported submodule?");
3025
3.51k
}
3026
3027
void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3028
7.24k
                                              bool isModule) {
3029
7.24k
  llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3030
7.24k
      DiagStateIDMap;
3031
7.24k
  unsigned CurrID = 0;
3032
7.24k
  RecordData Record;
3033
3034
7.24k
  auto EncodeDiagStateFlags =
3035
31.5k
      [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3036
31.5k
    unsigned Result = (unsigned)DS->ExtBehavior;
3037
31.5k
    for (unsigned Val :
3038
31.5k
         {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3039
31.5k
          (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3040
31.5k
          (unsigned)DS->SuppressSystemWarnings})
3041
157k
      Result = (Result << 1) | Val;
3042
31.5k
    return Result;
3043
31.5k
  };
3044
3045
7.24k
  unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3046
7.24k
  Record.push_back(Flags);
3047
3048
7.24k
  auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3049
24.3k
                          bool IncludeNonPragmaStates) {
3050
    // Ensure that the diagnostic state wasn't modified since it was created.
3051
    // We will not correctly round-trip this information otherwise.
3052
24.3k
    assert(Flags == EncodeDiagStateFlags(State) &&
3053
24.3k
           "diag state flags vary in single AST file");
3054
3055
    // If we ever serialize non-pragma mappings outside the initial state, the
3056
    // code below will need to consider more than getDefaultMapping.
3057
24.3k
    assert(!IncludeNonPragmaStates ||
3058
24.3k
           State == Diag.DiagStatesByLoc.FirstDiagState);
3059
3060
24.3k
    unsigned &DiagStateID = DiagStateIDMap[State];
3061
24.3k
    Record.push_back(DiagStateID);
3062
3063
24.3k
    if (DiagStateID == 0) {
3064
8.54k
      DiagStateID = ++CurrID;
3065
8.54k
      SmallVector<std::pair<unsigned, DiagnosticMapping>> Mappings;
3066
3067
      // Add a placeholder for the number of mappings.
3068
8.54k
      auto SizeIdx = Record.size();
3069
8.54k
      Record.emplace_back();
3070
239k
      for (const auto &I : *State) {
3071
        // Maybe skip non-pragmas.
3072
239k
        if (!I.second.isPragma() && 
!IncludeNonPragmaStates225k
)
3073
143k
          continue;
3074
        // Skip default mappings. We have a mapping for every diagnostic ever
3075
        // emitted, regardless of whether it was customized.
3076
96.0k
        if (!I.second.isPragma() &&
3077
96.0k
            
I.second == DiagnosticIDs::getDefaultMapping(I.first)81.6k
)
3078
74.8k
          continue;
3079
21.1k
        Mappings.push_back(I);
3080
21.1k
      }
3081
3082
      // Sort by diag::kind for deterministic output.
3083
151k
      llvm::sort(Mappings, [](const auto &LHS, const auto &RHS) {
3084
151k
        return LHS.first < RHS.first;
3085
151k
      });
3086
3087
21.1k
      for (const auto &I : Mappings) {
3088
21.1k
        Record.push_back(I.first);
3089
21.1k
        Record.push_back(I.second.serialize());
3090
21.1k
      }
3091
      // Update the placeholder.
3092
8.54k
      Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
3093
8.54k
    }
3094
24.3k
  };
3095
3096
7.24k
  AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
3097
3098
  // Reserve a spot for the number of locations with state transitions.
3099
7.24k
  auto NumLocationsIdx = Record.size();
3100
7.24k
  Record.emplace_back();
3101
3102
  // Emit the state transitions.
3103
7.24k
  unsigned NumLocations = 0;
3104
595k
  for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3105
595k
    if (!FileIDAndFile.first.isValid() ||
3106
595k
        
!FileIDAndFile.second.HasLocalTransitions594k
)
3107
591k
      continue;
3108
4.77k
    ++NumLocations;
3109
3110
4.77k
    SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3111
4.77k
    assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3112
4.77k
    AddSourceLocation(Loc, Record);
3113
3114
4.77k
    Record.push_back(FileIDAndFile.second.StateTransitions.size());
3115
9.82k
    for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3116
9.82k
      Record.push_back(getAdjustedOffset(StatePoint.Offset));
3117
9.82k
      AddDiagState(StatePoint.State, false);
3118
9.82k
    }
3119
4.77k
  }
3120
3121
  // Backpatch the number of locations.
3122
7.24k
  Record[NumLocationsIdx] = NumLocations;
3123
3124
  // Emit CurDiagStateLoc.  Do it last in order to match source order.
3125
  //
3126
  // This also protects against a hypothetical corner case with simulating
3127
  // -Werror settings for implicit modules in the ASTReader, where reading
3128
  // CurDiagState out of context could change whether warning pragmas are
3129
  // treated as errors.
3130
7.24k
  AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3131
7.24k
  AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3132
3133
7.24k
  Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
3134
7.24k
}
3135
3136
//===----------------------------------------------------------------------===//
3137
// Type Serialization
3138
//===----------------------------------------------------------------------===//
3139
3140
/// Write the representation of a type to the AST stream.
3141
805k
void ASTWriter::WriteType(QualType T) {
3142
805k
  TypeIdx &IdxRef = TypeIdxs[T];
3143
805k
  if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3144
0
    IdxRef = TypeIdx(NextTypeID++);
3145
805k
  TypeIdx Idx = IdxRef;
3146
3147
805k
  assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
3148
3149
  // Emit the type's representation.
3150
805k
  uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
3151
3152
  // Record the offset for this type.
3153
805k
  unsigned Index = Idx.getIndex() - FirstTypeID;
3154
805k
  if (TypeOffsets.size() == Index)
3155
805k
    TypeOffsets.emplace_back(Offset);
3156
0
  else if (TypeOffsets.size() < Index) {
3157
0
    TypeOffsets.resize(Index + 1);
3158
0
    TypeOffsets[Index].setBitOffset(Offset);
3159
0
  } else {
3160
0
    llvm_unreachable("Types emitted in wrong order");
3161
0
  }
3162
805k
}
3163
3164
//===----------------------------------------------------------------------===//
3165
// Declaration Serialization
3166
//===----------------------------------------------------------------------===//
3167
3168
/// Write the block containing all of the declaration IDs
3169
/// lexically declared within the given DeclContext.
3170
///
3171
/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3172
/// bitstream, or 0 if no block was written.
3173
uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3174
495k
                                                 DeclContext *DC) {
3175
495k
  if (DC->decls_empty())
3176
289k
    return 0;
3177
3178
206k
  uint64_t Offset = Stream.GetCurrentBitNo();
3179
206k
  SmallVector<uint32_t, 128> KindDeclPairs;
3180
956k
  for (const auto *D : DC->decls()) {
3181
956k
    KindDeclPairs.push_back(D->getKind());
3182
956k
    KindDeclPairs.push_back(GetDeclRef(D));
3183
956k
  }
3184
3185
206k
  ++NumLexicalDeclContexts;
3186
206k
  RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3187
206k
  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3188
206k
                            bytes(KindDeclPairs));
3189
206k
  return Offset;
3190
495k
}
3191
3192
7.24k
void ASTWriter::WriteTypeDeclOffsets() {
3193
7.24k
  using namespace llvm;
3194
3195
  // Write the type offsets array
3196
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3197
7.24k
  Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
3198
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3199
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
3200
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3201
7.24k
  unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3202
7.24k
  {
3203
7.24k
    RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3204
7.24k
                                       FirstTypeID - NUM_PREDEF_TYPE_IDS};
3205
7.24k
    Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3206
7.24k
  }
3207
3208
  // Write the declaration offsets array
3209
7.24k
  Abbrev = std::make_shared<BitCodeAbbrev>();
3210
7.24k
  Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
3211
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3212
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
3213
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3214
7.24k
  unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3215
7.24k
  {
3216
7.24k
    RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3217
7.24k
                                       FirstDeclID - NUM_PREDEF_DECL_IDS};
3218
7.24k
    Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3219
7.24k
  }
3220
7.24k
}
3221
3222
7.24k
void ASTWriter::WriteFileDeclIDsMap() {
3223
7.24k
  using namespace llvm;
3224
3225
7.24k
  SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3226
7.24k
  SortedFileDeclIDs.reserve(FileDeclIDs.size());
3227
7.24k
  for (const auto &P : FileDeclIDs)
3228
29.0k
    SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
3229
7.24k
  llvm::sort(SortedFileDeclIDs, llvm::less_first());
3230
3231
  // Join the vectors of DeclIDs from all files.
3232
7.24k
  SmallVector<DeclID, 256> FileGroupedDeclIDs;
3233
29.0k
  for (auto &FileDeclEntry : SortedFileDeclIDs) {
3234
29.0k
    DeclIDInFileInfo &Info = *FileDeclEntry.second;
3235
29.0k
    Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3236
29.0k
    llvm::stable_sort(Info.DeclIDs);
3237
29.0k
    for (auto &LocDeclEntry : Info.DeclIDs)
3238
311k
      FileGroupedDeclIDs.push_back(LocDeclEntry.second);
3239
29.0k
  }
3240
3241
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3242
7.24k
  Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
3243
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3244
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3245
7.24k
  unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
3246
7.24k
  RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3247
7.24k
                                     FileGroupedDeclIDs.size()};
3248
7.24k
  Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
3249
7.24k
}
3250
3251
7.24k
void ASTWriter::WriteComments() {
3252
7.24k
  Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
3253
7.24k
  auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3254
7.24k
  if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3255
0
    return;
3256
3257
  // Don't write comments to BMI to reduce the size of BMI.
3258
  // If language services (e.g., clangd) want such abilities,
3259
  // we can offer a special option then.
3260
7.24k
  if (isWritingStdCXXNamedModules())
3261
320
    return;
3262
3263
6.92k
  RecordData Record;
3264
6.92k
  for (const auto &FO : Context->Comments.OrderedComments) {
3265
6.79k
    for (const auto &OC : FO.second) {
3266
6.79k
      const RawComment *I = OC.second;
3267
6.79k
      Record.clear();
3268
6.79k
      AddSourceRange(I->getSourceRange(), Record);
3269
6.79k
      Record.push_back(I->getKind());
3270
6.79k
      Record.push_back(I->isTrailingComment());
3271
6.79k
      Record.push_back(I->isAlmostTrailingComment());
3272
6.79k
      Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3273
6.79k
    }
3274
735
  }
3275
6.92k
}
3276
3277
//===----------------------------------------------------------------------===//
3278
// Global Method Pool and Selector Serialization
3279
//===----------------------------------------------------------------------===//
3280
3281
namespace {
3282
3283
// Trait used for the on-disk hash table used in the method pool.
3284
class ASTMethodPoolTrait {
3285
  ASTWriter &Writer;
3286
3287
public:
3288
  using key_type = Selector;
3289
  using key_type_ref = key_type;
3290
3291
  struct data_type {
3292
    SelectorID ID;
3293
    ObjCMethodList Instance, Factory;
3294
  };
3295
  using data_type_ref = const data_type &;
3296
3297
  using hash_value_type = unsigned;
3298
  using offset_type = unsigned;
3299
3300
528
  explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3301
3302
37.2k
  static hash_value_type ComputeHash(Selector Sel) {
3303
37.2k
    return serialization::ComputeHash(Sel);
3304
37.2k
  }
3305
3306
  std::pair<unsigned, unsigned>
3307
    EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3308
37.2k
                      data_type_ref Methods) {
3309
37.2k
    unsigned KeyLen = 2 + (Sel.getNumArgs()? 
Sel.getNumArgs() * 424.0k
:
413.2k
);
3310
37.2k
    unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3311
85.3k
    for (const ObjCMethodList *Method = &Methods.Instance; Method;
3312
48.0k
         Method = Method->getNext())
3313
48.0k
      if (ShouldWriteMethodListNode(Method))
3314
37.3k
        DataLen += 4;
3315
74.9k
    for (const ObjCMethodList *Method = &Methods.Factory; Method;
3316
37.6k
         Method = Method->getNext())
3317
37.6k
      if (ShouldWriteMethodListNode(Method))
3318
5.13k
        DataLen += 4;
3319
37.2k
    return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3320
37.2k
  }
3321
3322
37.2k
  void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3323
37.2k
    using namespace llvm::support;
3324
3325
37.2k
    endian::Writer LE(Out, little);
3326
37.2k
    uint64_t Start = Out.tell();
3327
37.2k
    assert((Start >> 32) == 0 && "Selector key offset too large");
3328
37.2k
    Writer.SetSelectorOffset(Sel, Start);
3329
37.2k
    unsigned N = Sel.getNumArgs();
3330
37.2k
    LE.write<uint16_t>(N);
3331
37.2k
    if (N == 0)
3332
13.2k
      N = 1;
3333
86.9k
    for (unsigned I = 0; I != N; 
++I49.6k
)
3334
49.6k
      LE.write<uint32_t>(
3335
49.6k
          Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
3336
37.2k
  }
3337
3338
  void EmitData(raw_ostream& Out, key_type_ref,
3339
37.2k
                data_type_ref Methods, unsigned DataLen) {
3340
37.2k
    using namespace llvm::support;
3341
3342
37.2k
    endian::Writer LE(Out, little);
3343
37.2k
    uint64_t Start = Out.tell(); (void)Start;
3344
37.2k
    LE.write<uint32_t>(Methods.ID);
3345
37.2k
    unsigned NumInstanceMethods = 0;
3346
85.3k
    for (const ObjCMethodList *Method = &Methods.Instance; Method;
3347
48.0k
         Method = Method->getNext())
3348
48.0k
      if (ShouldWriteMethodListNode(Method))
3349
37.3k
        ++NumInstanceMethods;
3350
3351
37.2k
    unsigned NumFactoryMethods = 0;
3352
74.9k
    for (const ObjCMethodList *Method = &Methods.Factory; Method;
3353
37.6k
         Method = Method->getNext())
3354
37.6k
      if (ShouldWriteMethodListNode(Method))
3355
5.13k
        ++NumFactoryMethods;
3356
3357
37.2k
    unsigned InstanceBits = Methods.Instance.getBits();
3358
37.2k
    assert(InstanceBits < 4);
3359
37.2k
    unsigned InstanceHasMoreThanOneDeclBit =
3360
37.2k
        Methods.Instance.hasMoreThanOneDecl();
3361
37.2k
    unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3362
37.2k
                                (InstanceHasMoreThanOneDeclBit << 2) |
3363
37.2k
                                InstanceBits;
3364
37.2k
    unsigned FactoryBits = Methods.Factory.getBits();
3365
37.2k
    assert(FactoryBits < 4);
3366
37.2k
    unsigned FactoryHasMoreThanOneDeclBit =
3367
37.2k
        Methods.Factory.hasMoreThanOneDecl();
3368
37.2k
    unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3369
37.2k
                               (FactoryHasMoreThanOneDeclBit << 2) |
3370
37.2k
                               FactoryBits;
3371
37.2k
    LE.write<uint16_t>(FullInstanceBits);
3372
37.2k
    LE.write<uint16_t>(FullFactoryBits);
3373
85.3k
    for (const ObjCMethodList *Method = &Methods.Instance; Method;
3374
48.0k
         Method = Method->getNext())
3375
48.0k
      if (ShouldWriteMethodListNode(Method))
3376
37.3k
        LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3377
74.9k
    for (const ObjCMethodList *Method = &Methods.Factory; Method;
3378
37.6k
         Method = Method->getNext())
3379
37.6k
      if (ShouldWriteMethodListNode(Method))
3380
5.13k
        LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3381
3382
37.2k
    assert(Out.tell() - Start == DataLen && "Data length is wrong");
3383
37.2k
  }
3384
3385
private:
3386
257k
  static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3387
257k
    return (Node->getMethod() && 
!Node->getMethod()->isFromASTFile()131k
);
3388
257k
  }
3389
};
3390
3391
} // namespace
3392
3393
/// Write ObjC data: selectors and the method pool.
3394
///
3395
/// The method pool contains both instance and factory methods, stored
3396
/// in an on-disk hash table indexed by the selector. The hash table also
3397
/// contains an empty entry for every other selector known to Sema.
3398
7.24k
void ASTWriter::WriteSelectors(Sema &SemaRef) {
3399
7.24k
  using namespace llvm;
3400
3401
  // Do we have to do anything at all?
3402
7.24k
  if (SemaRef.MethodPool.empty() && 
SelectorIDs.empty()6.98k
)
3403
6.97k
    return;
3404
264
  unsigned NumTableEntries = 0;
3405
  // Create and write out the blob that contains selectors and the method pool.
3406
264
  {
3407
264
    llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3408
264
    ASTMethodPoolTrait Trait(*this);
3409
3410
    // Create the on-disk hash table representation. We walk through every
3411
    // selector we've seen and look it up in the method pool.
3412
264
    SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3413
38.1k
    for (auto &SelectorAndID : SelectorIDs) {
3414
38.1k
      Selector S = SelectorAndID.first;
3415
38.1k
      SelectorID ID = SelectorAndID.second;
3416
38.1k
      Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3417
38.1k
      ASTMethodPoolTrait::data_type Data = {
3418
38.1k
        ID,
3419
38.1k
        ObjCMethodList(),
3420
38.1k
        ObjCMethodList()
3421
38.1k
      };
3422
38.1k
      if (F != SemaRef.MethodPool.end()) {
3423
32.3k
        Data.Instance = F->second.first;
3424
32.3k
        Data.Factory = F->second.second;
3425
32.3k
      }
3426
      // Only write this selector if it's not in an existing AST or something
3427
      // changed.
3428
38.1k
      if (Chain && 
ID < FirstSelectorID37.8k
) {
3429
        // Selector already exists. Did it change?
3430
2.01k
        bool changed = false;
3431
5.70k
        for (ObjCMethodList *M = &Data.Instance; M && 
M->getMethod()5.59k
;
3432
4.77k
             
M = M->getNext()3.68k
) {
3433
4.77k
          if (!M->getMethod()->isFromASTFile()) {
3434
1.08k
            changed = true;
3435
1.08k
            Data.Instance = *M;
3436
1.08k
            break;
3437
1.08k
          }
3438
4.77k
        }
3439
2.18k
        for (ObjCMethodList *M = &Data.Factory; M && 
M->getMethod()2.13k
;
3440
2.01k
             
M = M->getNext()170
) {
3441
244
          if (!M->getMethod()->isFromASTFile()) {
3442
74
            changed = true;
3443
74
            Data.Factory = *M;
3444
74
            break;
3445
74
          }
3446
244
        }
3447
2.01k
        if (!changed)
3448
862
          continue;
3449
36.1k
      } else if (Data.Instance.getMethod() || 
Data.Factory.getMethod()9.54k
) {
3450
        // A new method pool entry.
3451
31.1k
        ++NumTableEntries;
3452
31.1k
      }
3453
37.2k
      Generator.insert(S, Data, Trait);
3454
37.2k
    }
3455
3456
    // Create the on-disk hash table in a buffer.
3457
264
    SmallString<4096> MethodPool;
3458
264
    uint32_t BucketOffset;
3459
264
    {
3460
264
      using namespace llvm::support;
3461
3462
264
      ASTMethodPoolTrait Trait(*this);
3463
264
      llvm::raw_svector_ostream Out(MethodPool);
3464
      // Make sure that no bucket is at offset 0
3465
264
      endian::write<uint32_t>(Out, 0, little);
3466
264
      BucketOffset = Generator.Emit(Out, Trait);
3467
264
    }
3468
3469
    // Create a blob abbreviation
3470
264
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
3471
264
    Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3472
264
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3473
264
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3474
264
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3475
264
    unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3476
3477
    // Write the method pool
3478
264
    {
3479
264
      RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3480
264
                                         NumTableEntries};
3481
264
      Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3482
264
    }
3483
3484
    // Create a blob abbreviation for the selector table offsets.
3485
264
    Abbrev = std::make_shared<BitCodeAbbrev>();
3486
264
    Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3487
264
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3488
264
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3489
264
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3490
264
    unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3491
3492
    // Write the selector offsets table.
3493
264
    {
3494
264
      RecordData::value_type Record[] = {
3495
264
          SELECTOR_OFFSETS, SelectorOffsets.size(),
3496
264
          FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3497
264
      Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3498
264
                                bytes(SelectorOffsets));
3499
264
    }
3500
264
  }
3501
264
}
3502
3503
/// Write the selectors referenced in @selector expression into AST file.
3504
7.24k
void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3505
7.24k
  using namespace llvm;
3506
3507
7.24k
  if (SemaRef.ReferencedSelectors.empty())
3508
7.24k
    return;
3509
3510
2
  RecordData Record;
3511
2
  ASTRecordWriter Writer(*this, Record);
3512
3513
  // Note: this writes out all references even for a dependent AST. But it is
3514
  // very tricky to fix, and given that @selector shouldn't really appear in
3515
  // headers, probably not worth it. It's not a correctness issue.
3516
2
  for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3517
2
    Selector Sel = SelectorAndLocation.first;
3518
2
    SourceLocation Loc = SelectorAndLocation.second;
3519
2
    Writer.AddSelectorRef(Sel);
3520
2
    Writer.AddSourceLocation(Loc);
3521
2
  }
3522
2
  Writer.Emit(REFERENCED_SELECTOR_POOL);
3523
2
}
3524
3525
//===----------------------------------------------------------------------===//
3526
// Identifier Table Serialization
3527
//===----------------------------------------------------------------------===//
3528
3529
/// Determine the declaration that should be put into the name lookup table to
3530
/// represent the given declaration in this module. This is usually D itself,
3531
/// but if D was imported and merged into a local declaration, we want the most
3532
/// recent local declaration instead. The chosen declaration will be the most
3533
/// recent declaration in any module that imports this one.
3534
static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3535
1.06M
                                        NamedDecl *D) {
3536
1.06M
  if (!LangOpts.Modules || 
!D->isFromASTFile()937k
)
3537
828k
    return D;
3538
3539
231k
  if (Decl *Redecl = D->getPreviousDecl()) {
3540
    // For Redeclarable decls, a prior declaration might be local.
3541
54.8M
    for (; Redecl; 
Redecl = Redecl->getPreviousDecl()54.6M
) {
3542
      // If we find a local decl, we're done.
3543
54.6M
      if (!Redecl->isFromASTFile()) {
3544
        // Exception: in very rare cases (for injected-class-names), not all
3545
        // redeclarations are in the same semantic context. Skip ones in a
3546
        // different context. They don't go in this lookup table at all.
3547
46.2k
        if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3548
46.2k
                D->getDeclContext()->getRedeclContext()))
3549
0
          continue;
3550
46.2k
        return cast<NamedDecl>(Redecl);
3551
46.2k
      }
3552
3553
      // If we find a decl from a (chained-)PCH stop since we won't find a
3554
      // local one.
3555
54.6M
      if (Redecl->getOwningModuleID() == 0)
3556
3
        break;
3557
54.6M
    }
3558
187k
  } else 
if (Decl *44.4k
First44.4k
= D->getCanonicalDecl()) {
3559
    // For Mergeable decls, the first decl might be local.
3560
44.4k
    if (!First->isFromASTFile())
3561
103
      return cast<NamedDecl>(First);
3562
44.4k
  }
3563
3564
  // All declarations are imported. Our most recent declaration will also be
3565
  // the most recent one in anyone who imports us.
3566
185k
  return D;
3567
231k
}
3568
3569
namespace {
3570
3571
class ASTIdentifierTableTrait {
3572
  ASTWriter &Writer;
3573
  Preprocessor &PP;
3574
  IdentifierResolver &IdResolver;
3575
  bool IsModule;
3576
  bool NeedDecls;
3577
  ASTWriter::RecordData *InterestingIdentifierOffsets;
3578
3579
  /// Determines whether this is an "interesting" identifier that needs a
3580
  /// full IdentifierInfo structure written into the hash table. Notably, this
3581
  /// doesn't check whether the name has macros defined; use PublicMacroIterator
3582
  /// to check that.
3583
61.0M
  bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3584
61.0M
    if (MacroOffset || 
II->isPoisoned()57.6M
||
3585
61.0M
        
(57.5M
!IsModule57.5M
&&
II->getObjCOrBuiltinID()33.7M
) ||
3586
61.0M
        
II->hasRevertedTokenIDToIdentifier()26.7M
||
3587
61.0M
        
(26.7M
NeedDecls26.7M
&&
II->getFETokenInfo()7.77M
))
3588
35.0M
      return true;
3589
3590
26.0M
    return false;
3591
61.0M
  }
3592
3593
public:
3594
  using key_type = IdentifierInfo *;
3595
  using key_type_ref = key_type;
3596
3597
  using data_type = IdentID;
3598
  using data_type_ref = data_type;
3599
3600
  using hash_value_type = unsigned;
3601
  using offset_type = unsigned;
3602
3603
  ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3604
                          IdentifierResolver &IdResolver, bool IsModule,
3605
                          ASTWriter::RecordData *InterestingIdentifierOffsets)
3606
7.24k
      : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3607
7.24k
        NeedDecls(!IsModule || 
!Writer.getLangOpts().CPlusPlus3.51k
),
3608
7.24k
        InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3609
3610
8.44M
  bool needDecls() const { return NeedDecls; }
3611
3612
12.4M
  static hash_value_type ComputeHash(const IdentifierInfo* II) {
3613
12.4M
    return llvm::djbHash(II->getName());
3614
12.4M
  }
3615
3616
0
  bool isInterestingIdentifier(const IdentifierInfo *II) {
3617
0
    auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3618
0
    return isInterestingIdentifier(II, MacroOffset);
3619
0
  }
3620
3621
35.4M
  bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3622
35.4M
    return isInterestingIdentifier(II, 0);
3623
35.4M
  }
3624
3625
  std::pair<unsigned, unsigned>
3626
12.4M
  EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3627
    // Record the location of the identifier data. This is used when generating
3628
    // the mapping from persistent IDs to strings.
3629
12.4M
    Writer.SetIdentifierOffset(II, Out.tell());
3630
3631
12.4M
    auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3632
3633
    // Emit the offset of the key/data length information to the interesting
3634
    // identifiers table if necessary.
3635
12.4M
    if (InterestingIdentifierOffsets &&
3636
12.4M
        
isInterestingIdentifier(II, MacroOffset)684k
)
3637
377k
      InterestingIdentifierOffsets->push_back(Out.tell());
3638
3639
12.4M
    unsigned KeyLen = II->getLength() + 1;
3640
12.4M
    unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3641
12.4M
    if (isInterestingIdentifier(II, MacroOffset)) {
3642
12.0M
      DataLen += 2; // 2 bytes for builtin ID
3643
12.0M
      DataLen += 2; // 2 bytes for flags
3644
12.0M
      if (MacroOffset)
3645
1.61M
        DataLen += 4; // MacroDirectives offset.
3646
3647
12.0M
      if (NeedDecls)
3648
11.9M
        DataLen += std::distance(IdResolver.begin(II), IdResolver.end()) * 4;
3649
12.0M
    }
3650
12.4M
    return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3651
12.4M
  }
3652
3653
  void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3654
12.4M
               unsigned KeyLen) {
3655
12.4M
    Out.write(II->getNameStart(), KeyLen);
3656
12.4M
  }
3657
3658
  void EmitData(raw_ostream& Out, IdentifierInfo* II,
3659
12.4M
                IdentID ID, unsigned) {
3660
12.4M
    using namespace llvm::support;
3661
3662
12.4M
    endian::Writer LE(Out, little);
3663
3664
12.4M
    auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3665
12.4M
    if (!isInterestingIdentifier(II, MacroOffset)) {
3666
416k
      LE.write<uint32_t>(ID << 1);
3667
416k
      return;
3668
416k
    }
3669
3670
12.0M
    LE.write<uint32_t>((ID << 1) | 0x01);
3671
12.0M
    uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3672
12.0M
    assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3673
12.0M
    LE.write<uint16_t>(Bits);
3674
12.0M
    Bits = 0;
3675
12.0M
    bool HadMacroDefinition = MacroOffset != 0;
3676
12.0M
    Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3677
12.0M
    Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3678
12.0M
    Bits = (Bits << 1) | unsigned(II->isPoisoned());
3679
12.0M
    Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3680
12.0M
    Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3681
12.0M
    LE.write<uint16_t>(Bits);
3682
3683
12.0M
    if (HadMacroDefinition)
3684
1.61M
      LE.write<uint32_t>(MacroOffset);
3685
3686
12.0M
    if (NeedDecls) {
3687
      // Emit the declaration IDs in reverse order, because the
3688
      // IdentifierResolver provides the declarations as they would be
3689
      // visible (e.g., the function "stat" would come before the struct
3690
      // "stat"), but the ASTReader adds declarations to the end of the list
3691
      // (so we need to see the struct "stat" before the function "stat").
3692
      // Only emit declarations that aren't from a chained PCH, though.
3693
11.9M
      SmallVector<NamedDecl *, 16> Decls(IdResolver.decls(II));
3694
11.9M
      for (NamedDecl *D : llvm::reverse(Decls))
3695
238k
        LE.write<uint32_t>(
3696
238k
            Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), D)));
3697
11.9M
    }
3698
12.0M
  }
3699
};
3700
3701
} // namespace
3702
3703
/// Write the identifier table into the AST file.
3704
///
3705
/// The identifier table consists of a blob containing string data
3706
/// (the actual identifiers themselves) and a separate "offsets" index
3707
/// that maps identifier IDs to locations within the blob.
3708
void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3709
                                     IdentifierResolver &IdResolver,
3710
7.24k
                                     bool IsModule) {
3711
7.24k
  using namespace llvm;
3712
3713
7.24k
  RecordData InterestingIdents;
3714
3715
  // Create and write out the blob that contains the identifier
3716
  // strings.
3717
7.24k
  {
3718
7.24k
    llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3719
7.24k
    ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule,
3720
7.24k
                                  IsModule ? 
&InterestingIdents3.51k
:
nullptr3.72k
);
3721
3722
    // Look for any identifiers that were named while processing the
3723
    // headers, but are otherwise not needed. We add these to the hash
3724
    // table to enable checking of the predefines buffer in the case
3725
    // where the user adds new macro definitions when building the AST
3726
    // file.
3727
7.24k
    SmallVector<const IdentifierInfo *, 128> IIs;
3728
7.24k
    for (const auto &ID : PP.getIdentifierTable())
3729
35.4M
      if (Trait.isInterestingNonMacroIdentifier(ID.second))
3730
10.4M
        IIs.push_back(ID.second);
3731
    // Sort the identifiers lexicographically before getting the references so
3732
    // that their order is stable.
3733
7.24k
    llvm::sort(IIs, llvm::deref<std::less<>>());
3734
7.24k
    for (const IdentifierInfo *II : IIs)
3735
10.4M
      getIdentifierRef(II);
3736
3737
    // Create the on-disk hash table representation. We only store offsets
3738
    // for identifiers that appear here for the first time.
3739
7.24k
    IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3740
20.9M
    for (auto IdentIDPair : IdentifierIDs) {
3741
20.9M
      auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3742
20.9M
      IdentID ID = IdentIDPair.second;
3743
20.9M
      assert(II && "NULL identifier in identifier table");
3744
      // Write out identifiers if either the ID is local or the identifier has
3745
      // changed since it was loaded.
3746
20.9M
      if (ID >= FirstIdentID || 
!Chain8.53M
||
!II->isFromAST()8.53M
3747
20.9M
          || 
II->hasChangedSinceDeserialization()8.53M
||
3748
20.9M
          
(8.44M
Trait.needDecls()8.44M
&&
3749
8.44M
           
II->hasFETokenInfoChangedSinceDeserialization()65.4k
))
3750
12.4M
        Generator.insert(II, ID, Trait);
3751
20.9M
    }
3752
3753
    // Create the on-disk hash table in a buffer.
3754
7.24k
    SmallString<4096> IdentifierTable;
3755
7.24k
    uint32_t BucketOffset;
3756
7.24k
    {
3757
7.24k
      using namespace llvm::support;
3758
3759
7.24k
      llvm::raw_svector_ostream Out(IdentifierTable);
3760
      // Make sure that no bucket is at offset 0
3761
7.24k
      endian::write<uint32_t>(Out, 0, little);
3762
7.24k
      BucketOffset = Generator.Emit(Out, Trait);
3763
7.24k
    }
3764
3765
    // Create a blob abbreviation
3766
7.24k
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
3767
7.24k
    Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3768
7.24k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3769
7.24k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3770
7.24k
    unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3771
3772
    // Write the identifier table
3773
7.24k
    RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3774
7.24k
    Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3775
7.24k
  }
3776
3777
  // Write the offsets table for identifier IDs.
3778
0
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3779
7.24k
  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3780
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3781
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3782
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3783
7.24k
  unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3784
3785
7.24k
#ifndef NDEBUG
3786
12.3M
  for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; 
++I12.3M
)
3787
12.3M
    assert(IdentifierOffsets[I] && "Missing identifier offset?");
3788
7.24k
#endif
3789
3790
7.24k
  RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3791
7.24k
                                     IdentifierOffsets.size(),
3792
7.24k
                                     FirstIdentID - NUM_PREDEF_IDENT_IDS};
3793
7.24k
  Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3794
7.24k
                            bytes(IdentifierOffsets));
3795
3796
  // In C++, write the list of interesting identifiers (those that are
3797
  // defined as macros, poisoned, or similar unusual things).
3798
7.24k
  if (!InterestingIdents.empty())
3799
3.51k
    Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3800
7.24k
}
3801
3802
//===----------------------------------------------------------------------===//
3803
// DeclContext's Name Lookup Table Serialization
3804
//===----------------------------------------------------------------------===//
3805
3806
namespace {
3807
3808
// Trait used for the on-disk hash table used in the method pool.
3809
class ASTDeclContextNameLookupTrait {
3810
  ASTWriter &Writer;
3811
  llvm::SmallVector<DeclID, 64> DeclIDs;
3812
3813
public:
3814
  using key_type = DeclarationNameKey;
3815
  using key_type_ref = key_type;
3816
3817
  /// A start and end index into DeclIDs, representing a sequence of decls.
3818
  using data_type = std::pair<unsigned, unsigned>;
3819
  using data_type_ref = const data_type &;
3820
3821
  using hash_value_type = unsigned;
3822
  using offset_type = unsigned;
3823
3824
61.0k
  explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3825
3826
  template<typename Coll>
3827
578k
  data_type getData(const Coll &Decls) {
3828
578k
    unsigned Start = DeclIDs.size();
3829
819k
    for (NamedDecl *D : Decls) {
3830
819k
      DeclIDs.push_back(
3831
819k
          Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3832
819k
    }
3833
578k
    return std::make_pair(Start, DeclIDs.size());
3834
578k
  }
ASTWriter.cpp:std::__1::pair<unsigned int, unsigned int> (anonymous namespace)::ASTDeclContextNameLookupTrait::getData<clang::DeclContextLookupResult>(clang::DeclContextLookupResult const&)
Line
Count
Source
3827
569k
  data_type getData(const Coll &Decls) {
3828
569k
    unsigned Start = DeclIDs.size();
3829
795k
    for (NamedDecl *D : Decls) {
3830
795k
      DeclIDs.push_back(
3831
795k
          Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3832
795k
    }
3833
569k
    return std::make_pair(Start, DeclIDs.size());
3834
569k
  }
ASTWriter.cpp:std::__1::pair<unsigned int, unsigned int> (anonymous namespace)::ASTDeclContextNameLookupTrait::getData<llvm::SmallVector<clang::NamedDecl*, 8u> >(llvm::SmallVector<clang::NamedDecl*, 8u> const&)
Line
Count
Source
3827
8.97k
  data_type getData(const Coll &Decls) {
3828
8.97k
    unsigned Start = DeclIDs.size();
3829
23.7k
    for (NamedDecl *D : Decls) {
3830
23.7k
      DeclIDs.push_back(
3831
23.7k
          Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3832
23.7k
    }
3833
8.97k
    return std::make_pair(Start, DeclIDs.size());
3834
8.97k
  }
3835
3836
2.19M
  data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3837
2.19M
    unsigned Start = DeclIDs.size();
3838
2.19M
    llvm::append_range(DeclIDs, FromReader);
3839
2.19M
    return std::make_pair(Start, DeclIDs.size());
3840
2.19M
  }
3841
3842
16.1k
  static bool EqualKey(key_type_ref a, key_type_ref b) {
3843
16.1k
    return a == b;
3844
16.1k
  }
3845
3846
4.98M
  hash_value_type ComputeHash(DeclarationNameKey Name) {
3847
4.98M
    return Name.getHash();
3848
4.98M
  }
3849
3850
11.2k
  void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3851
11.2k
    assert(Writer.hasChain() &&
3852
11.2k
           "have reference to loaded module file but no chain?");
3853
3854
11.2k
    using namespace llvm::support;
3855
3856
11.2k
    endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
3857
11.2k
  }
3858
3859
  std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3860
                                                  DeclarationNameKey Name,
3861
2.77M
                                                  data_type_ref Lookup) {
3862
2.77M
    unsigned KeyLen = 1;
3863
2.77M
    switch (Name.getKind()) {
3864
2.70M
    case DeclarationName::Identifier:
3865
2.72M
    case DeclarationName::ObjCZeroArgSelector:
3866
2.74M
    case DeclarationName::ObjCOneArgSelector:
3867
2.75M
    case DeclarationName::ObjCMultiArgSelector:
3868
2.75M
    case DeclarationName::CXXLiteralOperatorName:
3869
2.75M
    case DeclarationName::CXXDeductionGuideName:
3870
2.75M
      KeyLen += 4;
3871
2.75M
      break;
3872
9.59k
    case DeclarationName::CXXOperatorName:
3873
9.59k
      KeyLen += 1;
3874
9.59k
      break;
3875
7.73k
    case DeclarationName::CXXConstructorName:
3876
13.3k
    case DeclarationName::CXXDestructorName:
3877
14.6k
    case DeclarationName::CXXConversionFunctionName:
3878
14.8k
    case DeclarationName::CXXUsingDirective:
3879
14.8k
      break;
3880
2.77M
    }
3881
3882
    // 4 bytes for each DeclID.
3883
2.77M
    unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3884
3885
2.77M
    return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3886
2.77M
  }
3887
3888
2.77M
  void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3889
2.77M
    using namespace llvm::support;
3890
3891
2.77M
    endian::Writer LE(Out, little);
3892
2.77M
    LE.write<uint8_t>(Name.getKind());
3893
2.77M
    switch (Name.getKind()) {
3894
2.70M
    case DeclarationName::Identifier:
3895
2.70M
    case DeclarationName::CXXLiteralOperatorName:
3896
2.70M
    case DeclarationName::CXXDeductionGuideName:
3897
2.70M
      LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3898
2.70M
      return;
3899
18.5k
    case DeclarationName::ObjCZeroArgSelector:
3900
34.7k
    case DeclarationName::ObjCOneArgSelector:
3901
42.4k
    case DeclarationName::ObjCMultiArgSelector:
3902
42.4k
      LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3903
42.4k
      return;
3904
9.59k
    case DeclarationName::CXXOperatorName:
3905
9.59k
      assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3906
9.59k
             "Invalid operator?");
3907
9.59k
      LE.write<uint8_t>(Name.getOperatorKind());
3908
9.59k
      return;
3909
7.73k
    case DeclarationName::CXXConstructorName:
3910
13.3k
    case DeclarationName::CXXDestructorName:
3911
14.6k
    case DeclarationName::CXXConversionFunctionName:
3912
14.8k
    case DeclarationName::CXXUsingDirective:
3913
14.8k
      return;
3914
2.77M
    }
3915
3916
0
    llvm_unreachable("Invalid name kind?");
3917
0
  }
3918
3919
  void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3920
2.77M
                unsigned DataLen) {
3921
2.77M
    using namespace llvm::support;
3922
3923
2.77M
    endian::Writer LE(Out, little);
3924
2.77M
    uint64_t Start = Out.tell(); (void)Start;
3925
8.95M
    for (unsigned I = Lookup.first, N = Lookup.second; I != N; 
++I6.17M
)
3926
6.17M
      LE.write<uint32_t>(DeclIDs[I]);
3927
2.77M
    assert(Out.tell() - Start == DataLen && "Data length is wrong");
3928
2.77M
  }
3929
};
3930
3931
} // namespace
3932
3933
bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3934
628k
                                       DeclContext *DC) {
3935
628k
  return Result.hasExternalDecls() &&
3936
628k
         
DC->hasNeedToReconcileExternalVisibleStorage()96.9k
;
3937
628k
}
3938
3939
bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3940
2.23k
                                               DeclContext *DC) {
3941
2.23k
  for (auto *D : Result.getLookupResult())
3942
2.19k
    if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3943
1.06k
      return false;
3944
3945
1.16k
  return true;
3946
2.23k
}
3947
3948
void
3949
ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3950
61.0k
                                   llvm::SmallVectorImpl<char> &LookupTable) {
3951
61.0k
  assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3952
61.0k
         !ConstDC->hasLazyExternalLexicalLookups() &&
3953
61.0k
         "must call buildLookups first");
3954
3955
  // FIXME: We need to build the lookups table, which is logically const.
3956
61.0k
  auto *DC = const_cast<DeclContext*>(ConstDC);
3957
61.0k
  assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3958
3959
  // Create the on-disk hash table representation.
3960
61.0k
  MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3961
61.0k
                                ASTDeclContextNameLookupTrait> Generator;
3962
61.0k
  ASTDeclContextNameLookupTrait Trait(*this);
3963
3964
  // The first step is to collect the declaration names which we need to
3965
  // serialize into the name lookup table, and to collect them in a stable
3966
  // order.
3967
61.0k
  SmallVector<DeclarationName, 16> Names;
3968
3969
  // We also build up small sets of the constructor and conversion function
3970
  // names which are visible.
3971
61.0k
  llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3972
3973
628k
  for (auto &Lookup : *DC->buildLookup()) {
3974
628k
    auto &Name = Lookup.first;
3975
628k
    auto &Result = Lookup.second;
3976
3977
    // If there are no local declarations in our lookup result, we
3978
    // don't need to write an entry for the name at all. If we can't
3979
    // write out a lookup set without performing more deserialization,
3980
    // just skip this entry.
3981
628k
    if (isLookupResultExternal(Result, DC) &&
3982
628k
        
isLookupResultEntirelyExternal(Result, DC)2.23k
)
3983
1.16k
      continue;
3984
3985
    // We also skip empty results. If any of the results could be external and
3986
    // the currently available results are empty, then all of the results are
3987
    // external and we skip it above. So the only way we get here with an empty
3988
    // results is when no results could have been external *and* we have
3989
    // external results.
3990
    //
3991
    // FIXME: While we might want to start emitting on-disk entries for negative
3992
    // lookups into a decl context as an optimization, today we *have* to skip
3993
    // them because there are names with empty lookup results in decl contexts
3994
    // which we can't emit in any stable ordering: we lookup constructors and
3995
    // conversion functions in the enclosing namespace scope creating empty
3996
    // results for them. This in almost certainly a bug in Clang's name lookup,
3997
    // but that is likely to be hard or impossible to fix and so we tolerate it
3998
    // here by omitting lookups with empty results.
3999
627k
    if (Lookup.second.getLookupResult().empty())
4000
48.1k
      continue;
4001
4002
578k
    switch (Lookup.first.getNameKind()) {
4003
569k
    default:
4004
569k
      Names.push_back(Lookup.first);
4005
569k
      break;
4006
4007
7.75k
    case DeclarationName::CXXConstructorName:
4008
7.75k
      assert(isa<CXXRecordDecl>(DC) &&
4009
7.75k
             "Cannot have a constructor name outside of a class!");
4010
7.75k
      ConstructorNameSet.insert(Name);
4011
7.75k
      break;
4012
4013
1.28k
    case DeclarationName::CXXConversionFunctionName:
4014
1.28k
      assert(isa<CXXRecordDecl>(DC) &&
4015
1.28k
             "Cannot have a conversion function name outside of a class!");
4016
1.28k
      ConversionNameSet.insert(Name);
4017
1.28k
      break;
4018
578k
    }
4019
578k
  }
4020
4021
  // Sort the names into a stable order.
4022
61.0k
  llvm::sort(Names);
4023
4024
61.0k
  if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
4025
    // We need to establish an ordering of constructor and conversion function
4026
    // names, and they don't have an intrinsic ordering.
4027
4028
    // First we try the easy case by forming the current context's constructor
4029
    // name and adding that name first. This is a very useful optimization to
4030
    // avoid walking the lexical declarations in many cases, and it also
4031
    // handles the only case where a constructor name can come from some other
4032
    // lexical context -- when that name is an implicit constructor merged from
4033
    // another declaration in the redecl chain. Any non-implicit constructor or
4034
    // conversion function which doesn't occur in all the lexical contexts
4035
    // would be an ODR violation.
4036
32.7k
    auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4037
32.7k
        Context->getCanonicalType(Context->getRecordType(D)));
4038
32.7k
    if (ConstructorNameSet.erase(ImplicitCtorName))
4039
7.72k
      Names.push_back(ImplicitCtorName);
4040
4041
    // If we still have constructors or conversion functions, we walk all the
4042
    // names in the decl and add the constructors and conversion functions
4043
    // which are visible in the order they lexically occur within the context.
4044
32.7k
    if (!ConstructorNameSet.empty() || 
!ConversionNameSet.empty()32.7k
)
4045
1.27k
      for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4046
10.1k
        if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4047
9.58k
          auto Name = ChildND->getDeclName();
4048
9.58k
          switch (Name.getNameKind()) {
4049
5.12k
          default:
4050
5.12k
            continue;
4051
4052
5.12k
          case DeclarationName::CXXConstructorName:
4053
3.18k
            if (ConstructorNameSet.erase(Name))
4054
35
              Names.push_back(Name);
4055
3.18k
            break;
4056
4057
1.28k
          case DeclarationName::CXXConversionFunctionName:
4058
1.28k
            if (ConversionNameSet.erase(Name))
4059
1.28k
              Names.push_back(Name);
4060
1.28k
            break;
4061
9.58k
          }
4062
4063
4.46k
          if (ConstructorNameSet.empty() && 
ConversionNameSet.empty()4.44k
)
4064
1.27k
            break;
4065
4.46k
        }
4066
4067
32.7k
    assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4068
32.7k
                                         "constructors by walking all the "
4069
32.7k
                                         "lexical members of the context.");
4070
32.7k
    assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4071
32.7k
                                        "conversion functions by walking all "
4072
32.7k
                                        "the lexical members of the context.");
4073
32.7k
  }
4074
4075
  // Next we need to do a lookup with each name into this decl context to fully
4076
  // populate any results from external sources. We don't actually use the
4077
  // results of these lookups because we only want to use the results after all
4078
  // results have been loaded and the pointers into them will be stable.
4079
61.0k
  for (auto &Name : Names)
4080
578k
    DC->lookup(Name);
4081
4082
  // Now we need to insert the results for each name into the hash table. For
4083
  // constructor names and conversion function names, we actually need to merge
4084
  // all of the results for them into one list of results each and insert
4085
  // those.
4086
61.0k
  SmallVector<NamedDecl *, 8> ConstructorDecls;
4087
61.0k
  SmallVector<NamedDecl *, 8> ConversionDecls;
4088
4089
  // Now loop over the names, either inserting them or appending for the two
4090
  // special cases.
4091
578k
  for (auto &Name : Names) {
4092
578k
    DeclContext::lookup_result Result = DC->noload_lookup(Name);
4093
4094
578k
    switch (Name.getNameKind()) {
4095
569k
    default:
4096
569k
      Generator.insert(Name, Trait.getData(Result), Trait);
4097
569k
      break;
4098
4099
7.75k
    case DeclarationName::CXXConstructorName:
4100
7.75k
      ConstructorDecls.append(Result.begin(), Result.end());
4101
7.75k
      break;
4102
4103
1.28k
    case DeclarationName::CXXConversionFunctionName:
4104
1.28k
      ConversionDecls.append(Result.begin(), Result.end());
4105
1.28k
      break;
4106
578k
    }
4107
578k
  }
4108
4109
  // Handle our two special cases if we ended up having any. We arbitrarily use
4110
  // the first declaration's name here because the name itself isn't part of
4111
  // the key, only the kind of name is used.
4112
61.0k
  if (!ConstructorDecls.empty())
4113
7.73k
    Generator.insert(ConstructorDecls.front()->getDeclName(),
4114
7.73k
                     Trait.getData(ConstructorDecls), Trait);
4115
61.0k
  if (!ConversionDecls.empty())
4116
1.24k
    Generator.insert(ConversionDecls.front()->getDeclName(),
4117
1.24k
                     Trait.getData(ConversionDecls), Trait);
4118
4119
  // Create the on-disk hash table. Also emit the existing imported and
4120
  // merged table if there is one.
4121
61.0k
  auto *Lookups = Chain ? 
Chain->getLoadedLookupTables(DC)50.0k
:
nullptr10.9k
;
4122
61.0k
  Generator.emit(LookupTable, Trait, Lookups ? 
&Lookups->Table3.50k
:
nullptr57.5k
);
4123
61.0k
}
4124
4125
/// Write the block containing all of the declaration IDs
4126
/// visible from the given DeclContext.
4127
///
4128
/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4129
/// bitstream, or 0 if no block was written.
4130
uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4131
495k
                                                 DeclContext *DC) {
4132
  // If we imported a key declaration of this namespace, write the visible
4133
  // lookup results as an update record for it rather than including them
4134
  // on this declaration. We will only look at key declarations on reload.
4135
495k
  if (isa<NamespaceDecl>(DC) && 
Chain4.83k
&&
4136
495k
      
Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()4.59k
) {
4137
    // Only do this once, for the first local declaration of the namespace.
4138
367k
    for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
4139
364k
         Prev = Prev->getPreviousDecl())
4140
365k
      if (!Prev->isFromASTFile())
4141
1.43k
        return 0;
4142
4143
    // Note that we need to emit an update record for the primary context.
4144
1.88k
    UpdatedDeclContexts.insert(DC->getPrimaryContext());
4145
4146
    // Make sure all visible decls are written. They will be recorded later. We
4147
    // do this using a side data structure so we can sort the names into
4148
    // a deterministic order.
4149
1.88k
    StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4150
1.88k
    SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4151
1.88k
        LookupResults;
4152
1.88k
    if (Map) {
4153
1.68k
      LookupResults.reserve(Map->size());
4154
1.68k
      for (auto &Entry : *Map)
4155
42.2k
        LookupResults.push_back(
4156
42.2k
            std::make_pair(Entry.first, Entry.second.getLookupResult()));
4157
1.68k
    }
4158
4159
1.88k
    llvm::sort(LookupResults, llvm::less_first());
4160
42.2k
    for (auto &NameAndResult : LookupResults) {
4161
42.2k
      DeclarationName Name = NameAndResult.first;
4162
42.2k
      DeclContext::lookup_result Result = NameAndResult.second;
4163
42.2k
      if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4164
42.2k
          
Name.getNameKind() == DeclarationName::CXXConversionFunctionName41.7k
) {
4165
        // We have to work around a name lookup bug here where negative lookup
4166
        // results for these names get cached in namespace lookup tables (these
4167
        // names should never be looked up in a namespace).
4168
510
        assert(Result.empty() && "Cannot have a constructor or conversion "
4169
510
                                 "function name in a namespace!");
4170
510
        continue;
4171
510
      }
4172
4173
41.7k
      for (NamedDecl *ND : Result)
4174
114k
        if (!ND->isFromASTFile())
4175
26.0k
          GetDeclRef(ND);
4176
41.7k
    }
4177
4178
1.88k
    return 0;
4179
1.88k
  }
4180
4181
492k
  if (DC->getPrimaryContext() != DC)
4182
8.62k
    return 0;
4183
4184
  // Skip contexts which don't support name lookup.
4185
483k
  if (!DC->isLookupContext())
4186
357k
    return 0;
4187
4188
  // If not in C++, we perform name lookup for the translation unit via the
4189
  // IdentifierInfo chains, don't bother to build a visible-declarations table.
4190
125k
  if (DC->isTranslationUnit() && 
!Context.getLangOpts().CPlusPlus0
)
4191
0
    return 0;
4192
4193
  // Serialize the contents of the mapping used for lookup. Note that,
4194
  // although we have two very different code paths, the serialized
4195
  // representation is the same for both cases: a declaration name,
4196
  // followed by a size, followed by references to the visible
4197
  // declarations that have that name.
4198
125k
  uint64_t Offset = Stream.GetCurrentBitNo();
4199
125k
  StoredDeclsMap *Map = DC->buildLookup();
4200
125k
  if (!Map || 
Map->empty()53.4k
)
4201
72.4k
    return 0;
4202
4203
  // Create the on-disk hash table in a buffer.
4204
53.4k
  SmallString<4096> LookupTable;
4205
53.4k
  GenerateNameLookupTable(DC, LookupTable);
4206
4207
  // Write the lookup table
4208
53.4k
  RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4209
53.4k
  Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
4210
53.4k
                            LookupTable);
4211
53.4k
  ++NumVisibleDeclContexts;
4212
53.4k
  return Offset;
4213
125k
}
4214
4215
/// Write an UPDATE_VISIBLE block for the given context.
4216
///
4217
/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4218
/// DeclContext in a dependent AST file. As such, they only exist for the TU
4219
/// (in C++), for namespaces, and for classes with forward-declared unscoped
4220
/// enumeration members (in C++11).
4221
14.5k
void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
4222
14.5k
  StoredDeclsMap *Map = DC->getLookupPtr();
4223
14.5k
  if (!Map || 
Map->empty()7.64k
)
4224
6.88k
    return;
4225
4226
  // Create the on-disk hash table in a buffer.
4227
7.64k
  SmallString<4096> LookupTable;
4228
7.64k
  GenerateNameLookupTable(DC, LookupTable);
4229
4230
  // If we're updating a namespace, select a key declaration as the key for the
4231
  // update record; those are the only ones that will be checked on reload.
4232
7.64k
  if (isa<NamespaceDecl>(DC))
4233
1.69k
    DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4234
4235
  // Write the lookup table
4236
7.64k
  RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
4237
7.64k
  Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
4238
7.64k
}
4239
4240
/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4241
7.24k
void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4242
7.24k
  RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4243
7.24k
  Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4244
7.24k
}
4245
4246
/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4247
7.24k
void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4248
7.24k
  if (!SemaRef.Context.getLangOpts().OpenCL)
4249
7.22k
    return;
4250
4251
14
  const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4252
14
  RecordData Record;
4253
537
  for (const auto &I:Opts.OptMap) {
4254
537
    AddString(I.getKey(), Record);
4255
537
    auto V = I.getValue();
4256
537
    Record.push_back(V.Supported ? 
1341
:
0196
);
4257
537
    Record.push_back(V.Enabled ? 
13
:
0534
);
4258
537
    Record.push_back(V.WithPragma ? 
1383
:
0154
);
4259
537
    Record.push_back(V.Avail);
4260
537
    Record.push_back(V.Core);
4261
537
    Record.push_back(V.Opt);
4262
537
  }
4263
14
  Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4264
14
}
4265
7.24k
void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4266
7.24k
  if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4267
1
    RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4268
1
    Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4269
1
  }
4270
7.24k
}
4271
4272
7.24k
void ASTWriter::WriteObjCCategories() {
4273
7.24k
  SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4274
7.24k
  RecordData Categories;
4275
4276
7.76k
  for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; 
++I518
) {
4277
518
    unsigned Size = 0;
4278
518
    unsigned StartIndex = Categories.size();
4279
4280
518
    ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4281
4282
    // Allocate space for the size.
4283
518
    Categories.push_back(0);
4284
4285
    // Add the categories.
4286
518
    for (ObjCInterfaceDecl::known_categories_iterator
4287
518
           Cat = Class->known_categories_begin(),
4288
518
           CatEnd = Class->known_categories_end();
4289
2.10k
         Cat != CatEnd; 
++Cat, ++Size1.58k
) {
4290
1.58k
      assert(getDeclID(*Cat) != 0 && "Bogus category");
4291
1.58k
      AddDeclRef(*Cat, Categories);
4292
1.58k
    }
4293
4294
    // Update the size.
4295
518
    Categories[StartIndex] = Size;
4296
4297
    // Record this interface -> category map.
4298
518
    ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4299
518
    CategoriesMap.push_back(CatInfo);
4300
518
  }
4301
4302
  // Sort the categories map by the definition ID, since the reader will be
4303
  // performing binary searches on this information.
4304
7.24k
  llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4305
4306
  // Emit the categories map.
4307
7.24k
  using namespace llvm;
4308
4309
7.24k
  auto Abbrev = std::make_shared<BitCodeAbbrev>();
4310
7.24k
  Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4311
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4312
7.24k
  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4313
7.24k
  unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4314
4315
7.24k
  RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4316
7.24k
  Stream.EmitRecordWithBlob(AbbrevID, Record,
4317
7.24k
                            reinterpret_cast<char *>(CategoriesMap.data()),
4318
7.24k
                            CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4319
4320
  // Emit the category lists.
4321
7.24k
  Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4322
7.24k
}
4323
4324
7.24k
void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4325
7.24k
  Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4326
4327
7.24k
  if (LPTMap.empty())
4328
7.23k
    return;
4329
4330
9
  RecordData Record;
4331
40
  for (auto &LPTMapEntry : LPTMap) {
4332
40
    const FunctionDecl *FD = LPTMapEntry.first;
4333
40
    LateParsedTemplate &LPT = *LPTMapEntry.second;
4334
40
    AddDeclRef(FD, Record);
4335
40
    AddDeclRef(LPT.D, Record);
4336
40
    Record.push_back(LPT.FPO.getAsOpaqueInt());
4337
40
    Record.push_back(LPT.Toks.size());
4338
4339
579
    for (const auto &Tok : LPT.Toks) {
4340
579
      AddToken(Tok, Record);
4341
579
    }
4342
40
  }
4343
9
  Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4344
9
}
4345
4346
/// Write the state of 'pragma clang optimize' at the end of the module.
4347
3.72k
void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4348
3.72k
  RecordData Record;
4349
3.72k
  SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4350
3.72k
  AddSourceLocation(PragmaLoc, Record);
4351
3.72k
  Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4352
3.72k
}
4353
4354
/// Write the state of 'pragma ms_struct' at the end of the module.
4355
3.72k
void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4356
3.72k
  RecordData Record;
4357
3.72k
  Record.push_back(SemaRef.MSStructPragmaOn ? 
PMSST_ON1
:
PMSST_OFF3.72k
);
4358
3.72k
  Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4359
3.72k
}
4360
4361
/// Write the state of 'pragma pointers_to_members' at the end of the
4362
//module.
4363
3.72k
void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4364
3.72k
  RecordData Record;
4365
3.72k
  Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4366
3.72k
  AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4367
3.72k
  Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4368
3.72k
}
4369
4370
/// Write the state of 'pragma align/pack' at the end of the module.
4371
7.24k
void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4372
  // Don't serialize pragma align/pack state for modules, since it should only
4373
  // take effect on a per-submodule basis.
4374
7.24k
  if (WritingModule)
4375
3.51k
    return;
4376
4377
3.72k
  RecordData Record;
4378
3.72k
  AddAlignPackInfo(SemaRef.AlignPackStack.CurrentValue, Record);
4379
3.72k
  AddSourceLocation(SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4380
3.72k
  Record.push_back(SemaRef.AlignPackStack.Stack.size());
4381
3.72k
  for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4382
14
    AddAlignPackInfo(StackEntry.Value, Record);
4383
14
    AddSourceLocation(StackEntry.PragmaLocation, Record);
4384
14
    AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4385
14
    AddString(StackEntry.StackSlotLabel, Record);
4386
14
  }
4387
3.72k
  Stream.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS, Record);
4388
3.72k
}
4389
4390
/// Write the state of 'pragma float_control' at the end of the module.
4391
7.24k
void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4392
  // Don't serialize pragma float_control state for modules,
4393
  // since it should only take effect on a per-submodule basis.
4394
7.24k
  if (WritingModule)
4395
3.51k
    return;
4396
4397
3.72k
  RecordData Record;
4398
3.72k
  Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4399
3.72k
  AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4400
3.72k
  Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4401
3.72k
  for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4402
2
    Record.push_back(StackEntry.Value.getAsOpaqueInt());
4403
2
    AddSourceLocation(StackEntry.PragmaLocation, Record);
4404
2
    AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4405
2
    AddString(StackEntry.StackSlotLabel, Record);
4406
2
  }
4407
3.72k
  Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4408
3.72k
}
4409
4410
void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4411
12
                                         ModuleFileExtensionWriter &Writer) {
4412
  // Enter the extension block.
4413
12
  Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4414
4415
  // Emit the metadata record abbreviation.
4416
12
  auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4417
12
  Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4418
12
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4419
12
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4420
12
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4421
12
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4422
12
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4423
12
  unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4424
4425
  // Emit the metadata record.
4426
12
  RecordData Record;
4427
12
  auto Metadata = Writer.getExtension()->getExtensionMetadata();
4428
12
  Record.push_back(EXTENSION_METADATA);
4429
12
  Record.push_back(Metadata.MajorVersion);
4430
12
  Record.push_back(Metadata.MinorVersion);
4431
12
  Record.push_back(Metadata.BlockName.size());
4432
12
  Record.push_back(Metadata.UserInfo.size());
4433
12
  SmallString<64> Buffer;
4434
12
  Buffer += Metadata.BlockName;
4435
12
  Buffer += Metadata.UserInfo;
4436
12
  Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4437
4438
  // Emit the contents of the extension block.
4439
12
  Writer.writeExtensionContents(SemaRef, Stream);
4440
4441
  // Exit the extension block.
4442
12
  Stream.ExitBlock();
4443
12
}
4444
4445
//===----------------------------------------------------------------------===//
4446
// General Serialization Routines
4447
//===----------------------------------------------------------------------===//
4448
4449
816k
void ASTRecordWriter::AddAttr(const Attr *A) {
4450
816k
  auto &Record = *this;
4451
  // FIXME: Clang can't handle the serialization/deserialization of
4452
  // preferred_name properly now. See
4453
  // https://github.com/llvm/llvm-project/issues/56490 for example.
4454
816k
  if (!A || 
(816k
isa<PreferredNameAttr>(A)816k
&&
4455
816k
             
Writer->isWritingStdCXXNamedModules()115
))
4456
13
    return Record.push_back(0);
4457
4458
816k
  Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4459
4460
816k
  Record.AddIdentifierRef(A->getAttrName());
4461
816k
  Record.AddIdentifierRef(A->getScopeName());
4462
816k
  Record.AddSourceRange(A->getRange());
4463
816k
  Record.AddSourceLocation(A->getScopeLoc());
4464
816k
  Record.push_back(A->getParsedKind());
4465
816k
  Record.push_back(A->getSyntax());
4466
816k
  Record.push_back(A->getAttributeSpellingListIndexRaw());
4467
816k
  Record.push_back(A->isRegularKeywordAttribute());
4468
4469
816k
#include "clang/Serialization/AttrPCHWrite.inc"
4470
816k
}
4471
4472
/// Emit the list of attributes to the specified record.
4473
386k
void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4474
386k
  push_back(Attrs.size());
4475
386k
  for (const auto *A : Attrs)
4476
743k
    AddAttr(A);
4477
386k
}
4478
4479
3.10M
void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4480
3.10M
  AddSourceLocation(Tok.getLocation(), Record);
4481
  // FIXME: Should translate token kind to a stable encoding.
4482
3.10M
  Record.push_back(Tok.getKind());
4483
  // FIXME: Should translate token flags to a stable encoding.
4484
3.10M
  Record.push_back(Tok.getFlags());
4485
4486
3.10M
  if (Tok.isAnnotation()) {
4487
12
    AddSourceLocation(Tok.getAnnotationEndLoc(), Record);
4488
12
    switch (Tok.getKind()) {
4489
1
    case tok::annot_pragma_loop_hint: {
4490
1
      auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4491
1
      AddToken(Info->PragmaName, Record);
4492
1
      AddToken(Info->Option, Record);
4493
1
      Record.push_back(Info->Toks.size());
4494
1
      for (const auto &T : Info->Toks)
4495
2
        AddToken(T, Record);
4496
1
      break;
4497
0
    }
4498
6
    case tok::annot_pragma_pack: {
4499
6
      auto *Info =
4500
6
          static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
4501
6
      Record.push_back(static_cast<unsigned>(Info->Action));
4502
6
      AddString(Info->SlotLabel, Record);
4503
6
      AddToken(Info->Alignment, Record);
4504
6
      break;
4505
0
    }
4506
    // Some annotation tokens do not use the PtrData field.
4507
1
    case tok::annot_pragma_openmp:
4508
2
    case tok::annot_pragma_openmp_end:
4509
5
    case tok::annot_pragma_unused:
4510
5
      break;
4511
0
    default:
4512
0
      llvm_unreachable("missing serialization code for annotation token");
4513
12
    }
4514
3.10M
  } else {
4515
3.10M
    Record.push_back(Tok.getLength());
4516
    // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4517
    // is needed.
4518
3.10M
    AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4519
3.10M
  }
4520
3.10M
}
4521
4522
621k
void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4523
621k
  Record.push_back(Str.size());
4524
621k
  Record.insert(Record.end(), Str.begin(), Str.end());
4525
621k
}
4526
4527
113k
bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4528
113k
  assert(Context && "should have context when outputting path");
4529
4530
  // Leave special file names as they are.
4531
113k
  StringRef PathStr(Path.data(), Path.size());
4532
113k
  if (PathStr == "<built-in>" || 
PathStr == "<command line>"106k
)
4533
14.4k
    return false;
4534
4535
99.3k
  bool Changed =
4536
99.3k
      cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4537
4538
  // Remove a prefix to make the path relative, if relevant.
4539
99.3k
  const char *PathBegin = Path.data();
4540
99.3k
  const char *PathPtr =
4541
99.3k
      adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4542
99.3k
  if (PathPtr != PathBegin) {
4543
68.0k
    Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4544
68.0k
    Changed = true;
4545
68.0k
  }
4546
4547
99.3k
  return Changed;
4548
113k
}
4549
4550
29.9k
void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4551
29.9k
  SmallString<128> FilePath(Path);
4552
29.9k
  PreparePathForOutput(FilePath);
4553
29.9k
  AddString(FilePath, Record);
4554
29.9k
}
4555
4556
void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4557
4.16k
                                   StringRef Path) {
4558
4.16k
  SmallString<128> FilePath(Path);
4559
4.16k
  PreparePathForOutput(FilePath);
4560
4.16k
  Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4561
4.16k
}
4562
4563
void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4564
535k
                                RecordDataImpl &Record) {
4565
535k
  Record.push_back(Version.getMajor());
4566
535k
  if (std::optional<unsigned> Minor = Version.getMinor())
4567
132k
    Record.push_back(*Minor + 1);
4568
402k
  else
4569
402k
    Record.push_back(0);
4570
535k
  if (std::optional<unsigned> Subminor = Version.getSubminor())
4571
1.77k
    Record.push_back(*Subminor + 1);
4572
534k
  else
4573
534k
    Record.push_back(0);
4574
535k
}
4575
4576
/// Note that the identifier II occurs at the given offset
4577
/// within the identifier table.
4578
12.4M
void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4579
12.4M
  IdentID ID = IdentifierIDs[II];
4580
  // Only store offsets new to this AST file. Other identifier names are looked
4581
  // up earlier in the chain and thus don't need an offset.
4582
12.4M
  if (ID >= FirstIdentID)
4583
12.3M
    IdentifierOffsets[ID - FirstIdentID] = Offset;
4584
12.4M
}
4585
4586
/// Note that the selector Sel occurs at the given offset
4587
/// within the method pool/selector table.
4588
37.2k
void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4589
37.2k
  unsigned ID = SelectorIDs[Sel];
4590
37.2k
  assert(ID && "Unknown selector");
4591
  // Don't record offsets for selectors that are also available in a different
4592
  // file.
4593
37.2k
  if (ID < FirstSelectorID)
4594
1.15k
    return;
4595
36.1k
  SelectorOffsets[ID - FirstSelectorID] = Offset;
4596
36.1k
}
4597
4598
ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4599
                     SmallVectorImpl<char> &Buffer,
4600
                     InMemoryModuleCache &ModuleCache,
4601
                     ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4602
                     bool IncludeTimestamps, bool BuildingImplicitModule)
4603
7.31k
    : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4604
7.31k
      IncludeTimestamps(IncludeTimestamps),
4605
7.31k
      BuildingImplicitModule(BuildingImplicitModule) {
4606
7.31k
  for (const auto &Ext : Extensions) {
4607
12
    if (auto Writer = Ext->createExtensionWriter(*this))
4608
12
      ModuleFileExtensionWriters.push_back(std::move(Writer));
4609
12
  }
4610
7.31k
}
4611
4612
7.31k
ASTWriter::~ASTWriter() = default;
4613
4614
834k
const LangOptions &ASTWriter::getLangOpts() const {
4615
834k
  assert(WritingAST && "can't determine lang opts when not writing AST");
4616
834k
  return Context->getLangOpts();
4617
834k
}
4618
4619
44.2k
time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4620
44.2k
  return IncludeTimestamps ? 
E->getModificationTime()41.8k
:
02.32k
;
4621
44.2k
}
4622
4623
ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
4624
                                     Module *WritingModule, StringRef isysroot,
4625
                                     bool hasErrors,
4626
7.24k
                                     bool ShouldCacheASTInMemory) {
4627
7.24k
  llvm::TimeTraceScope scope("WriteAST", OutputFile);
4628
7.24k
  WritingAST = true;
4629
4630
7.24k
  ASTHasCompilerErrors = hasErrors;
4631
4632
  // Emit the file header.
4633
7.24k
  Stream.Emit((unsigned)'C', 8);
4634
7.24k
  Stream.Emit((unsigned)'P', 8);
4635
7.24k
  Stream.Emit((unsigned)'C', 8);
4636
7.24k
  Stream.Emit((unsigned)'H', 8);
4637
4638
7.24k
  WriteBlockInfoBlock();
4639
4640
7.24k
  Context = &SemaRef.Context;
4641
7.24k
  PP = &SemaRef.PP;
4642
7.24k
  this->WritingModule = WritingModule;
4643
7.24k
  ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
4644
7.24k
  Context = nullptr;
4645
7.24k
  PP = nullptr;
4646
7.24k
  this->WritingModule = nullptr;
4647
7.24k
  this->BaseDirectory.clear();
4648
4649
7.24k
  WritingAST = false;
4650
7.24k
  if (ShouldCacheASTInMemory) {
4651
    // Construct MemoryBuffer and update buffer manager.
4652
2.88k
    ModuleCache.addBuiltPCM(OutputFile,
4653
2.88k
                            llvm::MemoryBuffer::getMemBufferCopy(
4654
2.88k
                                StringRef(Buffer.begin(), Buffer.size())));
4655
2.88k
  }
4656
7.24k
  return Signature;
4657
7.24k
}
4658
4659
template<typename Vector>
4660
static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4661
21.9k
                               ASTWriter::RecordData &Record) {
4662
21.9k
  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4663
23.6k
       I != E; 
++I1.70k
) {
4664
1.70k
    Writer.AddDeclRef(*I, Record);
4665
1.70k
  }
4666
21.9k
}
ASTWriter.cpp:void AddLazyVectorDecls<clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u> >(clang::ASTWriter&, clang::LazyVector<clang::VarDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadTentativeDefinitions(llvm::SmallVectorImpl<clang::VarDecl*>&)), 2u, 2u>&, llvm::SmallVector<unsigned long long, 64u>&)
Line
Count
Source
4661
7.24k
                               ASTWriter::RecordData &Record) {
4662
7.24k
  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4663
8.38k
       I != E; 
++I1.14k
) {
4664
1.14k
    Writer.AddDeclRef(*I, Record);
4665
1.14k
  }
4666
7.24k
}
ASTWriter.cpp:void AddLazyVectorDecls<clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u> >(clang::ASTWriter&, clang::LazyVector<clang::DeclaratorDecl const*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadUnusedFileScopedDecls(llvm::SmallVectorImpl<clang::DeclaratorDecl const*>&)), 2u, 2u>&, llvm::SmallVector<unsigned long long, 64u>&)
Line
Count
Source
4661
3.72k
                               ASTWriter::RecordData &Record) {
4662
3.72k
  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4663
3.92k
       I != E; 
++I200
) {
4664
200
    Writer.AddDeclRef(*I, Record);
4665
200
  }
4666
3.72k
}
ASTWriter.cpp:void AddLazyVectorDecls<clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u> >(clang::ASTWriter&, clang::LazyVector<clang::CXXConstructorDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadDelegatingConstructors(llvm::SmallVectorImpl<clang::CXXConstructorDecl*>&)), 2u, 2u>&, llvm::SmallVector<unsigned long long, 64u>&)
Line
Count
Source
4661
3.72k
                               ASTWriter::RecordData &Record) {
4662
3.72k
  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4663
3.73k
       I != E; 
++I6
) {
4664
6
    Writer.AddDeclRef(*I, Record);
4665
6
  }
4666
3.72k
}
ASTWriter.cpp:void AddLazyVectorDecls<clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u> >(clang::ASTWriter&, clang::LazyVector<clang::TypedefNameDecl*, clang::ExternalSemaSource, &(clang::ExternalSemaSource::ReadExtVectorDecls(llvm::SmallVectorImpl<clang::TypedefNameDecl*>&)), 2u, 2u>&, llvm::SmallVector<unsigned long long, 64u>&)
Line
Count
Source
4661
7.24k
                               ASTWriter::RecordData &Record) {
4662
7.24k
  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4663
7.59k
       I != E; 
++I354
) {
4664
354
    Writer.AddDeclRef(*I, Record);
4665
354
  }
4666
7.24k
}
4667
4668
7.24k
void ASTWriter::collectNonAffectingInputFiles() {
4669
7.24k
  SourceManager &SrcMgr = PP->getSourceManager();
4670
7.24k
  unsigned N = SrcMgr.local_sloc_entry_size();
4671
4672
7.24k
  IsSLocAffecting.resize(N, true);
4673
4674
7.24k
  if (!WritingModule)
4675
3.72k
    return;
4676
4677
3.51k
  auto AffectingModuleMaps = GetAffectingModuleMaps(*PP, WritingModule);
4678
4679
3.51k
  unsigned FileIDAdjustment = 0;
4680
3.51k
  unsigned OffsetAdjustment = 0;
4681
4682
3.51k
  NonAffectingFileIDAdjustments.reserve(N);
4683
3.51k
  NonAffectingOffsetAdjustments.reserve(N);
4684
4685
3.51k
  NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4686
3.51k
  NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4687
4688
2.02M
  for (unsigned I = 1; I != N; 
++I2.02M
) {
4689
2.02M
    const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
4690
2.02M
    FileID FID = FileID::get(I);
4691
2.02M
    assert(&SrcMgr.getSLocEntry(FID) == SLoc);
4692
4693
2.02M
    if (!SLoc->isFile())
4694
1.98M
      continue;
4695
43.4k
    const SrcMgr::FileInfo &File = SLoc->getFile();
4696
43.4k
    const SrcMgr::ContentCache *Cache = &File.getContentCache();
4697
43.4k
    if (!Cache->OrigEntry)
4698
8.78k
      continue;
4699
4700
34.6k
    if (!isModuleMap(File.getFileCharacteristic()) ||
4701
34.6k
        
AffectingModuleMaps.empty()6.78k
||
4702
34.6k
        
AffectingModuleMaps.find(Cache->OrigEntry) != AffectingModuleMaps.end()6.78k
)
4703
31.6k
      continue;
4704
4705
2.98k
    IsSLocAffecting[I] = false;
4706
4707
2.98k
    FileIDAdjustment += 1;
4708
    // Even empty files take up one element in the offset table.
4709
2.98k
    OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
4710
4711
    // If the previous file was non-affecting as well, just extend its entry
4712
    // with our information.
4713
2.98k
    if (!NonAffectingFileIDs.empty() &&
4714
2.98k
        
NonAffectingFileIDs.back().ID == FID.ID - 11.98k
) {
4715
1.77k
      NonAffectingFileIDs.back() = FID;
4716
1.77k
      NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
4717
1.77k
      NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
4718
1.77k
      NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
4719
1.77k
      continue;
4720
1.77k
    }
4721
4722
1.20k
    NonAffectingFileIDs.push_back(FID);
4723
1.20k
    NonAffectingRanges.emplace_back(SrcMgr.getLocForStartOfFile(FID),
4724
1.20k
                                    SrcMgr.getLocForEndOfFile(FID));
4725
1.20k
    NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4726
1.20k
    NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4727
1.20k
  }
4728
3.51k
}
4729
4730
ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4731
7.24k
                                         Module *WritingModule) {
4732
7.24k
  using namespace llvm;
4733
4734
7.24k
  bool isModule = WritingModule != nullptr;
4735
4736
  // Make sure that the AST reader knows to finalize itself.
4737
7.24k
  if (Chain)
4738
3.70k
    Chain->finalizeForWriting();
4739
4740
7.24k
  ASTContext &Context = SemaRef.Context;
4741
7.24k
  Preprocessor &PP = SemaRef.PP;
4742
4743
  // This needs to be done very early, since everything that writes
4744
  // SourceLocations or FileIDs depends on it.
4745
7.24k
  collectNonAffectingInputFiles();
4746
4747
7.24k
  writeUnhashedControlBlock(PP, Context);
4748
4749
  // Set up predefined declaration IDs.
4750
123k
  auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4751
123k
    if (D) {
4752
66.2k
      assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4753
66.2k
      DeclIDs[D] = ID;
4754
66.2k
    }
4755
123k
  };
4756
7.24k
  RegisterPredefDecl(Context.getTranslationUnitDecl(),
4757
7.24k
                     PREDEF_DECL_TRANSLATION_UNIT_ID);
4758
7.24k
  RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4759
7.24k
  RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4760
7.24k
  RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4761
7.24k
  RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4762
7.24k
                     PREDEF_DECL_OBJC_PROTOCOL_ID);
4763
7.24k
  RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4764
7.24k
  RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4765
7.24k
  RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4766
7.24k
                     PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4767
7.24k
  RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4768
7.24k
  RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4769
7.24k
  RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4770
7.24k
                     PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
4771
7.24k
  RegisterPredefDecl(Context.MSGuidTagDecl,
4772
7.24k
                     PREDEF_DECL_BUILTIN_MS_GUID_ID);
4773
7.24k
  RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4774
7.24k
  RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4775
7.24k
                     PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
4776
7.24k
  RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4777
7.24k
                     PREDEF_DECL_CF_CONSTANT_STRING_ID);
4778
7.24k
  RegisterPredefDecl(Context.CFConstantStringTagDecl,
4779
7.24k
                     PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
4780
7.24k
  RegisterPredefDecl(Context.TypePackElementDecl,
4781
7.24k
                     PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
4782
4783
  // Build a record containing all of the tentative definitions in this file, in
4784
  // TentativeDefinitions order.  Generally, this record will be empty for
4785
  // headers.
4786
7.24k
  RecordData TentativeDefinitions;
4787
7.24k
  AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4788
4789
  // Build a record containing all of the file scoped decls in this file.
4790
7.24k
  RecordData UnusedFileScopedDecls;
4791
7.24k
  if (!isModule)
4792
3.72k
    AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4793
3.72k
                       UnusedFileScopedDecls);
4794
4795
  // Build a record containing all of the delegating constructors we still need
4796
  // to resolve.
4797
7.24k
  RecordData DelegatingCtorDecls;
4798
7.24k
  if (!isModule)
4799
3.72k
    AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4800
4801
  // Write the set of weak, undeclared identifiers. We always write the
4802
  // entire table, since later PCH files in a PCH chain are only interested in
4803
  // the results at the end of the chain.
4804
7.24k
  RecordData WeakUndeclaredIdentifiers;
4805
7.24k
  for (const auto &WeakUndeclaredIdentifierList :
4806
7.24k
       SemaRef.WeakUndeclaredIdentifiers) {
4807
59
    const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
4808
59
    for (const auto &WI : WeakUndeclaredIdentifierList.second) {
4809
7
      AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4810
7
      AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4811
7
      AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4812
7
    }
4813
59
  }
4814
4815
  // Build a record containing all of the ext_vector declarations.
4816
7.24k
  RecordData ExtVectorDecls;
4817
7.24k
  AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4818
4819
  // Build a record containing all of the VTable uses information.
4820
7.24k
  RecordData VTableUses;
4821
7.24k
  if (!SemaRef.VTableUses.empty()) {
4822
44
    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; 
++I31
) {
4823
31
      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4824
31
      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4825
31
      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4826
31
    }
4827
13
  }
4828
4829
  // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4830
7.24k
  RecordData UnusedLocalTypedefNameCandidates;
4831
7.24k
  for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4832
5
    AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4833
4834
  // Build a record containing all of pending implicit instantiations.
4835
7.24k
  RecordData PendingInstantiations;
4836
7.24k
  for (const auto &I : SemaRef.PendingInstantiations) {
4837
4.08k
    AddDeclRef(I.first, PendingInstantiations);
4838
4.08k
    AddSourceLocation(I.second, PendingInstantiations);
4839
4.08k
  }
4840
7.24k
  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4841
7.24k
         "There are local ones at end of translation unit!");
4842
4843
  // Build a record containing some declaration references.
4844
7.24k
  RecordData SemaDeclRefs;
4845
7.24k
  if (SemaRef.StdNamespace || 
SemaRef.StdBadAlloc5.97k
||
SemaRef.StdAlignValT5.97k
) {
4846
1.26k
    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4847
1.26k
    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4848
1.26k
    AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
4849
1.26k
  }
4850
4851
7.24k
  RecordData CUDASpecialDeclRefs;
4852
7.24k
  if (Context.getcudaConfigureCallDecl()) {
4853
1
    AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4854
1
  }
4855
4856
  // Build a record containing all of the known namespaces.
4857
7.24k
  RecordData KnownNamespaces;
4858
7.24k
  for (const auto &I : SemaRef.KnownNamespaces) {
4859
1.46k
    if (!I.second)
4860
1.46k
      AddDeclRef(I.first, KnownNamespaces);
4861
1.46k
  }
4862
4863
  // Build a record of all used, undefined objects that require definitions.
4864
7.24k
  RecordData UndefinedButUsed;
4865
4866
7.24k
  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4867
7.24k
  SemaRef.getUndefinedButUsed(Undefined);
4868
7.24k
  for (const auto &I : Undefined) {
4869
2.55k
    AddDeclRef(I.first, UndefinedButUsed);
4870
2.55k
    AddSourceLocation(I.second, UndefinedButUsed);
4871
2.55k
  }
4872
4873
  // Build a record containing all delete-expressions that we would like to
4874
  // analyze later in AST.
4875
7.24k
  RecordData DeleteExprsToAnalyze;
4876
4877
7.24k
  if (!isModule) {
4878
3.72k
    for (const auto &DeleteExprsInfo :
4879
3.72k
         SemaRef.getMismatchingDeleteExpressions()) {
4880
1
      AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4881
1
      DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4882
1
      for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4883
1
        AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4884
1
        DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4885
1
      }
4886
1
    }
4887
3.72k
  }
4888
4889
  // Write the control block
4890
7.24k
  WriteControlBlock(PP, Context, isysroot);
4891
4892
  // Write the remaining AST contents.
4893
7.24k
  Stream.FlushToWord();
4894
7.24k
  ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
4895
7.24k
  Stream.EnterSubblock(AST_BLOCK_ID, 5);
4896
7.24k
  ASTBlockStartOffset = Stream.GetCurrentBitNo();
4897
4898
  // This is so that older clang versions, before the introduction
4899
  // of the control block, can read and reject the newer PCH format.
4900
7.24k
  {
4901
7.24k
    RecordData Record = {VERSION_MAJOR};
4902
7.24k
    Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4903
7.24k
  }
4904
4905
  // Create a lexical update block containing all of the declarations in the
4906
  // translation unit that do not come from other AST files.
4907
7.24k
  const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4908
7.24k
  SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4909
287k
  for (const auto *D : TU->noload_decls()) {
4910
287k
    if (!D->isFromASTFile()) {
4911
287k
      NewGlobalKindDeclPairs.push_back(D->getKind());
4912
287k
      NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4913
287k
    }
4914
287k
  }
4915
4916
7.24k
  auto Abv = std::make_shared<BitCodeAbbrev>();
4917
7.24k
  Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4918
7.24k
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4919
7.24k
  unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4920
7.24k
  {
4921
7.24k
    RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4922
7.24k
    Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4923
7.24k
                              bytes(NewGlobalKindDeclPairs));
4924
7.24k
  }
4925
4926
  // And a visible updates block for the translation unit.
4927
7.24k
  Abv = std::make_shared<BitCodeAbbrev>();
4928
7.24k
  Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4929
7.24k
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4930
7.24k
  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4931
7.24k
  UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4932
7.24k
  WriteDeclContextVisibleUpdate(TU);
4933
4934
  // If we have any extern "C" names, write out a visible update for them.
4935
7.24k
  if (Context.ExternCContext)
4936
5.00k
    WriteDeclContextVisibleUpdate(Context.ExternCContext);
4937
4938
  // If the translation unit has an anonymous namespace, and we don't already
4939
  // have an update block for it, write it as an update block.
4940
  // FIXME: Why do we not do this if there's already an update block?
4941
7.24k
  if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4942
11
    ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4943
11
    if (Record.empty())
4944
11
      Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4945
11
  }
4946
4947
  // Add update records for all mangling numbers and static local numbers.
4948
  // These aren't really update records, but this is a convenient way of
4949
  // tagging this rare extra data onto the declarations.
4950
7.24k
  for (const auto &Number : Context.MangleNumbers)
4951
398
    if (!Number.first->isFromASTFile())
4952
341
      DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4953
341
                                                     Number.second));
4954
7.24k
  for (const auto &Number : Context.StaticLocalNumbers)
4955
0
    if (!Number.first->isFromASTFile())
4956
0
      DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4957
0
                                                     Number.second));
4958
4959
  // Make sure visible decls, added to DeclContexts previously loaded from
4960
  // an AST file, are registered for serialization. Likewise for template
4961
  // specializations added to imported templates.
4962
12.5k
  for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
4963
12.5k
    GetDeclRef(I);
4964
12.5k
  }
4965
4966
  // Make sure all decls associated with an identifier are registered for
4967
  // serialization, if we're storing decls with identifiers.
4968
7.24k
  if (!WritingModule || 
!getLangOpts().CPlusPlus3.51k
) {
4969
4.74k
    llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4970
17.1M
    for (const auto &ID : PP.getIdentifierTable()) {
4971
17.1M
      const IdentifierInfo *II = ID.second;
4972
17.1M
      if (!Chain || 
!II->isFromAST()4.64M
||
II->hasChangedSinceDeserialization()64.4k
)
4973
17.0M
        IIs.push_back(II);
4974
17.1M
    }
4975
    // Sort the identifiers to visit based on their name.
4976
4.74k
    llvm::sort(IIs, llvm::deref<std::less<>>());
4977
4.74k
    for (const IdentifierInfo *II : IIs)
4978
17.0M
      for (const Decl *D : SemaRef.IdResolver.decls(II))
4979
229k
        GetDeclRef(D);
4980
4.74k
  }
4981
4982
  // For method pool in the module, if it contains an entry for a selector,
4983
  // the entry should be complete, containing everything introduced by that
4984
  // module and all modules it imports. It's possible that the entry is out of
4985
  // date, so we need to pull in the new content here.
4986
4987
  // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4988
  // safe, we copy all selectors out.
4989
7.24k
  llvm::SmallVector<Selector, 256> AllSelectors;
4990
7.24k
  for (auto &SelectorAndID : SelectorIDs)
4991
1.73k
    AllSelectors.push_back(SelectorAndID.first);
4992
7.24k
  for (auto &Selector : AllSelectors)
4993
1.73k
    SemaRef.updateOutOfDateSelector(Selector);
4994
4995
  // Form the record of special types.
4996
7.24k
  RecordData SpecialTypes;
4997
7.24k
  AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4998
7.24k
  AddTypeRef(Context.getFILEType(), SpecialTypes);
4999
7.24k
  AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
5000
7.24k
  AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
5001
7.24k
  AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
5002
7.24k
  AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
5003
7.24k
  AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
5004
7.24k
  AddTypeRef(Context.getucontext_tType(), SpecialTypes);
5005
5006
7.24k
  if (Chain) {
5007
    // Write the mapping information describing our module dependencies and how
5008
    // each of those modules were mapped into our own offset/ID space, so that
5009
    // the reader can build the appropriate mapping to its own offset/ID space.
5010
    // The map consists solely of a blob with the following format:
5011
    // *(module-kind:i8
5012
    //   module-name-len:i16 module-name:len*i8
5013
    //   source-location-offset:i32
5014
    //   identifier-id:i32
5015
    //   preprocessed-entity-id:i32
5016
    //   macro-definition-id:i32
5017
    //   submodule-id:i32
5018
    //   selector-id:i32
5019
    //   declaration-id:i32
5020
    //   c++-base-specifiers-id:i32
5021
    //   type-id:i32)
5022
    //
5023
    // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5024
    // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5025
    // module name. Otherwise, it is the module file name.
5026
3.70k
    auto Abbrev = std::make_shared<BitCodeAbbrev>();
5027
3.70k
    Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5028
3.70k
    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5029
3.70k
    unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
5030
3.70k
    SmallString<2048> Buffer;
5031
3.70k
    {
5032
3.70k
      llvm::raw_svector_ostream Out(Buffer);
5033
173k
      for (ModuleFile &M : Chain->ModuleMgr) {
5034
173k
        using namespace llvm::support;
5035
5036
173k
        endian::Writer LE(Out, little);
5037
173k
        LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5038
173k
        StringRef Name = M.isModule() ? 
M.ModuleName173k
:
M.FileName71
;
5039
173k
        LE.write<uint16_t>(Name.size());
5040
173k
        Out.write(Name.data(), Name.size());
5041
5042
        // Note: if a base ID was uint max, it would not be possible to load
5043
        // another module after it or have more than one entity inside it.
5044
173k
        uint32_t None = std::numeric_limits<uint32_t>::max();
5045
5046
1.38M
        auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
5047
1.38M
          assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5048
1.38M
          if (ShouldWrite)
5049
1.03M
            LE.write<uint32_t>(BaseID);
5050
349k
          else
5051
349k
            LE.write<uint32_t>(None);
5052
1.38M
        };
5053
5054
        // These values should be unique within a chain, since they will be read
5055
        // as keys into ContinuousRangeMaps.
5056
173k
        writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
5057
173k
        writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
5058
173k
        writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5059
173k
        writeBaseIDOrNone(M.BasePreprocessedEntityID,
5060
173k
                          M.NumPreprocessedEntities);
5061
173k
        writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5062
173k
        writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5063
173k
        writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
5064
173k
        writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
5065
173k
      }
5066
3.70k
    }
5067
3.70k
    RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
5068
3.70k
    Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5069
3.70k
                              Buffer.data(), Buffer.size());
5070
3.70k
  }
5071
5072
  // Build a record containing all of the DeclsToCheckForDeferredDiags.
5073
7.24k
  SmallVector<serialization::DeclID, 64> DeclsToCheckForDeferredDiags;
5074
7.24k
  for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5075
7.96k
    DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));
5076
5077
7.24k
  RecordData DeclUpdatesOffsetsRecord;
5078
5079
  // Keep writing types, declarations, and declaration update records
5080
  // until we've emitted all of them.
5081
7.24k
  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5082
7.24k
  DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5083
7.24k
  WriteTypeAbbrevs();
5084
7.24k
  WriteDeclAbbrevs();
5085
7.43k
  do {
5086
7.43k
    WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5087
2.61M
    while (!DeclTypesToEmit.empty()) {
5088
2.61M
      DeclOrType DOT = DeclTypesToEmit.front();
5089
2.61M
      DeclTypesToEmit.pop();
5090
2.61M
      if (DOT.isType())
5091
805k
        WriteType(DOT.getType());
5092
1.80M
      else
5093
1.80M
        WriteDecl(Context, DOT.getDecl());
5094
2.61M