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/OpenACCClause.h"
33#include "clang/AST/OpenMPClause.h"
34#include "clang/AST/RawCommentList.h"
35#include "clang/AST/TemplateName.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/TypeLocVisitor.h"
38#include "clang/Basic/Diagnostic.h"
39#include "clang/Basic/DiagnosticOptions.h"
40#include "clang/Basic/FileManager.h"
41#include "clang/Basic/FileSystemOptions.h"
42#include "clang/Basic/IdentifierTable.h"
43#include "clang/Basic/LLVM.h"
44#include "clang/Basic/Lambda.h"
45#include "clang/Basic/LangOptions.h"
46#include "clang/Basic/Module.h"
47#include "clang/Basic/ObjCRuntime.h"
48#include "clang/Basic/OpenACCKinds.h"
49#include "clang/Basic/OpenCLOptions.h"
50#include "clang/Basic/SourceLocation.h"
51#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/SourceManagerInternals.h"
53#include "clang/Basic/Specifiers.h"
54#include "clang/Basic/TargetInfo.h"
55#include "clang/Basic/TargetOptions.h"
56#include "clang/Basic/Version.h"
57#include "clang/Lex/HeaderSearch.h"
58#include "clang/Lex/HeaderSearchOptions.h"
59#include "clang/Lex/MacroInfo.h"
60#include "clang/Lex/ModuleMap.h"
61#include "clang/Lex/PreprocessingRecord.h"
62#include "clang/Lex/Preprocessor.h"
63#include "clang/Lex/PreprocessorOptions.h"
64#include "clang/Lex/Token.h"
65#include "clang/Sema/IdentifierResolver.h"
66#include "clang/Sema/ObjCMethodList.h"
67#include "clang/Sema/Sema.h"
68#include "clang/Sema/SemaCUDA.h"
69#include "clang/Sema/SemaObjC.h"
70#include "clang/Sema/Weak.h"
71#include "clang/Serialization/ASTBitCodes.h"
72#include "clang/Serialization/ASTReader.h"
73#include "clang/Serialization/ASTRecordWriter.h"
74#include "clang/Serialization/InMemoryModuleCache.h"
75#include "clang/Serialization/ModuleFile.h"
76#include "clang/Serialization/ModuleFileExtension.h"
77#include "clang/Serialization/SerializationDiagnostic.h"
78#include "llvm/ADT/APFloat.h"
79#include "llvm/ADT/APInt.h"
80#include "llvm/ADT/APSInt.h"
81#include "llvm/ADT/ArrayRef.h"
82#include "llvm/ADT/DenseMap.h"
83#include "llvm/ADT/Hashing.h"
84#include "llvm/ADT/PointerIntPair.h"
85#include "llvm/ADT/STLExtras.h"
86#include "llvm/ADT/ScopeExit.h"
87#include "llvm/ADT/SmallPtrSet.h"
88#include "llvm/ADT/SmallString.h"
89#include "llvm/ADT/SmallVector.h"
90#include "llvm/ADT/StringMap.h"
91#include "llvm/ADT/StringRef.h"
92#include "llvm/Bitstream/BitCodes.h"
93#include "llvm/Bitstream/BitstreamWriter.h"
94#include "llvm/Support/Casting.h"
95#include "llvm/Support/Compression.h"
96#include "llvm/Support/DJB.h"
97#include "llvm/Support/Endian.h"
98#include "llvm/Support/EndianStream.h"
99#include "llvm/Support/Error.h"
100#include "llvm/Support/ErrorHandling.h"
101#include "llvm/Support/LEB128.h"
102#include "llvm/Support/MemoryBuffer.h"
103#include "llvm/Support/OnDiskHashTable.h"
104#include "llvm/Support/Path.h"
105#include "llvm/Support/SHA1.h"
106#include "llvm/Support/TimeProfiler.h"
107#include "llvm/Support/VersionTuple.h"
108#include "llvm/Support/raw_ostream.h"
109#include <algorithm>
110#include <cassert>
111#include <cstdint>
112#include <cstdlib>
113#include <cstring>
114#include <ctime>
115#include <limits>
116#include <memory>
117#include <optional>
118#include <queue>
119#include <tuple>
120#include <utility>
121#include <vector>
122
123using namespace clang;
124using namespace clang::serialization;
125
126template <typename T, typename Allocator>
127static StringRef bytes(const std::vector<T, Allocator> &v) {
128 if (v.empty()) return StringRef();
129 return StringRef(reinterpret_cast<const char*>(&v[0]),
130 sizeof(T) * v.size());
131}
132
133template <typename T>
134static StringRef bytes(const SmallVectorImpl<T> &v) {
135 return StringRef(reinterpret_cast<const char*>(v.data()),
136 sizeof(T) * v.size());
137}
138
139static std::string bytes(const std::vector<bool> &V) {
140 std::string Str;
141 Str.reserve(res_arg: V.size() / 8);
142 for (unsigned I = 0, E = V.size(); I < E;) {
143 char Byte = 0;
144 for (unsigned Bit = 0; Bit < 8 && I < E; ++Bit, ++I)
145 Byte |= V[I] << Bit;
146 Str += Byte;
147 }
148 return Str;
149}
150
151//===----------------------------------------------------------------------===//
152// Type serialization
153//===----------------------------------------------------------------------===//
154
155static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
156 switch (id) {
157#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
158 case Type::CLASS_ID: return TYPE_##CODE_ID;
159#include "clang/Serialization/TypeBitCodes.def"
160 case Type::Builtin:
161 llvm_unreachable("shouldn't be serializing a builtin type this way");
162 }
163 llvm_unreachable("bad type kind");
164}
165
166namespace {
167
168std::optional<std::set<const FileEntry *>>
169GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
170 if (!PP.getHeaderSearchInfo()
171 .getHeaderSearchOpts()
172 .ModulesPruneNonAffectingModuleMaps)
173 return std::nullopt;
174
175 const HeaderSearch &HS = PP.getHeaderSearchInfo();
176 const ModuleMap &MM = HS.getModuleMap();
177
178 std::set<const FileEntry *> ModuleMaps;
179 std::set<const Module *> ProcessedModules;
180 auto CollectModuleMapsForHierarchy = [&](const Module *M) {
181 M = M->getTopLevelModule();
182
183 if (!ProcessedModules.insert(x: M).second)
184 return;
185
186 std::queue<const Module *> Q;
187 Q.push(x: M);
188 while (!Q.empty()) {
189 const Module *Mod = Q.front();
190 Q.pop();
191
192 // The containing module map is affecting, because it's being pointed
193 // into by Module::DefinitionLoc.
194 if (auto FE = MM.getContainingModuleMapFile(Module: Mod))
195 ModuleMaps.insert(x: *FE);
196 // For inferred modules, the module map that allowed inferring is not
197 // related to the virtual containing module map file. It did affect the
198 // compilation, though.
199 if (auto FE = MM.getModuleMapFileForUniquing(M: Mod))
200 ModuleMaps.insert(x: *FE);
201
202 for (auto *SubM : Mod->submodules())
203 Q.push(x: SubM);
204 }
205 };
206
207 // Handle all the affecting modules referenced from the root module.
208
209 CollectModuleMapsForHierarchy(RootModule);
210
211 std::queue<const Module *> Q;
212 Q.push(x: RootModule);
213 while (!Q.empty()) {
214 const Module *CurrentModule = Q.front();
215 Q.pop();
216
217 for (const Module *ImportedModule : CurrentModule->Imports)
218 CollectModuleMapsForHierarchy(ImportedModule);
219 for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
220 CollectModuleMapsForHierarchy(UndeclaredModule);
221
222 for (auto *M : CurrentModule->submodules())
223 Q.push(x: M);
224 }
225
226 // Handle textually-included headers that belong to other modules.
227
228 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
229 HS.getFileMgr().GetUniqueIDMapping(UIDToFiles&: FilesByUID);
230
231 if (FilesByUID.size() > HS.header_file_size())
232 FilesByUID.resize(N: HS.header_file_size());
233
234 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
235 OptionalFileEntryRef File = FilesByUID[UID];
236 if (!File)
237 continue;
238
239 const HeaderFileInfo *HFI = HS.getExistingLocalFileInfo(FE: *File);
240 if (!HFI)
241 continue; // We have no information on this being a header file.
242 if (!HFI->isCompilingModuleHeader && HFI->isModuleHeader)
243 continue; // Modular header, handled in the above module-based loop.
244 if (!HFI->isCompilingModuleHeader && !HFI->IsLocallyIncluded)
245 continue; // Non-modular header not included locally is not affecting.
246
247 for (const auto &KH : HS.findResolvedModulesForHeader(File: *File))
248 if (const Module *M = KH.getModule())
249 CollectModuleMapsForHierarchy(M);
250 }
251
252 // FIXME: This algorithm is not correct for module map hierarchies where
253 // module map file defining a (sub)module of a top-level module X includes
254 // a module map file that defines a (sub)module of another top-level module Y.
255 // Whenever X is affecting and Y is not, "replaying" this PCM file will fail
256 // when parsing module map files for X due to not knowing about the `extern`
257 // module map for Y.
258 //
259 // We don't have a good way to fix it here. We could mark all children of
260 // affecting module map files as being affecting as well, but that's
261 // expensive. SourceManager does not model the edge from parent to child
262 // SLocEntries, so instead, we would need to iterate over leaf module map
263 // files, walk up their include hierarchy and check whether we arrive at an
264 // affecting module map.
265 //
266 // Instead of complicating and slowing down this function, we should probably
267 // just ban module map hierarchies where module map defining a (sub)module X
268 // includes a module map defining a module that's not a submodule of X.
269
270 return ModuleMaps;
271}
272
273class ASTTypeWriter {
274 ASTWriter &Writer;
275 ASTWriter::RecordData Record;
276 ASTRecordWriter BasicWriter;
277
278public:
279 ASTTypeWriter(ASTWriter &Writer)
280 : Writer(Writer), BasicWriter(Writer, Record) {}
281
282 uint64_t write(QualType T) {
283 if (T.hasLocalNonFastQualifiers()) {
284 Qualifiers Qs = T.getLocalQualifiers();
285 BasicWriter.writeQualType(T: T.getLocalUnqualifiedType());
286 BasicWriter.writeQualifiers(value: Qs);
287 return BasicWriter.Emit(Code: TYPE_EXT_QUAL, Abbrev: Writer.getTypeExtQualAbbrev());
288 }
289
290 const Type *typePtr = T.getTypePtr();
291 serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
292 atw.write(node: typePtr);
293 return BasicWriter.Emit(Code: getTypeCodeForTypeClass(id: typePtr->getTypeClass()),
294 /*abbrev*/ Abbrev: 0);
295 }
296};
297
298class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
299 using LocSeq = SourceLocationSequence;
300
301 ASTRecordWriter &Record;
302 LocSeq *Seq;
303
304 void addSourceLocation(SourceLocation Loc) {
305 Record.AddSourceLocation(Loc, Seq);
306 }
307 void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range, Seq); }
308
309public:
310 TypeLocWriter(ASTRecordWriter &Record, LocSeq *Seq)
311 : Record(Record), Seq(Seq) {}
312
313#define ABSTRACT_TYPELOC(CLASS, PARENT)
314#define TYPELOC(CLASS, PARENT) \
315 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
316#include "clang/AST/TypeLocNodes.def"
317
318 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
319 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
320};
321
322} // namespace
323
324void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
325 // nothing to do
326}
327
328void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
329 addSourceLocation(Loc: TL.getBuiltinLoc());
330 if (TL.needsExtraLocalData()) {
331 Record.push_back(N: TL.getWrittenTypeSpec());
332 Record.push_back(N: static_cast<uint64_t>(TL.getWrittenSignSpec()));
333 Record.push_back(N: static_cast<uint64_t>(TL.getWrittenWidthSpec()));
334 Record.push_back(N: TL.hasModeAttr());
335 }
336}
337
338void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
339 addSourceLocation(Loc: TL.getNameLoc());
340}
341
342void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
343 addSourceLocation(Loc: TL.getStarLoc());
344}
345
346void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
347 // nothing to do
348}
349
350void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
351 // nothing to do
352}
353
354void TypeLocWriter::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
355 // nothing to do
356}
357
358void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
359 addSourceLocation(Loc: TL.getCaretLoc());
360}
361
362void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
363 addSourceLocation(Loc: TL.getAmpLoc());
364}
365
366void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
367 addSourceLocation(Loc: TL.getAmpAmpLoc());
368}
369
370void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
371 addSourceLocation(Loc: TL.getStarLoc());
372 Record.AddTypeSourceInfo(TInfo: TL.getClassTInfo());
373}
374
375void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
376 addSourceLocation(Loc: TL.getLBracketLoc());
377 addSourceLocation(Loc: TL.getRBracketLoc());
378 Record.push_back(N: TL.getSizeExpr() ? 1 : 0);
379 if (TL.getSizeExpr())
380 Record.AddStmt(S: TL.getSizeExpr());
381}
382
383void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
384 VisitArrayTypeLoc(TL);
385}
386
387void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
388 VisitArrayTypeLoc(TL);
389}
390
391void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
392 VisitArrayTypeLoc(TL);
393}
394
395void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
396 DependentSizedArrayTypeLoc TL) {
397 VisitArrayTypeLoc(TL);
398}
399
400void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
401 DependentAddressSpaceTypeLoc TL) {
402 addSourceLocation(Loc: TL.getAttrNameLoc());
403 SourceRange range = TL.getAttrOperandParensRange();
404 addSourceLocation(Loc: range.getBegin());
405 addSourceLocation(Loc: range.getEnd());
406 Record.AddStmt(S: TL.getAttrExprOperand());
407}
408
409void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
410 DependentSizedExtVectorTypeLoc TL) {
411 addSourceLocation(Loc: TL.getNameLoc());
412}
413
414void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
415 addSourceLocation(Loc: TL.getNameLoc());
416}
417
418void TypeLocWriter::VisitDependentVectorTypeLoc(
419 DependentVectorTypeLoc TL) {
420 addSourceLocation(Loc: TL.getNameLoc());
421}
422
423void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
424 addSourceLocation(Loc: TL.getNameLoc());
425}
426
427void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
428 addSourceLocation(Loc: TL.getAttrNameLoc());
429 SourceRange range = TL.getAttrOperandParensRange();
430 addSourceLocation(Loc: range.getBegin());
431 addSourceLocation(Loc: range.getEnd());
432 Record.AddStmt(S: TL.getAttrRowOperand());
433 Record.AddStmt(S: TL.getAttrColumnOperand());
434}
435
436void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
437 DependentSizedMatrixTypeLoc TL) {
438 addSourceLocation(Loc: TL.getAttrNameLoc());
439 SourceRange range = TL.getAttrOperandParensRange();
440 addSourceLocation(Loc: range.getBegin());
441 addSourceLocation(Loc: range.getEnd());
442 Record.AddStmt(S: TL.getAttrRowOperand());
443 Record.AddStmt(S: TL.getAttrColumnOperand());
444}
445
446void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
447 addSourceLocation(Loc: TL.getLocalRangeBegin());
448 addSourceLocation(Loc: TL.getLParenLoc());
449 addSourceLocation(Loc: TL.getRParenLoc());
450 addSourceRange(Range: TL.getExceptionSpecRange());
451 addSourceLocation(Loc: TL.getLocalRangeEnd());
452 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
453 Record.AddDeclRef(D: TL.getParam(i));
454}
455
456void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
457 VisitFunctionTypeLoc(TL);
458}
459
460void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
461 VisitFunctionTypeLoc(TL);
462}
463
464void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
465 addSourceLocation(Loc: TL.getNameLoc());
466}
467
468void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
469 addSourceLocation(Loc: TL.getNameLoc());
470}
471
472void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
473 addSourceLocation(Loc: TL.getNameLoc());
474}
475
476void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
477 if (TL.getNumProtocols()) {
478 addSourceLocation(Loc: TL.getProtocolLAngleLoc());
479 addSourceLocation(Loc: TL.getProtocolRAngleLoc());
480 }
481 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
482 addSourceLocation(Loc: TL.getProtocolLoc(i));
483}
484
485void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
486 addSourceLocation(Loc: TL.getTypeofLoc());
487 addSourceLocation(Loc: TL.getLParenLoc());
488 addSourceLocation(Loc: TL.getRParenLoc());
489}
490
491void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
492 addSourceLocation(Loc: TL.getTypeofLoc());
493 addSourceLocation(Loc: TL.getLParenLoc());
494 addSourceLocation(Loc: TL.getRParenLoc());
495 Record.AddTypeSourceInfo(TInfo: TL.getUnmodifiedTInfo());
496}
497
498void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
499 addSourceLocation(Loc: TL.getDecltypeLoc());
500 addSourceLocation(Loc: TL.getRParenLoc());
501}
502
503void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
504 addSourceLocation(Loc: TL.getKWLoc());
505 addSourceLocation(Loc: TL.getLParenLoc());
506 addSourceLocation(Loc: TL.getRParenLoc());
507 Record.AddTypeSourceInfo(TInfo: TL.getUnderlyingTInfo());
508}
509
510void ASTRecordWriter::AddConceptReference(const ConceptReference *CR) {
511 assert(CR);
512 AddNestedNameSpecifierLoc(NNS: CR->getNestedNameSpecifierLoc());
513 AddSourceLocation(Loc: CR->getTemplateKWLoc());
514 AddDeclarationNameInfo(NameInfo: CR->getConceptNameInfo());
515 AddDeclRef(D: CR->getFoundDecl());
516 AddDeclRef(D: CR->getNamedConcept());
517 push_back(N: CR->getTemplateArgsAsWritten() != nullptr);
518 if (CR->getTemplateArgsAsWritten())
519 AddASTTemplateArgumentListInfo(ASTTemplArgList: CR->getTemplateArgsAsWritten());
520}
521
522void TypeLocWriter::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
523 addSourceLocation(Loc: TL.getEllipsisLoc());
524}
525
526void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
527 addSourceLocation(Loc: TL.getNameLoc());
528 auto *CR = TL.getConceptReference();
529 Record.push_back(N: TL.isConstrained() && CR);
530 if (TL.isConstrained() && CR)
531 Record.AddConceptReference(CR);
532 Record.push_back(N: TL.isDecltypeAuto());
533 if (TL.isDecltypeAuto())
534 addSourceLocation(Loc: TL.getRParenLoc());
535}
536
537void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
538 DeducedTemplateSpecializationTypeLoc TL) {
539 addSourceLocation(Loc: TL.getTemplateNameLoc());
540}
541
542void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
543 addSourceLocation(Loc: TL.getNameLoc());
544}
545
546void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
547 addSourceLocation(Loc: TL.getNameLoc());
548}
549
550void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
551 Record.AddAttr(A: TL.getAttr());
552}
553
554void TypeLocWriter::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
555 // Nothing to do
556}
557
558void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
559 // Nothing to do.
560}
561
562void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
563 addSourceLocation(Loc: TL.getNameLoc());
564}
565
566void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
567 SubstTemplateTypeParmTypeLoc TL) {
568 addSourceLocation(Loc: TL.getNameLoc());
569}
570
571void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
572 SubstTemplateTypeParmPackTypeLoc TL) {
573 addSourceLocation(Loc: TL.getNameLoc());
574}
575
576void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
577 TemplateSpecializationTypeLoc TL) {
578 addSourceLocation(Loc: TL.getTemplateKeywordLoc());
579 addSourceLocation(Loc: TL.getTemplateNameLoc());
580 addSourceLocation(Loc: TL.getLAngleLoc());
581 addSourceLocation(Loc: TL.getRAngleLoc());
582 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
583 Record.AddTemplateArgumentLocInfo(Kind: TL.getArgLoc(i).getArgument().getKind(),
584 Arg: TL.getArgLoc(i).getLocInfo());
585}
586
587void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
588 addSourceLocation(Loc: TL.getLParenLoc());
589 addSourceLocation(Loc: TL.getRParenLoc());
590}
591
592void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
593 addSourceLocation(Loc: TL.getExpansionLoc());
594}
595
596void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
597 addSourceLocation(Loc: TL.getElaboratedKeywordLoc());
598 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
599}
600
601void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
602 addSourceLocation(Loc: TL.getNameLoc());
603}
604
605void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
606 addSourceLocation(Loc: TL.getElaboratedKeywordLoc());
607 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
608 addSourceLocation(Loc: TL.getNameLoc());
609}
610
611void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
612 DependentTemplateSpecializationTypeLoc TL) {
613 addSourceLocation(Loc: TL.getElaboratedKeywordLoc());
614 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
615 addSourceLocation(Loc: TL.getTemplateKeywordLoc());
616 addSourceLocation(Loc: TL.getTemplateNameLoc());
617 addSourceLocation(Loc: TL.getLAngleLoc());
618 addSourceLocation(Loc: TL.getRAngleLoc());
619 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
620 Record.AddTemplateArgumentLocInfo(Kind: TL.getArgLoc(i: I).getArgument().getKind(),
621 Arg: TL.getArgLoc(i: I).getLocInfo());
622}
623
624void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
625 addSourceLocation(Loc: TL.getEllipsisLoc());
626}
627
628void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
629 addSourceLocation(Loc: TL.getNameLoc());
630 addSourceLocation(Loc: TL.getNameEndLoc());
631}
632
633void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
634 Record.push_back(N: TL.hasBaseTypeAsWritten());
635 addSourceLocation(Loc: TL.getTypeArgsLAngleLoc());
636 addSourceLocation(Loc: TL.getTypeArgsRAngleLoc());
637 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
638 Record.AddTypeSourceInfo(TInfo: TL.getTypeArgTInfo(i));
639 addSourceLocation(Loc: TL.getProtocolLAngleLoc());
640 addSourceLocation(Loc: TL.getProtocolRAngleLoc());
641 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
642 addSourceLocation(Loc: TL.getProtocolLoc(i));
643}
644
645void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
646 addSourceLocation(Loc: TL.getStarLoc());
647}
648
649void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
650 addSourceLocation(Loc: TL.getKWLoc());
651 addSourceLocation(Loc: TL.getLParenLoc());
652 addSourceLocation(Loc: TL.getRParenLoc());
653}
654
655void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
656 addSourceLocation(Loc: TL.getKWLoc());
657}
658
659void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
660 addSourceLocation(Loc: TL.getNameLoc());
661}
662void TypeLocWriter::VisitDependentBitIntTypeLoc(
663 clang::DependentBitIntTypeLoc TL) {
664 addSourceLocation(Loc: TL.getNameLoc());
665}
666
667void ASTWriter::WriteTypeAbbrevs() {
668 using namespace llvm;
669
670 std::shared_ptr<BitCodeAbbrev> Abv;
671
672 // Abbreviation for TYPE_EXT_QUAL
673 Abv = std::make_shared<BitCodeAbbrev>();
674 Abv->Add(OpInfo: BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
675 Abv->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
676 Abv->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
677 TypeExtQualAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
678}
679
680//===----------------------------------------------------------------------===//
681// ASTWriter Implementation
682//===----------------------------------------------------------------------===//
683
684static void EmitBlockID(unsigned ID, const char *Name,
685 llvm::BitstreamWriter &Stream,
686 ASTWriter::RecordDataImpl &Record) {
687 Record.clear();
688 Record.push_back(Elt: ID);
689 Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_SETBID, Vals: Record);
690
691 // Emit the block name if present.
692 if (!Name || Name[0] == 0)
693 return;
694 Record.clear();
695 while (*Name)
696 Record.push_back(Elt: *Name++);
697 Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Vals: Record);
698}
699
700static void EmitRecordID(unsigned ID, const char *Name,
701 llvm::BitstreamWriter &Stream,
702 ASTWriter::RecordDataImpl &Record) {
703 Record.clear();
704 Record.push_back(Elt: ID);
705 while (*Name)
706 Record.push_back(Elt: *Name++);
707 Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Vals: Record);
708}
709
710static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
711 ASTWriter::RecordDataImpl &Record) {
712#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
713 RECORD(STMT_STOP);
714 RECORD(STMT_NULL_PTR);
715 RECORD(STMT_REF_PTR);
716 RECORD(STMT_NULL);
717 RECORD(STMT_COMPOUND);
718 RECORD(STMT_CASE);
719 RECORD(STMT_DEFAULT);
720 RECORD(STMT_LABEL);
721 RECORD(STMT_ATTRIBUTED);
722 RECORD(STMT_IF);
723 RECORD(STMT_SWITCH);
724 RECORD(STMT_WHILE);
725 RECORD(STMT_DO);
726 RECORD(STMT_FOR);
727 RECORD(STMT_GOTO);
728 RECORD(STMT_INDIRECT_GOTO);
729 RECORD(STMT_CONTINUE);
730 RECORD(STMT_BREAK);
731 RECORD(STMT_RETURN);
732 RECORD(STMT_DECL);
733 RECORD(STMT_GCCASM);
734 RECORD(STMT_MSASM);
735 RECORD(EXPR_PREDEFINED);
736 RECORD(EXPR_DECL_REF);
737 RECORD(EXPR_INTEGER_LITERAL);
738 RECORD(EXPR_FIXEDPOINT_LITERAL);
739 RECORD(EXPR_FLOATING_LITERAL);
740 RECORD(EXPR_IMAGINARY_LITERAL);
741 RECORD(EXPR_STRING_LITERAL);
742 RECORD(EXPR_CHARACTER_LITERAL);
743 RECORD(EXPR_PAREN);
744 RECORD(EXPR_PAREN_LIST);
745 RECORD(EXPR_UNARY_OPERATOR);
746 RECORD(EXPR_SIZEOF_ALIGN_OF);
747 RECORD(EXPR_ARRAY_SUBSCRIPT);
748 RECORD(EXPR_CALL);
749 RECORD(EXPR_MEMBER);
750 RECORD(EXPR_BINARY_OPERATOR);
751 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
752 RECORD(EXPR_CONDITIONAL_OPERATOR);
753 RECORD(EXPR_IMPLICIT_CAST);
754 RECORD(EXPR_CSTYLE_CAST);
755 RECORD(EXPR_COMPOUND_LITERAL);
756 RECORD(EXPR_EXT_VECTOR_ELEMENT);
757 RECORD(EXPR_INIT_LIST);
758 RECORD(EXPR_DESIGNATED_INIT);
759 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
760 RECORD(EXPR_IMPLICIT_VALUE_INIT);
761 RECORD(EXPR_NO_INIT);
762 RECORD(EXPR_VA_ARG);
763 RECORD(EXPR_ADDR_LABEL);
764 RECORD(EXPR_STMT);
765 RECORD(EXPR_CHOOSE);
766 RECORD(EXPR_GNU_NULL);
767 RECORD(EXPR_SHUFFLE_VECTOR);
768 RECORD(EXPR_BLOCK);
769 RECORD(EXPR_GENERIC_SELECTION);
770 RECORD(EXPR_OBJC_STRING_LITERAL);
771 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
772 RECORD(EXPR_OBJC_ARRAY_LITERAL);
773 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
774 RECORD(EXPR_OBJC_ENCODE);
775 RECORD(EXPR_OBJC_SELECTOR_EXPR);
776 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
777 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
778 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
779 RECORD(EXPR_OBJC_KVC_REF_EXPR);
780 RECORD(EXPR_OBJC_MESSAGE_EXPR);
781 RECORD(STMT_OBJC_FOR_COLLECTION);
782 RECORD(STMT_OBJC_CATCH);
783 RECORD(STMT_OBJC_FINALLY);
784 RECORD(STMT_OBJC_AT_TRY);
785 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
786 RECORD(STMT_OBJC_AT_THROW);
787 RECORD(EXPR_OBJC_BOOL_LITERAL);
788 RECORD(STMT_CXX_CATCH);
789 RECORD(STMT_CXX_TRY);
790 RECORD(STMT_CXX_FOR_RANGE);
791 RECORD(EXPR_CXX_OPERATOR_CALL);
792 RECORD(EXPR_CXX_MEMBER_CALL);
793 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
794 RECORD(EXPR_CXX_CONSTRUCT);
795 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
796 RECORD(EXPR_CXX_STATIC_CAST);
797 RECORD(EXPR_CXX_DYNAMIC_CAST);
798 RECORD(EXPR_CXX_REINTERPRET_CAST);
799 RECORD(EXPR_CXX_CONST_CAST);
800 RECORD(EXPR_CXX_ADDRSPACE_CAST);
801 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
802 RECORD(EXPR_USER_DEFINED_LITERAL);
803 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
804 RECORD(EXPR_CXX_BOOL_LITERAL);
805 RECORD(EXPR_CXX_PAREN_LIST_INIT);
806 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
807 RECORD(EXPR_CXX_TYPEID_EXPR);
808 RECORD(EXPR_CXX_TYPEID_TYPE);
809 RECORD(EXPR_CXX_THIS);
810 RECORD(EXPR_CXX_THROW);
811 RECORD(EXPR_CXX_DEFAULT_ARG);
812 RECORD(EXPR_CXX_DEFAULT_INIT);
813 RECORD(EXPR_CXX_BIND_TEMPORARY);
814 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
815 RECORD(EXPR_CXX_NEW);
816 RECORD(EXPR_CXX_DELETE);
817 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
818 RECORD(EXPR_EXPR_WITH_CLEANUPS);
819 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
820 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
821 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
822 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
823 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
824 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
825 RECORD(EXPR_CXX_NOEXCEPT);
826 RECORD(EXPR_OPAQUE_VALUE);
827 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
828 RECORD(EXPR_TYPE_TRAIT);
829 RECORD(EXPR_ARRAY_TYPE_TRAIT);
830 RECORD(EXPR_PACK_EXPANSION);
831 RECORD(EXPR_SIZEOF_PACK);
832 RECORD(EXPR_PACK_INDEXING);
833 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
834 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
835 RECORD(EXPR_FUNCTION_PARM_PACK);
836 RECORD(EXPR_MATERIALIZE_TEMPORARY);
837 RECORD(EXPR_CUDA_KERNEL_CALL);
838 RECORD(EXPR_CXX_UUIDOF_EXPR);
839 RECORD(EXPR_CXX_UUIDOF_TYPE);
840 RECORD(EXPR_LAMBDA);
841#undef RECORD
842}
843
844void ASTWriter::WriteBlockInfoBlock() {
845 RecordData Record;
846 Stream.EnterBlockInfoBlock();
847
848#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
849#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
850
851 // Control Block.
852 BLOCK(CONTROL_BLOCK);
853 RECORD(METADATA);
854 RECORD(MODULE_NAME);
855 RECORD(MODULE_DIRECTORY);
856 RECORD(MODULE_MAP_FILE);
857 RECORD(IMPORTS);
858 RECORD(ORIGINAL_FILE);
859 RECORD(ORIGINAL_FILE_ID);
860 RECORD(INPUT_FILE_OFFSETS);
861
862 BLOCK(OPTIONS_BLOCK);
863 RECORD(LANGUAGE_OPTIONS);
864 RECORD(TARGET_OPTIONS);
865 RECORD(FILE_SYSTEM_OPTIONS);
866 RECORD(HEADER_SEARCH_OPTIONS);
867 RECORD(PREPROCESSOR_OPTIONS);
868
869 BLOCK(INPUT_FILES_BLOCK);
870 RECORD(INPUT_FILE);
871 RECORD(INPUT_FILE_HASH);
872
873 // AST Top-Level Block.
874 BLOCK(AST_BLOCK);
875 RECORD(TYPE_OFFSET);
876 RECORD(DECL_OFFSET);
877 RECORD(IDENTIFIER_OFFSET);
878 RECORD(IDENTIFIER_TABLE);
879 RECORD(EAGERLY_DESERIALIZED_DECLS);
880 RECORD(MODULAR_CODEGEN_DECLS);
881 RECORD(SPECIAL_TYPES);
882 RECORD(STATISTICS);
883 RECORD(TENTATIVE_DEFINITIONS);
884 RECORD(SELECTOR_OFFSETS);
885 RECORD(METHOD_POOL);
886 RECORD(PP_COUNTER_VALUE);
887 RECORD(SOURCE_LOCATION_OFFSETS);
888 RECORD(EXT_VECTOR_DECLS);
889 RECORD(UNUSED_FILESCOPED_DECLS);
890 RECORD(PPD_ENTITIES_OFFSETS);
891 RECORD(VTABLE_USES);
892 RECORD(PPD_SKIPPED_RANGES);
893 RECORD(REFERENCED_SELECTOR_POOL);
894 RECORD(TU_UPDATE_LEXICAL);
895 RECORD(SEMA_DECL_REFS);
896 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
897 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
898 RECORD(UPDATE_VISIBLE);
899 RECORD(DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD);
900 RECORD(DECL_UPDATE_OFFSETS);
901 RECORD(DECL_UPDATES);
902 RECORD(CUDA_SPECIAL_DECL_REFS);
903 RECORD(HEADER_SEARCH_TABLE);
904 RECORD(FP_PRAGMA_OPTIONS);
905 RECORD(OPENCL_EXTENSIONS);
906 RECORD(OPENCL_EXTENSION_TYPES);
907 RECORD(OPENCL_EXTENSION_DECLS);
908 RECORD(DELEGATING_CTORS);
909 RECORD(KNOWN_NAMESPACES);
910 RECORD(MODULE_OFFSET_MAP);
911 RECORD(SOURCE_MANAGER_LINE_TABLE);
912 RECORD(OBJC_CATEGORIES_MAP);
913 RECORD(FILE_SORTED_DECLS);
914 RECORD(IMPORTED_MODULES);
915 RECORD(OBJC_CATEGORIES);
916 RECORD(MACRO_OFFSET);
917 RECORD(INTERESTING_IDENTIFIERS);
918 RECORD(UNDEFINED_BUT_USED);
919 RECORD(LATE_PARSED_TEMPLATE);
920 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
921 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
922 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
923 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
924 RECORD(DELETE_EXPRS_TO_ANALYZE);
925 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
926 RECORD(PP_CONDITIONAL_STACK);
927 RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
928 RECORD(PP_ASSUME_NONNULL_LOC);
929 RECORD(PP_UNSAFE_BUFFER_USAGE);
930 RECORD(VTABLES_TO_EMIT);
931
932 // SourceManager Block.
933 BLOCK(SOURCE_MANAGER_BLOCK);
934 RECORD(SM_SLOC_FILE_ENTRY);
935 RECORD(SM_SLOC_BUFFER_ENTRY);
936 RECORD(SM_SLOC_BUFFER_BLOB);
937 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
938 RECORD(SM_SLOC_EXPANSION_ENTRY);
939
940 // Preprocessor Block.
941 BLOCK(PREPROCESSOR_BLOCK);
942 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
943 RECORD(PP_MACRO_FUNCTION_LIKE);
944 RECORD(PP_MACRO_OBJECT_LIKE);
945 RECORD(PP_MODULE_MACRO);
946 RECORD(PP_TOKEN);
947
948 // Submodule Block.
949 BLOCK(SUBMODULE_BLOCK);
950 RECORD(SUBMODULE_METADATA);
951 RECORD(SUBMODULE_DEFINITION);
952 RECORD(SUBMODULE_UMBRELLA_HEADER);
953 RECORD(SUBMODULE_HEADER);
954 RECORD(SUBMODULE_TOPHEADER);
955 RECORD(SUBMODULE_UMBRELLA_DIR);
956 RECORD(SUBMODULE_IMPORTS);
957 RECORD(SUBMODULE_AFFECTING_MODULES);
958 RECORD(SUBMODULE_EXPORTS);
959 RECORD(SUBMODULE_REQUIRES);
960 RECORD(SUBMODULE_EXCLUDED_HEADER);
961 RECORD(SUBMODULE_LINK_LIBRARY);
962 RECORD(SUBMODULE_CONFIG_MACRO);
963 RECORD(SUBMODULE_CONFLICT);
964 RECORD(SUBMODULE_PRIVATE_HEADER);
965 RECORD(SUBMODULE_TEXTUAL_HEADER);
966 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
967 RECORD(SUBMODULE_INITIALIZERS);
968 RECORD(SUBMODULE_EXPORT_AS);
969
970 // Comments Block.
971 BLOCK(COMMENTS_BLOCK);
972 RECORD(COMMENTS_RAW_COMMENT);
973
974 // Decls and Types block.
975 BLOCK(DECLTYPES_BLOCK);
976 RECORD(TYPE_EXT_QUAL);
977 RECORD(TYPE_COMPLEX);
978 RECORD(TYPE_POINTER);
979 RECORD(TYPE_BLOCK_POINTER);
980 RECORD(TYPE_LVALUE_REFERENCE);
981 RECORD(TYPE_RVALUE_REFERENCE);
982 RECORD(TYPE_MEMBER_POINTER);
983 RECORD(TYPE_CONSTANT_ARRAY);
984 RECORD(TYPE_INCOMPLETE_ARRAY);
985 RECORD(TYPE_VARIABLE_ARRAY);
986 RECORD(TYPE_VECTOR);
987 RECORD(TYPE_EXT_VECTOR);
988 RECORD(TYPE_FUNCTION_NO_PROTO);
989 RECORD(TYPE_FUNCTION_PROTO);
990 RECORD(TYPE_TYPEDEF);
991 RECORD(TYPE_TYPEOF_EXPR);
992 RECORD(TYPE_TYPEOF);
993 RECORD(TYPE_RECORD);
994 RECORD(TYPE_ENUM);
995 RECORD(TYPE_OBJC_INTERFACE);
996 RECORD(TYPE_OBJC_OBJECT_POINTER);
997 RECORD(TYPE_DECLTYPE);
998 RECORD(TYPE_ELABORATED);
999 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1000 RECORD(TYPE_UNRESOLVED_USING);
1001 RECORD(TYPE_INJECTED_CLASS_NAME);
1002 RECORD(TYPE_OBJC_OBJECT);
1003 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1004 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1005 RECORD(TYPE_DEPENDENT_NAME);
1006 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1007 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1008 RECORD(TYPE_PAREN);
1009 RECORD(TYPE_MACRO_QUALIFIED);
1010 RECORD(TYPE_PACK_EXPANSION);
1011 RECORD(TYPE_ATTRIBUTED);
1012 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
1013 RECORD(TYPE_AUTO);
1014 RECORD(TYPE_UNARY_TRANSFORM);
1015 RECORD(TYPE_ATOMIC);
1016 RECORD(TYPE_DECAYED);
1017 RECORD(TYPE_ADJUSTED);
1018 RECORD(TYPE_OBJC_TYPE_PARAM);
1019 RECORD(LOCAL_REDECLARATIONS);
1020 RECORD(DECL_TYPEDEF);
1021 RECORD(DECL_TYPEALIAS);
1022 RECORD(DECL_ENUM);
1023 RECORD(DECL_RECORD);
1024 RECORD(DECL_ENUM_CONSTANT);
1025 RECORD(DECL_FUNCTION);
1026 RECORD(DECL_OBJC_METHOD);
1027 RECORD(DECL_OBJC_INTERFACE);
1028 RECORD(DECL_OBJC_PROTOCOL);
1029 RECORD(DECL_OBJC_IVAR);
1030 RECORD(DECL_OBJC_AT_DEFS_FIELD);
1031 RECORD(DECL_OBJC_CATEGORY);
1032 RECORD(DECL_OBJC_CATEGORY_IMPL);
1033 RECORD(DECL_OBJC_IMPLEMENTATION);
1034 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1035 RECORD(DECL_OBJC_PROPERTY);
1036 RECORD(DECL_OBJC_PROPERTY_IMPL);
1037 RECORD(DECL_FIELD);
1038 RECORD(DECL_MS_PROPERTY);
1039 RECORD(DECL_VAR);
1040 RECORD(DECL_IMPLICIT_PARAM);
1041 RECORD(DECL_PARM_VAR);
1042 RECORD(DECL_FILE_SCOPE_ASM);
1043 RECORD(DECL_BLOCK);
1044 RECORD(DECL_CONTEXT_LEXICAL);
1045 RECORD(DECL_CONTEXT_VISIBLE);
1046 RECORD(DECL_NAMESPACE);
1047 RECORD(DECL_NAMESPACE_ALIAS);
1048 RECORD(DECL_USING);
1049 RECORD(DECL_USING_SHADOW);
1050 RECORD(DECL_USING_DIRECTIVE);
1051 RECORD(DECL_UNRESOLVED_USING_VALUE);
1052 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1053 RECORD(DECL_LINKAGE_SPEC);
1054 RECORD(DECL_EXPORT);
1055 RECORD(DECL_CXX_RECORD);
1056 RECORD(DECL_CXX_METHOD);
1057 RECORD(DECL_CXX_CONSTRUCTOR);
1058 RECORD(DECL_CXX_DESTRUCTOR);
1059 RECORD(DECL_CXX_CONVERSION);
1060 RECORD(DECL_ACCESS_SPEC);
1061 RECORD(DECL_FRIEND);
1062 RECORD(DECL_FRIEND_TEMPLATE);
1063 RECORD(DECL_CLASS_TEMPLATE);
1064 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1065 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1066 RECORD(DECL_VAR_TEMPLATE);
1067 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1068 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1069 RECORD(DECL_FUNCTION_TEMPLATE);
1070 RECORD(DECL_TEMPLATE_TYPE_PARM);
1071 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1072 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1073 RECORD(DECL_CONCEPT);
1074 RECORD(DECL_REQUIRES_EXPR_BODY);
1075 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
1076 RECORD(DECL_STATIC_ASSERT);
1077 RECORD(DECL_CXX_BASE_SPECIFIERS);
1078 RECORD(DECL_CXX_CTOR_INITIALIZERS);
1079 RECORD(DECL_INDIRECTFIELD);
1080 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1081 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1082 RECORD(DECL_IMPORT);
1083 RECORD(DECL_OMP_THREADPRIVATE);
1084 RECORD(DECL_EMPTY);
1085 RECORD(DECL_OBJC_TYPE_PARAM);
1086 RECORD(DECL_OMP_CAPTUREDEXPR);
1087 RECORD(DECL_PRAGMA_COMMENT);
1088 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1089 RECORD(DECL_OMP_DECLARE_REDUCTION);
1090 RECORD(DECL_OMP_ALLOCATE);
1091 RECORD(DECL_HLSL_BUFFER);
1092
1093 // Statements and Exprs can occur in the Decls and Types block.
1094 AddStmtsExprs(Stream, Record);
1095
1096 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1097 RECORD(PPD_MACRO_EXPANSION);
1098 RECORD(PPD_MACRO_DEFINITION);
1099 RECORD(PPD_INCLUSION_DIRECTIVE);
1100
1101 // Decls and Types block.
1102 BLOCK(EXTENSION_BLOCK);
1103 RECORD(EXTENSION_METADATA);
1104
1105 BLOCK(UNHASHED_CONTROL_BLOCK);
1106 RECORD(SIGNATURE);
1107 RECORD(AST_BLOCK_HASH);
1108 RECORD(DIAGNOSTIC_OPTIONS);
1109 RECORD(HEADER_SEARCH_PATHS);
1110 RECORD(DIAG_PRAGMA_MAPPINGS);
1111
1112#undef RECORD
1113#undef BLOCK
1114 Stream.ExitBlock();
1115}
1116
1117/// Prepares a path for being written to an AST file by converting it
1118/// to an absolute path and removing nested './'s.
1119///
1120/// \return \c true if the path was changed.
1121static bool cleanPathForOutput(FileManager &FileMgr,
1122 SmallVectorImpl<char> &Path) {
1123 bool Changed = FileMgr.makeAbsolutePath(Path);
1124 return Changed | llvm::sys::path::remove_dots(path&: Path);
1125}
1126
1127/// Adjusts the given filename to only write out the portion of the
1128/// filename that is not part of the system root directory.
1129///
1130/// \param Filename the file name to adjust.
1131///
1132/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1133/// the returned filename will be adjusted by this root directory.
1134///
1135/// \returns either the original filename (if it needs no adjustment) or the
1136/// adjusted filename (which points into the @p Filename parameter).
1137static const char *
1138adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1139 assert(Filename && "No file name to adjust?");
1140
1141 if (BaseDir.empty())
1142 return Filename;
1143
1144 // Verify that the filename and the system root have the same prefix.
1145 unsigned Pos = 0;
1146 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1147 if (Filename[Pos] != BaseDir[Pos])
1148 return Filename; // Prefixes don't match.
1149
1150 // We hit the end of the filename before we hit the end of the system root.
1151 if (!Filename[Pos])
1152 return Filename;
1153
1154 // If there's not a path separator at the end of the base directory nor
1155 // immediately after it, then this isn't within the base directory.
1156 if (!llvm::sys::path::is_separator(value: Filename[Pos])) {
1157 if (!llvm::sys::path::is_separator(value: BaseDir.back()))
1158 return Filename;
1159 } else {
1160 // If the file name has a '/' at the current position, skip over the '/'.
1161 // We distinguish relative paths from absolute paths by the
1162 // absence of '/' at the beginning of relative paths.
1163 //
1164 // FIXME: This is wrong. We distinguish them by asking if the path is
1165 // absolute, which isn't the same thing. And there might be multiple '/'s
1166 // in a row. Use a better mechanism to indicate whether we have emitted an
1167 // absolute or relative path.
1168 ++Pos;
1169 }
1170
1171 return Filename + Pos;
1172}
1173
1174std::pair<ASTFileSignature, ASTFileSignature>
1175ASTWriter::createSignature() const {
1176 StringRef AllBytes(Buffer.data(), Buffer.size());
1177
1178 llvm::SHA1 Hasher;
1179 Hasher.update(Str: AllBytes.slice(Start: ASTBlockRange.first, End: ASTBlockRange.second));
1180 ASTFileSignature ASTBlockHash = ASTFileSignature::create(Bytes: Hasher.result());
1181
1182 // Add the remaining bytes:
1183 // 1. Before the unhashed control block.
1184 Hasher.update(Str: AllBytes.slice(Start: 0, End: UnhashedControlBlockRange.first));
1185 // 2. Between the unhashed control block and the AST block.
1186 Hasher.update(
1187 Str: AllBytes.slice(Start: UnhashedControlBlockRange.second, End: ASTBlockRange.first));
1188 // 3. After the AST block.
1189 Hasher.update(Str: AllBytes.slice(Start: ASTBlockRange.second, End: StringRef::npos));
1190 ASTFileSignature Signature = ASTFileSignature::create(Bytes: Hasher.result());
1191
1192 return std::make_pair(x&: ASTBlockHash, y&: Signature);
1193}
1194
1195ASTFileSignature ASTWriter::createSignatureForNamedModule() const {
1196 llvm::SHA1 Hasher;
1197 Hasher.update(Str: StringRef(Buffer.data(), Buffer.size()));
1198
1199 assert(WritingModule);
1200 assert(WritingModule->isNamedModule());
1201
1202 // We need to combine all the export imported modules no matter
1203 // we used it or not.
1204 for (auto [ExportImported, _] : WritingModule->Exports)
1205 Hasher.update(Data: ExportImported->Signature);
1206
1207 // We combine all the used modules to make sure the signature is precise.
1208 // Consider the case like:
1209 //
1210 // // a.cppm
1211 // export module a;
1212 // export inline int a() { ... }
1213 //
1214 // // b.cppm
1215 // export module b;
1216 // import a;
1217 // export inline int b() { return a(); }
1218 //
1219 // Since both `a()` and `b()` are inline, we need to make sure the BMI of
1220 // `b.pcm` will change after the implementation of `a()` changes. We can't
1221 // get that naturally since we won't record the body of `a()` during the
1222 // writing process. We can't reuse ODRHash here since ODRHash won't calculate
1223 // the called function recursively. So ODRHash will be problematic if `a()`
1224 // calls other inline functions.
1225 //
1226 // Probably we can solve this by a new hash mechanism. But the safety and
1227 // efficiency may a problem too. Here we just combine the hash value of the
1228 // used modules conservatively.
1229 for (Module *M : TouchedTopLevelModules)
1230 Hasher.update(Data: M->Signature);
1231
1232 return ASTFileSignature::create(Bytes: Hasher.result());
1233}
1234
1235static void BackpatchSignatureAt(llvm::BitstreamWriter &Stream,
1236 const ASTFileSignature &S, uint64_t BitNo) {
1237 for (uint8_t Byte : S) {
1238 Stream.BackpatchByte(BitNo, NewByte: Byte);
1239 BitNo += 8;
1240 }
1241}
1242
1243ASTFileSignature ASTWriter::backpatchSignature() {
1244 if (isWritingStdCXXNamedModules()) {
1245 ASTFileSignature Signature = createSignatureForNamedModule();
1246 BackpatchSignatureAt(Stream, S: Signature, BitNo: SignatureOffset);
1247 return Signature;
1248 }
1249
1250 if (!WritingModule ||
1251 !PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent)
1252 return {};
1253
1254 // For implicit modules, write the hash of the PCM as its signature.
1255 ASTFileSignature ASTBlockHash;
1256 ASTFileSignature Signature;
1257 std::tie(args&: ASTBlockHash, args&: Signature) = createSignature();
1258
1259 BackpatchSignatureAt(Stream, S: ASTBlockHash, BitNo: ASTBlockHashOffset);
1260 BackpatchSignatureAt(Stream, S: Signature, BitNo: SignatureOffset);
1261
1262 return Signature;
1263}
1264
1265void ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1266 ASTContext &Context) {
1267 using namespace llvm;
1268
1269 // Flush first to prepare the PCM hash (signature).
1270 Stream.FlushToWord();
1271 UnhashedControlBlockRange.first = Stream.GetCurrentBitNo() >> 3;
1272
1273 // Enter the block and prepare to write records.
1274 RecordData Record;
1275 Stream.EnterSubblock(BlockID: UNHASHED_CONTROL_BLOCK_ID, CodeLen: 5);
1276
1277 // For implicit modules and C++20 named modules, write the hash of the PCM as
1278 // its signature.
1279 if (isWritingStdCXXNamedModules() ||
1280 (WritingModule &&
1281 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent)) {
1282 // At this point, we don't know the actual signature of the file or the AST
1283 // block - we're only able to compute those at the end of the serialization
1284 // process. Let's store dummy signatures for now, and replace them with the
1285 // real ones later on.
1286 // The bitstream VBR-encodes record elements, which makes backpatching them
1287 // really difficult. Let's store the signatures as blobs instead - they are
1288 // guaranteed to be word-aligned, and we control their format/encoding.
1289 auto Dummy = ASTFileSignature::createDummy();
1290 SmallString<128> Blob{Dummy.begin(), Dummy.end()};
1291
1292 // We don't need AST Block hash in named modules.
1293 if (!isWritingStdCXXNamedModules()) {
1294 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1295 Abbrev->Add(OpInfo: BitCodeAbbrevOp(AST_BLOCK_HASH));
1296 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1297 unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1298
1299 Record.push_back(Elt: AST_BLOCK_HASH);
1300 Stream.EmitRecordWithBlob(Abbrev: ASTBlockHashAbbrev, Vals: Record, Blob);
1301 ASTBlockHashOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1302 Record.clear();
1303 }
1304
1305 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1306 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SIGNATURE));
1307 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1308 unsigned SignatureAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1309
1310 Record.push_back(Elt: SIGNATURE);
1311 Stream.EmitRecordWithBlob(Abbrev: SignatureAbbrev, Vals: Record, Blob);
1312 SignatureOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1313 Record.clear();
1314 }
1315
1316 const auto &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1317
1318 // Diagnostic options.
1319 const auto &Diags = Context.getDiagnostics();
1320 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1321 if (!HSOpts.ModulesSkipDiagnosticOptions) {
1322#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1323#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1324 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1325#include "clang/Basic/DiagnosticOptions.def"
1326 Record.push_back(Elt: DiagOpts.Warnings.size());
1327 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1328 AddString(Str: DiagOpts.Warnings[I], Record);
1329 Record.push_back(Elt: DiagOpts.Remarks.size());
1330 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1331 AddString(Str: DiagOpts.Remarks[I], Record);
1332 // Note: we don't serialize the log or serialization file names, because
1333 // they are generally transient files and will almost always be overridden.
1334 Stream.EmitRecord(Code: DIAGNOSTIC_OPTIONS, Vals: Record);
1335 Record.clear();
1336 }
1337
1338 // Header search paths.
1339 if (!HSOpts.ModulesSkipHeaderSearchPaths) {
1340 // Include entries.
1341 Record.push_back(Elt: HSOpts.UserEntries.size());
1342 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1343 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1344 AddString(Str: Entry.Path, Record);
1345 Record.push_back(Elt: static_cast<unsigned>(Entry.Group));
1346 Record.push_back(Elt: Entry.IsFramework);
1347 Record.push_back(Elt: Entry.IgnoreSysRoot);
1348 }
1349
1350 // System header prefixes.
1351 Record.push_back(Elt: HSOpts.SystemHeaderPrefixes.size());
1352 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1353 AddString(Str: HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1354 Record.push_back(Elt: HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1355 }
1356
1357 // VFS overlay files.
1358 Record.push_back(Elt: HSOpts.VFSOverlayFiles.size());
1359 for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1360 AddString(Str: VFSOverlayFile, Record);
1361
1362 Stream.EmitRecord(Code: HEADER_SEARCH_PATHS, Vals: Record);
1363 }
1364
1365 if (!HSOpts.ModulesSkipPragmaDiagnosticMappings)
1366 WritePragmaDiagnosticMappings(Diag: Diags, /* isModule = */ WritingModule);
1367
1368 // Header search entry usage.
1369 {
1370 auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1371 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1372 Abbrev->Add(OpInfo: BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1373 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1374 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1375 unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1376 RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1377 HSEntryUsage.size()};
1378 Stream.EmitRecordWithBlob(Abbrev: HSUsageAbbrevCode, Vals: Record, Blob: bytes(V: HSEntryUsage));
1379 }
1380
1381 // VFS usage.
1382 {
1383 auto VFSUsage = PP.getHeaderSearchInfo().collectVFSUsageAndClear();
1384 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1385 Abbrev->Add(OpInfo: BitCodeAbbrevOp(VFS_USAGE));
1386 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1387 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1388 unsigned VFSUsageAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1389 RecordData::value_type Record[] = {VFS_USAGE, VFSUsage.size()};
1390 Stream.EmitRecordWithBlob(Abbrev: VFSUsageAbbrevCode, Vals: Record, Blob: bytes(V: VFSUsage));
1391 }
1392
1393 // Leave the options block.
1394 Stream.ExitBlock();
1395 UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1396}
1397
1398/// Write the control block.
1399void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1400 StringRef isysroot) {
1401 using namespace llvm;
1402
1403 Stream.EnterSubblock(BlockID: CONTROL_BLOCK_ID, CodeLen: 5);
1404 RecordData Record;
1405
1406 // Metadata
1407 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1408 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(METADATA));
1409 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1410 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1411 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1412 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1413 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1414 // Standard C++ module
1415 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1416 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1417 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1418 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1419 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(MetadataAbbrev));
1420 assert((!WritingModule || isysroot.empty()) &&
1421 "writing module as a relocatable PCH?");
1422 {
1423 RecordData::value_type Record[] = {METADATA,
1424 VERSION_MAJOR,
1425 VERSION_MINOR,
1426 CLANG_VERSION_MAJOR,
1427 CLANG_VERSION_MINOR,
1428 !isysroot.empty(),
1429 isWritingStdCXXNamedModules(),
1430 IncludeTimestamps,
1431 ASTHasCompilerErrors};
1432 Stream.EmitRecordWithBlob(Abbrev: MetadataAbbrevCode, Vals: Record,
1433 Blob: getClangFullRepositoryVersion());
1434 }
1435
1436 if (WritingModule) {
1437 // Module name
1438 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1439 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MODULE_NAME));
1440 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1441 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1442 RecordData::value_type Record[] = {MODULE_NAME};
1443 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob: WritingModule->Name);
1444 }
1445
1446 if (WritingModule && WritingModule->Directory) {
1447 SmallString<128> BaseDir;
1448 if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1449 // Use the current working directory as the base path for all inputs.
1450 auto CWD =
1451 Context.getSourceManager().getFileManager().getOptionalDirectoryRef(
1452 DirName: ".");
1453 BaseDir.assign(RHS: CWD->getName());
1454 } else {
1455 BaseDir.assign(RHS: WritingModule->Directory->getName());
1456 }
1457 cleanPathForOutput(FileMgr&: Context.getSourceManager().getFileManager(), Path&: BaseDir);
1458
1459 // If the home of the module is the current working directory, then we
1460 // want to pick up the cwd of the build process loading the module, not
1461 // our cwd, when we load this module.
1462 if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1463 (!PP.getHeaderSearchInfo()
1464 .getHeaderSearchOpts()
1465 .ModuleMapFileHomeIsCwd ||
1466 WritingModule->Directory->getName() != ".")) {
1467 // Module directory.
1468 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1469 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MODULE_DIRECTORY));
1470 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1471 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1472
1473 RecordData::value_type Record[] = {MODULE_DIRECTORY};
1474 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob: BaseDir);
1475 }
1476
1477 // Write out all other paths relative to the base directory if possible.
1478 BaseDirectory.assign(first: BaseDir.begin(), last: BaseDir.end());
1479 } else if (!isysroot.empty()) {
1480 // Write out paths relative to the sysroot if possible.
1481 BaseDirectory = std::string(isysroot);
1482 }
1483
1484 // Module map file
1485 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1486 Record.clear();
1487
1488 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1489 AddPath(Path: WritingModule->PresumedModuleMapFile.empty()
1490 ? Map.getModuleMapFileForUniquing(M: WritingModule)
1491 ->getNameAsRequested()
1492 : StringRef(WritingModule->PresumedModuleMapFile),
1493 Record);
1494
1495 // Additional module map files.
1496 if (auto *AdditionalModMaps =
1497 Map.getAdditionalModuleMapFiles(M: WritingModule)) {
1498 Record.push_back(Elt: AdditionalModMaps->size());
1499 SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1500 AdditionalModMaps->end());
1501 llvm::sort(C&: ModMaps, Comp: [](FileEntryRef A, FileEntryRef B) {
1502 return A.getName() < B.getName();
1503 });
1504 for (FileEntryRef F : ModMaps)
1505 AddPath(Path: F.getName(), Record);
1506 } else {
1507 Record.push_back(Elt: 0);
1508 }
1509
1510 Stream.EmitRecord(Code: MODULE_MAP_FILE, Vals: Record);
1511 }
1512
1513 // Imports
1514 if (Chain) {
1515 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1516 Record.clear();
1517
1518 for (ModuleFile &M : Mgr) {
1519 // Skip modules that weren't directly imported.
1520 if (!M.isDirectlyImported())
1521 continue;
1522
1523 Record.push_back(Elt: (unsigned)M.Kind); // FIXME: Stable encoding
1524 Record.push_back(Elt: M.StandardCXXModule);
1525 AddSourceLocation(Loc: M.ImportLoc, Record);
1526
1527 // We don't want to hard code the information about imported modules
1528 // in the C++20 named modules.
1529 if (!M.StandardCXXModule) {
1530 // If we have calculated signature, there is no need to store
1531 // the size or timestamp.
1532 Record.push_back(Elt: M.Signature ? 0 : M.File.getSize());
1533 Record.push_back(Elt: M.Signature ? 0 : getTimestampForOutput(E: M.File));
1534 llvm::append_range(C&: Record, R&: M.Signature);
1535 }
1536
1537 AddString(Str: M.ModuleName, Record);
1538
1539 if (!M.StandardCXXModule)
1540 AddPath(Path: M.FileName, Record);
1541 }
1542 Stream.EmitRecord(Code: IMPORTS, Vals: Record);
1543 }
1544
1545 // Write the options block.
1546 Stream.EnterSubblock(BlockID: OPTIONS_BLOCK_ID, CodeLen: 4);
1547
1548 // Language options.
1549 Record.clear();
1550 const LangOptions &LangOpts = Context.getLangOpts();
1551#define LANGOPT(Name, Bits, Default, Description) \
1552 Record.push_back(LangOpts.Name);
1553#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1554 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1555#include "clang/Basic/LangOptions.def"
1556#define SANITIZER(NAME, ID) \
1557 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1558#include "clang/Basic/Sanitizers.def"
1559
1560 Record.push_back(Elt: LangOpts.ModuleFeatures.size());
1561 for (StringRef Feature : LangOpts.ModuleFeatures)
1562 AddString(Str: Feature, Record);
1563
1564 Record.push_back(Elt: (unsigned) LangOpts.ObjCRuntime.getKind());
1565 AddVersionTuple(Version: LangOpts.ObjCRuntime.getVersion(), Record);
1566
1567 AddString(Str: LangOpts.CurrentModule, Record);
1568
1569 // Comment options.
1570 Record.push_back(Elt: LangOpts.CommentOpts.BlockCommandNames.size());
1571 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1572 AddString(Str: I, Record);
1573 }
1574 Record.push_back(Elt: LangOpts.CommentOpts.ParseAllComments);
1575
1576 // OpenMP offloading options.
1577 Record.push_back(Elt: LangOpts.OMPTargetTriples.size());
1578 for (auto &T : LangOpts.OMPTargetTriples)
1579 AddString(Str: T.getTriple(), Record);
1580
1581 AddString(Str: LangOpts.OMPHostIRFile, Record);
1582
1583 Stream.EmitRecord(Code: LANGUAGE_OPTIONS, Vals: Record);
1584
1585 // Target options.
1586 Record.clear();
1587 const TargetInfo &Target = Context.getTargetInfo();
1588 const TargetOptions &TargetOpts = Target.getTargetOpts();
1589 AddString(Str: TargetOpts.Triple, Record);
1590 AddString(Str: TargetOpts.CPU, Record);
1591 AddString(Str: TargetOpts.TuneCPU, Record);
1592 AddString(Str: TargetOpts.ABI, Record);
1593 Record.push_back(Elt: TargetOpts.FeaturesAsWritten.size());
1594 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1595 AddString(Str: TargetOpts.FeaturesAsWritten[I], Record);
1596 }
1597 Record.push_back(Elt: TargetOpts.Features.size());
1598 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1599 AddString(Str: TargetOpts.Features[I], Record);
1600 }
1601 Stream.EmitRecord(Code: TARGET_OPTIONS, Vals: Record);
1602
1603 // File system options.
1604 Record.clear();
1605 const FileSystemOptions &FSOpts =
1606 Context.getSourceManager().getFileManager().getFileSystemOpts();
1607 AddString(Str: FSOpts.WorkingDir, Record);
1608 Stream.EmitRecord(Code: FILE_SYSTEM_OPTIONS, Vals: Record);
1609
1610 // Header search options.
1611 Record.clear();
1612 const HeaderSearchOptions &HSOpts =
1613 PP.getHeaderSearchInfo().getHeaderSearchOpts();
1614
1615 AddString(Str: HSOpts.Sysroot, Record);
1616 AddString(Str: HSOpts.ResourceDir, Record);
1617 AddString(Str: HSOpts.ModuleCachePath, Record);
1618 AddString(Str: HSOpts.ModuleUserBuildPath, Record);
1619 Record.push_back(Elt: HSOpts.DisableModuleHash);
1620 Record.push_back(Elt: HSOpts.ImplicitModuleMaps);
1621 Record.push_back(Elt: HSOpts.ModuleMapFileHomeIsCwd);
1622 Record.push_back(Elt: HSOpts.EnablePrebuiltImplicitModules);
1623 Record.push_back(Elt: HSOpts.UseBuiltinIncludes);
1624 Record.push_back(Elt: HSOpts.UseStandardSystemIncludes);
1625 Record.push_back(Elt: HSOpts.UseStandardCXXIncludes);
1626 Record.push_back(Elt: HSOpts.UseLibcxx);
1627 // Write out the specific module cache path that contains the module files.
1628 AddString(Str: PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1629 Stream.EmitRecord(Code: HEADER_SEARCH_OPTIONS, Vals: Record);
1630
1631 // Preprocessor options.
1632 Record.clear();
1633 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1634
1635 // If we're building an implicit module with a context hash, the importer is
1636 // guaranteed to have the same macros defined on the command line. Skip
1637 // writing them.
1638 bool SkipMacros = BuildingImplicitModule && !HSOpts.DisableModuleHash;
1639 bool WriteMacros = !SkipMacros;
1640 Record.push_back(Elt: WriteMacros);
1641 if (WriteMacros) {
1642 // Macro definitions.
1643 Record.push_back(Elt: PPOpts.Macros.size());
1644 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1645 AddString(Str: PPOpts.Macros[I].first, Record);
1646 Record.push_back(Elt: PPOpts.Macros[I].second);
1647 }
1648 }
1649
1650 // Includes
1651 Record.push_back(Elt: PPOpts.Includes.size());
1652 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1653 AddString(Str: PPOpts.Includes[I], Record);
1654
1655 // Macro includes
1656 Record.push_back(Elt: PPOpts.MacroIncludes.size());
1657 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1658 AddString(Str: PPOpts.MacroIncludes[I], Record);
1659
1660 Record.push_back(Elt: PPOpts.UsePredefines);
1661 // Detailed record is important since it is used for the module cache hash.
1662 Record.push_back(Elt: PPOpts.DetailedRecord);
1663 AddString(Str: PPOpts.ImplicitPCHInclude, Record);
1664 Record.push_back(Elt: static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1665 Stream.EmitRecord(Code: PREPROCESSOR_OPTIONS, Vals: Record);
1666
1667 // Leave the options block.
1668 Stream.ExitBlock();
1669
1670 // Original file name and file ID
1671 SourceManager &SM = Context.getSourceManager();
1672 if (auto MainFile = SM.getFileEntryRefForID(FID: SM.getMainFileID())) {
1673 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1674 FileAbbrev->Add(OpInfo: BitCodeAbbrevOp(ORIGINAL_FILE));
1675 FileAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1676 FileAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1677 unsigned FileAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(FileAbbrev));
1678
1679 Record.clear();
1680 Record.push_back(Elt: ORIGINAL_FILE);
1681 AddFileID(FID: SM.getMainFileID(), Record);
1682 EmitRecordWithPath(Abbrev: FileAbbrevCode, Record, Path: MainFile->getName());
1683 }
1684
1685 Record.clear();
1686 AddFileID(FID: SM.getMainFileID(), Record);
1687 Stream.EmitRecord(Code: ORIGINAL_FILE_ID, Vals: Record);
1688
1689 WriteInputFiles(SourceMgr&: Context.SourceMgr,
1690 HSOpts&: PP.getHeaderSearchInfo().getHeaderSearchOpts());
1691 Stream.ExitBlock();
1692}
1693
1694namespace {
1695
1696/// An input file.
1697struct InputFileEntry {
1698 FileEntryRef File;
1699 bool IsSystemFile;
1700 bool IsTransient;
1701 bool BufferOverridden;
1702 bool IsTopLevel;
1703 bool IsModuleMap;
1704 uint32_t ContentHash[2];
1705
1706 InputFileEntry(FileEntryRef File) : File(File) {}
1707};
1708
1709} // namespace
1710
1711SourceLocation ASTWriter::getAffectingIncludeLoc(const SourceManager &SourceMgr,
1712 const SrcMgr::FileInfo &File) {
1713 SourceLocation IncludeLoc = File.getIncludeLoc();
1714 if (IncludeLoc.isValid()) {
1715 FileID IncludeFID = SourceMgr.getFileID(SpellingLoc: IncludeLoc);
1716 assert(IncludeFID.isValid() && "IncludeLoc in invalid file");
1717 if (!IsSLocAffecting[IncludeFID.ID])
1718 IncludeLoc = SourceLocation();
1719 }
1720 return IncludeLoc;
1721}
1722
1723void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1724 HeaderSearchOptions &HSOpts) {
1725 using namespace llvm;
1726
1727 Stream.EnterSubblock(BlockID: INPUT_FILES_BLOCK_ID, CodeLen: 4);
1728
1729 // Create input-file abbreviation.
1730 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1731 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(INPUT_FILE));
1732 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1733 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1734 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1735 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1736 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1737 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1738 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1739 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1740 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1741 unsigned IFAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(IFAbbrev));
1742
1743 // Create input file hash abbreviation.
1744 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1745 IFHAbbrev->Add(OpInfo: BitCodeAbbrevOp(INPUT_FILE_HASH));
1746 IFHAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1747 IFHAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1748 unsigned IFHAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(IFHAbbrev));
1749
1750 uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1751
1752 // Get all ContentCache objects for files.
1753 std::vector<InputFileEntry> UserFiles;
1754 std::vector<InputFileEntry> SystemFiles;
1755 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1756 // Get this source location entry.
1757 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(Index: I);
1758 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1759
1760 // We only care about file entries that were not overridden.
1761 if (!SLoc->isFile())
1762 continue;
1763 const SrcMgr::FileInfo &File = SLoc->getFile();
1764 const SrcMgr::ContentCache *Cache = &File.getContentCache();
1765 if (!Cache->OrigEntry)
1766 continue;
1767
1768 // Do not emit input files that do not affect current module.
1769 if (!IsSLocAffecting[I])
1770 continue;
1771
1772 InputFileEntry Entry(*Cache->OrigEntry);
1773 Entry.IsSystemFile = isSystem(CK: File.getFileCharacteristic());
1774 Entry.IsTransient = Cache->IsTransient;
1775 Entry.BufferOverridden = Cache->BufferOverridden;
1776 Entry.IsTopLevel = getAffectingIncludeLoc(SourceMgr, File).isInvalid();
1777 Entry.IsModuleMap = isModuleMap(CK: File.getFileCharacteristic());
1778
1779 uint64_t ContentHash = 0;
1780 if (PP->getHeaderSearchInfo()
1781 .getHeaderSearchOpts()
1782 .ValidateASTInputFilesContent) {
1783 auto MemBuff = Cache->getBufferIfLoaded();
1784 if (MemBuff)
1785 ContentHash = xxh3_64bits(data: MemBuff->getBuffer());
1786 else
1787 PP->Diag(Loc: SourceLocation(), DiagID: diag::err_module_unable_to_hash_content)
1788 << Entry.File.getName();
1789 }
1790 Entry.ContentHash[0] = uint32_t(ContentHash);
1791 Entry.ContentHash[1] = uint32_t(ContentHash >> 32);
1792 if (Entry.IsSystemFile)
1793 SystemFiles.push_back(x: Entry);
1794 else
1795 UserFiles.push_back(x: Entry);
1796 }
1797
1798 // User files go at the front, system files at the back.
1799 auto SortedFiles = llvm::concat<InputFileEntry>(Ranges: std::move(UserFiles),
1800 Ranges: std::move(SystemFiles));
1801
1802 unsigned UserFilesNum = 0;
1803 // Write out all of the input files.
1804 std::vector<uint64_t> InputFileOffsets;
1805 for (const auto &Entry : SortedFiles) {
1806 uint32_t &InputFileID = InputFileIDs[Entry.File];
1807 if (InputFileID != 0)
1808 continue; // already recorded this file.
1809
1810 // Record this entry's offset.
1811 InputFileOffsets.push_back(x: Stream.GetCurrentBitNo() - InputFilesOffsetBase);
1812
1813 InputFileID = InputFileOffsets.size();
1814
1815 if (!Entry.IsSystemFile)
1816 ++UserFilesNum;
1817
1818 // Emit size/modification time for this file.
1819 // And whether this file was overridden.
1820 {
1821 SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1822 SmallString<128> Name = Entry.File.getName();
1823
1824 PreparePathForOutput(Path&: NameAsRequested);
1825 PreparePathForOutput(Path&: Name);
1826
1827 if (Name == NameAsRequested)
1828 Name.clear();
1829
1830 RecordData::value_type Record[] = {
1831 INPUT_FILE,
1832 InputFileOffsets.size(),
1833 (uint64_t)Entry.File.getSize(),
1834 (uint64_t)getTimestampForOutput(E: Entry.File),
1835 Entry.BufferOverridden,
1836 Entry.IsTransient,
1837 Entry.IsTopLevel,
1838 Entry.IsModuleMap,
1839 NameAsRequested.size()};
1840
1841 Stream.EmitRecordWithBlob(Abbrev: IFAbbrevCode, Vals: Record,
1842 Blob: (NameAsRequested + Name).str());
1843 }
1844
1845 // Emit content hash for this file.
1846 {
1847 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1848 Entry.ContentHash[1]};
1849 Stream.EmitRecordWithAbbrev(Abbrev: IFHAbbrevCode, Vals: Record);
1850 }
1851 }
1852
1853 Stream.ExitBlock();
1854
1855 // Create input file offsets abbreviation.
1856 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1857 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1858 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1859 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1860 // input files
1861 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1862 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(OffsetsAbbrev));
1863
1864 // Write input file offsets.
1865 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1866 InputFileOffsets.size(), UserFilesNum};
1867 Stream.EmitRecordWithBlob(Abbrev: OffsetsAbbrevCode, Vals: Record, Blob: bytes(v: InputFileOffsets));
1868}
1869
1870//===----------------------------------------------------------------------===//
1871// Source Manager Serialization
1872//===----------------------------------------------------------------------===//
1873
1874/// Create an abbreviation for the SLocEntry that refers to a
1875/// file.
1876static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1877 using namespace llvm;
1878
1879 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1880 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1881 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1882 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1883 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1884 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1885 // FileEntry fields.
1886 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1887 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1888 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1889 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1890 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1891}
1892
1893/// Create an abbreviation for the SLocEntry that refers to a
1894/// buffer.
1895static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1896 using namespace llvm;
1897
1898 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1899 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1900 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1901 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1902 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1903 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1904 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1905 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1906}
1907
1908/// Create an abbreviation for the SLocEntry that refers to a
1909/// buffer's blob.
1910static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1911 bool Compressed) {
1912 using namespace llvm;
1913
1914 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1915 Abbrev->Add(OpInfo: BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1916 : SM_SLOC_BUFFER_BLOB));
1917 if (Compressed)
1918 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1919 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1920 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1921}
1922
1923/// Create an abbreviation for the SLocEntry that refers to a macro
1924/// expansion.
1925static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1926 using namespace llvm;
1927
1928 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1929 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1930 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1931 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1932 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1933 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1934 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1935 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1936 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1937}
1938
1939/// Emit key length and data length as ULEB-encoded data, and return them as a
1940/// pair.
1941static std::pair<unsigned, unsigned>
1942emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1943 llvm::encodeULEB128(Value: KeyLen, OS&: Out);
1944 llvm::encodeULEB128(Value: DataLen, OS&: Out);
1945 return std::make_pair(x&: KeyLen, y&: DataLen);
1946}
1947
1948namespace {
1949
1950 // Trait used for the on-disk hash table of header search information.
1951 class HeaderFileInfoTrait {
1952 ASTWriter &Writer;
1953
1954 // Keep track of the framework names we've used during serialization.
1955 SmallString<128> FrameworkStringData;
1956 llvm::StringMap<unsigned> FrameworkNameOffset;
1957
1958 public:
1959 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1960
1961 struct key_type {
1962 StringRef Filename;
1963 off_t Size;
1964 time_t ModTime;
1965 };
1966 using key_type_ref = const key_type &;
1967
1968 using UnresolvedModule =
1969 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1970
1971 struct data_type {
1972 data_type(const HeaderFileInfo &HFI, bool AlreadyIncluded,
1973 ArrayRef<ModuleMap::KnownHeader> KnownHeaders,
1974 UnresolvedModule Unresolved)
1975 : HFI(HFI), AlreadyIncluded(AlreadyIncluded),
1976 KnownHeaders(KnownHeaders), Unresolved(Unresolved) {}
1977
1978 HeaderFileInfo HFI;
1979 bool AlreadyIncluded;
1980 SmallVector<ModuleMap::KnownHeader, 1> KnownHeaders;
1981 UnresolvedModule Unresolved;
1982 };
1983 using data_type_ref = const data_type &;
1984
1985 using hash_value_type = unsigned;
1986 using offset_type = unsigned;
1987
1988 hash_value_type ComputeHash(key_type_ref key) {
1989 // The hash is based only on size/time of the file, so that the reader can
1990 // match even when symlinking or excess path elements ("foo/../", "../")
1991 // change the form of the name. However, complete path is still the key.
1992 uint8_t buf[sizeof(key.Size) + sizeof(key.ModTime)];
1993 memcpy(dest: buf, src: &key.Size, n: sizeof(key.Size));
1994 memcpy(dest: buf + sizeof(key.Size), src: &key.ModTime, n: sizeof(key.ModTime));
1995 return llvm::xxh3_64bits(data: buf);
1996 }
1997
1998 std::pair<unsigned, unsigned>
1999 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
2000 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
2001 unsigned DataLen = 1 + sizeof(IdentifierID) + 4;
2002 for (auto ModInfo : Data.KnownHeaders)
2003 if (Writer.getLocalOrImportedSubmoduleID(Mod: ModInfo.getModule()))
2004 DataLen += 4;
2005 if (Data.Unresolved.getPointer())
2006 DataLen += 4;
2007 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
2008 }
2009
2010 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
2011 using namespace llvm::support;
2012
2013 endian::Writer LE(Out, llvm::endianness::little);
2014 LE.write<uint64_t>(Val: key.Size);
2015 KeyLen -= 8;
2016 LE.write<uint64_t>(Val: key.ModTime);
2017 KeyLen -= 8;
2018 Out.write(Ptr: key.Filename.data(), Size: KeyLen);
2019 }
2020
2021 void EmitData(raw_ostream &Out, key_type_ref key,
2022 data_type_ref Data, unsigned DataLen) {
2023 using namespace llvm::support;
2024
2025 endian::Writer LE(Out, llvm::endianness::little);
2026 uint64_t Start = Out.tell(); (void)Start;
2027
2028 unsigned char Flags = (Data.AlreadyIncluded << 6)
2029 | (Data.HFI.isImport << 5)
2030 | (Writer.isWritingStdCXXNamedModules() ? 0 :
2031 Data.HFI.isPragmaOnce << 4)
2032 | (Data.HFI.DirInfo << 1)
2033 | Data.HFI.IndexHeaderMapHeader;
2034 LE.write<uint8_t>(Val: Flags);
2035
2036 if (Data.HFI.LazyControllingMacro.isID())
2037 LE.write<IdentifierID>(Val: Data.HFI.LazyControllingMacro.getID());
2038 else
2039 LE.write<IdentifierID>(
2040 Val: Writer.getIdentifierRef(II: Data.HFI.LazyControllingMacro.getPtr()));
2041
2042 unsigned Offset = 0;
2043 if (!Data.HFI.Framework.empty()) {
2044 // If this header refers into a framework, save the framework name.
2045 llvm::StringMap<unsigned>::iterator Pos
2046 = FrameworkNameOffset.find(Key: Data.HFI.Framework);
2047 if (Pos == FrameworkNameOffset.end()) {
2048 Offset = FrameworkStringData.size() + 1;
2049 FrameworkStringData.append(RHS: Data.HFI.Framework);
2050 FrameworkStringData.push_back(Elt: 0);
2051
2052 FrameworkNameOffset[Data.HFI.Framework] = Offset;
2053 } else
2054 Offset = Pos->second;
2055 }
2056 LE.write<uint32_t>(Val: Offset);
2057
2058 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2059 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(Mod: M)) {
2060 uint32_t Value = (ModID << 3) | (unsigned)Role;
2061 assert((Value >> 3) == ModID && "overflow in header module info");
2062 LE.write<uint32_t>(Val: Value);
2063 }
2064 };
2065
2066 for (auto ModInfo : Data.KnownHeaders)
2067 EmitModule(ModInfo.getModule(), ModInfo.getRole());
2068 if (Data.Unresolved.getPointer())
2069 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
2070
2071 assert(Out.tell() - Start == DataLen && "Wrong data length");
2072 }
2073
2074 const char *strings_begin() const { return FrameworkStringData.begin(); }
2075 const char *strings_end() const { return FrameworkStringData.end(); }
2076 };
2077
2078} // namespace
2079
2080/// Write the header search block for the list of files that
2081///
2082/// \param HS The header search structure to save.
2083void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
2084 HeaderFileInfoTrait GeneratorTrait(*this);
2085 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2086 SmallVector<const char *, 4> SavedStrings;
2087 unsigned NumHeaderSearchEntries = 0;
2088
2089 // Find all unresolved headers for the current module. We generally will
2090 // have resolved them before we get here, but not necessarily: we might be
2091 // compiling a preprocessed module, where there is no requirement for the
2092 // original files to exist any more.
2093 const HeaderFileInfo Empty; // So we can take a reference.
2094 if (WritingModule) {
2095 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2096 while (!Worklist.empty()) {
2097 Module *M = Worklist.pop_back_val();
2098 // We don't care about headers in unimportable submodules.
2099 if (M->isUnimportable())
2100 continue;
2101
2102 // Map to disk files where possible, to pick up any missing stat
2103 // information. This also means we don't need to check the unresolved
2104 // headers list when emitting resolved headers in the first loop below.
2105 // FIXME: It'd be preferable to avoid doing this if we were given
2106 // sufficient stat information in the module map.
2107 HS.getModuleMap().resolveHeaderDirectives(Mod: M, /*File=*/std::nullopt);
2108
2109 // If the file didn't exist, we can still create a module if we were given
2110 // enough information in the module map.
2111 for (const auto &U : M->MissingHeaders) {
2112 // Check that we were given enough information to build a module
2113 // without this file existing on disk.
2114 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2115 PP->Diag(Loc: U.FileNameLoc, DiagID: diag::err_module_no_size_mtime_for_header)
2116 << WritingModule->getFullModuleName() << U.Size.has_value()
2117 << U.FileName;
2118 continue;
2119 }
2120
2121 // Form the effective relative pathname for the file.
2122 SmallString<128> Filename(M->Directory->getName());
2123 llvm::sys::path::append(path&: Filename, a: U.FileName);
2124 PreparePathForOutput(Path&: Filename);
2125
2126 StringRef FilenameDup = strdup(s: Filename.c_str());
2127 SavedStrings.push_back(Elt: FilenameDup.data());
2128
2129 HeaderFileInfoTrait::key_type Key = {
2130 .Filename: FilenameDup, .Size: *U.Size, .ModTime: IncludeTimestamps ? *U.ModTime : 0};
2131 HeaderFileInfoTrait::data_type Data = {
2132 Empty, false, {}, {M, ModuleMap::headerKindToRole(Kind: U.Kind)}};
2133 // FIXME: Deal with cases where there are multiple unresolved header
2134 // directives in different submodules for the same header.
2135 Generator.insert(Key, Data, InfoObj&: GeneratorTrait);
2136 ++NumHeaderSearchEntries;
2137 }
2138 auto SubmodulesRange = M->submodules();
2139 Worklist.append(in_start: SubmodulesRange.begin(), in_end: SubmodulesRange.end());
2140 }
2141 }
2142
2143 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2144 HS.getFileMgr().GetUniqueIDMapping(UIDToFiles&: FilesByUID);
2145
2146 if (FilesByUID.size() > HS.header_file_size())
2147 FilesByUID.resize(N: HS.header_file_size());
2148
2149 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2150 OptionalFileEntryRef File = FilesByUID[UID];
2151 if (!File)
2152 continue;
2153
2154 const HeaderFileInfo *HFI = HS.getExistingLocalFileInfo(FE: *File);
2155 if (!HFI)
2156 continue; // We have no information on this being a header file.
2157 if (!HFI->isCompilingModuleHeader && HFI->isModuleHeader)
2158 continue; // Header file info is tracked by the owning module file.
2159 if (!HFI->isCompilingModuleHeader && !PP->alreadyIncluded(File: *File))
2160 continue; // Non-modular header not included is not needed.
2161
2162 // Massage the file path into an appropriate form.
2163 StringRef Filename = File->getName();
2164 SmallString<128> FilenameTmp(Filename);
2165 if (PreparePathForOutput(Path&: FilenameTmp)) {
2166 // If we performed any translation on the file name at all, we need to
2167 // save this string, since the generator will refer to it later.
2168 Filename = StringRef(strdup(s: FilenameTmp.c_str()));
2169 SavedStrings.push_back(Elt: Filename.data());
2170 }
2171
2172 bool Included = PP->alreadyIncluded(File: *File);
2173
2174 HeaderFileInfoTrait::key_type Key = {
2175 .Filename: Filename, .Size: File->getSize(), .ModTime: getTimestampForOutput(E: *File)
2176 };
2177 HeaderFileInfoTrait::data_type Data = {
2178 *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(File: *File), {}
2179 };
2180 Generator.insert(Key, Data, InfoObj&: GeneratorTrait);
2181 ++NumHeaderSearchEntries;
2182 }
2183
2184 // Create the on-disk hash table in a buffer.
2185 SmallString<4096> TableData;
2186 uint32_t BucketOffset;
2187 {
2188 using namespace llvm::support;
2189
2190 llvm::raw_svector_ostream Out(TableData);
2191 // Make sure that no bucket is at offset 0
2192 endian::write<uint32_t>(os&: Out, value: 0, endian: llvm::endianness::little);
2193 BucketOffset = Generator.Emit(Out, InfoObj&: GeneratorTrait);
2194 }
2195
2196 // Create a blob abbreviation
2197 using namespace llvm;
2198
2199 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2200 Abbrev->Add(OpInfo: BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2201 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2202 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2203 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2204 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2205 unsigned TableAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2206
2207 // Write the header search table
2208 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2209 NumHeaderSearchEntries, TableData.size()};
2210 TableData.append(in_start: GeneratorTrait.strings_begin(),in_end: GeneratorTrait.strings_end());
2211 Stream.EmitRecordWithBlob(Abbrev: TableAbbrev, Vals: Record, Blob: TableData);
2212
2213 // Free all of the strings we had to duplicate.
2214 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
2215 free(ptr: const_cast<char *>(SavedStrings[I]));
2216}
2217
2218static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2219 unsigned SLocBufferBlobCompressedAbbrv,
2220 unsigned SLocBufferBlobAbbrv) {
2221 using RecordDataType = ASTWriter::RecordData::value_type;
2222
2223 // Compress the buffer if possible. We expect that almost all PCM
2224 // consumers will not want its contents.
2225 SmallVector<uint8_t, 0> CompressedBuffer;
2226 if (llvm::compression::zstd::isAvailable()) {
2227 llvm::compression::zstd::compress(
2228 Input: llvm::arrayRefFromStringRef(Input: Blob.drop_back(N: 1)), CompressedBuffer, Level: 9);
2229 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2230 Stream.EmitRecordWithBlob(Abbrev: SLocBufferBlobCompressedAbbrv, Vals: Record,
2231 Blob: llvm::toStringRef(Input: CompressedBuffer));
2232 return;
2233 }
2234 if (llvm::compression::zlib::isAvailable()) {
2235 llvm::compression::zlib::compress(
2236 Input: llvm::arrayRefFromStringRef(Input: Blob.drop_back(N: 1)), CompressedBuffer);
2237 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2238 Stream.EmitRecordWithBlob(Abbrev: SLocBufferBlobCompressedAbbrv, Vals: Record,
2239 Blob: llvm::toStringRef(Input: CompressedBuffer));
2240 return;
2241 }
2242
2243 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2244 Stream.EmitRecordWithBlob(Abbrev: SLocBufferBlobAbbrv, Vals: Record, Blob);
2245}
2246
2247/// Writes the block containing the serialized form of the
2248/// source manager.
2249///
2250/// TODO: We should probably use an on-disk hash table (stored in a
2251/// blob), indexed based on the file name, so that we only create
2252/// entries for files that we actually need. In the common case (no
2253/// errors), we probably won't have to create file entries for any of
2254/// the files in the AST.
2255void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2256 const Preprocessor &PP) {
2257 RecordData Record;
2258
2259 // Enter the source manager block.
2260 Stream.EnterSubblock(BlockID: SOURCE_MANAGER_BLOCK_ID, CodeLen: 4);
2261 const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2262
2263 // Abbreviations for the various kinds of source-location entries.
2264 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2265 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2266 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, Compressed: false);
2267 unsigned SLocBufferBlobCompressedAbbrv =
2268 CreateSLocBufferBlobAbbrev(Stream, Compressed: true);
2269 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2270
2271 // Write out the source location entry table. We skip the first
2272 // entry, which is always the same dummy entry.
2273 std::vector<uint32_t> SLocEntryOffsets;
2274 uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2275 SLocEntryOffsets.reserve(n: SourceMgr.local_sloc_entry_size() - 1);
2276 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2277 I != N; ++I) {
2278 // Get this source location entry.
2279 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(Index: I);
2280 FileID FID = FileID::get(V: I);
2281 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2282
2283 // Record the offset of this source-location entry.
2284 uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2285 assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2286
2287 // Figure out which record code to use.
2288 unsigned Code;
2289 if (SLoc->isFile()) {
2290 const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2291 if (Cache->OrigEntry) {
2292 Code = SM_SLOC_FILE_ENTRY;
2293 } else
2294 Code = SM_SLOC_BUFFER_ENTRY;
2295 } else
2296 Code = SM_SLOC_EXPANSION_ENTRY;
2297 Record.clear();
2298 Record.push_back(Elt: Code);
2299
2300 if (SLoc->isFile()) {
2301 const SrcMgr::FileInfo &File = SLoc->getFile();
2302 const SrcMgr::ContentCache *Content = &File.getContentCache();
2303 // Do not emit files that were not listed as inputs.
2304 if (!IsSLocAffecting[I])
2305 continue;
2306 SLocEntryOffsets.push_back(x: Offset);
2307 // Starting offset of this entry within this module, so skip the dummy.
2308 Record.push_back(Elt: getAdjustedOffset(Offset: SLoc->getOffset()) - 2);
2309 AddSourceLocation(Loc: getAffectingIncludeLoc(SourceMgr, File), Record);
2310 Record.push_back(Elt: File.getFileCharacteristic()); // FIXME: stable encoding
2311 Record.push_back(Elt: File.hasLineDirectives());
2312
2313 bool EmitBlob = false;
2314 if (Content->OrigEntry) {
2315 assert(Content->OrigEntry == Content->ContentsEntry &&
2316 "Writing to AST an overridden file is not supported");
2317
2318 // The source location entry is a file. Emit input file ID.
2319 assert(InputFileIDs[*Content->OrigEntry] != 0 && "Missed file entry");
2320 Record.push_back(Elt: InputFileIDs[*Content->OrigEntry]);
2321
2322 Record.push_back(Elt: getAdjustedNumCreatedFIDs(FID));
2323
2324 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(Val: FID);
2325 if (FDI != FileDeclIDs.end()) {
2326 Record.push_back(Elt: FDI->second->FirstDeclIndex);
2327 Record.push_back(Elt: FDI->second->DeclIDs.size());
2328 } else {
2329 Record.push_back(Elt: 0);
2330 Record.push_back(Elt: 0);
2331 }
2332
2333 Stream.EmitRecordWithAbbrev(Abbrev: SLocFileAbbrv, Vals: Record);
2334
2335 if (Content->BufferOverridden || Content->IsTransient)
2336 EmitBlob = true;
2337 } else {
2338 // The source location entry is a buffer. The blob associated
2339 // with this entry contains the contents of the buffer.
2340
2341 // We add one to the size so that we capture the trailing NULL
2342 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2343 // the reader side).
2344 std::optional<llvm::MemoryBufferRef> Buffer =
2345 Content->getBufferOrNone(Diag&: PP.getDiagnostics(), FM&: PP.getFileManager());
2346 StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2347 Stream.EmitRecordWithBlob(Abbrev: SLocBufferAbbrv, Vals: Record,
2348 Blob: StringRef(Name.data(), Name.size() + 1));
2349 EmitBlob = true;
2350 }
2351
2352 if (EmitBlob) {
2353 // Include the implicit terminating null character in the on-disk buffer
2354 // if we're writing it uncompressed.
2355 std::optional<llvm::MemoryBufferRef> Buffer =
2356 Content->getBufferOrNone(Diag&: PP.getDiagnostics(), FM&: PP.getFileManager());
2357 if (!Buffer)
2358 Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2359 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2360 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2361 SLocBufferBlobAbbrv);
2362 }
2363 } else {
2364 // The source location entry is a macro expansion.
2365 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2366 SLocEntryOffsets.push_back(x: Offset);
2367 // Starting offset of this entry within this module, so skip the dummy.
2368 Record.push_back(Elt: getAdjustedOffset(Offset: SLoc->getOffset()) - 2);
2369 LocSeq::State Seq;
2370 AddSourceLocation(Loc: Expansion.getSpellingLoc(), Record, Seq);
2371 AddSourceLocation(Loc: Expansion.getExpansionLocStart(), Record, Seq);
2372 AddSourceLocation(Loc: Expansion.isMacroArgExpansion()
2373 ? SourceLocation()
2374 : Expansion.getExpansionLocEnd(),
2375 Record, Seq);
2376 Record.push_back(Elt: Expansion.isExpansionTokenRange());
2377
2378 // Compute the token length for this macro expansion.
2379 SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2380 if (I + 1 != N)
2381 NextOffset = SourceMgr.getLocalSLocEntry(Index: I + 1).getOffset();
2382 Record.push_back(Elt: getAdjustedOffset(Offset: NextOffset - SLoc->getOffset()) - 1);
2383 Stream.EmitRecordWithAbbrev(Abbrev: SLocExpansionAbbrv, Vals: Record);
2384 }
2385 }
2386
2387 Stream.ExitBlock();
2388
2389 if (SLocEntryOffsets.empty())
2390 return;
2391
2392 // Write the source-location offsets table into the AST block. This
2393 // table is used for lazily loading source-location information.
2394 using namespace llvm;
2395
2396 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2397 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2398 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2399 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2400 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2401 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2402 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2403 {
2404 RecordData::value_type Record[] = {
2405 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2406 getAdjustedOffset(Offset: SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2407 SLocEntryOffsetsBase - SourceManagerBlockOffset};
2408 Stream.EmitRecordWithBlob(Abbrev: SLocOffsetsAbbrev, Vals: Record,
2409 Blob: bytes(v: SLocEntryOffsets));
2410 }
2411
2412 // Write the line table. It depends on remapping working, so it must come
2413 // after the source location offsets.
2414 if (SourceMgr.hasLineTable()) {
2415 LineTableInfo &LineTable = SourceMgr.getLineTable();
2416
2417 Record.clear();
2418
2419 // Emit the needed file names.
2420 llvm::DenseMap<int, int> FilenameMap;
2421 FilenameMap[-1] = -1; // For unspecified filenames.
2422 for (const auto &L : LineTable) {
2423 if (L.first.ID < 0)
2424 continue;
2425 for (auto &LE : L.second) {
2426 if (FilenameMap.insert(KV: std::make_pair(x: LE.FilenameID,
2427 y: FilenameMap.size() - 1)).second)
2428 AddPath(Path: LineTable.getFilename(ID: LE.FilenameID), Record);
2429 }
2430 }
2431 Record.push_back(Elt: 0);
2432
2433 // Emit the line entries
2434 for (const auto &L : LineTable) {
2435 // Only emit entries for local files.
2436 if (L.first.ID < 0)
2437 continue;
2438
2439 AddFileID(FID: L.first, Record);
2440
2441 // Emit the line entries
2442 Record.push_back(Elt: L.second.size());
2443 for (const auto &LE : L.second) {
2444 Record.push_back(Elt: LE.FileOffset);
2445 Record.push_back(Elt: LE.LineNo);
2446 Record.push_back(Elt: FilenameMap[LE.FilenameID]);
2447 Record.push_back(Elt: (unsigned)LE.FileKind);
2448 Record.push_back(Elt: LE.IncludeOffset);
2449 }
2450 }
2451
2452 Stream.EmitRecord(Code: SOURCE_MANAGER_LINE_TABLE, Vals: Record);
2453 }
2454}
2455
2456//===----------------------------------------------------------------------===//
2457// Preprocessor Serialization
2458//===----------------------------------------------------------------------===//
2459
2460static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2461 const Preprocessor &PP) {
2462 if (MacroInfo *MI = MD->getMacroInfo())
2463 if (MI->isBuiltinMacro())
2464 return true;
2465
2466 if (IsModule) {
2467 SourceLocation Loc = MD->getLocation();
2468 if (Loc.isInvalid())
2469 return true;
2470 if (PP.getSourceManager().getFileID(SpellingLoc: Loc) == PP.getPredefinesFileID())
2471 return true;
2472 }
2473
2474 return false;
2475}
2476
2477/// Writes the block containing the serialized form of the
2478/// preprocessor.
2479void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2480 uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2481
2482 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2483 if (PPRec)
2484 WritePreprocessorDetail(PPRec&: *PPRec, MacroOffsetsBase);
2485
2486 RecordData Record;
2487 RecordData ModuleMacroRecord;
2488
2489 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2490 if (PP.getCounterValue() != 0) {
2491 RecordData::value_type Record[] = {PP.getCounterValue()};
2492 Stream.EmitRecord(Code: PP_COUNTER_VALUE, Vals: Record);
2493 }
2494
2495 // If we have a recorded #pragma assume_nonnull, remember it so it can be
2496 // replayed when the preamble terminates into the main file.
2497 SourceLocation AssumeNonNullLoc =
2498 PP.getPreambleRecordedPragmaAssumeNonNullLoc();
2499 if (AssumeNonNullLoc.isValid()) {
2500 assert(PP.isRecordingPreamble());
2501 AddSourceLocation(Loc: AssumeNonNullLoc, Record);
2502 Stream.EmitRecord(Code: PP_ASSUME_NONNULL_LOC, Vals: Record);
2503 Record.clear();
2504 }
2505
2506 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2507 assert(!IsModule);
2508 auto SkipInfo = PP.getPreambleSkipInfo();
2509 if (SkipInfo) {
2510 Record.push_back(Elt: true);
2511 AddSourceLocation(Loc: SkipInfo->HashTokenLoc, Record);
2512 AddSourceLocation(Loc: SkipInfo->IfTokenLoc, Record);
2513 Record.push_back(Elt: SkipInfo->FoundNonSkipPortion);
2514 Record.push_back(Elt: SkipInfo->FoundElse);
2515 AddSourceLocation(Loc: SkipInfo->ElseLoc, Record);
2516 } else {
2517 Record.push_back(Elt: false);
2518 }
2519 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2520 AddSourceLocation(Loc: Cond.IfLoc, Record);
2521 Record.push_back(Elt: Cond.WasSkipping);
2522 Record.push_back(Elt: Cond.FoundNonSkip);
2523 Record.push_back(Elt: Cond.FoundElse);
2524 }
2525 Stream.EmitRecord(Code: PP_CONDITIONAL_STACK, Vals: Record);
2526 Record.clear();
2527 }
2528
2529 // Write the safe buffer opt-out region map in PP
2530 for (SourceLocation &S : PP.serializeSafeBufferOptOutMap())
2531 AddSourceLocation(Loc: S, Record);
2532 Stream.EmitRecord(Code: PP_UNSAFE_BUFFER_USAGE, Vals: Record);
2533 Record.clear();
2534
2535 // Enter the preprocessor block.
2536 Stream.EnterSubblock(BlockID: PREPROCESSOR_BLOCK_ID, CodeLen: 3);
2537
2538 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2539 // FIXME: Include a location for the use, and say which one was used.
2540 if (PP.SawDateOrTime())
2541 PP.Diag(Loc: SourceLocation(), DiagID: diag::warn_module_uses_date_time) << IsModule;
2542
2543 // Loop over all the macro directives that are live at the end of the file,
2544 // emitting each to the PP section.
2545
2546 // Construct the list of identifiers with macro directives that need to be
2547 // serialized.
2548 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2549 // It is meaningless to emit macros for named modules. It only wastes times
2550 // and spaces.
2551 if (!isWritingStdCXXNamedModules())
2552 for (auto &Id : PP.getIdentifierTable())
2553 if (Id.second->hadMacroDefinition() &&
2554 (!Id.second->isFromAST() ||
2555 Id.second->hasChangedSinceDeserialization()))
2556 MacroIdentifiers.push_back(Elt: Id.second);
2557 // Sort the set of macro definitions that need to be serialized by the
2558 // name of the macro, to provide a stable ordering.
2559 llvm::sort(C&: MacroIdentifiers, Comp: llvm::deref<std::less<>>());
2560
2561 // Emit the macro directives as a list and associate the offset with the
2562 // identifier they belong to.
2563 for (const IdentifierInfo *Name : MacroIdentifiers) {
2564 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II: Name);
2565 uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2566 assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2567
2568 // Write out any exported module macros.
2569 bool EmittedModuleMacros = false;
2570 // C+=20 Header Units are compiled module interfaces, but they preserve
2571 // macros that are live (i.e. have a defined value) at the end of the
2572 // compilation. So when writing a header unit, we preserve only the final
2573 // value of each macro (and discard any that are undefined). Header units
2574 // do not have sub-modules (although they might import other header units).
2575 // PCH files, conversely, retain the history of each macro's define/undef
2576 // and of leaf macros in sub modules.
2577 if (IsModule && WritingModule->isHeaderUnit()) {
2578 // This is for the main TU when it is a C++20 header unit.
2579 // We preserve the final state of defined macros, and we do not emit ones
2580 // that are undefined.
2581 if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2582 MD->getKind() == MacroDirective::MD_Undefine)
2583 continue;
2584 AddSourceLocation(Loc: MD->getLocation(), Record);
2585 Record.push_back(Elt: MD->getKind());
2586 if (auto *DefMD = dyn_cast<DefMacroDirective>(Val: MD)) {
2587 Record.push_back(Elt: getMacroRef(MI: DefMD->getInfo(), Name));
2588 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(Val: MD)) {
2589 Record.push_back(Elt: VisMD->isPublic());
2590 }
2591 ModuleMacroRecord.push_back(Elt: getSubmoduleID(Mod: WritingModule));
2592 ModuleMacroRecord.push_back(Elt: getMacroRef(MI: MD->getMacroInfo(), Name));
2593 Stream.EmitRecord(Code: PP_MODULE_MACRO, Vals: ModuleMacroRecord);
2594 ModuleMacroRecord.clear();
2595 EmittedModuleMacros = true;
2596 } else {
2597 // Emit the macro directives in reverse source order.
2598 for (; MD; MD = MD->getPrevious()) {
2599 // Once we hit an ignored macro, we're done: the rest of the chain
2600 // will all be ignored macros.
2601 if (shouldIgnoreMacro(MD, IsModule, PP))
2602 break;
2603 AddSourceLocation(Loc: MD->getLocation(), Record);
2604 Record.push_back(Elt: MD->getKind());
2605 if (auto *DefMD = dyn_cast<DefMacroDirective>(Val: MD)) {
2606 Record.push_back(Elt: getMacroRef(MI: DefMD->getInfo(), Name));
2607 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(Val: MD)) {
2608 Record.push_back(Elt: VisMD->isPublic());
2609 }
2610 }
2611
2612 // We write out exported module macros for PCH as well.
2613 auto Leafs = PP.getLeafModuleMacros(II: Name);
2614 SmallVector<ModuleMacro *, 8> Worklist(Leafs.begin(), Leafs.end());
2615 llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2616 while (!Worklist.empty()) {
2617 auto *Macro = Worklist.pop_back_val();
2618
2619 // Emit a record indicating this submodule exports this macro.
2620 ModuleMacroRecord.push_back(Elt: getSubmoduleID(Mod: Macro->getOwningModule()));
2621 ModuleMacroRecord.push_back(Elt: getMacroRef(MI: Macro->getMacroInfo(), Name));
2622 for (auto *M : Macro->overrides())
2623 ModuleMacroRecord.push_back(Elt: getSubmoduleID(Mod: M->getOwningModule()));
2624
2625 Stream.EmitRecord(Code: PP_MODULE_MACRO, Vals: ModuleMacroRecord);
2626 ModuleMacroRecord.clear();
2627
2628 // Enqueue overridden macros once we've visited all their ancestors.
2629 for (auto *M : Macro->overrides())
2630 if (++Visits[M] == M->getNumOverridingMacros())
2631 Worklist.push_back(Elt: M);
2632
2633 EmittedModuleMacros = true;
2634 }
2635 }
2636 if (Record.empty() && !EmittedModuleMacros)
2637 continue;
2638
2639 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2640 Stream.EmitRecord(Code: PP_MACRO_DIRECTIVE_HISTORY, Vals: Record);
2641 Record.clear();
2642 }
2643
2644 /// Offsets of each of the macros into the bitstream, indexed by
2645 /// the local macro ID
2646 ///
2647 /// For each identifier that is associated with a macro, this map
2648 /// provides the offset into the bitstream where that macro is
2649 /// defined.
2650 std::vector<uint32_t> MacroOffsets;
2651
2652 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2653 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2654 MacroInfo *MI = MacroInfosToEmit[I].MI;
2655 MacroID ID = MacroInfosToEmit[I].ID;
2656
2657 if (ID < FirstMacroID) {
2658 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2659 continue;
2660 }
2661
2662 // Record the local offset of this macro.
2663 unsigned Index = ID - FirstMacroID;
2664 if (Index >= MacroOffsets.size())
2665 MacroOffsets.resize(new_size: Index + 1);
2666
2667 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2668 assert((Offset >> 32) == 0 && "Macro offset too large");
2669 MacroOffsets[Index] = Offset;
2670
2671 AddIdentifierRef(II: Name, Record);
2672 AddSourceLocation(Loc: MI->getDefinitionLoc(), Record);
2673 AddSourceLocation(Loc: MI->getDefinitionEndLoc(), Record);
2674 Record.push_back(Elt: MI->isUsed());
2675 Record.push_back(Elt: MI->isUsedForHeaderGuard());
2676 Record.push_back(Elt: MI->getNumTokens());
2677 unsigned Code;
2678 if (MI->isObjectLike()) {
2679 Code = PP_MACRO_OBJECT_LIKE;
2680 } else {
2681 Code = PP_MACRO_FUNCTION_LIKE;
2682
2683 Record.push_back(Elt: MI->isC99Varargs());
2684 Record.push_back(Elt: MI->isGNUVarargs());
2685 Record.push_back(Elt: MI->hasCommaPasting());
2686 Record.push_back(Elt: MI->getNumParams());
2687 for (const IdentifierInfo *Param : MI->params())
2688 AddIdentifierRef(II: Param, Record);
2689 }
2690
2691 // If we have a detailed preprocessing record, record the macro definition
2692 // ID that corresponds to this macro.
2693 if (PPRec)
2694 Record.push_back(Elt: MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2695
2696 Stream.EmitRecord(Code, Vals: Record);
2697 Record.clear();
2698
2699 // Emit the tokens array.
2700 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2701 // Note that we know that the preprocessor does not have any annotation
2702 // tokens in it because they are created by the parser, and thus can't
2703 // be in a macro definition.
2704 const Token &Tok = MI->getReplacementToken(Tok: TokNo);
2705 AddToken(Tok, Record);
2706 Stream.EmitRecord(Code: PP_TOKEN, Vals: Record);
2707 Record.clear();
2708 }
2709 ++NumMacros;
2710 }
2711
2712 Stream.ExitBlock();
2713
2714 // Write the offsets table for macro IDs.
2715 using namespace llvm;
2716
2717 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2718 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MACRO_OFFSET));
2719 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2720 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2721 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2722 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2723
2724 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2725 {
2726 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2727 FirstMacroID - NUM_PREDEF_MACRO_IDS,
2728 MacroOffsetsBase - ASTBlockStartOffset};
2729 Stream.EmitRecordWithBlob(Abbrev: MacroOffsetAbbrev, Vals: Record, Blob: bytes(v: MacroOffsets));
2730 }
2731}
2732
2733void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2734 uint64_t MacroOffsetsBase) {
2735 if (PPRec.local_begin() == PPRec.local_end())
2736 return;
2737
2738 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2739
2740 // Enter the preprocessor block.
2741 Stream.EnterSubblock(BlockID: PREPROCESSOR_DETAIL_BLOCK_ID, CodeLen: 3);
2742
2743 // If the preprocessor has a preprocessing record, emit it.
2744 unsigned NumPreprocessingRecords = 0;
2745 using namespace llvm;
2746
2747 // Set up the abbreviation for
2748 unsigned InclusionAbbrev = 0;
2749 {
2750 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2751 Abbrev->Add(OpInfo: BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2752 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2753 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2754 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2755 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2756 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2757 InclusionAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2758 }
2759
2760 unsigned FirstPreprocessorEntityID
2761 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2762 + NUM_PREDEF_PP_ENTITY_IDS;
2763 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2764 RecordData Record;
2765 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2766 EEnd = PPRec.local_end();
2767 E != EEnd;
2768 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2769 Record.clear();
2770
2771 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2772 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2773 SourceRange R = getAdjustedRange(Range: (*E)->getSourceRange());
2774 PreprocessedEntityOffsets.emplace_back(
2775 Args: getRawSourceLocationEncoding(Loc: R.getBegin()),
2776 Args: getRawSourceLocationEncoding(Loc: R.getEnd()), Args&: Offset);
2777
2778 if (auto *MD = dyn_cast<MacroDefinitionRecord>(Val: *E)) {
2779 // Record this macro definition's ID.
2780 MacroDefinitions[MD] = NextPreprocessorEntityID;
2781
2782 AddIdentifierRef(II: MD->getName(), Record);
2783 Stream.EmitRecord(Code: PPD_MACRO_DEFINITION, Vals: Record);
2784 continue;
2785 }
2786
2787 if (auto *ME = dyn_cast<MacroExpansion>(Val: *E)) {
2788 Record.push_back(Elt: ME->isBuiltinMacro());
2789 if (ME->isBuiltinMacro())
2790 AddIdentifierRef(II: ME->getName(), Record);
2791 else
2792 Record.push_back(Elt: MacroDefinitions[ME->getDefinition()]);
2793 Stream.EmitRecord(Code: PPD_MACRO_EXPANSION, Vals: Record);
2794 continue;
2795 }
2796
2797 if (auto *ID = dyn_cast<InclusionDirective>(Val: *E)) {
2798 Record.push_back(Elt: PPD_INCLUSION_DIRECTIVE);
2799 Record.push_back(Elt: ID->getFileName().size());
2800 Record.push_back(Elt: ID->wasInQuotes());
2801 Record.push_back(Elt: static_cast<unsigned>(ID->getKind()));
2802 Record.push_back(Elt: ID->importedModule());
2803 SmallString<64> Buffer;
2804 Buffer += ID->getFileName();
2805 // Check that the FileEntry is not null because it was not resolved and
2806 // we create a PCH even with compiler errors.
2807 if (ID->getFile())
2808 Buffer += ID->getFile()->getName();
2809 Stream.EmitRecordWithBlob(Abbrev: InclusionAbbrev, Vals: Record, Blob: Buffer);
2810 continue;
2811 }
2812
2813 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2814 }
2815 Stream.ExitBlock();
2816
2817 // Write the offsets table for the preprocessing record.
2818 if (NumPreprocessingRecords > 0) {
2819 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2820
2821 // Write the offsets table for identifier IDs.
2822 using namespace llvm;
2823
2824 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2825 Abbrev->Add(OpInfo: BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2826 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2827 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2828 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2829
2830 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2831 FirstPreprocessorEntityID -
2832 NUM_PREDEF_PP_ENTITY_IDS};
2833 Stream.EmitRecordWithBlob(Abbrev: PPEOffsetAbbrev, Vals: Record,
2834 Blob: bytes(v: PreprocessedEntityOffsets));
2835 }
2836
2837 // Write the skipped region table for the preprocessing record.
2838 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2839 if (SkippedRanges.size() > 0) {
2840 std::vector<PPSkippedRange> SerializedSkippedRanges;
2841 SerializedSkippedRanges.reserve(n: SkippedRanges.size());
2842 for (auto const& Range : SkippedRanges)
2843 SerializedSkippedRanges.emplace_back(
2844 args: getRawSourceLocationEncoding(Loc: Range.getBegin()),
2845 args: getRawSourceLocationEncoding(Loc: Range.getEnd()));
2846
2847 using namespace llvm;
2848 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2849 Abbrev->Add(OpInfo: BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2850 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2851 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2852
2853 Record.clear();
2854 Record.push_back(Elt: PPD_SKIPPED_RANGES);
2855 Stream.EmitRecordWithBlob(Abbrev: PPESkippedRangeAbbrev, Vals: Record,
2856 Blob: bytes(v: SerializedSkippedRanges));
2857 }
2858}
2859
2860unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2861 if (!Mod)
2862 return 0;
2863
2864 auto Known = SubmoduleIDs.find(Val: Mod);
2865 if (Known != SubmoduleIDs.end())
2866 return Known->second;
2867
2868 auto *Top = Mod->getTopLevelModule();
2869 if (Top != WritingModule &&
2870 (getLangOpts().CompilingPCH ||
2871 !Top->fullModuleNameIs(nameParts: StringRef(getLangOpts().CurrentModule))))
2872 return 0;
2873
2874 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2875}
2876
2877unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2878 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2879 // FIXME: This can easily happen, if we have a reference to a submodule that
2880 // did not result in us loading a module file for that submodule. For
2881 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2882 // assert((ID || !Mod) &&
2883 // "asked for module ID for non-local, non-imported module");
2884 return ID;
2885}
2886
2887/// Compute the number of modules within the given tree (including the
2888/// given module).
2889static unsigned getNumberOfModules(Module *Mod) {
2890 unsigned ChildModules = 0;
2891 for (auto *Submodule : Mod->submodules())
2892 ChildModules += getNumberOfModules(Mod: Submodule);
2893
2894 return ChildModules + 1;
2895}
2896
2897void ASTWriter::WriteSubmodules(Module *WritingModule) {
2898 // Enter the submodule description block.
2899 Stream.EnterSubblock(BlockID: SUBMODULE_BLOCK_ID, /*bits for abbreviations*/CodeLen: 5);
2900
2901 // Write the abbreviations needed for the submodules block.
2902 using namespace llvm;
2903
2904 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2905 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2906 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2907 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2908 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2909 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2910 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2911 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2912 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2913 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2914 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2915 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2916 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2917 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2918 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2919 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2920 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2921 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2922
2923 Abbrev = std::make_shared<BitCodeAbbrev>();
2924 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2925 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2926 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2927
2928 Abbrev = std::make_shared<BitCodeAbbrev>();
2929 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_HEADER));
2930 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2931 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2932
2933 Abbrev = std::make_shared<BitCodeAbbrev>();
2934 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2935 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2936 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2937
2938 Abbrev = std::make_shared<BitCodeAbbrev>();
2939 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2940 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2941 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2942
2943 Abbrev = std::make_shared<BitCodeAbbrev>();
2944 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2945 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2946 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2947 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2948
2949 Abbrev = std::make_shared<BitCodeAbbrev>();
2950 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2951 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2952 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2953
2954 Abbrev = std::make_shared<BitCodeAbbrev>();
2955 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2956 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2957 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2958
2959 Abbrev = std::make_shared<BitCodeAbbrev>();
2960 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2961 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2962 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2963
2964 Abbrev = std::make_shared<BitCodeAbbrev>();
2965 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2966 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2967 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2968
2969 Abbrev = std::make_shared<BitCodeAbbrev>();
2970 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2971 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2972 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2973 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2974
2975 Abbrev = std::make_shared<BitCodeAbbrev>();
2976 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2977 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2978 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2979
2980 Abbrev = std::make_shared<BitCodeAbbrev>();
2981 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2982 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2983 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2984 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2985
2986 Abbrev = std::make_shared<BitCodeAbbrev>();
2987 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2988 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2989 unsigned ExportAsAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2990
2991 // Write the submodule metadata block.
2992 RecordData::value_type Record[] = {
2993 getNumberOfModules(Mod: WritingModule),
2994 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2995 Stream.EmitRecord(Code: SUBMODULE_METADATA, Vals: Record);
2996
2997 // Write all of the submodules.
2998 std::queue<Module *> Q;
2999 Q.push(x: WritingModule);
3000 while (!Q.empty()) {
3001 Module *Mod = Q.front();
3002 Q.pop();
3003 unsigned ID = getSubmoduleID(Mod);
3004
3005 uint64_t ParentID = 0;
3006 if (Mod->Parent) {
3007 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
3008 ParentID = SubmoduleIDs[Mod->Parent];
3009 }
3010
3011 SourceLocationEncoding::RawLocEncoding DefinitionLoc =
3012 getRawSourceLocationEncoding(Loc: getAdjustedLocation(Loc: Mod->DefinitionLoc));
3013
3014 // Emit the definition of the block.
3015 {
3016 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
3017 ID,
3018 ParentID,
3019 (RecordData::value_type)Mod->Kind,
3020 DefinitionLoc,
3021 Mod->IsFramework,
3022 Mod->IsExplicit,
3023 Mod->IsSystem,
3024 Mod->IsExternC,
3025 Mod->InferSubmodules,
3026 Mod->InferExplicitSubmodules,
3027 Mod->InferExportWildcard,
3028 Mod->ConfigMacrosExhaustive,
3029 Mod->ModuleMapIsPrivate,
3030 Mod->NamedModuleHasInit};
3031 Stream.EmitRecordWithBlob(Abbrev: DefinitionAbbrev, Vals: Record, Blob: Mod->Name);
3032 }
3033
3034 // Emit the requirements.
3035 for (const auto &R : Mod->Requirements) {
3036 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.RequiredState};
3037 Stream.EmitRecordWithBlob(Abbrev: RequiresAbbrev, Vals: Record, Blob: R.FeatureName);
3038 }
3039
3040 // Emit the umbrella header, if there is one.
3041 if (std::optional<Module::Header> UmbrellaHeader =
3042 Mod->getUmbrellaHeaderAsWritten()) {
3043 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
3044 Stream.EmitRecordWithBlob(Abbrev: UmbrellaAbbrev, Vals: Record,
3045 Blob: UmbrellaHeader->NameAsWritten);
3046 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
3047 Mod->getUmbrellaDirAsWritten()) {
3048 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
3049 Stream.EmitRecordWithBlob(Abbrev: UmbrellaDirAbbrev, Vals: Record,
3050 Blob: UmbrellaDir->NameAsWritten);
3051 }
3052
3053 // Emit the headers.
3054 struct {
3055 unsigned RecordKind;
3056 unsigned Abbrev;
3057 Module::HeaderKind HeaderKind;
3058 } HeaderLists[] = {
3059 {.RecordKind: SUBMODULE_HEADER, .Abbrev: HeaderAbbrev, .HeaderKind: Module::HK_Normal},
3060 {.RecordKind: SUBMODULE_TEXTUAL_HEADER, .Abbrev: TextualHeaderAbbrev, .HeaderKind: Module::HK_Textual},
3061 {.RecordKind: SUBMODULE_PRIVATE_HEADER, .Abbrev: PrivateHeaderAbbrev, .HeaderKind: Module::HK_Private},
3062 {.RecordKind: SUBMODULE_PRIVATE_TEXTUAL_HEADER, .Abbrev: PrivateTextualHeaderAbbrev,
3063 .HeaderKind: Module::HK_PrivateTextual},
3064 {.RecordKind: SUBMODULE_EXCLUDED_HEADER, .Abbrev: ExcludedHeaderAbbrev, .HeaderKind: Module::HK_Excluded}
3065 };
3066 for (auto &HL : HeaderLists) {
3067 RecordData::value_type Record[] = {HL.RecordKind};
3068 for (auto &H : Mod->Headers[HL.HeaderKind])
3069 Stream.EmitRecordWithBlob(Abbrev: HL.Abbrev, Vals: Record, Blob: H.NameAsWritten);
3070 }
3071
3072 // Emit the top headers.
3073 {
3074 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
3075 for (FileEntryRef H : Mod->getTopHeaders(FileMgr&: PP->getFileManager())) {
3076 SmallString<128> HeaderName(H.getName());
3077 PreparePathForOutput(Path&: HeaderName);
3078 Stream.EmitRecordWithBlob(Abbrev: TopHeaderAbbrev, Vals: Record, Blob: HeaderName);
3079 }
3080 }
3081
3082 // Emit the imports.
3083 if (!Mod->Imports.empty()) {
3084 RecordData Record;
3085 for (auto *I : Mod->Imports)
3086 Record.push_back(Elt: getSubmoduleID(Mod: I));
3087 Stream.EmitRecord(Code: SUBMODULE_IMPORTS, Vals: Record);
3088 }
3089
3090 // Emit the modules affecting compilation that were not imported.
3091 if (!Mod->AffectingClangModules.empty()) {
3092 RecordData Record;
3093 for (auto *I : Mod->AffectingClangModules)
3094 Record.push_back(Elt: getSubmoduleID(Mod: I));
3095 Stream.EmitRecord(Code: SUBMODULE_AFFECTING_MODULES, Vals: Record);
3096 }
3097
3098 // Emit the exports.
3099 if (!Mod->Exports.empty()) {
3100 RecordData Record;
3101 for (const auto &E : Mod->Exports) {
3102 // FIXME: This may fail; we don't require that all exported modules
3103 // are local or imported.
3104 Record.push_back(Elt: getSubmoduleID(Mod: E.getPointer()));
3105 Record.push_back(Elt: E.getInt());
3106 }
3107 Stream.EmitRecord(Code: SUBMODULE_EXPORTS, Vals: Record);
3108 }
3109
3110 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
3111 // Might be unnecessary as use declarations are only used to build the
3112 // module itself.
3113
3114 // TODO: Consider serializing undeclared uses of modules.
3115
3116 // Emit the link libraries.
3117 for (const auto &LL : Mod->LinkLibraries) {
3118 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
3119 LL.IsFramework};
3120 Stream.EmitRecordWithBlob(Abbrev: LinkLibraryAbbrev, Vals: Record, Blob: LL.Library);
3121 }
3122
3123 // Emit the conflicts.
3124 for (const auto &C : Mod->Conflicts) {
3125 // FIXME: This may fail; we don't require that all conflicting modules
3126 // are local or imported.
3127 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
3128 getSubmoduleID(Mod: C.Other)};
3129 Stream.EmitRecordWithBlob(Abbrev: ConflictAbbrev, Vals: Record, Blob: C.Message);
3130 }
3131
3132 // Emit the configuration macros.
3133 for (const auto &CM : Mod->ConfigMacros) {
3134 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
3135 Stream.EmitRecordWithBlob(Abbrev: ConfigMacroAbbrev, Vals: Record, Blob: CM);
3136 }
3137
3138 // Emit the reachable initializers.
3139 // The initializer may only be unreachable in reduced BMI.
3140 RecordData Inits;
3141 for (Decl *D : Context->getModuleInitializers(M: Mod))
3142 if (wasDeclEmitted(D))
3143 AddDeclRef(D, Record&: Inits);
3144 if (!Inits.empty())
3145 Stream.EmitRecord(Code: SUBMODULE_INITIALIZERS, Vals: Inits);
3146
3147 // Emit the name of the re-exported module, if any.
3148 if (!Mod->ExportAsModule.empty()) {
3149 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3150 Stream.EmitRecordWithBlob(Abbrev: ExportAsAbbrev, Vals: Record, Blob: Mod->ExportAsModule);
3151 }
3152
3153 // Queue up the submodules of this module.
3154 for (auto *M : Mod->submodules())
3155 Q.push(x: M);
3156 }
3157
3158 Stream.ExitBlock();
3159
3160 assert((NextSubmoduleID - FirstSubmoduleID ==
3161 getNumberOfModules(WritingModule)) &&
3162 "Wrong # of submodules; found a reference to a non-local, "
3163 "non-imported submodule?");
3164}
3165
3166void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3167 bool isModule) {
3168 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3169 DiagStateIDMap;
3170 unsigned CurrID = 0;
3171 RecordData Record;
3172
3173 auto EncodeDiagStateFlags =
3174 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3175 unsigned Result = (unsigned)DS->ExtBehavior;
3176 for (unsigned Val :
3177 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3178 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3179 (unsigned)DS->SuppressSystemWarnings})
3180 Result = (Result << 1) | Val;
3181 return Result;
3182 };
3183
3184 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3185 Record.push_back(Elt: Flags);
3186
3187 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3188 bool IncludeNonPragmaStates) {
3189 // Ensure that the diagnostic state wasn't modified since it was created.
3190 // We will not correctly round-trip this information otherwise.
3191 assert(Flags == EncodeDiagStateFlags(State) &&
3192 "diag state flags vary in single AST file");
3193
3194 // If we ever serialize non-pragma mappings outside the initial state, the
3195 // code below will need to consider more than getDefaultMapping.
3196 assert(!IncludeNonPragmaStates ||
3197 State == Diag.DiagStatesByLoc.FirstDiagState);
3198
3199 unsigned &DiagStateID = DiagStateIDMap[State];
3200 Record.push_back(Elt: DiagStateID);
3201
3202 if (DiagStateID == 0) {
3203 DiagStateID = ++CurrID;
3204 SmallVector<std::pair<unsigned, DiagnosticMapping>> Mappings;
3205
3206 // Add a placeholder for the number of mappings.
3207 auto SizeIdx = Record.size();
3208 Record.emplace_back();
3209 for (const auto &I : *State) {
3210 // Maybe skip non-pragmas.
3211 if (!I.second.isPragma() && !IncludeNonPragmaStates)
3212 continue;
3213 // Skip default mappings. We have a mapping for every diagnostic ever
3214 // emitted, regardless of whether it was customized.
3215 if (!I.second.isPragma() &&
3216 I.second == DiagnosticIDs::getDefaultMapping(DiagID: I.first))
3217 continue;
3218 Mappings.push_back(Elt: I);
3219 }
3220
3221 // Sort by diag::kind for deterministic output.
3222 llvm::sort(C&: Mappings, Comp: llvm::less_first());
3223
3224 for (const auto &I : Mappings) {
3225 Record.push_back(Elt: I.first);
3226 Record.push_back(Elt: I.second.serialize());
3227 }
3228 // Update the placeholder.
3229 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
3230 }
3231 };
3232
3233 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
3234
3235 // Reserve a spot for the number of locations with state transitions.
3236 auto NumLocationsIdx = Record.size();
3237 Record.emplace_back();
3238
3239 // Emit the state transitions.
3240 unsigned NumLocations = 0;
3241 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3242 if (!FileIDAndFile.first.isValid() ||
3243 !FileIDAndFile.second.HasLocalTransitions)
3244 continue;
3245 ++NumLocations;
3246
3247 AddFileID(FID: FileIDAndFile.first, Record);
3248
3249 Record.push_back(Elt: FileIDAndFile.second.StateTransitions.size());
3250 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3251 Record.push_back(Elt: getAdjustedOffset(Offset: StatePoint.Offset));
3252 AddDiagState(StatePoint.State, false);
3253 }
3254 }
3255
3256 // Backpatch the number of locations.
3257 Record[NumLocationsIdx] = NumLocations;
3258
3259 // Emit CurDiagStateLoc. Do it last in order to match source order.
3260 //
3261 // This also protects against a hypothetical corner case with simulating
3262 // -Werror settings for implicit modules in the ASTReader, where reading
3263 // CurDiagState out of context could change whether warning pragmas are
3264 // treated as errors.
3265 AddSourceLocation(Loc: Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3266 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3267
3268 Stream.EmitRecord(Code: DIAG_PRAGMA_MAPPINGS, Vals: Record);
3269}
3270
3271//===----------------------------------------------------------------------===//
3272// Type Serialization
3273//===----------------------------------------------------------------------===//
3274
3275/// Write the representation of a type to the AST stream.
3276void ASTWriter::WriteType(QualType T) {
3277 TypeIdx &IdxRef = TypeIdxs[T];
3278 if (IdxRef.getValue() == 0) // we haven't seen this type before.
3279 IdxRef = TypeIdx(0, NextTypeID++);
3280 TypeIdx Idx = IdxRef;
3281
3282 assert(Idx.getModuleFileIndex() == 0 && "Re-writing a type from a prior AST");
3283 assert(Idx.getValue() >= FirstTypeID && "Writing predefined type");
3284
3285 // Emit the type's representation.
3286 uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
3287
3288 // Record the offset for this type.
3289 uint64_t Index = Idx.getValue() - FirstTypeID;
3290 if (TypeOffsets.size() == Index)
3291 TypeOffsets.emplace_back(args&: Offset);
3292 else if (TypeOffsets.size() < Index) {
3293 TypeOffsets.resize(new_size: Index + 1);
3294 TypeOffsets[Index].set(Offset);
3295 } else {
3296 llvm_unreachable("Types emitted in wrong order");
3297 }
3298}
3299
3300//===----------------------------------------------------------------------===//
3301// Declaration Serialization
3302//===----------------------------------------------------------------------===//
3303
3304static bool IsInternalDeclFromFileContext(const Decl *D) {
3305 auto *ND = dyn_cast<NamedDecl>(Val: D);
3306 if (!ND)
3307 return false;
3308
3309 if (!D->getDeclContext()->getRedeclContext()->isFileContext())
3310 return false;
3311
3312 return ND->getFormalLinkage() == Linkage::Internal;
3313}
3314
3315/// Write the block containing all of the declaration IDs
3316/// lexically declared within the given DeclContext.
3317///
3318/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3319/// bitstream, or 0 if no block was written.
3320uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3321 const DeclContext *DC) {
3322 if (DC->decls_empty())
3323 return 0;
3324
3325 // In reduced BMI, we don't care the declarations in functions.
3326 if (GeneratingReducedBMI && DC->isFunctionOrMethod())
3327 return 0;
3328
3329 uint64_t Offset = Stream.GetCurrentBitNo();
3330 SmallVector<DeclID, 128> KindDeclPairs;
3331 for (const auto *D : DC->decls()) {
3332 if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D))
3333 continue;
3334
3335 // We don't need to write decls with internal linkage into reduced BMI.
3336 // If such decls gets emitted due to it get used from inline functions,
3337 // the program illegal. However, there are too many use of static inline
3338 // functions in the global module fragment and it will be breaking change
3339 // to forbid that. So we have to allow to emit such declarations from GMF.
3340 if (GeneratingReducedBMI && !D->isFromExplicitGlobalModule() &&
3341 IsInternalDeclFromFileContext(D))
3342 continue;
3343
3344 KindDeclPairs.push_back(Elt: D->getKind());
3345 KindDeclPairs.push_back(Elt: GetDeclRef(D).getRawValue());
3346 }
3347
3348 ++NumLexicalDeclContexts;
3349 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3350 Stream.EmitRecordWithBlob(Abbrev: DeclContextLexicalAbbrev, Vals: Record,
3351 Blob: bytes(v: KindDeclPairs));
3352 return Offset;
3353}
3354
3355void ASTWriter::WriteTypeDeclOffsets() {
3356 using namespace llvm;
3357
3358 // Write the type offsets array
3359 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3360 Abbrev->Add(OpInfo: BitCodeAbbrevOp(TYPE_OFFSET));
3361 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3362 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3363 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3364 {
3365 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size()};
3366 Stream.EmitRecordWithBlob(Abbrev: TypeOffsetAbbrev, Vals: Record, Blob: bytes(v: TypeOffsets));
3367 }
3368
3369 // Write the declaration offsets array
3370 Abbrev = std::make_shared<BitCodeAbbrev>();
3371 Abbrev->Add(OpInfo: BitCodeAbbrevOp(DECL_OFFSET));
3372 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3373 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3374 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3375 {
3376 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size()};
3377 Stream.EmitRecordWithBlob(Abbrev: DeclOffsetAbbrev, Vals: Record, Blob: bytes(v: DeclOffsets));
3378 }
3379}
3380
3381void ASTWriter::WriteFileDeclIDsMap() {
3382 using namespace llvm;
3383
3384 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3385 SortedFileDeclIDs.reserve(N: FileDeclIDs.size());
3386 for (const auto &P : FileDeclIDs)
3387 SortedFileDeclIDs.push_back(Elt: std::make_pair(x: P.first, y: P.second.get()));
3388 llvm::sort(C&: SortedFileDeclIDs, Comp: llvm::less_first());
3389
3390 // Join the vectors of DeclIDs from all files.
3391 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3392 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3393 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3394 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3395 llvm::stable_sort(Range&: Info.DeclIDs);
3396 for (auto &LocDeclEntry : Info.DeclIDs)
3397 FileGroupedDeclIDs.push_back(Elt: LocDeclEntry.second.getRawValue());
3398 }
3399
3400 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3401 Abbrev->Add(OpInfo: BitCodeAbbrevOp(FILE_SORTED_DECLS));
3402 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3403 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3404 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3405 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3406 FileGroupedDeclIDs.size()};
3407 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob: bytes(v: FileGroupedDeclIDs));
3408}
3409
3410void ASTWriter::WriteComments() {
3411 Stream.EnterSubblock(BlockID: COMMENTS_BLOCK_ID, CodeLen: 3);
3412 auto _ = llvm::make_scope_exit(F: [this] { Stream.ExitBlock(); });
3413 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3414 return;
3415
3416 // Don't write comments to BMI to reduce the size of BMI.
3417 // If language services (e.g., clangd) want such abilities,
3418 // we can offer a special option then.
3419 if (isWritingStdCXXNamedModules())
3420 return;
3421
3422 RecordData Record;
3423 for (const auto &FO : Context->Comments.OrderedComments) {
3424 for (const auto &OC : FO.second) {
3425 const RawComment *I = OC.second;
3426 Record.clear();
3427 AddSourceRange(Range: I->getSourceRange(), Record);
3428 Record.push_back(Elt: I->getKind());
3429 Record.push_back(Elt: I->isTrailingComment());
3430 Record.push_back(Elt: I->isAlmostTrailingComment());
3431 Stream.EmitRecord(Code: COMMENTS_RAW_COMMENT, Vals: Record);
3432 }
3433 }
3434}
3435
3436//===----------------------------------------------------------------------===//
3437// Global Method Pool and Selector Serialization
3438//===----------------------------------------------------------------------===//
3439
3440namespace {
3441
3442// Trait used for the on-disk hash table used in the method pool.
3443class ASTMethodPoolTrait {
3444 ASTWriter &Writer;
3445
3446public:
3447 using key_type = Selector;
3448 using key_type_ref = key_type;
3449
3450 struct data_type {
3451 SelectorID ID;
3452 ObjCMethodList Instance, Factory;
3453 };
3454 using data_type_ref = const data_type &;
3455
3456 using hash_value_type = unsigned;
3457 using offset_type = unsigned;
3458
3459 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3460
3461 static hash_value_type ComputeHash(Selector Sel) {
3462 return serialization::ComputeHash(Sel);
3463 }
3464
3465 std::pair<unsigned, unsigned>
3466 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3467 data_type_ref Methods) {
3468 unsigned KeyLen =
3469 2 + (Sel.getNumArgs() ? Sel.getNumArgs() * sizeof(IdentifierID)
3470 : sizeof(IdentifierID));
3471 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3472 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3473 Method = Method->getNext())
3474 if (ShouldWriteMethodListNode(Node: Method))
3475 DataLen += sizeof(DeclID);
3476 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3477 Method = Method->getNext())
3478 if (ShouldWriteMethodListNode(Node: Method))
3479 DataLen += sizeof(DeclID);
3480 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3481 }
3482
3483 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3484 using namespace llvm::support;
3485
3486 endian::Writer LE(Out, llvm::endianness::little);
3487 uint64_t Start = Out.tell();
3488 assert((Start >> 32) == 0 && "Selector key offset too large");
3489 Writer.SetSelectorOffset(Sel, Offset: Start);
3490 unsigned N = Sel.getNumArgs();
3491 LE.write<uint16_t>(Val: N);
3492 if (N == 0)
3493 N = 1;
3494 for (unsigned I = 0; I != N; ++I)
3495 LE.write<IdentifierID>(
3496 Val: Writer.getIdentifierRef(II: Sel.getIdentifierInfoForSlot(argIndex: I)));
3497 }
3498
3499 void EmitData(raw_ostream& Out, key_type_ref,
3500 data_type_ref Methods, unsigned DataLen) {
3501 using namespace llvm::support;
3502
3503 endian::Writer LE(Out, llvm::endianness::little);
3504 uint64_t Start = Out.tell(); (void)Start;
3505 LE.write<uint32_t>(Val: Methods.ID);
3506 unsigned NumInstanceMethods = 0;
3507 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3508 Method = Method->getNext())
3509 if (ShouldWriteMethodListNode(Node: Method))
3510 ++NumInstanceMethods;
3511
3512 unsigned NumFactoryMethods = 0;
3513 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3514 Method = Method->getNext())
3515 if (ShouldWriteMethodListNode(Node: Method))
3516 ++NumFactoryMethods;
3517
3518 unsigned InstanceBits = Methods.Instance.getBits();
3519 assert(InstanceBits < 4);
3520 unsigned InstanceHasMoreThanOneDeclBit =
3521 Methods.Instance.hasMoreThanOneDecl();
3522 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3523 (InstanceHasMoreThanOneDeclBit << 2) |
3524 InstanceBits;
3525 unsigned FactoryBits = Methods.Factory.getBits();
3526 assert(FactoryBits < 4);
3527 unsigned FactoryHasMoreThanOneDeclBit =
3528 Methods.Factory.hasMoreThanOneDecl();
3529 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3530 (FactoryHasMoreThanOneDeclBit << 2) |
3531 FactoryBits;
3532 LE.write<uint16_t>(Val: FullInstanceBits);
3533 LE.write<uint16_t>(Val: FullFactoryBits);
3534 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3535 Method = Method->getNext())
3536 if (ShouldWriteMethodListNode(Node: Method))
3537 LE.write<DeclID>(Val: (DeclID)Writer.getDeclID(D: Method->getMethod()));
3538 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3539 Method = Method->getNext())
3540 if (ShouldWriteMethodListNode(Node: Method))
3541 LE.write<DeclID>(Val: (DeclID)Writer.getDeclID(D: Method->getMethod()));
3542
3543 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3544 }
3545
3546private:
3547 static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3548 return (Node->getMethod() && !Node->getMethod()->isFromASTFile());
3549 }
3550};
3551
3552} // namespace
3553
3554/// Write ObjC data: selectors and the method pool.
3555///
3556/// The method pool contains both instance and factory methods, stored
3557/// in an on-disk hash table indexed by the selector. The hash table also
3558/// contains an empty entry for every other selector known to Sema.
3559void ASTWriter::WriteSelectors(Sema &SemaRef) {
3560 using namespace llvm;
3561
3562 // Do we have to do anything at all?
3563 if (SemaRef.ObjC().MethodPool.empty() && SelectorIDs.empty())
3564 return;
3565 unsigned NumTableEntries = 0;
3566 // Create and write out the blob that contains selectors and the method pool.
3567 {
3568 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3569 ASTMethodPoolTrait Trait(*this);
3570
3571 // Create the on-disk hash table representation. We walk through every
3572 // selector we've seen and look it up in the method pool.
3573 SelectorOffsets.resize(new_size: NextSelectorID - FirstSelectorID);
3574 for (auto &SelectorAndID : SelectorIDs) {
3575 Selector S = SelectorAndID.first;
3576 SelectorID ID = SelectorAndID.second;
3577 SemaObjC::GlobalMethodPool::iterator F =
3578 SemaRef.ObjC().MethodPool.find(Sel: S);
3579 ASTMethodPoolTrait::data_type Data = {
3580 .ID: ID,
3581 .Instance: ObjCMethodList(),
3582 .Factory: ObjCMethodList()
3583 };
3584 if (F != SemaRef.ObjC().MethodPool.end()) {
3585 Data.Instance = F->second.first;
3586 Data.Factory = F->second.second;
3587 }
3588 // Only write this selector if it's not in an existing AST or something
3589 // changed.
3590 if (Chain && ID < FirstSelectorID) {
3591 // Selector already exists. Did it change?
3592 bool changed = false;
3593 for (ObjCMethodList *M = &Data.Instance; M && M->getMethod();
3594 M = M->getNext()) {
3595 if (!M->getMethod()->isFromASTFile()) {
3596 changed = true;
3597 Data.Instance = *M;
3598 break;
3599 }
3600 }
3601 for (ObjCMethodList *M = &Data.Factory; M && M->getMethod();
3602 M = M->getNext()) {
3603 if (!M->getMethod()->isFromASTFile()) {
3604 changed = true;
3605 Data.Factory = *M;
3606 break;
3607 }
3608 }
3609 if (!changed)
3610 continue;
3611 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3612 // A new method pool entry.
3613 ++NumTableEntries;
3614 }
3615 Generator.insert(Key: S, Data, InfoObj&: Trait);
3616 }
3617
3618 // Create the on-disk hash table in a buffer.
3619 SmallString<4096> MethodPool;
3620 uint32_t BucketOffset;
3621 {
3622 using namespace llvm::support;
3623
3624 ASTMethodPoolTrait Trait(*this);
3625 llvm::raw_svector_ostream Out(MethodPool);
3626 // Make sure that no bucket is at offset 0
3627 endian::write<uint32_t>(os&: Out, value: 0, endian: llvm::endianness::little);
3628 BucketOffset = Generator.Emit(Out, InfoObj&: Trait);
3629 }
3630
3631 // Create a blob abbreviation
3632 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3633 Abbrev->Add(OpInfo: BitCodeAbbrevOp(METHOD_POOL));
3634 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3635 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3636 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3637 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3638
3639 // Write the method pool
3640 {
3641 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3642 NumTableEntries};
3643 Stream.EmitRecordWithBlob(Abbrev: MethodPoolAbbrev, Vals: Record, Blob: MethodPool);
3644 }
3645
3646 // Create a blob abbreviation for the selector table offsets.
3647 Abbrev = std::make_shared<BitCodeAbbrev>();
3648 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SELECTOR_OFFSETS));
3649 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3650 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3651 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3652 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3653
3654 // Write the selector offsets table.
3655 {
3656 RecordData::value_type Record[] = {
3657 SELECTOR_OFFSETS, SelectorOffsets.size(),
3658 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3659 Stream.EmitRecordWithBlob(Abbrev: SelectorOffsetAbbrev, Vals: Record,
3660 Blob: bytes(v: SelectorOffsets));
3661 }
3662 }
3663}
3664
3665/// Write the selectors referenced in @selector expression into AST file.
3666void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3667 using namespace llvm;
3668
3669 if (SemaRef.ObjC().ReferencedSelectors.empty())
3670 return;
3671
3672 RecordData Record;
3673 ASTRecordWriter Writer(*this, Record);
3674
3675 // Note: this writes out all references even for a dependent AST. But it is
3676 // very tricky to fix, and given that @selector shouldn't really appear in
3677 // headers, probably not worth it. It's not a correctness issue.
3678 for (auto &SelectorAndLocation : SemaRef.ObjC().ReferencedSelectors) {
3679 Selector Sel = SelectorAndLocation.first;
3680 SourceLocation Loc = SelectorAndLocation.second;
3681 Writer.AddSelectorRef(S: Sel);
3682 Writer.AddSourceLocation(Loc);
3683 }
3684 Writer.Emit(Code: REFERENCED_SELECTOR_POOL);
3685}
3686
3687//===----------------------------------------------------------------------===//
3688// Identifier Table Serialization
3689//===----------------------------------------------------------------------===//
3690
3691/// Determine the declaration that should be put into the name lookup table to
3692/// represent the given declaration in this module. This is usually D itself,
3693/// but if D was imported and merged into a local declaration, we want the most
3694/// recent local declaration instead. The chosen declaration will be the most
3695/// recent declaration in any module that imports this one.
3696static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3697 NamedDecl *D) {
3698 if (!LangOpts.Modules || !D->isFromASTFile())
3699 return D;
3700
3701 if (Decl *Redecl = D->getPreviousDecl()) {
3702 // For Redeclarable decls, a prior declaration might be local.
3703 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3704 // If we find a local decl, we're done.
3705 if (!Redecl->isFromASTFile()) {
3706 // Exception: in very rare cases (for injected-class-names), not all
3707 // redeclarations are in the same semantic context. Skip ones in a
3708 // different context. They don't go in this lookup table at all.
3709 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3710 DC: D->getDeclContext()->getRedeclContext()))
3711 continue;
3712 return cast<NamedDecl>(Val: Redecl);
3713 }
3714
3715 // If we find a decl from a (chained-)PCH stop since we won't find a
3716 // local one.
3717 if (Redecl->getOwningModuleID() == 0)
3718 break;
3719 }
3720 } else if (Decl *First = D->getCanonicalDecl()) {
3721 // For Mergeable decls, the first decl might be local.
3722 if (!First->isFromASTFile())
3723 return cast<NamedDecl>(Val: First);
3724 }
3725
3726 // All declarations are imported. Our most recent declaration will also be
3727 // the most recent one in anyone who imports us.
3728 return D;
3729}
3730
3731namespace {
3732
3733bool IsInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset,
3734 bool IsModule, bool IsCPlusPlus) {
3735 bool NeedDecls = !IsModule || !IsCPlusPlus;
3736
3737 bool IsInteresting =
3738 II->getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
3739 II->getBuiltinID() != Builtin::ID::NotBuiltin ||
3740 II->getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
3741 if (MacroOffset || II->isPoisoned() || (!IsModule && IsInteresting) ||
3742 II->hasRevertedTokenIDToIdentifier() ||
3743 (NeedDecls && II->getFETokenInfo()))
3744 return true;
3745
3746 return false;
3747}
3748
3749bool IsInterestingNonMacroIdentifier(const IdentifierInfo *II,
3750 ASTWriter &Writer) {
3751 bool IsModule = Writer.isWritingModule();
3752 bool IsCPlusPlus = Writer.getLangOpts().CPlusPlus;
3753 return IsInterestingIdentifier(II, /*MacroOffset=*/0, IsModule, IsCPlusPlus);
3754}
3755
3756class ASTIdentifierTableTrait {
3757 ASTWriter &Writer;
3758 Preprocessor &PP;
3759 IdentifierResolver &IdResolver;
3760 bool IsModule;
3761 bool NeedDecls;
3762 ASTWriter::RecordData *InterestingIdentifierOffsets;
3763
3764 /// Determines whether this is an "interesting" identifier that needs a
3765 /// full IdentifierInfo structure written into the hash table. Notably, this
3766 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3767 /// to check that.
3768 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3769 return IsInterestingIdentifier(II, MacroOffset, IsModule,
3770 IsCPlusPlus: Writer.getLangOpts().CPlusPlus);
3771 }
3772
3773public:
3774 using key_type = const IdentifierInfo *;
3775 using key_type_ref = key_type;
3776
3777 using data_type = IdentifierID;
3778 using data_type_ref = data_type;
3779
3780 using hash_value_type = unsigned;
3781 using offset_type = unsigned;
3782
3783 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3784 IdentifierResolver &IdResolver, bool IsModule,
3785 ASTWriter::RecordData *InterestingIdentifierOffsets)
3786 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3787 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3788 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3789
3790 bool needDecls() const { return NeedDecls; }
3791
3792 static hash_value_type ComputeHash(const IdentifierInfo* II) {
3793 return llvm::djbHash(Buffer: II->getName());
3794 }
3795
3796 bool isInterestingIdentifier(const IdentifierInfo *II) {
3797 auto MacroOffset = Writer.getMacroDirectivesOffset(Name: II);
3798 return isInterestingIdentifier(II, MacroOffset);
3799 }
3800
3801 std::pair<unsigned, unsigned>
3802 EmitKeyDataLength(raw_ostream &Out, const IdentifierInfo *II, IdentifierID ID) {
3803 // Record the location of the identifier data. This is used when generating
3804 // the mapping from persistent IDs to strings.
3805 Writer.SetIdentifierOffset(II, Offset: Out.tell());
3806
3807 auto MacroOffset = Writer.getMacroDirectivesOffset(Name: II);
3808
3809 // Emit the offset of the key/data length information to the interesting
3810 // identifiers table if necessary.
3811 if (InterestingIdentifierOffsets &&
3812 isInterestingIdentifier(II, MacroOffset))
3813 InterestingIdentifierOffsets->push_back(Elt: Out.tell());
3814
3815 unsigned KeyLen = II->getLength() + 1;
3816 unsigned DataLen = sizeof(IdentifierID); // bytes for the persistent ID << 1
3817 if (isInterestingIdentifier(II, MacroOffset)) {
3818 DataLen += 2; // 2 bytes for builtin ID
3819 DataLen += 2; // 2 bytes for flags
3820 if (MacroOffset)
3821 DataLen += 4; // MacroDirectives offset.
3822
3823 if (NeedDecls)
3824 DataLen += std::distance(first: IdResolver.begin(Name: II), last: IdResolver.end()) *
3825 sizeof(DeclID);
3826 }
3827 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3828 }
3829
3830 void EmitKey(raw_ostream &Out, const IdentifierInfo *II, unsigned KeyLen) {
3831 Out.write(Ptr: II->getNameStart(), Size: KeyLen);
3832 }
3833
3834 void EmitData(raw_ostream &Out, const IdentifierInfo *II, IdentifierID ID,
3835 unsigned) {
3836 using namespace llvm::support;
3837
3838 endian::Writer LE(Out, llvm::endianness::little);
3839
3840 auto MacroOffset = Writer.getMacroDirectivesOffset(Name: II);
3841 if (!isInterestingIdentifier(II, MacroOffset)) {
3842 LE.write<IdentifierID>(Val: ID << 1);
3843 return;
3844 }
3845
3846 LE.write<IdentifierID>(Val: (ID << 1) | 0x01);
3847 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3848 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3849 LE.write<uint16_t>(Val: Bits);
3850 Bits = 0;
3851 bool HadMacroDefinition = MacroOffset != 0;
3852 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3853 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3854 Bits = (Bits << 1) | unsigned(II->isPoisoned());
3855 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3856 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3857 LE.write<uint16_t>(Val: Bits);
3858
3859 if (HadMacroDefinition)
3860 LE.write<uint32_t>(Val: MacroOffset);
3861
3862 if (NeedDecls) {
3863 // Emit the declaration IDs in reverse order, because the
3864 // IdentifierResolver provides the declarations as they would be
3865 // visible (e.g., the function "stat" would come before the struct
3866 // "stat"), but the ASTReader adds declarations to the end of the list
3867 // (so we need to see the struct "stat" before the function "stat").
3868 // Only emit declarations that aren't from a chained PCH, though.
3869 SmallVector<NamedDecl *, 16> Decls(IdResolver.decls(Name: II));
3870 for (NamedDecl *D : llvm::reverse(C&: Decls))
3871 LE.write<DeclID>(Val: (DeclID)Writer.getDeclID(
3872 D: getDeclForLocalLookup(LangOpts: PP.getLangOpts(), D)));
3873 }
3874 }
3875};
3876
3877} // namespace
3878
3879/// If the \param IdentifierID ID is a local Identifier ID. If the higher
3880/// bits of ID is 0, it implies that the ID doesn't come from AST files.
3881static bool isLocalIdentifierID(IdentifierID ID) { return !(ID >> 32); }
3882
3883/// Write the identifier table into the AST file.
3884///
3885/// The identifier table consists of a blob containing string data
3886/// (the actual identifiers themselves) and a separate "offsets" index
3887/// that maps identifier IDs to locations within the blob.
3888void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3889 IdentifierResolver &IdResolver,
3890 bool IsModule) {
3891 using namespace llvm;
3892
3893 RecordData InterestingIdents;
3894
3895 // Create and write out the blob that contains the identifier
3896 // strings.
3897 {
3898 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3899 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule,
3900 IsModule ? &InterestingIdents : nullptr);
3901
3902 // Create the on-disk hash table representation. We only store offsets
3903 // for identifiers that appear here for the first time.
3904 IdentifierOffsets.resize(new_size: NextIdentID - FirstIdentID);
3905 for (auto IdentIDPair : IdentifierIDs) {
3906 const IdentifierInfo *II = IdentIDPair.first;
3907 IdentifierID ID = IdentIDPair.second;
3908 assert(II && "NULL identifier in identifier table");
3909
3910 // Write out identifiers if either the ID is local or the identifier has
3911 // changed since it was loaded.
3912 if (isLocalIdentifierID(ID) || II->hasChangedSinceDeserialization() ||
3913 (Trait.needDecls() &&
3914 II->hasFETokenInfoChangedSinceDeserialization()))
3915 Generator.insert(Key: II, Data: ID, InfoObj&: Trait);
3916 }
3917
3918 // Create the on-disk hash table in a buffer.
3919 SmallString<4096> IdentifierTable;
3920 uint32_t BucketOffset;
3921 {
3922 using namespace llvm::support;
3923
3924 llvm::raw_svector_ostream Out(IdentifierTable);
3925 // Make sure that no bucket is at offset 0
3926 endian::write<uint32_t>(os&: Out, value: 0, endian: llvm::endianness::little);
3927 BucketOffset = Generator.Emit(Out, InfoObj&: Trait);
3928 }
3929
3930 // Create a blob abbreviation
3931 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3932 Abbrev->Add(OpInfo: BitCodeAbbrevOp(IDENTIFIER_TABLE));
3933 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3934 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3935 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3936
3937 // Write the identifier table
3938 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3939 Stream.EmitRecordWithBlob(Abbrev: IDTableAbbrev, Vals: Record, Blob: IdentifierTable);
3940 }
3941
3942 // Write the offsets table for identifier IDs.
3943 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3944 Abbrev->Add(OpInfo: BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3945 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3946 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3947 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3948
3949#ifndef NDEBUG
3950 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3951 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3952#endif
3953
3954 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3955 IdentifierOffsets.size()};
3956 Stream.EmitRecordWithBlob(Abbrev: IdentifierOffsetAbbrev, Vals: Record,
3957 Blob: bytes(v: IdentifierOffsets));
3958
3959 // In C++, write the list of interesting identifiers (those that are
3960 // defined as macros, poisoned, or similar unusual things).
3961 if (!InterestingIdents.empty())
3962 Stream.EmitRecord(Code: INTERESTING_IDENTIFIERS, Vals: InterestingIdents);
3963}
3964
3965void ASTWriter::handleVTable(CXXRecordDecl *RD) {
3966 if (!RD->isInNamedModule())
3967 return;
3968
3969 PendingEmittingVTables.push_back(Elt: RD);
3970}
3971
3972//===----------------------------------------------------------------------===//
3973// DeclContext's Name Lookup Table Serialization
3974//===----------------------------------------------------------------------===//
3975
3976namespace {
3977
3978// Trait used for the on-disk hash table used in the method pool.
3979class ASTDeclContextNameLookupTrait {
3980 ASTWriter &Writer;
3981 llvm::SmallVector<LocalDeclID, 64> DeclIDs;
3982
3983public:
3984 using key_type = DeclarationNameKey;
3985 using key_type_ref = key_type;
3986
3987 /// A start and end index into DeclIDs, representing a sequence of decls.
3988 using data_type = std::pair<unsigned, unsigned>;
3989 using data_type_ref = const data_type &;
3990
3991 using hash_value_type = unsigned;
3992 using offset_type = unsigned;
3993
3994 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3995
3996 template<typename Coll>
3997 data_type getData(const Coll &Decls) {
3998 unsigned Start = DeclIDs.size();
3999 for (NamedDecl *D : Decls) {
4000 NamedDecl *DeclForLocalLookup =
4001 getDeclForLocalLookup(LangOpts: Writer.getLangOpts(), D);
4002
4003 if (Writer.getDoneWritingDeclsAndTypes() &&
4004 !Writer.wasDeclEmitted(D: DeclForLocalLookup))
4005 continue;
4006
4007 // Try to avoid writing internal decls to reduced BMI.
4008 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4009 if (Writer.isGeneratingReducedBMI() &&
4010 !DeclForLocalLookup->isFromExplicitGlobalModule() &&
4011 IsInternalDeclFromFileContext(D: DeclForLocalLookup))
4012 continue;
4013
4014 DeclIDs.push_back(Elt: Writer.GetDeclRef(D: DeclForLocalLookup));
4015 }
4016 return std::make_pair(x&: Start, y: DeclIDs.size());
4017 }
4018
4019 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
4020 unsigned Start = DeclIDs.size();
4021 DeclIDs.insert(
4022 I: DeclIDs.end(),
4023 From: DeclIDIterator<GlobalDeclID, LocalDeclID>(FromReader.begin()),
4024 To: DeclIDIterator<GlobalDeclID, LocalDeclID>(FromReader.end()));
4025 return std::make_pair(x&: Start, y: DeclIDs.size());
4026 }
4027
4028 static bool EqualKey(key_type_ref a, key_type_ref b) {
4029 return a == b;
4030 }
4031
4032 hash_value_type ComputeHash(DeclarationNameKey Name) {
4033 return Name.getHash();
4034 }
4035
4036 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
4037 assert(Writer.hasChain() &&
4038 "have reference to loaded module file but no chain?");
4039
4040 using namespace llvm::support;
4041
4042 endian::write<uint32_t>(os&: Out, value: Writer.getChain()->getModuleFileID(M: F),
4043 endian: llvm::endianness::little);
4044 }
4045
4046 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
4047 DeclarationNameKey Name,
4048 data_type_ref Lookup) {
4049 unsigned KeyLen = 1;
4050 switch (Name.getKind()) {
4051 case DeclarationName::Identifier:
4052 case DeclarationName::CXXLiteralOperatorName:
4053 case DeclarationName::CXXDeductionGuideName:
4054 KeyLen += sizeof(IdentifierID);
4055 break;
4056 case DeclarationName::ObjCZeroArgSelector:
4057 case DeclarationName::ObjCOneArgSelector:
4058 case DeclarationName::ObjCMultiArgSelector:
4059 KeyLen += 4;
4060 break;
4061 case DeclarationName::CXXOperatorName:
4062 KeyLen += 1;
4063 break;
4064 case DeclarationName::CXXConstructorName:
4065 case DeclarationName::CXXDestructorName:
4066 case DeclarationName::CXXConversionFunctionName:
4067 case DeclarationName::CXXUsingDirective:
4068 break;
4069 }
4070
4071 // length of DeclIDs.
4072 unsigned DataLen = sizeof(DeclID) * (Lookup.second - Lookup.first);
4073
4074 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
4075 }
4076
4077 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
4078 using namespace llvm::support;
4079
4080 endian::Writer LE(Out, llvm::endianness::little);
4081 LE.write<uint8_t>(Val: Name.getKind());
4082 switch (Name.getKind()) {
4083 case DeclarationName::Identifier:
4084 case DeclarationName::CXXLiteralOperatorName:
4085 case DeclarationName::CXXDeductionGuideName:
4086 LE.write<IdentifierID>(Val: Writer.getIdentifierRef(II: Name.getIdentifier()));
4087 return;
4088 case DeclarationName::ObjCZeroArgSelector:
4089 case DeclarationName::ObjCOneArgSelector:
4090 case DeclarationName::ObjCMultiArgSelector:
4091 LE.write<uint32_t>(Val: Writer.getSelectorRef(Sel: Name.getSelector()));
4092 return;
4093 case DeclarationName::CXXOperatorName:
4094 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
4095 "Invalid operator?");
4096 LE.write<uint8_t>(Val: Name.getOperatorKind());
4097 return;
4098 case DeclarationName::CXXConstructorName:
4099 case DeclarationName::CXXDestructorName:
4100 case DeclarationName::CXXConversionFunctionName:
4101 case DeclarationName::CXXUsingDirective:
4102 return;
4103 }
4104
4105 llvm_unreachable("Invalid name kind?");
4106 }
4107
4108 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
4109 unsigned DataLen) {
4110 using namespace llvm::support;
4111
4112 endian::Writer LE(Out, llvm::endianness::little);
4113 uint64_t Start = Out.tell(); (void)Start;
4114 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
4115 LE.write<DeclID>(Val: (DeclID)DeclIDs[I]);
4116 assert(Out.tell() - Start == DataLen && "Data length is wrong");
4117 }
4118};
4119
4120} // namespace
4121
4122bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
4123 DeclContext *DC) {
4124 return Result.hasExternalDecls() &&
4125 DC->hasNeedToReconcileExternalVisibleStorage();
4126}
4127
4128/// Returns ture if all of the lookup result are either external, not emitted or
4129/// predefined. In such cases, the lookup result is not interesting and we don't
4130/// need to record the result in the current being written module. Return false
4131/// otherwise.
4132static bool isLookupResultNotInteresting(ASTWriter &Writer,
4133 StoredDeclsList &Result) {
4134 for (auto *D : Result.getLookupResult()) {
4135 auto *LocalD = getDeclForLocalLookup(LangOpts: Writer.getLangOpts(), D);
4136 if (LocalD->isFromASTFile())
4137 continue;
4138
4139 // We can only be sure whether the local declaration is reachable
4140 // after we done writing the declarations and types.
4141 if (Writer.getDoneWritingDeclsAndTypes() && !Writer.wasDeclEmitted(D: LocalD))
4142 continue;
4143
4144 // We don't need to emit the predefined decls.
4145 if (Writer.isDeclPredefined(D: LocalD))
4146 continue;
4147
4148 return false;
4149 }
4150
4151 return true;
4152}
4153
4154void
4155ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
4156 llvm::SmallVectorImpl<char> &LookupTable) {
4157 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
4158 !ConstDC->hasLazyExternalLexicalLookups() &&
4159 "must call buildLookups first");
4160
4161 // FIXME: We need to build the lookups table, which is logically const.
4162 auto *DC = const_cast<DeclContext*>(ConstDC);
4163 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
4164
4165 // Create the on-disk hash table representation.
4166 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4167 ASTDeclContextNameLookupTrait> Generator;
4168 ASTDeclContextNameLookupTrait Trait(*this);
4169
4170 // The first step is to collect the declaration names which we need to
4171 // serialize into the name lookup table, and to collect them in a stable
4172 // order.
4173 SmallVector<DeclarationName, 16> Names;
4174
4175 // We also build up small sets of the constructor and conversion function
4176 // names which are visible.
4177 llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
4178
4179 for (auto &Lookup : *DC->buildLookup()) {
4180 auto &Name = Lookup.first;
4181 auto &Result = Lookup.second;
4182
4183 // If there are no local declarations in our lookup result, we
4184 // don't need to write an entry for the name at all. If we can't
4185 // write out a lookup set without performing more deserialization,
4186 // just skip this entry.
4187 //
4188 // Also in reduced BMI, we'd like to avoid writing unreachable
4189 // declarations in GMF, so we need to avoid writing declarations
4190 // that entirely external or unreachable.
4191 //
4192 // FIMXE: It looks sufficient to test
4193 // isLookupResultNotInteresting here. But due to bug we have
4194 // to test isLookupResultExternal here. See
4195 // https://github.com/llvm/llvm-project/issues/61065 for details.
4196 if ((GeneratingReducedBMI || isLookupResultExternal(Result, DC)) &&
4197 isLookupResultNotInteresting(Writer&: *this, Result))
4198 continue;
4199
4200 // We also skip empty results. If any of the results could be external and
4201 // the currently available results are empty, then all of the results are
4202 // external and we skip it above. So the only way we get here with an empty
4203 // results is when no results could have been external *and* we have
4204 // external results.
4205 //
4206 // FIXME: While we might want to start emitting on-disk entries for negative
4207 // lookups into a decl context as an optimization, today we *have* to skip
4208 // them because there are names with empty lookup results in decl contexts
4209 // which we can't emit in any stable ordering: we lookup constructors and
4210 // conversion functions in the enclosing namespace scope creating empty
4211 // results for them. This in almost certainly a bug in Clang's name lookup,
4212 // but that is likely to be hard or impossible to fix and so we tolerate it
4213 // here by omitting lookups with empty results.
4214 if (Lookup.second.getLookupResult().empty())
4215 continue;
4216
4217 switch (Lookup.first.getNameKind()) {
4218 default:
4219 Names.push_back(Elt: Lookup.first);
4220 break;
4221
4222 case DeclarationName::CXXConstructorName:
4223 assert(isa<CXXRecordDecl>(DC) &&
4224 "Cannot have a constructor name outside of a class!");
4225 ConstructorNameSet.insert(Ptr: Name);
4226 break;
4227
4228 case DeclarationName::CXXConversionFunctionName:
4229 assert(isa<CXXRecordDecl>(DC) &&
4230 "Cannot have a conversion function name outside of a class!");
4231 ConversionNameSet.insert(Ptr: Name);
4232 break;
4233 }
4234 }
4235
4236 // Sort the names into a stable order.
4237 llvm::sort(C&: Names);
4238
4239 if (auto *D = dyn_cast<CXXRecordDecl>(Val: DC)) {
4240 // We need to establish an ordering of constructor and conversion function
4241 // names, and they don't have an intrinsic ordering.
4242
4243 // First we try the easy case by forming the current context's constructor
4244 // name and adding that name first. This is a very useful optimization to
4245 // avoid walking the lexical declarations in many cases, and it also
4246 // handles the only case where a constructor name can come from some other
4247 // lexical context -- when that name is an implicit constructor merged from
4248 // another declaration in the redecl chain. Any non-implicit constructor or
4249 // conversion function which doesn't occur in all the lexical contexts
4250 // would be an ODR violation.
4251 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4252 Ty: Context->getCanonicalType(T: Context->getRecordType(Decl: D)));
4253 if (ConstructorNameSet.erase(Ptr: ImplicitCtorName))
4254 Names.push_back(Elt: ImplicitCtorName);
4255
4256 // If we still have constructors or conversion functions, we walk all the
4257 // names in the decl and add the constructors and conversion functions
4258 // which are visible in the order they lexically occur within the context.
4259 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4260 for (Decl *ChildD : cast<CXXRecordDecl>(Val: DC)->decls())
4261 if (auto *ChildND = dyn_cast<NamedDecl>(Val: ChildD)) {
4262 auto Name = ChildND->getDeclName();
4263 switch (Name.getNameKind()) {
4264 default:
4265 continue;
4266
4267 case DeclarationName::CXXConstructorName:
4268 if (ConstructorNameSet.erase(Ptr: Name))
4269 Names.push_back(Elt: Name);
4270 break;
4271
4272 case DeclarationName::CXXConversionFunctionName:
4273 if (ConversionNameSet.erase(Ptr: Name))
4274 Names.push_back(Elt: Name);
4275 break;
4276 }
4277
4278 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4279 break;
4280 }
4281
4282 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4283 "constructors by walking all the "
4284 "lexical members of the context.");
4285 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4286 "conversion functions by walking all "
4287 "the lexical members of the context.");
4288 }
4289
4290 // Next we need to do a lookup with each name into this decl context to fully
4291 // populate any results from external sources. We don't actually use the
4292 // results of these lookups because we only want to use the results after all
4293 // results have been loaded and the pointers into them will be stable.
4294 for (auto &Name : Names)
4295 DC->lookup(Name);
4296
4297 // Now we need to insert the results for each name into the hash table. For
4298 // constructor names and conversion function names, we actually need to merge
4299 // all of the results for them into one list of results each and insert
4300 // those.
4301 SmallVector<NamedDecl *, 8> ConstructorDecls;
4302 SmallVector<NamedDecl *, 8> ConversionDecls;
4303
4304 // Now loop over the names, either inserting them or appending for the two
4305 // special cases.
4306 for (auto &Name : Names) {
4307 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4308
4309 switch (Name.getNameKind()) {
4310 default:
4311 Generator.insert(Key: Name, Data: Trait.getData(Decls: Result), Info&: Trait);
4312 break;
4313
4314 case DeclarationName::CXXConstructorName:
4315 ConstructorDecls.append(in_start: Result.begin(), in_end: Result.end());
4316 break;
4317
4318 case DeclarationName::CXXConversionFunctionName:
4319 ConversionDecls.append(in_start: Result.begin(), in_end: Result.end());
4320 break;
4321 }
4322 }
4323
4324 // Handle our two special cases if we ended up having any. We arbitrarily use
4325 // the first declaration's name here because the name itself isn't part of
4326 // the key, only the kind of name is used.
4327 if (!ConstructorDecls.empty())
4328 Generator.insert(Key: ConstructorDecls.front()->getDeclName(),
4329 Data: Trait.getData(Decls: ConstructorDecls), Info&: Trait);
4330 if (!ConversionDecls.empty())
4331 Generator.insert(Key: ConversionDecls.front()->getDeclName(),
4332 Data: Trait.getData(Decls: ConversionDecls), Info&: Trait);
4333
4334 // Create the on-disk hash table. Also emit the existing imported and
4335 // merged table if there is one.
4336 auto *Lookups = Chain ? Chain->getLoadedLookupTables(Primary: DC) : nullptr;
4337 Generator.emit(Out&: LookupTable, Info&: Trait, Base: Lookups ? &Lookups->Table : nullptr);
4338}
4339
4340/// Write the block containing all of the declaration IDs
4341/// visible from the given DeclContext.
4342///
4343/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4344/// bitstream, or 0 if no block was written.
4345uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4346 DeclContext *DC) {
4347 // If we imported a key declaration of this namespace, write the visible
4348 // lookup results as an update record for it rather than including them
4349 // on this declaration. We will only look at key declarations on reload.
4350 if (isa<NamespaceDecl>(Val: DC) && Chain &&
4351 Chain->getKeyDeclaration(D: cast<Decl>(Val: DC))->isFromASTFile()) {
4352 // Only do this once, for the first local declaration of the namespace.
4353 for (auto *Prev = cast<NamespaceDecl>(Val: DC)->getPreviousDecl(); Prev;
4354 Prev = Prev->getPreviousDecl())
4355 if (!Prev->isFromASTFile())
4356 return 0;
4357
4358 // Note that we need to emit an update record for the primary context.
4359 UpdatedDeclContexts.insert(X: DC->getPrimaryContext());
4360
4361 // Make sure all visible decls are written. They will be recorded later. We
4362 // do this using a side data structure so we can sort the names into
4363 // a deterministic order.
4364 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4365 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4366 LookupResults;
4367 if (Map) {
4368 LookupResults.reserve(N: Map->size());
4369 for (auto &Entry : *Map)
4370 LookupResults.push_back(
4371 Elt: std::make_pair(x&: Entry.first, y: Entry.second.getLookupResult()));
4372 }
4373
4374 llvm::sort(C&: LookupResults, Comp: llvm::less_first());
4375 for (auto &NameAndResult : LookupResults) {
4376 DeclarationName Name = NameAndResult.first;
4377 DeclContext::lookup_result Result = NameAndResult.second;
4378 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4379 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4380 // We have to work around a name lookup bug here where negative lookup
4381 // results for these names get cached in namespace lookup tables (these
4382 // names should never be looked up in a namespace).
4383 assert(Result.empty() && "Cannot have a constructor or conversion "
4384 "function name in a namespace!");
4385 continue;
4386 }
4387
4388 for (NamedDecl *ND : Result) {
4389 if (ND->isFromASTFile())
4390 continue;
4391
4392 if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D: ND))
4393 continue;
4394
4395 // We don't need to force emitting internal decls into reduced BMI.
4396 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4397 if (GeneratingReducedBMI && !ND->isFromExplicitGlobalModule() &&
4398 IsInternalDeclFromFileContext(D: ND))
4399 continue;
4400
4401 GetDeclRef(D: ND);
4402 }
4403 }
4404
4405 return 0;
4406 }
4407
4408 if (DC->getPrimaryContext() != DC)
4409 return 0;
4410
4411 // Skip contexts which don't support name lookup.
4412 if (!DC->isLookupContext())
4413 return 0;
4414
4415 // If not in C++, we perform name lookup for the translation unit via the
4416 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4417 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4418 return 0;
4419
4420 // Serialize the contents of the mapping used for lookup. Note that,
4421 // although we have two very different code paths, the serialized
4422 // representation is the same for both cases: a declaration name,
4423 // followed by a size, followed by references to the visible
4424 // declarations that have that name.
4425 uint64_t Offset = Stream.GetCurrentBitNo();
4426 StoredDeclsMap *Map = DC->buildLookup();
4427 if (!Map || Map->empty())
4428 return 0;
4429
4430 // Create the on-disk hash table in a buffer.
4431 SmallString<4096> LookupTable;
4432 GenerateNameLookupTable(ConstDC: DC, LookupTable);
4433
4434 // Write the lookup table
4435 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4436 Stream.EmitRecordWithBlob(Abbrev: DeclContextVisibleLookupAbbrev, Vals: Record,
4437 Blob: LookupTable);
4438 ++NumVisibleDeclContexts;
4439 return Offset;
4440}
4441
4442/// Write an UPDATE_VISIBLE block for the given context.
4443///
4444/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4445/// DeclContext in a dependent AST file. As such, they only exist for the TU
4446/// (in C++), for namespaces, and for classes with forward-declared unscoped
4447/// enumeration members (in C++11).
4448void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
4449 StoredDeclsMap *Map = DC->getLookupPtr();
4450 if (!Map || Map->empty())
4451 return;
4452
4453 // Create the on-disk hash table in a buffer.
4454 SmallString<4096> LookupTable;
4455 GenerateNameLookupTable(ConstDC: DC, LookupTable);
4456
4457 // If we're updating a namespace, select a key declaration as the key for the
4458 // update record; those are the only ones that will be checked on reload.
4459 if (isa<NamespaceDecl>(Val: DC))
4460 DC = cast<DeclContext>(Val: Chain->getKeyDeclaration(D: cast<Decl>(Val: DC)));
4461
4462 // Write the lookup table
4463 RecordData::value_type Record[] = {UPDATE_VISIBLE,
4464 getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4465 Stream.EmitRecordWithBlob(Abbrev: UpdateVisibleAbbrev, Vals: Record, Blob: LookupTable);
4466}
4467
4468/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4469void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4470 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4471 Stream.EmitRecord(Code: FP_PRAGMA_OPTIONS, Vals: Record);
4472}
4473
4474/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4475void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4476 if (!SemaRef.Context.getLangOpts().OpenCL)
4477 return;
4478
4479 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4480 RecordData Record;
4481 for (const auto &I:Opts.OptMap) {
4482 AddString(Str: I.getKey(), Record);
4483 auto V = I.getValue();
4484 Record.push_back(Elt: V.Supported ? 1 : 0);
4485 Record.push_back(Elt: V.Enabled ? 1 : 0);
4486 Record.push_back(Elt: V.WithPragma ? 1 : 0);
4487 Record.push_back(Elt: V.Avail);
4488 Record.push_back(Elt: V.Core);
4489 Record.push_back(Elt: V.Opt);
4490 }
4491 Stream.EmitRecord(Code: OPENCL_EXTENSIONS, Vals: Record);
4492}
4493void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4494 if (SemaRef.CUDA().ForceHostDeviceDepth > 0) {
4495 RecordData::value_type Record[] = {SemaRef.CUDA().ForceHostDeviceDepth};
4496 Stream.EmitRecord(Code: CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Vals: Record);
4497 }
4498}
4499
4500void ASTWriter::WriteObjCCategories() {
4501 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4502 RecordData Categories;
4503
4504 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4505 unsigned Size = 0;
4506 unsigned StartIndex = Categories.size();
4507
4508 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4509
4510 // Allocate space for the size.
4511 Categories.push_back(Elt: 0);
4512
4513 // Add the categories.
4514 for (ObjCInterfaceDecl::known_categories_iterator
4515 Cat = Class->known_categories_begin(),
4516 CatEnd = Class->known_categories_end();
4517 Cat != CatEnd; ++Cat, ++Size) {
4518 assert(getDeclID(*Cat).isValid() && "Bogus category");
4519 AddDeclRef(D: *Cat, Record&: Categories);
4520 }
4521
4522 // Update the size.
4523 Categories[StartIndex] = Size;
4524
4525 // Record this interface -> category map.
4526 ObjCCategoriesInfo CatInfo = { getDeclID(D: Class), StartIndex };
4527 CategoriesMap.push_back(Elt: CatInfo);
4528 }
4529
4530 // Sort the categories map by the definition ID, since the reader will be
4531 // performing binary searches on this information.
4532 llvm::array_pod_sort(Start: CategoriesMap.begin(), End: CategoriesMap.end());
4533
4534 // Emit the categories map.
4535 using namespace llvm;
4536
4537 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4538 Abbrev->Add(OpInfo: BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4539 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4540 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4541 unsigned AbbrevID = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
4542
4543 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4544 Stream.EmitRecordWithBlob(Abbrev: AbbrevID, Vals: Record,
4545 BlobData: reinterpret_cast<char *>(CategoriesMap.data()),
4546 BlobLen: CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4547
4548 // Emit the category lists.
4549 Stream.EmitRecord(Code: OBJC_CATEGORIES, Vals: Categories);
4550}
4551
4552void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4553 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4554
4555 if (LPTMap.empty())
4556 return;
4557
4558 RecordData Record;
4559 for (auto &LPTMapEntry : LPTMap) {
4560 const FunctionDecl *FD = LPTMapEntry.first;
4561 LateParsedTemplate &LPT = *LPTMapEntry.second;
4562 AddDeclRef(D: FD, Record);
4563 AddDeclRef(D: LPT.D, Record);
4564 Record.push_back(Elt: LPT.FPO.getAsOpaqueInt());
4565 Record.push_back(Elt: LPT.Toks.size());
4566
4567 for (const auto &Tok : LPT.Toks) {
4568 AddToken(Tok, Record);
4569 }
4570 }
4571 Stream.EmitRecord(Code: LATE_PARSED_TEMPLATE, Vals: Record);
4572}
4573
4574/// Write the state of 'pragma clang optimize' at the end of the module.
4575void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4576 RecordData Record;
4577 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4578 AddSourceLocation(Loc: PragmaLoc, Record);
4579 Stream.EmitRecord(Code: OPTIMIZE_PRAGMA_OPTIONS, Vals: Record);
4580}
4581
4582/// Write the state of 'pragma ms_struct' at the end of the module.
4583void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4584 RecordData Record;
4585 Record.push_back(Elt: SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4586 Stream.EmitRecord(Code: MSSTRUCT_PRAGMA_OPTIONS, Vals: Record);
4587}
4588
4589/// Write the state of 'pragma pointers_to_members' at the end of the
4590//module.
4591void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4592 RecordData Record;
4593 Record.push_back(Elt: SemaRef.MSPointerToMemberRepresentationMethod);
4594 AddSourceLocation(Loc: SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4595 Stream.EmitRecord(Code: POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Vals: Record);
4596}
4597
4598/// Write the state of 'pragma align/pack' at the end of the module.
4599void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4600 // Don't serialize pragma align/pack state for modules, since it should only
4601 // take effect on a per-submodule basis.
4602 if (WritingModule)
4603 return;
4604
4605 RecordData Record;
4606 AddAlignPackInfo(Info: SemaRef.AlignPackStack.CurrentValue, Record);
4607 AddSourceLocation(Loc: SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4608 Record.push_back(Elt: SemaRef.AlignPackStack.Stack.size());
4609 for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4610 AddAlignPackInfo(Info: StackEntry.Value, Record);
4611 AddSourceLocation(Loc: StackEntry.PragmaLocation, Record);
4612 AddSourceLocation(Loc: StackEntry.PragmaPushLocation, Record);
4613 AddString(Str: StackEntry.StackSlotLabel, Record);
4614 }
4615 Stream.EmitRecord(Code: ALIGN_PACK_PRAGMA_OPTIONS, Vals: Record);
4616}
4617
4618/// Write the state of 'pragma float_control' at the end of the module.
4619void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4620 // Don't serialize pragma float_control state for modules,
4621 // since it should only take effect on a per-submodule basis.
4622 if (WritingModule)
4623 return;
4624
4625 RecordData Record;
4626 Record.push_back(Elt: SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4627 AddSourceLocation(Loc: SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4628 Record.push_back(Elt: SemaRef.FpPragmaStack.Stack.size());
4629 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4630 Record.push_back(Elt: StackEntry.Value.getAsOpaqueInt());
4631 AddSourceLocation(Loc: StackEntry.PragmaLocation, Record);
4632 AddSourceLocation(Loc: StackEntry.PragmaPushLocation, Record);
4633 AddString(Str: StackEntry.StackSlotLabel, Record);
4634 }
4635 Stream.EmitRecord(Code: FLOAT_CONTROL_PRAGMA_OPTIONS, Vals: Record);
4636}
4637
4638void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4639 ModuleFileExtensionWriter &Writer) {
4640 // Enter the extension block.
4641 Stream.EnterSubblock(BlockID: EXTENSION_BLOCK_ID, CodeLen: 4);
4642
4643 // Emit the metadata record abbreviation.
4644 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4645 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4646 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4647 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4648 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4649 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4650 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4651 unsigned Abbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
4652
4653 // Emit the metadata record.
4654 RecordData Record;
4655 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4656 Record.push_back(Elt: EXTENSION_METADATA);
4657 Record.push_back(Elt: Metadata.MajorVersion);
4658 Record.push_back(Elt: Metadata.MinorVersion);
4659 Record.push_back(Elt: Metadata.BlockName.size());
4660 Record.push_back(Elt: Metadata.UserInfo.size());
4661 SmallString<64> Buffer;
4662 Buffer += Metadata.BlockName;
4663 Buffer += Metadata.UserInfo;
4664 Stream.EmitRecordWithBlob(Abbrev, Vals: Record, Blob: Buffer);
4665
4666 // Emit the contents of the extension block.
4667 Writer.writeExtensionContents(SemaRef, Stream);
4668
4669 // Exit the extension block.
4670 Stream.ExitBlock();
4671}
4672
4673//===----------------------------------------------------------------------===//
4674// General Serialization Routines
4675//===----------------------------------------------------------------------===//
4676
4677void ASTRecordWriter::AddAttr(const Attr *A) {
4678 auto &Record = *this;
4679 // FIXME: Clang can't handle the serialization/deserialization of
4680 // preferred_name properly now. See
4681 // https://github.com/llvm/llvm-project/issues/56490 for example.
4682 if (!A || (isa<PreferredNameAttr>(Val: A) &&
4683 Writer->isWritingStdCXXNamedModules()))
4684 return Record.push_back(N: 0);
4685
4686 Record.push_back(N: A->getKind() + 1); // FIXME: stable encoding, target attrs
4687
4688 Record.AddIdentifierRef(II: A->getAttrName());
4689 Record.AddIdentifierRef(II: A->getScopeName());
4690 Record.AddSourceRange(Range: A->getRange());
4691 Record.AddSourceLocation(Loc: A->getScopeLoc());
4692 Record.push_back(N: A->getParsedKind());
4693 Record.push_back(N: A->getSyntax());
4694 Record.push_back(N: A->getAttributeSpellingListIndexRaw());
4695 Record.push_back(N: A->isRegularKeywordAttribute());
4696
4697#include "clang/Serialization/AttrPCHWrite.inc"
4698}
4699
4700/// Emit the list of attributes to the specified record.
4701void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4702 push_back(N: Attrs.size());
4703 for (const auto *A : Attrs)
4704 AddAttr(A);
4705}
4706
4707void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4708 AddSourceLocation(Loc: Tok.getLocation(), Record);
4709 // FIXME: Should translate token kind to a stable encoding.
4710 Record.push_back(Elt: Tok.getKind());
4711 // FIXME: Should translate token flags to a stable encoding.
4712 Record.push_back(Elt: Tok.getFlags());
4713
4714 if (Tok.isAnnotation()) {
4715 AddSourceLocation(Loc: Tok.getAnnotationEndLoc(), Record);
4716 switch (Tok.getKind()) {
4717 case tok::annot_pragma_loop_hint: {
4718 auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4719 AddToken(Tok: Info->PragmaName, Record);
4720 AddToken(Tok: Info->Option, Record);
4721 Record.push_back(Elt: Info->Toks.size());
4722 for (const auto &T : Info->Toks)
4723 AddToken(Tok: T, Record);
4724 break;
4725 }
4726 case tok::annot_pragma_pack: {
4727 auto *Info =
4728 static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
4729 Record.push_back(Elt: static_cast<unsigned>(Info->Action));
4730 AddString(Str: Info->SlotLabel, Record);
4731 AddToken(Tok: Info->Alignment, Record);
4732 break;
4733 }
4734 // Some annotation tokens do not use the PtrData field.
4735 case tok::annot_pragma_openmp:
4736 case tok::annot_pragma_openmp_end:
4737 case tok::annot_pragma_unused:
4738 case tok::annot_pragma_openacc:
4739 case tok::annot_pragma_openacc_end:
4740 break;
4741 default:
4742 llvm_unreachable("missing serialization code for annotation token");
4743 }
4744 } else {
4745 Record.push_back(Elt: Tok.getLength());
4746 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4747 // is needed.
4748 AddIdentifierRef(II: Tok.getIdentifierInfo(), Record);
4749 }
4750}
4751
4752void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4753 Record.push_back(Elt: Str.size());
4754 Record.insert(I: Record.end(), From: Str.begin(), To: Str.end());
4755}
4756
4757bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4758 assert(Context && "should have context when outputting path");
4759
4760 // Leave special file names as they are.
4761 StringRef PathStr(Path.data(), Path.size());
4762 if (PathStr == "<built-in>" || PathStr == "<command line>")
4763 return false;
4764
4765 bool Changed =
4766 cleanPathForOutput(FileMgr&: Context->getSourceManager().getFileManager(), Path);
4767
4768 // Remove a prefix to make the path relative, if relevant.
4769 const char *PathBegin = Path.data();
4770 const char *PathPtr =
4771 adjustFilenameForRelocatableAST(Filename: PathBegin, BaseDir: BaseDirectory);
4772 if (PathPtr != PathBegin) {
4773 Path.erase(CS: Path.begin(), CE: Path.begin() + (PathPtr - PathBegin));
4774 Changed = true;
4775 }
4776
4777 return Changed;
4778}
4779
4780void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4781 SmallString<128> FilePath(Path);
4782 PreparePathForOutput(Path&: FilePath);
4783 AddString(Str: FilePath, Record);
4784}
4785
4786void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4787 StringRef Path) {
4788 SmallString<128> FilePath(Path);
4789 PreparePathForOutput(Path&: FilePath);
4790 Stream.EmitRecordWithBlob(Abbrev, Vals: Record, Blob: FilePath);
4791}
4792
4793void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4794 RecordDataImpl &Record) {
4795 Record.push_back(Elt: Version.getMajor());
4796 if (std::optional<unsigned> Minor = Version.getMinor())
4797 Record.push_back(Elt: *Minor + 1);
4798 else
4799 Record.push_back(Elt: 0);
4800 if (std::optional<unsigned> Subminor = Version.getSubminor())
4801 Record.push_back(Elt: *Subminor + 1);
4802 else
4803 Record.push_back(Elt: 0);
4804}
4805
4806/// Note that the identifier II occurs at the given offset
4807/// within the identifier table.
4808void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4809 IdentifierID ID = IdentifierIDs[II];
4810 // Only store offsets new to this AST file. Other identifier names are looked
4811 // up earlier in the chain and thus don't need an offset.
4812 if (!isLocalIdentifierID(ID))
4813 return;
4814
4815 // For local identifiers, the module file index must be 0.
4816
4817 assert(ID != 0);
4818 ID -= NUM_PREDEF_IDENT_IDS;
4819 assert(ID < IdentifierOffsets.size());
4820 IdentifierOffsets[ID] = Offset;
4821}
4822
4823/// Note that the selector Sel occurs at the given offset
4824/// within the method pool/selector table.
4825void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4826 unsigned ID = SelectorIDs[Sel];
4827 assert(ID && "Unknown selector");
4828 // Don't record offsets for selectors that are also available in a different
4829 // file.
4830 if (ID < FirstSelectorID)
4831 return;
4832 SelectorOffsets[ID - FirstSelectorID] = Offset;
4833}
4834
4835ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4836 SmallVectorImpl<char> &Buffer,
4837 InMemoryModuleCache &ModuleCache,
4838 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4839 bool IncludeTimestamps, bool BuildingImplicitModule,
4840 bool GeneratingReducedBMI)
4841 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4842 IncludeTimestamps(IncludeTimestamps),
4843 BuildingImplicitModule(BuildingImplicitModule),
4844 GeneratingReducedBMI(GeneratingReducedBMI) {
4845 for (const auto &Ext : Extensions) {
4846 if (auto Writer = Ext->createExtensionWriter(Writer&: *this))
4847 ModuleFileExtensionWriters.push_back(x: std::move(Writer));
4848 }
4849}
4850
4851ASTWriter::~ASTWriter() = default;
4852
4853const LangOptions &ASTWriter::getLangOpts() const {
4854 assert(WritingAST && "can't determine lang opts when not writing AST");
4855 return Context->getLangOpts();
4856}
4857
4858time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4859 return IncludeTimestamps ? E->getModificationTime() : 0;
4860}
4861
4862ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
4863 Module *WritingModule, StringRef isysroot,
4864 bool ShouldCacheASTInMemory) {
4865 llvm::TimeTraceScope scope("WriteAST", OutputFile);
4866 WritingAST = true;
4867
4868 ASTHasCompilerErrors =
4869 SemaRef.PP.getDiagnostics().hasUncompilableErrorOccurred();
4870
4871 // Emit the file header.
4872 Stream.Emit(Val: (unsigned)'C', NumBits: 8);
4873 Stream.Emit(Val: (unsigned)'P', NumBits: 8);
4874 Stream.Emit(Val: (unsigned)'C', NumBits: 8);
4875 Stream.Emit(Val: (unsigned)'H', NumBits: 8);
4876
4877 WriteBlockInfoBlock();
4878
4879 Context = &SemaRef.Context;
4880 PP = &SemaRef.PP;
4881 this->WritingModule = WritingModule;
4882 ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
4883 Context = nullptr;
4884 PP = nullptr;
4885 this->WritingModule = nullptr;
4886 this->BaseDirectory.clear();
4887
4888 WritingAST = false;
4889 if (ShouldCacheASTInMemory) {
4890 // Construct MemoryBuffer and update buffer manager.
4891 ModuleCache.addBuiltPCM(Filename: OutputFile,
4892 Buffer: llvm::MemoryBuffer::getMemBufferCopy(
4893 InputData: StringRef(Buffer.begin(), Buffer.size())));
4894 }
4895 return Signature;
4896}
4897
4898template<typename Vector>
4899static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec) {
4900 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4901 I != E; ++I) {
4902 Writer.GetDeclRef(D: *I);
4903 }
4904}
4905
4906template <typename Vector>
4907static void AddLazyVectorEmiitedDecls(ASTWriter &Writer, Vector &Vec,
4908 ASTWriter::RecordData &Record) {
4909 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4910 I != E; ++I) {
4911 Writer.AddEmittedDeclRef(D: *I, Record);
4912 }
4913}
4914
4915void ASTWriter::computeNonAffectingInputFiles() {
4916 SourceManager &SrcMgr = PP->getSourceManager();
4917 unsigned N = SrcMgr.local_sloc_entry_size();
4918
4919 IsSLocAffecting.resize(N, t: true);
4920
4921 if (!WritingModule)
4922 return;
4923
4924 auto AffectingModuleMaps = GetAffectingModuleMaps(PP: *PP, RootModule: WritingModule);
4925
4926 unsigned FileIDAdjustment = 0;
4927 unsigned OffsetAdjustment = 0;
4928
4929 NonAffectingFileIDAdjustments.reserve(n: N);
4930 NonAffectingOffsetAdjustments.reserve(n: N);
4931
4932 NonAffectingFileIDAdjustments.push_back(x: FileIDAdjustment);
4933 NonAffectingOffsetAdjustments.push_back(x: OffsetAdjustment);
4934
4935 for (unsigned I = 1; I != N; ++I) {
4936 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(Index: I);
4937 FileID FID = FileID::get(V: I);
4938 assert(&SrcMgr.getSLocEntry(FID) == SLoc);
4939
4940 if (!SLoc->isFile())
4941 continue;
4942 const SrcMgr::FileInfo &File = SLoc->getFile();
4943 const SrcMgr::ContentCache *Cache = &File.getContentCache();
4944 if (!Cache->OrigEntry)
4945 continue;
4946
4947 // Don't prune anything other than module maps.
4948 if (!isModuleMap(CK: File.getFileCharacteristic()))
4949 continue;
4950
4951 // Don't prune module maps if all are guaranteed to be affecting.
4952 if (!AffectingModuleMaps)
4953 continue;
4954
4955 // Don't prune module maps that are affecting.
4956 if (llvm::is_contained(Range&: *AffectingModuleMaps, Element: *Cache->OrigEntry))
4957 continue;
4958
4959 IsSLocAffecting[I] = false;
4960
4961 FileIDAdjustment += 1;
4962 // Even empty files take up one element in the offset table.
4963 OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
4964
4965 // If the previous file was non-affecting as well, just extend its entry
4966 // with our information.
4967 if (!NonAffectingFileIDs.empty() &&
4968 NonAffectingFileIDs.back().ID == FID.ID - 1) {
4969 NonAffectingFileIDs.back() = FID;
4970 NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
4971 NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
4972 NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
4973 continue;
4974 }
4975
4976 NonAffectingFileIDs.push_back(x: FID);
4977 NonAffectingRanges.emplace_back(args: SrcMgr.getLocForStartOfFile(FID),
4978 args: SrcMgr.getLocForEndOfFile(FID));
4979 NonAffectingFileIDAdjustments.push_back(x: FileIDAdjustment);
4980 NonAffectingOffsetAdjustments.push_back(x: OffsetAdjustment);
4981 }
4982
4983 if (!PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesIncludeVFSUsage)
4984 return;
4985
4986 FileManager &FileMgr = PP->getFileManager();
4987 FileMgr.trackVFSUsage(Active: true);
4988 // Lookup the paths in the VFS to trigger `-ivfsoverlay` usage tracking.
4989 for (StringRef Path :
4990 PP->getHeaderSearchInfo().getHeaderSearchOpts().VFSOverlayFiles)
4991 FileMgr.getVirtualFileSystem().exists(Path);
4992 for (unsigned I = 1; I != N; ++I) {
4993 if (IsSLocAffecting[I]) {
4994 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(Index: I);
4995 if (!SLoc->isFile())
4996 continue;
4997 const SrcMgr::FileInfo &File = SLoc->getFile();
4998 const SrcMgr::ContentCache *Cache = &File.getContentCache();
4999 if (!Cache->OrigEntry)
5000 continue;
5001 FileMgr.getVirtualFileSystem().exists(
5002 Path: Cache->OrigEntry->getNameAsRequested());
5003 }
5004 }
5005 FileMgr.trackVFSUsage(Active: false);
5006}
5007
5008void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
5009 ASTContext &Context = SemaRef.Context;
5010
5011 bool isModule = WritingModule != nullptr;
5012
5013 // Set up predefined declaration IDs.
5014 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
5015 if (D) {
5016 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
5017 DeclIDs[D] = ID;
5018 PredefinedDecls.insert(Ptr: D);
5019 }
5020 };
5021 RegisterPredefDecl(Context.getTranslationUnitDecl(),
5022 PREDEF_DECL_TRANSLATION_UNIT_ID);
5023 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
5024 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
5025 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
5026 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
5027 PREDEF_DECL_OBJC_PROTOCOL_ID);
5028 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
5029 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
5030 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
5031 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
5032 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
5033 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
5034 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
5035 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
5036 RegisterPredefDecl(Context.MSGuidTagDecl,
5037 PREDEF_DECL_BUILTIN_MS_GUID_ID);
5038 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
5039 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
5040 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
5041 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
5042 PREDEF_DECL_CF_CONSTANT_STRING_ID);
5043 RegisterPredefDecl(Context.CFConstantStringTagDecl,
5044 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
5045 RegisterPredefDecl(Context.TypePackElementDecl,
5046 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
5047
5048 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5049
5050 // Force all top level declarations to be emitted.
5051 //
5052 // We start emitting top level declarations from the module purview to
5053 // implement the eliding unreachable declaration feature.
5054 for (const auto *D : TU->noload_decls()) {
5055 if (D->isFromASTFile())
5056 continue;
5057
5058 if (GeneratingReducedBMI) {
5059 if (D->isFromExplicitGlobalModule())
5060 continue;
5061
5062 // Don't force emitting static entities.
5063 //
5064 // Technically, all static entities shouldn't be in reduced BMI. The
5065 // language also specifies that the program exposes TU-local entities
5066 // is ill-formed. However, in practice, there are a lot of projects
5067 // uses `static inline` in the headers. So we can't get rid of all
5068 // static entities in reduced BMI now.
5069 if (IsInternalDeclFromFileContext(D))
5070 continue;
5071 }
5072
5073 // If we're writing C++ named modules, don't emit declarations which are
5074 // not from modules by default. They may be built in declarations (be
5075 // handled above) or implcit declarations (see the implementation of
5076 // `Sema::Initialize()` for example).
5077 if (isWritingStdCXXNamedModules() && !D->getOwningModule() &&
5078 D->isImplicit())
5079 continue;
5080
5081 GetDeclRef(D);
5082 }
5083
5084 if (GeneratingReducedBMI)
5085 return;
5086
5087 // Writing all of the tentative definitions in this file, in
5088 // TentativeDefinitions order. Generally, this record will be empty for
5089 // headers.
5090 RecordData TentativeDefinitions;
5091 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.TentativeDefinitions);
5092
5093 // Writing all of the file scoped decls in this file.
5094 if (!isModule)
5095 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.UnusedFileScopedDecls);
5096
5097 // Writing all of the delegating constructors we still need
5098 // to resolve.
5099 if (!isModule)
5100 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.DelegatingCtorDecls);
5101
5102 // Writing all of the ext_vector declarations.
5103 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.ExtVectorDecls);
5104
5105 // Writing all of the VTable uses information.
5106 if (!SemaRef.VTableUses.empty())
5107 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I)
5108 GetDeclRef(D: SemaRef.VTableUses[I].first);
5109
5110 // Writing all of the UnusedLocalTypedefNameCandidates.
5111 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5112 GetDeclRef(D: TD);
5113
5114 // Writing all of pending implicit instantiations.
5115 for (const auto &I : SemaRef.PendingInstantiations)
5116 GetDeclRef(D: I.first);
5117 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
5118 "There are local ones at end of translation unit!");
5119
5120 // Writing some declaration references.
5121 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5122 GetDeclRef(D: SemaRef.getStdNamespace());
5123 GetDeclRef(D: SemaRef.getStdBadAlloc());
5124 GetDeclRef(D: SemaRef.getStdAlignValT());
5125 }
5126
5127 if (Context.getcudaConfigureCallDecl())
5128 GetDeclRef(D: Context.getcudaConfigureCallDecl());
5129
5130 // Writing all of the known namespaces.
5131 for (const auto &I : SemaRef.KnownNamespaces)
5132 if (!I.second)
5133 GetDeclRef(D: I.first);
5134
5135 // Writing all used, undefined objects that require definitions.
5136 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5137 SemaRef.getUndefinedButUsed(Undefined);
5138 for (const auto &I : Undefined)
5139 GetDeclRef(D: I.first);
5140
5141 // Writing all delete-expressions that we would like to
5142 // analyze later in AST.
5143 if (!isModule)
5144 for (const auto &DeleteExprsInfo :
5145 SemaRef.getMismatchingDeleteExpressions())
5146 GetDeclRef(D: DeleteExprsInfo.first);
5147
5148 // Make sure visible decls, added to DeclContexts previously loaded from
5149 // an AST file, are registered for serialization. Likewise for template
5150 // specializations added to imported templates.
5151 for (const auto *I : DeclsToEmitEvenIfUnreferenced)
5152 GetDeclRef(D: I);
5153 DeclsToEmitEvenIfUnreferenced.clear();
5154
5155 // Make sure all decls associated with an identifier are registered for
5156 // serialization, if we're storing decls with identifiers.
5157 if (!WritingModule || !getLangOpts().CPlusPlus) {
5158 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
5159 for (const auto &ID : SemaRef.PP.getIdentifierTable()) {
5160 const IdentifierInfo *II = ID.second;
5161 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
5162 IIs.push_back(Elt: II);
5163 }
5164 // Sort the identifiers to visit based on their name.
5165 llvm::sort(C&: IIs, Comp: llvm::deref<std::less<>>());
5166 for (const IdentifierInfo *II : IIs)
5167 for (const Decl *D : SemaRef.IdResolver.decls(Name: II))
5168 GetDeclRef(D);
5169 }
5170
5171 // Write all of the DeclsToCheckForDeferredDiags.
5172 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5173 GetDeclRef(D);
5174
5175 // Write all classes that need to emit the vtable definitions if required.
5176 if (isWritingStdCXXNamedModules())
5177 for (CXXRecordDecl *RD : PendingEmittingVTables)
5178 GetDeclRef(D: RD);
5179 else
5180 PendingEmittingVTables.clear();
5181}
5182
5183void ASTWriter::WriteSpecialDeclRecords(Sema &SemaRef) {
5184 ASTContext &Context = SemaRef.Context;
5185
5186 bool isModule = WritingModule != nullptr;
5187
5188 // Write the record containing external, unnamed definitions.
5189 if (!EagerlyDeserializedDecls.empty())
5190 Stream.EmitRecord(Code: EAGERLY_DESERIALIZED_DECLS, Vals: EagerlyDeserializedDecls);
5191
5192 if (!ModularCodegenDecls.empty())
5193 Stream.EmitRecord(Code: MODULAR_CODEGEN_DECLS, Vals: ModularCodegenDecls);
5194
5195 // Write the record containing tentative definitions.
5196 RecordData TentativeDefinitions;
5197 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.TentativeDefinitions,
5198 Record&: TentativeDefinitions);
5199 if (!TentativeDefinitions.empty())
5200 Stream.EmitRecord(Code: TENTATIVE_DEFINITIONS, Vals: TentativeDefinitions);
5201
5202 // Write the record containing unused file scoped decls.
5203 RecordData UnusedFileScopedDecls;
5204 if (!isModule)
5205 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.UnusedFileScopedDecls,
5206 Record&: UnusedFileScopedDecls);
5207 if (!UnusedFileScopedDecls.empty())
5208 Stream.EmitRecord(Code: UNUSED_FILESCOPED_DECLS, Vals: UnusedFileScopedDecls);
5209
5210 // Write the record containing ext_vector type names.
5211 RecordData ExtVectorDecls;
5212 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.ExtVectorDecls, Record&: ExtVectorDecls);
5213 if (!ExtVectorDecls.empty())
5214 Stream.EmitRecord(Code: EXT_VECTOR_DECLS, Vals: ExtVectorDecls);
5215
5216 // Write the record containing VTable uses information.
5217 RecordData VTableUses;
5218 if (!SemaRef.VTableUses.empty()) {
5219 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
5220 CXXRecordDecl *D = SemaRef.VTableUses[I].first;
5221 if (!wasDeclEmitted(D))
5222 continue;
5223
5224 AddDeclRef(D, Record&: VTableUses);
5225 AddSourceLocation(Loc: SemaRef.VTableUses[I].second, Record&: VTableUses);
5226 VTableUses.push_back(Elt: SemaRef.VTablesUsed[D]);
5227 }
5228 Stream.EmitRecord(Code: VTABLE_USES, Vals: VTableUses);
5229 }
5230
5231 // Write the record containing potentially unused local typedefs.
5232 RecordData UnusedLocalTypedefNameCandidates;
5233 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5234 AddEmittedDeclRef(D: TD, Record&: UnusedLocalTypedefNameCandidates);
5235 if (!UnusedLocalTypedefNameCandidates.empty())
5236 Stream.EmitRecord(Code: UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5237 Vals: UnusedLocalTypedefNameCandidates);
5238
5239 // Write the record containing pending implicit instantiations.
5240 RecordData PendingInstantiations;
5241 for (const auto &I : SemaRef.PendingInstantiations) {
5242 if (!wasDeclEmitted(D: I.first))
5243 continue;
5244
5245 AddDeclRef(D: I.first, Record&: PendingInstantiations);
5246 AddSourceLocation(Loc: I.second, Record&: PendingInstantiations);
5247 }
5248 if (!PendingInstantiations.empty())
5249 Stream.EmitRecord(Code: PENDING_IMPLICIT_INSTANTIATIONS, Vals: PendingInstantiations);
5250
5251 // Write the record containing declaration references of Sema.
5252 RecordData SemaDeclRefs;
5253 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5254 auto AddEmittedDeclRefOrZero = [this, &SemaDeclRefs](Decl *D) {
5255 if (!D || !wasDeclEmitted(D))
5256 SemaDeclRefs.push_back(Elt: 0);
5257 else
5258 AddDeclRef(D, Record&: SemaDeclRefs);
5259 };
5260
5261 AddEmittedDeclRefOrZero(SemaRef.getStdNamespace());
5262 AddEmittedDeclRefOrZero(SemaRef.getStdBadAlloc());
5263 AddEmittedDeclRefOrZero(SemaRef.getStdAlignValT());
5264 }
5265 if (!SemaDeclRefs.empty())
5266 Stream.EmitRecord(Code: SEMA_DECL_REFS, Vals: SemaDeclRefs);
5267
5268 // Write the record containing decls to be checked for deferred diags.
5269 RecordData DeclsToCheckForDeferredDiags;
5270 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5271 if (wasDeclEmitted(D))
5272 AddDeclRef(D, Record&: DeclsToCheckForDeferredDiags);
5273 if (!DeclsToCheckForDeferredDiags.empty())
5274 Stream.EmitRecord(Code: DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5275 Vals: DeclsToCheckForDeferredDiags);
5276
5277 // Write the record containing CUDA-specific declaration references.
5278 RecordData CUDASpecialDeclRefs;
5279 if (auto *CudaCallDecl = Context.getcudaConfigureCallDecl();
5280 CudaCallDecl && wasDeclEmitted(D: CudaCallDecl)) {
5281 AddDeclRef(D: CudaCallDecl, Record&: CUDASpecialDeclRefs);
5282 Stream.EmitRecord(Code: CUDA_SPECIAL_DECL_REFS, Vals: CUDASpecialDeclRefs);
5283 }
5284
5285 // Write the delegating constructors.
5286 RecordData DelegatingCtorDecls;
5287 if (!isModule)
5288 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.DelegatingCtorDecls,
5289 Record&: DelegatingCtorDecls);
5290 if (!DelegatingCtorDecls.empty())
5291 Stream.EmitRecord(Code: DELEGATING_CTORS, Vals: DelegatingCtorDecls);
5292
5293 // Write the known namespaces.
5294 RecordData KnownNamespaces;
5295 for (const auto &I : SemaRef.KnownNamespaces) {
5296 if (!I.second && wasDeclEmitted(D: I.first))
5297 AddDeclRef(D: I.first, Record&: KnownNamespaces);
5298 }
5299 if (!KnownNamespaces.empty())
5300 Stream.EmitRecord(Code: KNOWN_NAMESPACES, Vals: KnownNamespaces);
5301
5302 // Write the undefined internal functions and variables, and inline functions.
5303 RecordData UndefinedButUsed;
5304 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5305 SemaRef.getUndefinedButUsed(Undefined);
5306 for (const auto &I : Undefined) {
5307 if (!wasDeclEmitted(D: I.first))
5308 continue;
5309
5310 AddDeclRef(D: I.first, Record&: UndefinedButUsed);
5311 AddSourceLocation(Loc: I.second, Record&: UndefinedButUsed);
5312 }
5313 if (!UndefinedButUsed.empty())
5314 Stream.EmitRecord(Code: UNDEFINED_BUT_USED, Vals: UndefinedButUsed);
5315
5316 // Write all delete-expressions that we would like to
5317 // analyze later in AST.
5318 RecordData DeleteExprsToAnalyze;
5319 if (!isModule) {
5320 for (const auto &DeleteExprsInfo :
5321 SemaRef.getMismatchingDeleteExpressions()) {
5322 if (!wasDeclEmitted(D: DeleteExprsInfo.first))
5323 continue;
5324
5325 AddDeclRef(D: DeleteExprsInfo.first, Record&: DeleteExprsToAnalyze);
5326 DeleteExprsToAnalyze.push_back(Elt: DeleteExprsInfo.second.size());
5327 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
5328 AddSourceLocation(Loc: DeleteLoc.first, Record&: DeleteExprsToAnalyze);
5329 DeleteExprsToAnalyze.push_back(Elt: DeleteLoc.second);
5330 }
5331 }
5332 }
5333 if (!DeleteExprsToAnalyze.empty())
5334 Stream.EmitRecord(Code: DELETE_EXPRS_TO_ANALYZE, Vals: DeleteExprsToAnalyze);
5335
5336 RecordData VTablesToEmit;
5337 for (CXXRecordDecl *RD : PendingEmittingVTables) {
5338 if (!wasDeclEmitted(D: RD))
5339 continue;
5340
5341 AddDeclRef(D: RD, Record&: VTablesToEmit);
5342 }
5343
5344 if (!VTablesToEmit.empty())
5345 Stream.EmitRecord(Code: VTABLES_TO_EMIT, Vals: VTablesToEmit);
5346}
5347
5348ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
5349 Module *WritingModule) {
5350 using namespace llvm;
5351
5352 bool isModule = WritingModule != nullptr;
5353
5354 // Make sure that the AST reader knows to finalize itself.
5355 if (Chain)
5356 Chain->finalizeForWriting();
5357
5358 ASTContext &Context = SemaRef.Context;
5359 Preprocessor &PP = SemaRef.PP;
5360
5361 // This needs to be done very early, since everything that writes
5362 // SourceLocations or FileIDs depends on it.
5363 computeNonAffectingInputFiles();
5364
5365 writeUnhashedControlBlock(PP, Context);
5366
5367 // Don't reuse type ID and Identifier ID from readers for C++ standard named
5368 // modules since we want to support no-transitive-change model for named
5369 // modules. The theory for no-transitive-change model is,
5370 // for a user of a named module, the user can only access the indirectly
5371 // imported decls via the directly imported module. So that it is possible to
5372 // control what matters to the users when writing the module. It would be
5373 // problematic if the users can reuse the type IDs and identifier IDs from
5374 // indirectly imported modules arbitrarily. So we choose to clear these ID
5375 // here.
5376 if (isWritingStdCXXNamedModules()) {
5377 TypeIdxs.clear();
5378 IdentifierIDs.clear();
5379 }
5380
5381 // Look for any identifiers that were named while processing the
5382 // headers, but are otherwise not needed. We add these to the hash
5383 // table to enable checking of the predefines buffer in the case
5384 // where the user adds new macro definitions when building the AST
5385 // file.
5386 //
5387 // We do this before emitting any Decl and Types to make sure the
5388 // Identifier ID is stable.
5389 SmallVector<const IdentifierInfo *, 128> IIs;
5390 for (const auto &ID : PP.getIdentifierTable())
5391 if (IsInterestingNonMacroIdentifier(II: ID.second, Writer&: *this))
5392 IIs.push_back(Elt: ID.second);
5393 // Sort the identifiers lexicographically before getting the references so
5394 // that their order is stable.
5395 llvm::sort(C&: IIs, Comp: llvm::deref<std::less<>>());
5396 for (const IdentifierInfo *II : IIs)
5397 getIdentifierRef(II);
5398
5399 // Write the set of weak, undeclared identifiers. We always write the
5400 // entire table, since later PCH files in a PCH chain are only interested in
5401 // the results at the end of the chain.
5402 RecordData WeakUndeclaredIdentifiers;
5403 for (const auto &WeakUndeclaredIdentifierList :
5404 SemaRef.WeakUndeclaredIdentifiers) {
5405 const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
5406 for (const auto &WI : WeakUndeclaredIdentifierList.second) {
5407 AddIdentifierRef(II, Record&: WeakUndeclaredIdentifiers);
5408 AddIdentifierRef(II: WI.getAlias(), Record&: WeakUndeclaredIdentifiers);
5409 AddSourceLocation(Loc: WI.getLocation(), Record&: WeakUndeclaredIdentifiers);
5410 }
5411 }
5412
5413 // Form the record of special types.
5414 RecordData SpecialTypes;
5415 AddTypeRef(T: Context.getRawCFConstantStringType(), Record&: SpecialTypes);
5416 AddTypeRef(T: Context.getFILEType(), Record&: SpecialTypes);
5417 AddTypeRef(T: Context.getjmp_bufType(), Record&: SpecialTypes);
5418 AddTypeRef(T: Context.getsigjmp_bufType(), Record&: SpecialTypes);
5419 AddTypeRef(T: Context.ObjCIdRedefinitionType, Record&: SpecialTypes);
5420 AddTypeRef(T: Context.ObjCClassRedefinitionType, Record&: SpecialTypes);
5421 AddTypeRef(T: Context.ObjCSelRedefinitionType, Record&: SpecialTypes);
5422 AddTypeRef(T: Context.getucontext_tType(), Record&: SpecialTypes);
5423
5424 PrepareWritingSpecialDecls(SemaRef);
5425
5426 // Write the control block
5427 WriteControlBlock(PP, Context, isysroot);
5428
5429 // Write the remaining AST contents.
5430 Stream.FlushToWord();
5431 ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
5432 Stream.EnterSubblock(BlockID: AST_BLOCK_ID, CodeLen: 5);
5433 ASTBlockStartOffset = Stream.GetCurrentBitNo();
5434
5435 // This is so that older clang versions, before the introduction
5436 // of the control block, can read and reject the newer PCH format.
5437 {
5438 RecordData Record = {VERSION_MAJOR};
5439 Stream.EmitRecord(Code: METADATA_OLD_FORMAT, Vals: Record);
5440 }
5441
5442 // For method pool in the module, if it contains an entry for a selector,
5443 // the entry should be complete, containing everything introduced by that
5444 // module and all modules it imports. It's possible that the entry is out of
5445 // date, so we need to pull in the new content here.
5446
5447 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
5448 // safe, we copy all selectors out.
5449 llvm::SmallVector<Selector, 256> AllSelectors;
5450 for (auto &SelectorAndID : SelectorIDs)
5451 AllSelectors.push_back(Elt: SelectorAndID.first);
5452 for (auto &Selector : AllSelectors)
5453 SemaRef.ObjC().updateOutOfDateSelector(Sel: Selector);
5454
5455 if (Chain) {
5456 // Write the mapping information describing our module dependencies and how
5457 // each of those modules were mapped into our own offset/ID space, so that
5458 // the reader can build the appropriate mapping to its own offset/ID space.
5459 // The map consists solely of a blob with the following format:
5460 // *(module-kind:i8
5461 // module-name-len:i16 module-name:len*i8
5462 // source-location-offset:i32
5463 // identifier-id:i32
5464 // preprocessed-entity-id:i32
5465 // macro-definition-id:i32
5466 // submodule-id:i32
5467 // selector-id:i32
5468 // declaration-id:i32
5469 // c++-base-specifiers-id:i32
5470 // type-id:i32)
5471 //
5472 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5473 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5474 // module name. Otherwise, it is the module file name.
5475 auto Abbrev = std::make_shared<BitCodeAbbrev>();
5476 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5477 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5478 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
5479 SmallString<2048> Buffer;
5480 {
5481 llvm::raw_svector_ostream Out(Buffer);
5482 for (ModuleFile &M : Chain->ModuleMgr) {
5483 using namespace llvm::support;
5484
5485 endian::Writer LE(Out, llvm::endianness::little);
5486 LE.write<uint8_t>(Val: static_cast<uint8_t>(M.Kind));
5487 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
5488 LE.write<uint16_t>(Val: Name.size());
5489 Out.write(Ptr: Name.data(), Size: Name.size());
5490
5491 // Note: if a base ID was uint max, it would not be possible to load
5492 // another module after it or have more than one entity inside it.
5493 uint32_t None = std::numeric_limits<uint32_t>::max();
5494
5495 auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
5496 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5497 if (ShouldWrite)
5498 LE.write<uint32_t>(BaseID);
5499 else
5500 LE.write<uint32_t>(Val: None);
5501 };
5502
5503 // These values should be unique within a chain, since they will be read
5504 // as keys into ContinuousRangeMaps.
5505 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5506 writeBaseIDOrNone(M.BasePreprocessedEntityID,
5507 M.NumPreprocessedEntities);
5508 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5509 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5510 }
5511 }
5512 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
5513 Stream.EmitRecordWithBlob(Abbrev: ModuleOffsetMapAbbrev, Vals: Record,
5514 BlobData: Buffer.data(), BlobLen: Buffer.size());
5515 }
5516
5517 WriteDeclAndTypes(Context);
5518
5519 WriteFileDeclIDsMap();
5520 WriteSourceManagerBlock(SourceMgr&: Context.getSourceManager(), PP);
5521 WriteComments();
5522 WritePreprocessor(PP, IsModule: isModule);
5523 WriteHeaderSearch(HS: PP.getHeaderSearchInfo());
5524 WriteSelectors(SemaRef);
5525 WriteReferencedSelectorsPool(SemaRef);
5526 WriteLateParsedTemplates(SemaRef);
5527 WriteIdentifierTable(PP, IdResolver&: SemaRef.IdResolver, IsModule: isModule);
5528 WriteFPPragmaOptions(Opts: SemaRef.CurFPFeatureOverrides());
5529 WriteOpenCLExtensions(SemaRef);
5530 WriteCUDAPragmas(SemaRef);
5531
5532 // If we're emitting a module, write out the submodule information.
5533 if (WritingModule)
5534 WriteSubmodules(WritingModule);
5535
5536 Stream.EmitRecord(Code: SPECIAL_TYPES, Vals: SpecialTypes);
5537
5538 WriteSpecialDeclRecords(SemaRef);
5539
5540 // Write the record containing weak undeclared identifiers.
5541 if (!WeakUndeclaredIdentifiers.empty())
5542 Stream.EmitRecord(Code: WEAK_UNDECLARED_IDENTIFIERS,
5543 Vals: WeakUndeclaredIdentifiers);
5544
5545 if (!WritingModule) {
5546 // Write the submodules that were imported, if any.
5547 struct ModuleInfo {
5548 uint64_t ID;
5549 Module *M;
5550 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5551 };
5552 llvm::SmallVector<ModuleInfo, 64> Imports;
5553 for (const auto *I : Context.local_imports()) {
5554 assert(SubmoduleIDs.contains(I->getImportedModule()));
5555 Imports.push_back(Elt: ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5556 I->getImportedModule()));
5557 }
5558
5559 if (!Imports.empty()) {
5560 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5561 return A.ID < B.ID;
5562 };
5563 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5564 return A.ID == B.ID;
5565 };
5566
5567 // Sort and deduplicate module IDs.
5568 llvm::sort(C&: Imports, Comp: Cmp);
5569 Imports.erase(CS: std::unique(first: Imports.begin(), last: Imports.end(), binary_pred: Eq),
5570 CE: Imports.end());
5571
5572 RecordData ImportedModules;
5573 for (const auto &Import : Imports) {
5574 ImportedModules.push_back(Elt: Import.ID);
5575 // FIXME: If the module has macros imported then later has declarations
5576 // imported, this location won't be the right one as a location for the
5577 // declaration imports.
5578 AddSourceLocation(Loc: PP.getModuleImportLoc(M: Import.M), Record&: ImportedModules);
5579 }
5580
5581 Stream.EmitRecord(Code: IMPORTED_MODULES, Vals: ImportedModules);
5582 }
5583 }
5584
5585 WriteObjCCategories();
5586 if(!WritingModule) {
5587 WriteOptimizePragmaOptions(SemaRef);
5588 WriteMSStructPragmaOptions(SemaRef);
5589 WriteMSPointersToMembersPragmaOptions(SemaRef);
5590 }
5591 WritePackPragmaOptions(SemaRef);
5592 WriteFloatControlPragmaOptions(SemaRef);
5593
5594 // Some simple statistics
5595 RecordData::value_type Record[] = {
5596 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
5597 Stream.EmitRecord(Code: STATISTICS, Vals: Record);
5598 Stream.ExitBlock();
5599 Stream.FlushToWord();
5600 ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
5601
5602 // Write the module file extension blocks.
5603 for (const auto &ExtWriter : ModuleFileExtensionWriters)
5604 WriteModuleFileExtension(SemaRef, Writer&: *ExtWriter);
5605
5606 return backpatchSignature();
5607}
5608
5609void ASTWriter::EnteringModulePurview() {
5610 // In C++20 named modules, all entities before entering the module purview
5611 // lives in the GMF.
5612 if (GeneratingReducedBMI)
5613 DeclUpdatesFromGMF.swap(RHS&: DeclUpdates);
5614}
5615
5616// Add update records for all mangling numbers and static local numbers.
5617// These aren't really update records, but this is a convenient way of
5618// tagging this rare extra data onto the declarations.
5619void ASTWriter::AddedManglingNumber(const Decl *D, unsigned Number) {
5620 if (D->isFromASTFile())
5621 return;
5622
5623 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_MANGLING_NUMBER, Number));
5624}
5625void ASTWriter::AddedStaticLocalNumbers(const Decl *D, unsigned Number) {
5626 if (D->isFromASTFile())
5627 return;
5628
5629 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_STATIC_LOCAL_NUMBER, Number));
5630}
5631
5632void ASTWriter::AddedAnonymousNamespace(const TranslationUnitDecl *TU,
5633 NamespaceDecl *AnonNamespace) {
5634 // If the translation unit has an anonymous namespace, and we don't already
5635 // have an update block for it, write it as an update block.
5636 // FIXME: Why do we not do this if there's already an update block?
5637 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
5638 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
5639 if (Record.empty())
5640 Record.push_back(Elt: DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
5641 }
5642}
5643
5644void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
5645 // Keep writing types, declarations, and declaration update records
5646 // until we've emitted all of them.
5647 RecordData DeclUpdatesOffsetsRecord;
5648 Stream.EnterSubblock(BlockID: DECLTYPES_BLOCK_ID, /*bits for abbreviations*/CodeLen: 5);
5649 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5650 WriteTypeAbbrevs();
5651 WriteDeclAbbrevs();
5652 do {
5653 WriteDeclUpdatesBlocks(OffsetsRecord&: DeclUpdatesOffsetsRecord);
5654 while (!DeclTypesToEmit.empty()) {
5655 DeclOrType DOT = DeclTypesToEmit.front();
5656 DeclTypesToEmit.pop();
5657 if (DOT.isType())
5658 WriteType(T: DOT.getType());
5659 else
5660 WriteDecl(Context, D: DOT.getDecl());
5661 }
5662 } while (!DeclUpdates.empty());
5663
5664 DoneWritingDeclsAndTypes = true;
5665
5666 // DelayedNamespace is only meaningful in reduced BMI.
5667 // See the comments of DelayedNamespace for details.
5668 assert(DelayedNamespace.empty() || GeneratingReducedBMI);
5669 RecordData DelayedNamespaceRecord;
5670 for (NamespaceDecl *NS : DelayedNamespace) {
5671 uint64_t LexicalOffset = WriteDeclContextLexicalBlock(Context, DC: NS);
5672 uint64_t VisibleOffset = WriteDeclContextVisibleBlock(Context, DC: NS);
5673
5674 // Write the offset relative to current block.
5675 if (LexicalOffset)
5676 LexicalOffset -= DeclTypesBlockStartOffset;
5677
5678 if (VisibleOffset)
5679 VisibleOffset -= DeclTypesBlockStartOffset;
5680
5681 AddDeclRef(D: NS, Record&: DelayedNamespaceRecord);
5682 DelayedNamespaceRecord.push_back(Elt: LexicalOffset);
5683 DelayedNamespaceRecord.push_back(Elt: VisibleOffset);
5684 }
5685
5686 // The process of writing lexical and visible block for delayed namespace
5687 // shouldn't introduce any new decls, types or update to emit.
5688 assert(DeclTypesToEmit.empty());
5689 assert(DeclUpdates.empty());
5690
5691 Stream.ExitBlock();
5692
5693 // These things can only be done once we've written out decls and types.
5694 WriteTypeDeclOffsets();
5695 if (!DeclUpdatesOffsetsRecord.empty())
5696 Stream.EmitRecord(Code: DECL_UPDATE_OFFSETS, Vals: DeclUpdatesOffsetsRecord);
5697
5698 if (!DelayedNamespaceRecord.empty())
5699 Stream.EmitRecord(Code: DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD,
5700 Vals: DelayedNamespaceRecord);
5701
5702 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5703 // Create a lexical update block containing all of the declarations in the
5704 // translation unit that do not come from other AST files.
5705 SmallVector<DeclID, 128> NewGlobalKindDeclPairs;
5706 for (const auto *D : TU->noload_decls()) {
5707 if (D->isFromASTFile())
5708 continue;
5709
5710 // In reduced BMI, skip unreached declarations.
5711 if (!wasDeclEmitted(D))
5712 continue;
5713
5714 NewGlobalKindDeclPairs.push_back(Elt: D->getKind());
5715 NewGlobalKindDeclPairs.push_back(Elt: GetDeclRef(D).getRawValue());
5716 }
5717
5718 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
5719 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
5720 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5721 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
5722
5723 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
5724 Stream.EmitRecordWithBlob(Abbrev: TuUpdateLexicalAbbrev, Vals: Record,
5725 Blob: bytes(v: NewGlobalKindDeclPairs));
5726
5727 Abv = std::make_shared<llvm::BitCodeAbbrev>();
5728 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
5729 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5730 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5731 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
5732
5733 // And a visible updates block for the translation unit.
5734 WriteDeclContextVisibleUpdate(DC: TU);
5735
5736 // If we have any extern "C" names, write out a visible update for them.
5737 if (Context.ExternCContext)
5738 WriteDeclContextVisibleUpdate(DC: Context.ExternCContext);
5739
5740 // Write the visible updates to DeclContexts.
5741 for (auto *DC : UpdatedDeclContexts)
5742 WriteDeclContextVisibleUpdate(DC);
5743}
5744
5745void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
5746 if (DeclUpdates.empty())
5747 return;
5748
5749 DeclUpdateMap LocalUpdates;
5750 LocalUpdates.swap(RHS&: DeclUpdates);
5751
5752 for (auto &DeclUpdate : LocalUpdates) {
5753 const Decl *D = DeclUpdate.first;
5754
5755 bool HasUpdatedBody = false;
5756 bool HasAddedVarDefinition = false;
5757 RecordData RecordData;
5758 ASTRecordWriter Record(*this, RecordData);
5759 for (auto &Update : DeclUpdate.second) {
5760 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5761
5762 // An updated body is emitted last, so that the reader doesn't need
5763 // to skip over the lazy body to reach statements for other records.
5764 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5765 HasUpdatedBody = true;
5766 else if (Kind == UPD_CXX_ADDED_VAR_DEFINITION)
5767 HasAddedVarDefinition = true;
5768 else
5769 Record.push_back(N: Kind);
5770
5771 switch (Kind) {
5772 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5773 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5774 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
5775 assert(Update.getDecl() && "no decl to add?");
5776 Record.AddDeclRef(D: Update.getDecl());
5777 break;
5778
5779 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
5780 case UPD_CXX_ADDED_VAR_DEFINITION:
5781 break;
5782
5783 case UPD_CXX_POINT_OF_INSTANTIATION:
5784 // FIXME: Do we need to also save the template specialization kind here?
5785 Record.AddSourceLocation(Loc: Update.getLoc());
5786 break;
5787
5788 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
5789 Record.writeStmtRef(
5790 S: cast<ParmVarDecl>(Val: Update.getDecl())->getDefaultArg());
5791 break;
5792
5793 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5794 Record.AddStmt(
5795 S: cast<FieldDecl>(Val: Update.getDecl())->getInClassInitializer());
5796 break;
5797
5798 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5799 auto *RD = cast<CXXRecordDecl>(Val: D);
5800 UpdatedDeclContexts.insert(X: RD->getPrimaryContext());
5801 Record.push_back(N: RD->isParamDestroyedInCallee());
5802 Record.push_back(N: llvm::to_underlying(E: RD->getArgPassingRestrictions()));
5803 Record.AddCXXDefinitionData(D: RD);
5804 Record.AddOffset(BitOffset: WriteDeclContextLexicalBlock(Context&: *Context, DC: RD));
5805
5806 // This state is sometimes updated by template instantiation, when we
5807 // switch from the specialization referring to the template declaration
5808 // to it referring to the template definition.
5809 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5810 Record.push_back(N: MSInfo->getTemplateSpecializationKind());
5811 Record.AddSourceLocation(Loc: MSInfo->getPointOfInstantiation());
5812 } else {
5813 auto *Spec = cast<ClassTemplateSpecializationDecl>(Val: RD);
5814 Record.push_back(N: Spec->getTemplateSpecializationKind());
5815 Record.AddSourceLocation(Loc: Spec->getPointOfInstantiation());
5816
5817 // The instantiation might have been resolved to a partial
5818 // specialization. If so, record which one.
5819 auto From = Spec->getInstantiatedFrom();
5820 if (auto PartialSpec =
5821 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5822 Record.push_back(N: true);
5823 Record.AddDeclRef(D: PartialSpec);
5824 Record.AddTemplateArgumentList(
5825 TemplateArgs: &Spec->getTemplateInstantiationArgs());
5826 } else {
5827 Record.push_back(N: false);
5828 }
5829 }
5830 Record.push_back(N: llvm::to_underlying(E: RD->getTagKind()));
5831 Record.AddSourceLocation(Loc: RD->getLocation());
5832 Record.AddSourceLocation(Loc: RD->getBeginLoc());
5833 Record.AddSourceRange(Range: RD->getBraceRange());
5834
5835 // Instantiation may change attributes; write them all out afresh.
5836 Record.push_back(N: D->hasAttrs());
5837 if (D->hasAttrs())
5838 Record.AddAttributes(Attrs: D->getAttrs());
5839
5840 // FIXME: Ensure we don't get here for explicit instantiations.
5841 break;
5842 }
5843
5844 case UPD_CXX_RESOLVED_DTOR_DELETE:
5845 Record.AddDeclRef(D: Update.getDecl());
5846 Record.AddStmt(S: cast<CXXDestructorDecl>(Val: D)->getOperatorDeleteThisArg());
5847 break;
5848
5849 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5850 auto prototype =
5851 cast<FunctionDecl>(Val: D)->getType()->castAs<FunctionProtoType>();
5852 Record.writeExceptionSpecInfo(esi: prototype->getExceptionSpecInfo());
5853 break;
5854 }
5855
5856 case UPD_CXX_DEDUCED_RETURN_TYPE:
5857 Record.push_back(N: GetOrCreateTypeID(T: Update.getType()));
5858 break;
5859
5860 case UPD_DECL_MARKED_USED:
5861 break;
5862
5863 case UPD_MANGLING_NUMBER:
5864 case UPD_STATIC_LOCAL_NUMBER:
5865 Record.push_back(N: Update.getNumber());
5866 break;
5867
5868 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
5869 Record.AddSourceRange(
5870 Range: D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5871 break;
5872
5873 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5874 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5875 Record.push_back(N: A->getAllocatorType());
5876 Record.AddStmt(S: A->getAllocator());
5877 Record.AddStmt(S: A->getAlignment());
5878 Record.AddSourceRange(Range: A->getRange());
5879 break;
5880 }
5881
5882 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5883 Record.push_back(N: D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5884 Record.AddSourceRange(
5885 Range: D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5886 break;
5887
5888 case UPD_DECL_EXPORTED:
5889 Record.push_back(N: getSubmoduleID(Mod: Update.getModule()));
5890 break;
5891
5892 case UPD_ADDED_ATTR_TO_RECORD:
5893 Record.AddAttributes(Attrs: llvm::ArrayRef(Update.getAttr()));
5894 break;
5895 }
5896 }
5897
5898 // Add a trailing update record, if any. These must go last because we
5899 // lazily load their attached statement.
5900 if (!GeneratingReducedBMI || !CanElideDeclDef(D)) {
5901 if (HasUpdatedBody) {
5902 const auto *Def = cast<FunctionDecl>(Val: D);
5903 Record.push_back(N: UPD_CXX_ADDED_FUNCTION_DEFINITION);
5904 Record.push_back(N: Def->isInlined());
5905 Record.AddSourceLocation(Loc: Def->getInnerLocStart());
5906 Record.AddFunctionDefinition(FD: Def);
5907 } else if (HasAddedVarDefinition) {
5908 const auto *VD = cast<VarDecl>(Val: D);
5909 Record.push_back(N: UPD_CXX_ADDED_VAR_DEFINITION);
5910 Record.push_back(N: VD->isInline());
5911 Record.push_back(N: VD->isInlineSpecified());
5912 Record.AddVarDeclInit(VD);
5913 }
5914 }
5915
5916 AddDeclRef(D, Record&: OffsetsRecord);
5917 OffsetsRecord.push_back(Elt: Record.Emit(Code: DECL_UPDATES));
5918 }
5919}
5920
5921void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
5922 RecordDataImpl &Record) {
5923 uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
5924 Record.push_back(Elt: Raw);
5925}
5926
5927FileID ASTWriter::getAdjustedFileID(FileID FID) const {
5928 if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
5929 NonAffectingFileIDs.empty())
5930 return FID;
5931 auto It = llvm::lower_bound(Range: NonAffectingFileIDs, Value&: FID);
5932 unsigned Idx = std::distance(first: NonAffectingFileIDs.begin(), last: It);
5933 unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5934 return FileID::get(V: FID.getOpaqueValue() - Offset);
5935}
5936
5937unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
5938 unsigned NumCreatedFIDs = PP->getSourceManager()
5939 .getLocalSLocEntry(Index: FID.ID)
5940 .getFile()
5941 .NumCreatedFIDs;
5942
5943 unsigned AdjustedNumCreatedFIDs = 0;
5944 for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
5945 if (IsSLocAffecting[I])
5946 ++AdjustedNumCreatedFIDs;
5947 return AdjustedNumCreatedFIDs;
5948}
5949
5950SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
5951 if (Loc.isInvalid())
5952 return Loc;
5953 return Loc.getLocWithOffset(Offset: -getAdjustment(Offset: Loc.getOffset()));
5954}
5955
5956SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
5957 return SourceRange(getAdjustedLocation(Loc: Range.getBegin()),
5958 getAdjustedLocation(Loc: Range.getEnd()));
5959}
5960
5961SourceLocation::UIntTy
5962ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
5963 return Offset - getAdjustment(Offset);
5964}
5965
5966SourceLocation::UIntTy
5967ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
5968 if (NonAffectingRanges.empty())
5969 return 0;
5970
5971 if (PP->getSourceManager().isLoadedOffset(SLocOffset: Offset))
5972 return 0;
5973
5974 if (Offset > NonAffectingRanges.back().getEnd().getOffset())
5975 return NonAffectingOffsetAdjustments.back();
5976
5977 if (Offset < NonAffectingRanges.front().getBegin().getOffset())
5978 return 0;
5979
5980 auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5981 return Range.getEnd().getOffset() < Offset;
5982 };
5983
5984 auto It = llvm::lower_bound(Range: NonAffectingRanges, Value&: Offset, C: Contains);
5985 unsigned Idx = std::distance(first: NonAffectingRanges.begin(), last: It);
5986 return NonAffectingOffsetAdjustments[Idx];
5987}
5988
5989void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
5990 Record.push_back(Elt: getAdjustedFileID(FID).getOpaqueValue());
5991}
5992
5993SourceLocationEncoding::RawLocEncoding
5994ASTWriter::getRawSourceLocationEncoding(SourceLocation Loc, LocSeq *Seq) {
5995 unsigned BaseOffset = 0;
5996 unsigned ModuleFileIndex = 0;
5997
5998 // See SourceLocationEncoding.h for the encoding details.
5999 if (Context->getSourceManager().isLoadedSourceLocation(Loc) &&
6000 Loc.isValid()) {
6001 assert(getChain());
6002 auto SLocMapI = getChain()->GlobalSLocOffsetMap.find(
6003 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6004 assert(SLocMapI != getChain()->GlobalSLocOffsetMap.end() &&
6005 "Corrupted global sloc offset map");
6006 ModuleFile *F = SLocMapI->second;
6007 BaseOffset = F->SLocEntryBaseOffset - 2;
6008 // 0 means the location is not loaded. So we need to add 1 to the index to
6009 // make it clear.
6010 ModuleFileIndex = F->Index + 1;
6011 assert(&getChain()->getModuleManager()[F->Index] == F);
6012 }
6013
6014 return SourceLocationEncoding::encode(Loc, BaseOffset, BaseModuleFileIndex: ModuleFileIndex, Seq);
6015}
6016
6017void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
6018 SourceLocationSequence *Seq) {
6019 Loc = getAdjustedLocation(Loc);
6020 Record.push_back(Elt: getRawSourceLocationEncoding(Loc, Seq));
6021}
6022
6023void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record,
6024 SourceLocationSequence *Seq) {
6025 AddSourceLocation(Loc: Range.getBegin(), Record, Seq);
6026 AddSourceLocation(Loc: Range.getEnd(), Record, Seq);
6027}
6028
6029void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
6030 AddAPInt(Value: Value.bitcastToAPInt());
6031}
6032
6033void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
6034 Record.push_back(Elt: getIdentifierRef(II));
6035}
6036
6037IdentifierID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
6038 if (!II)
6039 return 0;
6040
6041 IdentifierID &ID = IdentifierIDs[II];
6042 if (ID == 0)
6043 ID = NextIdentID++;
6044 return ID;
6045}
6046
6047MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
6048 // Don't emit builtin macros like __LINE__ to the AST file unless they
6049 // have been redefined by the header (in which case they are not
6050 // isBuiltinMacro).
6051 if (!MI || MI->isBuiltinMacro())
6052 return 0;
6053
6054 MacroID &ID = MacroIDs[MI];
6055 if (ID == 0) {
6056 ID = NextMacroID++;
6057 MacroInfoToEmitData Info = { .Name: Name, .MI: MI, .ID: ID };
6058 MacroInfosToEmit.push_back(x: Info);
6059 }
6060 return ID;
6061}
6062
6063MacroID ASTWriter::getMacroID(MacroInfo *MI) {
6064 if (!MI || MI->isBuiltinMacro())
6065 return 0;
6066
6067 assert(MacroIDs.contains(MI) && "Macro not emitted!");
6068 return MacroIDs[MI];
6069}
6070
6071uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
6072 return IdentMacroDirectivesOffsetMap.lookup(Val: Name);
6073}
6074
6075void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
6076 Record->push_back(Elt: Writer->getSelectorRef(Sel: SelRef));
6077}
6078
6079SelectorID ASTWriter::getSelectorRef(Selector Sel) {
6080 if (Sel.getAsOpaquePtr() == nullptr) {
6081 return 0;
6082 }
6083
6084 SelectorID SID = SelectorIDs[Sel];
6085 if (SID == 0 && Chain) {
6086 // This might trigger a ReadSelector callback, which will set the ID for
6087 // this selector.
6088 Chain->LoadSelector(Sel);
6089 SID = SelectorIDs[Sel];
6090 }
6091 if (SID == 0) {
6092 SID = NextSelectorID++;
6093 SelectorIDs[Sel] = SID;
6094 }
6095 return SID;
6096}
6097
6098void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
6099 AddDeclRef(D: Temp->getDestructor());
6100}
6101
6102void ASTRecordWriter::AddTemplateArgumentLocInfo(
6103 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
6104 switch (Kind) {
6105 case TemplateArgument::Expression:
6106 AddStmt(S: Arg.getAsExpr());
6107 break;
6108 case TemplateArgument::Type:
6109 AddTypeSourceInfo(TInfo: Arg.getAsTypeSourceInfo());
6110 break;
6111 case TemplateArgument::Template:
6112 AddNestedNameSpecifierLoc(NNS: Arg.getTemplateQualifierLoc());
6113 AddSourceLocation(Loc: Arg.getTemplateNameLoc());
6114 break;
6115 case TemplateArgument::TemplateExpansion:
6116 AddNestedNameSpecifierLoc(NNS: Arg.getTemplateQualifierLoc());
6117 AddSourceLocation(Loc: Arg.getTemplateNameLoc());
6118 AddSourceLocation(Loc: Arg.getTemplateEllipsisLoc());
6119 break;
6120 case TemplateArgument::Null:
6121 case TemplateArgument::Integral:
6122 case TemplateArgument::Declaration:
6123 case TemplateArgument::NullPtr:
6124 case TemplateArgument::StructuralValue:
6125 case TemplateArgument::Pack:
6126 // FIXME: Is this right?
6127 break;
6128 }
6129}
6130
6131void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
6132 AddTemplateArgument(Arg: Arg.getArgument());
6133
6134 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
6135 bool InfoHasSameExpr
6136 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
6137 Record->push_back(Elt: InfoHasSameExpr);
6138 if (InfoHasSameExpr)
6139 return; // Avoid storing the same expr twice.
6140 }
6141 AddTemplateArgumentLocInfo(Kind: Arg.getArgument().getKind(), Arg: Arg.getLocInfo());
6142}
6143
6144void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
6145 if (!TInfo) {
6146 AddTypeRef(T: QualType());
6147 return;
6148 }
6149
6150 AddTypeRef(T: TInfo->getType());
6151 AddTypeLoc(TL: TInfo->getTypeLoc());
6152}
6153
6154void ASTRecordWriter::AddTypeLoc(TypeLoc TL, LocSeq *OuterSeq) {
6155 LocSeq::State Seq(OuterSeq);
6156 TypeLocWriter TLW(*this, Seq);
6157 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6158 TLW.Visit(TyLoc: TL);
6159}
6160
6161void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
6162 Record.push_back(Elt: GetOrCreateTypeID(T));
6163}
6164
6165template <typename IdxForTypeTy>
6166static TypeID MakeTypeID(ASTContext &Context, QualType T,
6167 IdxForTypeTy IdxForType) {
6168 if (T.isNull())
6169 return PREDEF_TYPE_NULL_ID;
6170
6171 unsigned FastQuals = T.getLocalFastQualifiers();
6172 T.removeLocalFastQualifiers();
6173
6174 if (T.hasLocalNonFastQualifiers())
6175 return IdxForType(T).asTypeID(FastQuals);
6176
6177 assert(!T.hasLocalQualifiers());
6178
6179 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Val: T.getTypePtr()))
6180 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
6181
6182 if (T == Context.AutoDeductTy)
6183 return TypeIdx(0, PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
6184 if (T == Context.AutoRRefDeductTy)
6185 return TypeIdx(0, PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
6186
6187 return IdxForType(T).asTypeID(FastQuals);
6188}
6189
6190TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
6191 assert(Context);
6192 return MakeTypeID(Context&: *Context, T, IdxForType: [&](QualType T) -> TypeIdx {
6193 if (T.isNull())
6194 return TypeIdx();
6195 assert(!T.getLocalFastQualifiers());
6196
6197 TypeIdx &Idx = TypeIdxs[T];
6198 if (Idx.getValue() == 0) {
6199 if (DoneWritingDeclsAndTypes) {
6200 assert(0 && "New type seen after serializing all the types to emit!");
6201 return TypeIdx();
6202 }
6203
6204 // We haven't seen this type before. Assign it a new ID and put it
6205 // into the queue of types to emit.
6206 Idx = TypeIdx(0, NextTypeID++);
6207 DeclTypesToEmit.push(x: T);
6208 }
6209 return Idx;
6210 });
6211}
6212
6213void ASTWriter::AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record) {
6214 if (!wasDeclEmitted(D))
6215 return;
6216
6217 AddDeclRef(D, Record);
6218}
6219
6220void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
6221 Record.push_back(Elt: GetDeclRef(D).getRawValue());
6222}
6223
6224LocalDeclID ASTWriter::GetDeclRef(const Decl *D) {
6225 assert(WritingAST && "Cannot request a declaration ID before AST writing");
6226
6227 if (!D) {
6228 return LocalDeclID();
6229 }
6230
6231 // If the DeclUpdate from the GMF gets touched, emit it.
6232 if (auto *Iter = DeclUpdatesFromGMF.find(Key: D);
6233 Iter != DeclUpdatesFromGMF.end()) {
6234 for (DeclUpdate &Update : Iter->second)
6235 DeclUpdates[D].push_back(Elt: Update);
6236 DeclUpdatesFromGMF.erase(Iterator: Iter);
6237 }
6238
6239 // If D comes from an AST file, its declaration ID is already known and
6240 // fixed.
6241 if (D->isFromASTFile()) {
6242 if (isWritingStdCXXNamedModules() && D->getOwningModule())
6243 TouchedTopLevelModules.insert(V: D->getOwningModule()->getTopLevelModule());
6244
6245 return LocalDeclID(D->getGlobalID());
6246 }
6247
6248 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
6249 LocalDeclID &ID = DeclIDs[D];
6250 if (ID.isInvalid()) {
6251 if (DoneWritingDeclsAndTypes) {
6252 assert(0 && "New decl seen after serializing all the decls to emit!");
6253 return LocalDeclID();
6254 }
6255
6256 // We haven't seen this declaration before. Give it a new ID and
6257 // enqueue it in the list of declarations to emit.
6258 ID = NextDeclID++;
6259 DeclTypesToEmit.push(x: const_cast<Decl *>(D));
6260 }
6261
6262 return ID;
6263}
6264
6265LocalDeclID ASTWriter::getDeclID(const Decl *D) {
6266 if (!D)
6267 return LocalDeclID();
6268
6269 // If D comes from an AST file, its declaration ID is already known and
6270 // fixed.
6271 if (D->isFromASTFile())
6272 return LocalDeclID(D->getGlobalID());
6273
6274 assert(DeclIDs.contains(D) && "Declaration not emitted!");
6275 return DeclIDs[D];
6276}
6277
6278bool ASTWriter::wasDeclEmitted(const Decl *D) const {
6279 assert(D);
6280
6281 assert(DoneWritingDeclsAndTypes &&
6282 "wasDeclEmitted should only be called after writing declarations");
6283
6284 if (D->isFromASTFile())
6285 return true;
6286
6287 bool Emitted = DeclIDs.contains(Val: D);
6288 assert((Emitted || (!D->getOwningModule() && isWritingStdCXXNamedModules()) ||
6289 GeneratingReducedBMI) &&
6290 "The declaration within modules can only be omitted in reduced BMI.");
6291 return Emitted;
6292}
6293
6294void ASTWriter::associateDeclWithFile(const Decl *D, LocalDeclID ID) {
6295 assert(ID.isValid());
6296 assert(D);
6297
6298 SourceLocation Loc = D->getLocation();
6299 if (Loc.isInvalid())
6300 return;
6301
6302 // We only keep track of the file-level declarations of each file.
6303 if (!D->getLexicalDeclContext()->isFileContext())
6304 return;
6305 // FIXME: ParmVarDecls that are part of a function type of a parameter of
6306 // a function/objc method, should not have TU as lexical context.
6307 // TemplateTemplateParmDecls that are part of an alias template, should not
6308 // have TU as lexical context.
6309 if (isa<ParmVarDecl, TemplateTemplateParmDecl>(Val: D))
6310 return;
6311
6312 SourceManager &SM = Context->getSourceManager();
6313 SourceLocation FileLoc = SM.getFileLoc(Loc);
6314 assert(SM.isLocalSourceLocation(FileLoc));
6315 FileID FID;
6316 unsigned Offset;
6317 std::tie(args&: FID, args&: Offset) = SM.getDecomposedLoc(Loc: FileLoc);
6318 if (FID.isInvalid())
6319 return;
6320 assert(SM.getSLocEntry(FID).isFile());
6321 assert(IsSLocAffecting[FID.ID]);
6322
6323 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
6324 if (!Info)
6325 Info = std::make_unique<DeclIDInFileInfo>();
6326
6327 std::pair<unsigned, LocalDeclID> LocDecl(Offset, ID);
6328 LocDeclIDsTy &Decls = Info->DeclIDs;
6329 Decls.push_back(Elt: LocDecl);
6330}
6331
6332unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
6333 assert(needsAnonymousDeclarationNumber(D) &&
6334 "expected an anonymous declaration");
6335
6336 // Number the anonymous declarations within this context, if we've not
6337 // already done so.
6338 auto It = AnonymousDeclarationNumbers.find(Val: D);
6339 if (It == AnonymousDeclarationNumbers.end()) {
6340 auto *DC = D->getLexicalDeclContext();
6341 numberAnonymousDeclsWithin(DC, Visit: [&](const NamedDecl *ND, unsigned Number) {
6342 AnonymousDeclarationNumbers[ND] = Number;
6343 });
6344
6345 It = AnonymousDeclarationNumbers.find(Val: D);
6346 assert(It != AnonymousDeclarationNumbers.end() &&
6347 "declaration not found within its lexical context");
6348 }
6349
6350 return It->second;
6351}
6352
6353void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
6354 DeclarationName Name) {
6355 switch (Name.getNameKind()) {
6356 case DeclarationName::CXXConstructorName:
6357 case DeclarationName::CXXDestructorName:
6358 case DeclarationName::CXXConversionFunctionName:
6359 AddTypeSourceInfo(TInfo: DNLoc.getNamedTypeInfo());
6360 break;
6361
6362 case DeclarationName::CXXOperatorName:
6363 AddSourceRange(Range: DNLoc.getCXXOperatorNameRange());
6364 break;
6365
6366 case DeclarationName::CXXLiteralOperatorName:
6367 AddSourceLocation(Loc: DNLoc.getCXXLiteralOperatorNameLoc());
6368 break;
6369
6370 case DeclarationName::Identifier:
6371 case DeclarationName::ObjCZeroArgSelector:
6372 case DeclarationName::ObjCOneArgSelector:
6373 case DeclarationName::ObjCMultiArgSelector:
6374 case DeclarationName::CXXUsingDirective:
6375 case DeclarationName::CXXDeductionGuideName:
6376 break;
6377 }
6378}
6379
6380void ASTRecordWriter::AddDeclarationNameInfo(
6381 const DeclarationNameInfo &NameInfo) {
6382 AddDeclarationName(Name: NameInfo.getName());
6383 AddSourceLocation(Loc: NameInfo.getLoc());
6384 AddDeclarationNameLoc(DNLoc: NameInfo.getInfo(), Name: NameInfo.getName());
6385}
6386
6387void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
6388 AddNestedNameSpecifierLoc(NNS: Info.QualifierLoc);
6389 Record->push_back(Elt: Info.NumTemplParamLists);
6390 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
6391 AddTemplateParameterList(TemplateParams: Info.TemplParamLists[i]);
6392}
6393
6394void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
6395 // Nested name specifiers usually aren't too long. I think that 8 would
6396 // typically accommodate the vast majority.
6397 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6398
6399 // Push each of the nested-name-specifiers's onto a stack for
6400 // serialization in reverse order.
6401 while (NNS) {
6402 NestedNames.push_back(Elt: NNS);
6403 NNS = NNS.getPrefix();
6404 }
6405
6406 Record->push_back(Elt: NestedNames.size());
6407 while(!NestedNames.empty()) {
6408 NNS = NestedNames.pop_back_val();
6409 NestedNameSpecifier::SpecifierKind Kind
6410 = NNS.getNestedNameSpecifier()->getKind();
6411 Record->push_back(Elt: Kind);
6412 switch (Kind) {
6413 case NestedNameSpecifier::Identifier:
6414 AddIdentifierRef(II: NNS.getNestedNameSpecifier()->getAsIdentifier());
6415 AddSourceRange(Range: NNS.getLocalSourceRange());
6416 break;
6417
6418 case NestedNameSpecifier::Namespace:
6419 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsNamespace());
6420 AddSourceRange(Range: NNS.getLocalSourceRange());
6421 break;
6422
6423 case NestedNameSpecifier::NamespaceAlias:
6424 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
6425 AddSourceRange(Range: NNS.getLocalSourceRange());
6426 break;
6427
6428 case NestedNameSpecifier::TypeSpec:
6429 case NestedNameSpecifier::TypeSpecWithTemplate:
6430 Record->push_back(Elt: Kind == NestedNameSpecifier::TypeSpecWithTemplate);
6431 AddTypeRef(T: NNS.getTypeLoc().getType());
6432 AddTypeLoc(TL: NNS.getTypeLoc());
6433 AddSourceLocation(Loc: NNS.getLocalSourceRange().getEnd());
6434 break;
6435
6436 case NestedNameSpecifier::Global:
6437 AddSourceLocation(Loc: NNS.getLocalSourceRange().getEnd());
6438 break;
6439
6440 case NestedNameSpecifier::Super:
6441 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsRecordDecl());
6442 AddSourceRange(Range: NNS.getLocalSourceRange());
6443 break;
6444 }
6445 }
6446}
6447
6448void ASTRecordWriter::AddTemplateParameterList(
6449 const TemplateParameterList *TemplateParams) {
6450 assert(TemplateParams && "No TemplateParams!");
6451 AddSourceLocation(Loc: TemplateParams->getTemplateLoc());
6452 AddSourceLocation(Loc: TemplateParams->getLAngleLoc());
6453 AddSourceLocation(Loc: TemplateParams->getRAngleLoc());
6454
6455 Record->push_back(Elt: TemplateParams->size());
6456 for (const auto &P : *TemplateParams)
6457 AddDeclRef(D: P);
6458 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
6459 Record->push_back(Elt: true);
6460 writeStmtRef(S: RequiresClause);
6461 } else {
6462 Record->push_back(Elt: false);
6463 }
6464}
6465
6466/// Emit a template argument list.
6467void ASTRecordWriter::AddTemplateArgumentList(
6468 const TemplateArgumentList *TemplateArgs) {
6469 assert(TemplateArgs && "No TemplateArgs!");
6470 Record->push_back(Elt: TemplateArgs->size());
6471 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
6472 AddTemplateArgument(Arg: TemplateArgs->get(Idx: i));
6473}
6474
6475void ASTRecordWriter::AddASTTemplateArgumentListInfo(
6476 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
6477 assert(ASTTemplArgList && "No ASTTemplArgList!");
6478 AddSourceLocation(Loc: ASTTemplArgList->LAngleLoc);
6479 AddSourceLocation(Loc: ASTTemplArgList->RAngleLoc);
6480 Record->push_back(Elt: ASTTemplArgList->NumTemplateArgs);
6481 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
6482 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
6483 AddTemplateArgumentLoc(Arg: TemplArgs[i]);
6484}
6485
6486void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
6487 Record->push_back(Elt: Set.size());
6488 for (ASTUnresolvedSet::const_iterator
6489 I = Set.begin(), E = Set.end(); I != E; ++I) {
6490 AddDeclRef(D: I.getDecl());
6491 Record->push_back(Elt: I.getAccess());
6492 }
6493}
6494
6495// FIXME: Move this out of the main ASTRecordWriter interface.
6496void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
6497 Record->push_back(Elt: Base.isVirtual());
6498 Record->push_back(Elt: Base.isBaseOfClass());
6499 Record->push_back(Elt: Base.getAccessSpecifierAsWritten());
6500 Record->push_back(Elt: Base.getInheritConstructors());
6501 AddTypeSourceInfo(TInfo: Base.getTypeSourceInfo());
6502 AddSourceRange(Range: Base.getSourceRange());
6503 AddSourceLocation(Loc: Base.isPackExpansion()? Base.getEllipsisLoc()
6504 : SourceLocation());
6505}
6506
6507static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
6508 ArrayRef<CXXBaseSpecifier> Bases) {
6509 ASTWriter::RecordData Record;
6510 ASTRecordWriter Writer(W, Record);
6511 Writer.push_back(N: Bases.size());
6512
6513 for (auto &Base : Bases)
6514 Writer.AddCXXBaseSpecifier(Base);
6515
6516 return Writer.Emit(Code: serialization::DECL_CXX_BASE_SPECIFIERS);
6517}
6518
6519// FIXME: Move this out of the main ASTRecordWriter interface.
6520void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
6521 AddOffset(BitOffset: EmitCXXBaseSpecifiers(W&: *Writer, Bases));
6522}
6523
6524static uint64_t
6525EmitCXXCtorInitializers(ASTWriter &W,
6526 ArrayRef<CXXCtorInitializer *> CtorInits) {
6527 ASTWriter::RecordData Record;
6528 ASTRecordWriter Writer(W, Record);
6529 Writer.push_back(N: CtorInits.size());
6530
6531 for (auto *Init : CtorInits) {
6532 if (Init->isBaseInitializer()) {
6533 Writer.push_back(N: CTOR_INITIALIZER_BASE);
6534 Writer.AddTypeSourceInfo(TInfo: Init->getTypeSourceInfo());
6535 Writer.push_back(N: Init->isBaseVirtual());
6536 } else if (Init->isDelegatingInitializer()) {
6537 Writer.push_back(N: CTOR_INITIALIZER_DELEGATING);
6538 Writer.AddTypeSourceInfo(TInfo: Init->getTypeSourceInfo());
6539 } else if (Init->isMemberInitializer()){
6540 Writer.push_back(N: CTOR_INITIALIZER_MEMBER);
6541 Writer.AddDeclRef(D: Init->getMember());
6542 } else {
6543 Writer.push_back(N: CTOR_INITIALIZER_INDIRECT_MEMBER);
6544 Writer.AddDeclRef(D: Init->getIndirectMember());
6545 }
6546
6547 Writer.AddSourceLocation(Loc: Init->getMemberLocation());
6548 Writer.AddStmt(S: Init->getInit());
6549 Writer.AddSourceLocation(Loc: Init->getLParenLoc());
6550 Writer.AddSourceLocation(Loc: Init->getRParenLoc());
6551 Writer.push_back(N: Init->isWritten());
6552 if (Init->isWritten())
6553 Writer.push_back(N: Init->getSourceOrder());
6554 }
6555
6556 return Writer.Emit(Code: serialization::DECL_CXX_CTOR_INITIALIZERS);
6557}
6558
6559// FIXME: Move this out of the main ASTRecordWriter interface.
6560void ASTRecordWriter::AddCXXCtorInitializers(
6561 ArrayRef<CXXCtorInitializer *> CtorInits) {
6562 AddOffset(BitOffset: EmitCXXCtorInitializers(W&: *Writer, CtorInits));
6563}
6564
6565void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
6566 auto &Data = D->data();
6567
6568 Record->push_back(Elt: Data.IsLambda);
6569
6570 BitsPacker DefinitionBits;
6571
6572#define FIELD(Name, Width, Merge) \
6573 if (!DefinitionBits.canWriteNextNBits(Width)) { \
6574 Record->push_back(DefinitionBits); \
6575 DefinitionBits.reset(0); \
6576 } \
6577 DefinitionBits.addBits(Data.Name, Width);
6578
6579#include "clang/AST/CXXRecordDeclDefinitionBits.def"
6580#undef FIELD
6581
6582 Record->push_back(Elt: DefinitionBits);
6583
6584 // getODRHash will compute the ODRHash if it has not been previously
6585 // computed.
6586 Record->push_back(Elt: D->getODRHash());
6587
6588 bool ModulesCodegen =
6589 !D->isDependentType() &&
6590 (Writer->Context->getLangOpts().ModulesDebugInfo ||
6591 D->isInNamedModule());
6592 Record->push_back(Elt: ModulesCodegen);
6593 if (ModulesCodegen)
6594 Writer->AddDeclRef(D, Record&: Writer->ModularCodegenDecls);
6595
6596 // IsLambda bit is already saved.
6597
6598 AddUnresolvedSet(Set: Data.Conversions.get(C&: *Writer->Context));
6599 Record->push_back(Elt: Data.ComputedVisibleConversions);
6600 if (Data.ComputedVisibleConversions)
6601 AddUnresolvedSet(Set: Data.VisibleConversions.get(C&: *Writer->Context));
6602 // Data.Definition is the owning decl, no need to write it.
6603
6604 if (!Data.IsLambda) {
6605 Record->push_back(Elt: Data.NumBases);
6606 if (Data.NumBases > 0)
6607 AddCXXBaseSpecifiers(Bases: Data.bases());
6608
6609 // FIXME: Make VBases lazily computed when needed to avoid storing them.
6610 Record->push_back(Elt: Data.NumVBases);
6611 if (Data.NumVBases > 0)
6612 AddCXXBaseSpecifiers(Bases: Data.vbases());
6613
6614 AddDeclRef(D: D->getFirstFriend());
6615 } else {
6616 auto &Lambda = D->getLambdaData();
6617
6618 BitsPacker LambdaBits;
6619 LambdaBits.addBits(Value: Lambda.DependencyKind, /*Width=*/BitsWidth: 2);
6620 LambdaBits.addBit(Value: Lambda.IsGenericLambda);
6621 LambdaBits.addBits(Value: Lambda.CaptureDefault, /*Width=*/BitsWidth: 2);
6622 LambdaBits.addBits(Value: Lambda.NumCaptures, /*Width=*/BitsWidth: 15);
6623 LambdaBits.addBit(Value: Lambda.HasKnownInternalLinkage);
6624 Record->push_back(Elt: LambdaBits);
6625
6626 Record->push_back(Elt: Lambda.NumExplicitCaptures);
6627 Record->push_back(Elt: Lambda.ManglingNumber);
6628 Record->push_back(Elt: D->getDeviceLambdaManglingNumber());
6629 // The lambda context declaration and index within the context are provided
6630 // separately, so that they can be used for merging.
6631 AddTypeSourceInfo(TInfo: Lambda.MethodTyInfo);
6632 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
6633 const LambdaCapture &Capture = Lambda.Captures.front()[I];
6634 AddSourceLocation(Loc: Capture.getLocation());
6635
6636 BitsPacker CaptureBits;
6637 CaptureBits.addBit(Value: Capture.isImplicit());
6638 CaptureBits.addBits(Value: Capture.getCaptureKind(), /*Width=*/BitsWidth: 3);
6639 Record->push_back(Elt: CaptureBits);
6640
6641 switch (Capture.getCaptureKind()) {
6642 case LCK_StarThis:
6643 case LCK_This:
6644 case LCK_VLAType:
6645 break;
6646 case LCK_ByCopy:
6647 case LCK_ByRef:
6648 ValueDecl *Var =
6649 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
6650 AddDeclRef(D: Var);
6651 AddSourceLocation(Loc: Capture.isPackExpansion() ? Capture.getEllipsisLoc()
6652 : SourceLocation());
6653 break;
6654 }
6655 }
6656 }
6657}
6658
6659void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
6660 const Expr *Init = VD->getInit();
6661 if (!Init) {
6662 push_back(N: 0);
6663 return;
6664 }
6665
6666 uint64_t Val = 1;
6667 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
6668 Val |= (ES->HasConstantInitialization ? 2 : 0);
6669 Val |= (ES->HasConstantDestruction ? 4 : 0);
6670 APValue *Evaluated = VD->getEvaluatedValue();
6671 // If the evaluated result is constant, emit it.
6672 if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
6673 Val |= 8;
6674 }
6675 push_back(N: Val);
6676 if (Val & 8) {
6677 AddAPValue(Value: *VD->getEvaluatedValue());
6678 }
6679
6680 writeStmtRef(S: Init);
6681}
6682
6683void ASTWriter::ReaderInitialized(ASTReader *Reader) {
6684 assert(Reader && "Cannot remove chain");
6685 assert((!Chain || Chain == Reader) && "Cannot replace chain");
6686 assert(FirstDeclID == NextDeclID &&
6687 FirstTypeID == NextTypeID &&
6688 FirstIdentID == NextIdentID &&
6689 FirstMacroID == NextMacroID &&
6690 FirstSubmoduleID == NextSubmoduleID &&
6691 FirstSelectorID == NextSelectorID &&
6692 "Setting chain after writing has started.");
6693
6694 Chain = Reader;
6695
6696 // Note, this will get called multiple times, once one the reader starts up
6697 // and again each time it's done reading a PCH or module.
6698 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
6699 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
6700 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
6701 NextMacroID = FirstMacroID;
6702 NextSelectorID = FirstSelectorID;
6703 NextSubmoduleID = FirstSubmoduleID;
6704}
6705
6706void ASTWriter::IdentifierRead(IdentifierID ID, IdentifierInfo *II) {
6707 // Don't reuse Type ID from external modules for named modules. See the
6708 // comments in WriteASTCore for details.
6709 if (isWritingStdCXXNamedModules())
6710 return;
6711
6712 IdentifierID &StoredID = IdentifierIDs[II];
6713 unsigned OriginalModuleFileIndex = StoredID >> 32;
6714
6715 // Always keep the local identifier ID. See \p TypeRead() for more
6716 // information.
6717 if (OriginalModuleFileIndex == 0 && StoredID)
6718 return;
6719
6720 // Otherwise, keep the highest ID since the module file comes later has
6721 // higher module file indexes.
6722 if (ID > StoredID)
6723 StoredID = ID;
6724}
6725
6726void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
6727 // Always keep the highest ID. See \p TypeRead() for more information.
6728 MacroID &StoredID = MacroIDs[MI];
6729 if (ID > StoredID)
6730 StoredID = ID;
6731}
6732
6733void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
6734 // Don't reuse Type ID from external modules for named modules. See the
6735 // comments in WriteASTCore for details.
6736 if (isWritingStdCXXNamedModules())
6737 return;
6738
6739 // Always take the type index that comes in later module files.
6740 // This copes with an interesting
6741 // case for chained AST writing where we schedule writing the type and then,
6742 // later, deserialize the type from another AST. In this case, we want to
6743 // keep the entry from a later module so that we can properly write it out to
6744 // the AST file.
6745 TypeIdx &StoredIdx = TypeIdxs[T];
6746
6747 // Ignore it if the type comes from the current being written module file.
6748 // Since the current module file being written logically has the highest
6749 // index.
6750 unsigned ModuleFileIndex = StoredIdx.getModuleFileIndex();
6751 if (ModuleFileIndex == 0 && StoredIdx.getValue())
6752 return;
6753
6754 // Otherwise, keep the highest ID since the module file comes later has
6755 // higher module file indexes.
6756 if (Idx.getModuleFileIndex() >= StoredIdx.getModuleFileIndex())
6757 StoredIdx = Idx;
6758}
6759
6760void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
6761 // Always keep the highest ID. See \p TypeRead() for more information.
6762 SelectorID &StoredID = SelectorIDs[S];
6763 if (ID > StoredID)
6764 StoredID = ID;
6765}
6766
6767void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
6768 MacroDefinitionRecord *MD) {
6769 assert(!MacroDefinitions.contains(MD));
6770 MacroDefinitions[MD] = ID;
6771}
6772
6773void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6774 assert(!SubmoduleIDs.contains(Mod));
6775 SubmoduleIDs[Mod] = ID;
6776}
6777
6778void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
6779 if (Chain && Chain->isProcessingUpdateRecords()) return;
6780 assert(D->isCompleteDefinition());
6781 assert(!WritingAST && "Already writing the AST!");
6782 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) {
6783 // We are interested when a PCH decl is modified.
6784 if (RD->isFromASTFile()) {
6785 // A forward reference was mutated into a definition. Rewrite it.
6786 // FIXME: This happens during template instantiation, should we
6787 // have created a new definition decl instead ?
6788 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6789 "completed a tag from another module but not by instantiation?");
6790 DeclUpdates[RD].push_back(
6791 Elt: DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
6792 }
6793 }
6794}
6795
6796static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6797 if (D->isFromASTFile())
6798 return true;
6799
6800 // The predefined __va_list_tag struct is imported if we imported any decls.
6801 // FIXME: This is a gross hack.
6802 return D == D->getASTContext().getVaListTagDecl();
6803}
6804
6805void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
6806 if (Chain && Chain->isProcessingUpdateRecords()) return;
6807 assert(DC->isLookupContext() &&
6808 "Should not add lookup results to non-lookup contexts!");
6809
6810 // TU is handled elsewhere.
6811 if (isa<TranslationUnitDecl>(Val: DC))
6812 return;
6813
6814 // Namespaces are handled elsewhere, except for template instantiations of
6815 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6816 // local instantiations are added to an imported context. Only happens when
6817 // adding ADL lookup candidates, for example templated friends.
6818 if (isa<NamespaceDecl>(Val: DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6819 !isa<FunctionTemplateDecl>(Val: D))
6820 return;
6821
6822 // We're only interested in cases where a local declaration is added to an
6823 // imported context.
6824 if (D->isFromASTFile() || !isImportedDeclContext(Chain, D: cast<Decl>(Val: DC)))
6825 return;
6826
6827 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
6828 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
6829 assert(!WritingAST && "Already writing the AST!");
6830 if (UpdatedDeclContexts.insert(X: DC) && !cast<Decl>(Val: DC)->isFromASTFile()) {
6831 // We're adding a visible declaration to a predefined decl context. Ensure
6832 // that we write out all of its lookup results so we don't get a nasty
6833 // surprise when we try to emit its lookup table.
6834 llvm::append_range(C&: DeclsToEmitEvenIfUnreferenced, R: DC->decls());
6835 }
6836 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
6837}
6838
6839void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
6840 if (Chain && Chain->isProcessingUpdateRecords()) return;
6841 assert(D->isImplicit());
6842
6843 // We're only interested in cases where a local declaration is added to an
6844 // imported context.
6845 if (D->isFromASTFile() || !isImportedDeclContext(Chain, D: RD))
6846 return;
6847
6848 if (!isa<CXXMethodDecl>(Val: D))
6849 return;
6850
6851 // A decl coming from PCH was modified.
6852 assert(RD->isCompleteDefinition());
6853 assert(!WritingAST && "Already writing the AST!");
6854 DeclUpdates[RD].push_back(Elt: DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
6855}
6856
6857void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
6858 if (Chain && Chain->isProcessingUpdateRecords()) return;
6859 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6860 if (!Chain) return;
6861 Chain->forEachImportedKeyDecl(D: FD, Visit: [&](const Decl *D) {
6862 // If we don't already know the exception specification for this redecl
6863 // chain, add an update record for it.
6864 if (isUnresolvedExceptionSpec(ESpecType: cast<FunctionDecl>(Val: D)
6865 ->getType()
6866 ->castAs<FunctionProtoType>()
6867 ->getExceptionSpecType()))
6868 DeclUpdates[D].push_back(Elt: UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6869 });
6870}
6871
6872void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
6873 if (Chain && Chain->isProcessingUpdateRecords()) return;
6874 assert(!WritingAST && "Already writing the AST!");
6875 if (!Chain) return;
6876 Chain->forEachImportedKeyDecl(D: FD, Visit: [&](const Decl *D) {
6877 DeclUpdates[D].push_back(
6878 Elt: DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6879 });
6880}
6881
6882void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6883 const FunctionDecl *Delete,
6884 Expr *ThisArg) {
6885 if (Chain && Chain->isProcessingUpdateRecords()) return;
6886 assert(!WritingAST && "Already writing the AST!");
6887 assert(Delete && "Not given an operator delete");
6888 if (!Chain) return;
6889 Chain->forEachImportedKeyDecl(D: DD, Visit: [&](const Decl *D) {
6890 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6891 });
6892}
6893
6894void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
6895 if (Chain && Chain->isProcessingUpdateRecords()) return;
6896 assert(!WritingAST && "Already writing the AST!");
6897 if (!D->isFromASTFile())
6898 return; // Declaration not imported from PCH.
6899
6900 // Implicit function decl from a PCH was defined.
6901 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6902}
6903
6904void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6905 if (Chain && Chain->isProcessingUpdateRecords()) return;
6906 assert(!WritingAST && "Already writing the AST!");
6907 if (!D->isFromASTFile())
6908 return;
6909
6910 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6911}
6912
6913void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
6914 if (Chain && Chain->isProcessingUpdateRecords()) return;
6915 assert(!WritingAST && "Already writing the AST!");
6916 if (!D->isFromASTFile())
6917 return;
6918
6919 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6920}
6921
6922void ASTWriter::InstantiationRequested(const ValueDecl *D) {
6923 if (Chain && Chain->isProcessingUpdateRecords()) return;
6924 assert(!WritingAST && "Already writing the AST!");
6925 if (!D->isFromASTFile())
6926 return;
6927
6928 // Since the actual instantiation is delayed, this really means that we need
6929 // to update the instantiation location.
6930 SourceLocation POI;
6931 if (auto *VD = dyn_cast<VarDecl>(Val: D))
6932 POI = VD->getPointOfInstantiation();
6933 else
6934 POI = cast<FunctionDecl>(Val: D)->getPointOfInstantiation();
6935 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
6936}
6937
6938void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
6939 if (Chain && Chain->isProcessingUpdateRecords()) return;
6940 assert(!WritingAST && "Already writing the AST!");
6941 if (!D->isFromASTFile())
6942 return;
6943
6944 DeclUpdates[D].push_back(
6945 Elt: DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6946}
6947
6948void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6949 assert(!WritingAST && "Already writing the AST!");
6950 if (!D->isFromASTFile())
6951 return;
6952
6953 DeclUpdates[D].push_back(
6954 Elt: DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6955}
6956
6957void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6958 const ObjCInterfaceDecl *IFD) {
6959 if (Chain && Chain->isProcessingUpdateRecords()) return;
6960 assert(!WritingAST && "Already writing the AST!");
6961 if (!IFD->isFromASTFile())
6962 return; // Declaration not imported from PCH.
6963
6964 assert(IFD->getDefinition() && "Category on a class without a definition?");
6965 ObjCClassesWithCategories.insert(
6966 X: const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
6967}
6968
6969void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
6970 if (Chain && Chain->isProcessingUpdateRecords()) return;
6971 assert(!WritingAST && "Already writing the AST!");
6972
6973 // If there is *any* declaration of the entity that's not from an AST file,
6974 // we can skip writing the update record. We make sure that isUsed() triggers
6975 // completion of the redeclaration chain of the entity.
6976 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6977 if (IsLocalDecl(D: Prev))
6978 return;
6979
6980 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_DECL_MARKED_USED));
6981}
6982
6983void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
6984 if (Chain && Chain->isProcessingUpdateRecords()) return;
6985 assert(!WritingAST && "Already writing the AST!");
6986 if (!D->isFromASTFile())
6987 return;
6988
6989 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6990}
6991
6992void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6993 if (Chain && Chain->isProcessingUpdateRecords()) return;
6994 assert(!WritingAST && "Already writing the AST!");
6995 if (!D->isFromASTFile())
6996 return;
6997
6998 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6999}
7000
7001void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
7002 const Attr *Attr) {
7003 if (Chain && Chain->isProcessingUpdateRecords()) return;
7004 assert(!WritingAST && "Already writing the AST!");
7005 if (!D->isFromASTFile())
7006 return;
7007
7008 DeclUpdates[D].push_back(
7009 Elt: DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
7010}
7011
7012void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
7013 if (Chain && Chain->isProcessingUpdateRecords()) return;
7014 assert(!WritingAST && "Already writing the AST!");
7015 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
7016 DeclUpdates[D].push_back(Elt: DeclUpdate(UPD_DECL_EXPORTED, M));
7017}
7018
7019void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
7020 const RecordDecl *Record) {
7021 if (Chain && Chain->isProcessingUpdateRecords()) return;
7022 assert(!WritingAST && "Already writing the AST!");
7023 if (!Record->isFromASTFile())
7024 return;
7025 DeclUpdates[Record].push_back(Elt: DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
7026}
7027
7028void ASTWriter::AddedCXXTemplateSpecialization(
7029 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
7030 assert(!WritingAST && "Already writing the AST!");
7031
7032 if (!TD->getFirstDecl()->isFromASTFile())
7033 return;
7034 if (Chain && Chain->isProcessingUpdateRecords())
7035 return;
7036
7037 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7038}
7039
7040void ASTWriter::AddedCXXTemplateSpecialization(
7041 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
7042 assert(!WritingAST && "Already writing the AST!");
7043
7044 if (!TD->getFirstDecl()->isFromASTFile())
7045 return;
7046 if (Chain && Chain->isProcessingUpdateRecords())
7047 return;
7048
7049 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7050}
7051
7052void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
7053 const FunctionDecl *D) {
7054 assert(!WritingAST && "Already writing the AST!");
7055
7056 if (!TD->getFirstDecl()->isFromASTFile())
7057 return;
7058 if (Chain && Chain->isProcessingUpdateRecords())
7059 return;
7060
7061 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7062}
7063
7064//===----------------------------------------------------------------------===//
7065//// OMPClause Serialization
7066////===----------------------------------------------------------------------===//
7067
7068namespace {
7069
7070class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
7071 ASTRecordWriter &Record;
7072
7073public:
7074 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
7075#define GEN_CLANG_CLAUSE_CLASS
7076#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
7077#include "llvm/Frontend/OpenMP/OMP.inc"
7078 void writeClause(OMPClause *C);
7079 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
7080 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
7081};
7082
7083}
7084
7085void ASTRecordWriter::writeOMPClause(OMPClause *C) {
7086 OMPClauseWriter(*this).writeClause(C);
7087}
7088
7089void OMPClauseWriter::writeClause(OMPClause *C) {
7090 Record.push_back(N: unsigned(C->getClauseKind()));
7091 Visit(S: C);
7092 Record.AddSourceLocation(Loc: C->getBeginLoc());
7093 Record.AddSourceLocation(Loc: C->getEndLoc());
7094}
7095
7096void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
7097 Record.push_back(N: uint64_t(C->getCaptureRegion()));
7098 Record.AddStmt(S: C->getPreInitStmt());
7099}
7100
7101void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
7102 VisitOMPClauseWithPreInit(C);
7103 Record.AddStmt(S: C->getPostUpdateExpr());
7104}
7105
7106void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
7107 VisitOMPClauseWithPreInit(C);
7108 Record.push_back(N: uint64_t(C->getNameModifier()));
7109 Record.AddSourceLocation(Loc: C->getNameModifierLoc());
7110 Record.AddSourceLocation(Loc: C->getColonLoc());
7111 Record.AddStmt(S: C->getCondition());
7112 Record.AddSourceLocation(Loc: C->getLParenLoc());
7113}
7114
7115void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
7116 VisitOMPClauseWithPreInit(C);
7117 Record.AddStmt(S: C->getCondition());
7118 Record.AddSourceLocation(Loc: C->getLParenLoc());
7119}
7120
7121void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
7122 VisitOMPClauseWithPreInit(C);
7123 Record.AddStmt(S: C->getNumThreads());
7124 Record.AddSourceLocation(Loc: C->getLParenLoc());
7125}
7126
7127void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
7128 Record.AddStmt(S: C->getSafelen());
7129 Record.AddSourceLocation(Loc: C->getLParenLoc());
7130}
7131
7132void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
7133 Record.AddStmt(S: C->getSimdlen());
7134 Record.AddSourceLocation(Loc: C->getLParenLoc());
7135}
7136
7137void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
7138 Record.push_back(N: C->getNumSizes());
7139 for (Expr *Size : C->getSizesRefs())
7140 Record.AddStmt(S: Size);
7141 Record.AddSourceLocation(Loc: C->getLParenLoc());
7142}
7143
7144void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
7145
7146void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
7147 Record.AddStmt(S: C->getFactor());
7148 Record.AddSourceLocation(Loc: C->getLParenLoc());
7149}
7150
7151void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
7152 Record.AddStmt(S: C->getAllocator());
7153 Record.AddSourceLocation(Loc: C->getLParenLoc());
7154}
7155
7156void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
7157 Record.AddStmt(S: C->getNumForLoops());
7158 Record.AddSourceLocation(Loc: C->getLParenLoc());
7159}
7160
7161void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
7162 Record.AddStmt(S: C->getEventHandler());
7163 Record.AddSourceLocation(Loc: C->getLParenLoc());
7164}
7165
7166void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
7167 Record.push_back(N: unsigned(C->getDefaultKind()));
7168 Record.AddSourceLocation(Loc: C->getLParenLoc());
7169 Record.AddSourceLocation(Loc: C->getDefaultKindKwLoc());
7170}
7171
7172void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
7173 Record.push_back(N: unsigned(C->getProcBindKind()));
7174 Record.AddSourceLocation(Loc: C->getLParenLoc());
7175 Record.AddSourceLocation(Loc: C->getProcBindKindKwLoc());
7176}
7177
7178void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
7179 VisitOMPClauseWithPreInit(C);
7180 Record.push_back(N: C->getScheduleKind());
7181 Record.push_back(N: C->getFirstScheduleModifier());
7182 Record.push_back(N: C->getSecondScheduleModifier());
7183 Record.AddStmt(S: C->getChunkSize());
7184 Record.AddSourceLocation(Loc: C->getLParenLoc());
7185 Record.AddSourceLocation(Loc: C->getFirstScheduleModifierLoc());
7186 Record.AddSourceLocation(Loc: C->getSecondScheduleModifierLoc());
7187 Record.AddSourceLocation(Loc: C->getScheduleKindLoc());
7188 Record.AddSourceLocation(Loc: C->getCommaLoc());
7189}
7190
7191void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
7192 Record.push_back(N: C->getLoopNumIterations().size());
7193 Record.AddStmt(S: C->getNumForLoops());
7194 for (Expr *NumIter : C->getLoopNumIterations())
7195 Record.AddStmt(S: NumIter);
7196 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
7197 Record.AddStmt(S: C->getLoopCounter(NumLoop: I));
7198 Record.AddSourceLocation(Loc: C->getLParenLoc());
7199}
7200
7201void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
7202
7203void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
7204
7205void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
7206
7207void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
7208
7209void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
7210
7211void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
7212 Record.push_back(N: C->isExtended() ? 1 : 0);
7213 if (C->isExtended()) {
7214 Record.AddSourceLocation(Loc: C->getLParenLoc());
7215 Record.AddSourceLocation(Loc: C->getArgumentLoc());
7216 Record.writeEnum(value: C->getDependencyKind());
7217 }
7218}
7219
7220void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
7221
7222void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
7223
7224// Save the parameter of fail clause.
7225void OMPClauseWriter::VisitOMPFailClause(OMPFailClause *C) {
7226 Record.AddSourceLocation(Loc: C->getLParenLoc());
7227 Record.AddSourceLocation(Loc: C->getFailParameterLoc());
7228 Record.writeEnum(value: C->getFailParameter());
7229}
7230
7231void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
7232
7233void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
7234
7235void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
7236
7237void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
7238
7239void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
7240
7241void OMPClauseWriter::VisitOMPWeakClause(OMPWeakClause *) {}
7242
7243void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
7244
7245void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
7246
7247void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
7248
7249void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
7250 Record.push_back(N: C->varlist_size());
7251 for (Expr *VE : C->varlists())
7252 Record.AddStmt(S: VE);
7253 Record.writeBool(Value: C->getIsTarget());
7254 Record.writeBool(Value: C->getIsTargetSync());
7255 Record.AddSourceLocation(Loc: C->getLParenLoc());
7256 Record.AddSourceLocation(Loc: C->getVarLoc());
7257}
7258
7259void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
7260 Record.AddStmt(S: C->getInteropVar());
7261 Record.AddSourceLocation(Loc: C->getLParenLoc());
7262 Record.AddSourceLocation(Loc: C->getVarLoc());
7263}
7264
7265void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
7266 Record.AddStmt(S: C->getInteropVar());
7267 Record.AddSourceLocation(Loc: C->getLParenLoc());
7268 Record.AddSourceLocation(Loc: C->getVarLoc());
7269}
7270
7271void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
7272 VisitOMPClauseWithPreInit(C);
7273 Record.AddStmt(S: C->getCondition());
7274 Record.AddSourceLocation(Loc: C->getLParenLoc());
7275}
7276
7277void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
7278 VisitOMPClauseWithPreInit(C);
7279 Record.AddStmt(S: C->getCondition());
7280 Record.AddSourceLocation(Loc: C->getLParenLoc());
7281}
7282
7283void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
7284 VisitOMPClauseWithPreInit(C);
7285 Record.AddStmt(S: C->getThreadID());
7286 Record.AddSourceLocation(Loc: C->getLParenLoc());
7287}
7288
7289void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
7290 Record.AddStmt(S: C->getAlignment());
7291 Record.AddSourceLocation(Loc: C->getLParenLoc());
7292}
7293
7294void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
7295 Record.push_back(N: C->varlist_size());
7296 Record.AddSourceLocation(Loc: C->getLParenLoc());
7297 for (auto *VE : C->varlists()) {
7298 Record.AddStmt(S: VE);
7299 }
7300 for (auto *VE : C->private_copies()) {
7301 Record.AddStmt(S: VE);
7302 }
7303}
7304
7305void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
7306 Record.push_back(N: C->varlist_size());
7307 VisitOMPClauseWithPreInit(C);
7308 Record.AddSourceLocation(Loc: C->getLParenLoc());
7309 for (auto *VE : C->varlists()) {
7310 Record.AddStmt(S: VE);
7311 }
7312 for (auto *VE : C->private_copies()) {
7313 Record.AddStmt(S: VE);
7314 }
7315 for (auto *VE : C->inits()) {
7316 Record.AddStmt(S: VE);
7317 }
7318}
7319
7320void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
7321 Record.push_back(N: C->varlist_size());
7322 VisitOMPClauseWithPostUpdate(C);
7323 Record.AddSourceLocation(Loc: C->getLParenLoc());
7324 Record.writeEnum(value: C->getKind());
7325 Record.AddSourceLocation(Loc: C->getKindLoc());
7326 Record.AddSourceLocation(Loc: C->getColonLoc());
7327 for (auto *VE : C->varlists())
7328 Record.AddStmt(S: VE);
7329 for (auto *E : C->private_copies())
7330 Record.AddStmt(S: E);
7331 for (auto *E : C->source_exprs())
7332 Record.AddStmt(S: E);
7333 for (auto *E : C->destination_exprs())
7334 Record.AddStmt(S: E);
7335 for (auto *E : C->assignment_ops())
7336 Record.AddStmt(S: E);
7337}
7338
7339void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
7340 Record.push_back(N: C->varlist_size());
7341 Record.AddSourceLocation(Loc: C->getLParenLoc());
7342 for (auto *VE : C->varlists())
7343 Record.AddStmt(S: VE);
7344}
7345
7346void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
7347 Record.push_back(N: C->varlist_size());
7348 Record.writeEnum(value: C->getModifier());
7349 VisitOMPClauseWithPostUpdate(C);
7350 Record.AddSourceLocation(Loc: C->getLParenLoc());
7351 Record.AddSourceLocation(Loc: C->getModifierLoc());
7352 Record.AddSourceLocation(Loc: C->getColonLoc());
7353 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
7354 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
7355 for (auto *VE : C->varlists())
7356 Record.AddStmt(S: VE);
7357 for (auto *VE : C->privates())
7358 Record.AddStmt(S: VE);
7359 for (auto *E : C->lhs_exprs())
7360 Record.AddStmt(S: E);
7361 for (auto *E : C->rhs_exprs())
7362 Record.AddStmt(S: E);
7363 for (auto *E : C->reduction_ops())
7364 Record.AddStmt(S: E);
7365 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
7366 for (auto *E : C->copy_ops())
7367 Record.AddStmt(S: E);
7368 for (auto *E : C->copy_array_temps())
7369 Record.AddStmt(S: E);
7370 for (auto *E : C->copy_array_elems())
7371 Record.AddStmt(S: E);
7372 }
7373}
7374
7375void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
7376 Record.push_back(N: C->varlist_size());
7377 VisitOMPClauseWithPostUpdate(C);
7378 Record.AddSourceLocation(Loc: C->getLParenLoc());
7379 Record.AddSourceLocation(Loc: C->getColonLoc());
7380 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
7381 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
7382 for (auto *VE : C->varlists())
7383 Record.AddStmt(S: VE);
7384 for (auto *VE : C->privates())
7385 Record.AddStmt(S: VE);
7386 for (auto *E : C->lhs_exprs())
7387 Record.AddStmt(S: E);
7388 for (auto *E : C->rhs_exprs())
7389 Record.AddStmt(S: E);
7390 for (auto *E : C->reduction_ops())
7391 Record.AddStmt(S: E);
7392}
7393
7394void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
7395 Record.push_back(N: C->varlist_size());
7396 VisitOMPClauseWithPostUpdate(C);
7397 Record.AddSourceLocation(Loc: C->getLParenLoc());
7398 Record.AddSourceLocation(Loc: C->getColonLoc());
7399 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
7400 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
7401 for (auto *VE : C->varlists())
7402 Record.AddStmt(S: VE);
7403 for (auto *VE : C->privates())
7404 Record.AddStmt(S: VE);
7405 for (auto *E : C->lhs_exprs())
7406 Record.AddStmt(S: E);
7407 for (auto *E : C->rhs_exprs())
7408 Record.AddStmt(S: E);
7409 for (auto *E : C->reduction_ops())
7410 Record.AddStmt(S: E);
7411 for (auto *E : C->taskgroup_descriptors())
7412 Record.AddStmt(S: E);
7413}
7414
7415void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
7416 Record.push_back(N: C->varlist_size());
7417 VisitOMPClauseWithPostUpdate(C);
7418 Record.AddSourceLocation(Loc: C->getLParenLoc());
7419 Record.AddSourceLocation(Loc: C->getColonLoc());
7420 Record.push_back(N: C->getModifier());
7421 Record.AddSourceLocation(Loc: C->getModifierLoc());
7422 for (auto *VE : C->varlists()) {
7423 Record.AddStmt(S: VE);
7424 }
7425 for (auto *VE : C->privates()) {
7426 Record.AddStmt(S: VE);
7427 }
7428 for (auto *VE : C->inits()) {
7429 Record.AddStmt(S: VE);
7430 }
7431 for (auto *VE : C->updates()) {
7432 Record.AddStmt(S: VE);
7433 }
7434 for (auto *VE : C->finals()) {
7435 Record.AddStmt(S: VE);
7436 }
7437 Record.AddStmt(S: C->getStep());
7438 Record.AddStmt(S: C->getCalcStep());
7439 for (auto *VE : C->used_expressions())
7440 Record.AddStmt(S: VE);
7441}
7442
7443void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
7444 Record.push_back(N: C->varlist_size());
7445 Record.AddSourceLocation(Loc: C->getLParenLoc());
7446 Record.AddSourceLocation(Loc: C->getColonLoc());
7447 for (auto *VE : C->varlists())
7448 Record.AddStmt(S: VE);
7449 Record.AddStmt(S: C->getAlignment());
7450}
7451
7452void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
7453 Record.push_back(N: C->varlist_size());
7454 Record.AddSourceLocation(Loc: C->getLParenLoc());
7455 for (auto *VE : C->varlists())
7456 Record.AddStmt(S: VE);
7457 for (auto *E : C->source_exprs())
7458 Record.AddStmt(S: E);
7459 for (auto *E : C->destination_exprs())
7460 Record.AddStmt(S: E);
7461 for (auto *E : C->assignment_ops())
7462 Record.AddStmt(S: E);
7463}
7464
7465void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
7466 Record.push_back(N: C->varlist_size());
7467 Record.AddSourceLocation(Loc: C->getLParenLoc());
7468 for (auto *VE : C->varlists())
7469 Record.AddStmt(S: VE);
7470 for (auto *E : C->source_exprs())
7471 Record.AddStmt(S: E);
7472 for (auto *E : C->destination_exprs())
7473 Record.AddStmt(S: E);
7474 for (auto *E : C->assignment_ops())
7475 Record.AddStmt(S: E);
7476}
7477
7478void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
7479 Record.push_back(N: C->varlist_size());
7480 Record.AddSourceLocation(Loc: C->getLParenLoc());
7481 for (auto *VE : C->varlists())
7482 Record.AddStmt(S: VE);
7483}
7484
7485void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
7486 Record.AddStmt(S: C->getDepobj());
7487 Record.AddSourceLocation(Loc: C->getLParenLoc());
7488}
7489
7490void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
7491 Record.push_back(N: C->varlist_size());
7492 Record.push_back(N: C->getNumLoops());
7493 Record.AddSourceLocation(Loc: C->getLParenLoc());
7494 Record.AddStmt(S: C->getModifier());
7495 Record.push_back(N: C->getDependencyKind());
7496 Record.AddSourceLocation(Loc: C->getDependencyLoc());
7497 Record.AddSourceLocation(Loc: C->getColonLoc());
7498 Record.AddSourceLocation(Loc: C->getOmpAllMemoryLoc());
7499 for (auto *VE : C->varlists())
7500 Record.AddStmt(S: VE);
7501 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
7502 Record.AddStmt(S: C->getLoopData(NumLoop: I));
7503}
7504
7505void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
7506 VisitOMPClauseWithPreInit(C);
7507 Record.writeEnum(value: C->getModifier());
7508 Record.AddStmt(S: C->getDevice());
7509 Record.AddSourceLocation(Loc: C->getModifierLoc());
7510 Record.AddSourceLocation(Loc: C->getLParenLoc());
7511}
7512
7513void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
7514 Record.push_back(N: C->varlist_size());
7515 Record.push_back(N: C->getUniqueDeclarationsNum());
7516 Record.push_back(N: C->getTotalComponentListNum());
7517 Record.push_back(N: C->getTotalComponentsNum());
7518 Record.AddSourceLocation(Loc: C->getLParenLoc());
7519 bool HasIteratorModifier = false;
7520 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
7521 Record.push_back(N: C->getMapTypeModifier(Cnt: I));
7522 Record.AddSourceLocation(Loc: C->getMapTypeModifierLoc(Cnt: I));
7523 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
7524 HasIteratorModifier = true;
7525 }
7526 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
7527 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
7528 Record.push_back(N: C->getMapType());
7529 Record.AddSourceLocation(Loc: C->getMapLoc());
7530 Record.AddSourceLocation(Loc: C->getColonLoc());
7531 for (auto *E : C->varlists())
7532 Record.AddStmt(S: E);
7533 for (auto *E : C->mapperlists())
7534 Record.AddStmt(S: E);
7535 if (HasIteratorModifier)
7536 Record.AddStmt(S: C->getIteratorModifier());
7537 for (auto *D : C->all_decls())
7538 Record.AddDeclRef(D);
7539 for (auto N : C->all_num_lists())
7540 Record.push_back(N);
7541 for (auto N : C->all_lists_sizes())
7542 Record.push_back(N);
7543 for (auto &M : C->all_components()) {
7544 Record.AddStmt(S: M.getAssociatedExpression());
7545 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7546 }
7547}
7548
7549void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
7550 Record.push_back(N: C->varlist_size());
7551 Record.AddSourceLocation(Loc: C->getLParenLoc());
7552 Record.AddSourceLocation(Loc: C->getColonLoc());
7553 Record.AddStmt(S: C->getAllocator());
7554 for (auto *VE : C->varlists())
7555 Record.AddStmt(S: VE);
7556}
7557
7558void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
7559 VisitOMPClauseWithPreInit(C);
7560 Record.AddStmt(S: C->getNumTeams());
7561 Record.AddSourceLocation(Loc: C->getLParenLoc());
7562}
7563
7564void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
7565 VisitOMPClauseWithPreInit(C);
7566 Record.AddStmt(S: C->getThreadLimit());
7567 Record.AddSourceLocation(Loc: C->getLParenLoc());
7568}
7569
7570void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
7571 VisitOMPClauseWithPreInit(C);
7572 Record.AddStmt(S: C->getPriority());
7573 Record.AddSourceLocation(Loc: C->getLParenLoc());
7574}
7575
7576void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
7577 VisitOMPClauseWithPreInit(C);
7578 Record.writeEnum(value: C->getModifier());
7579 Record.AddStmt(S: C->getGrainsize());
7580 Record.AddSourceLocation(Loc: C->getModifierLoc());
7581 Record.AddSourceLocation(Loc: C->getLParenLoc());
7582}
7583
7584void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
7585 VisitOMPClauseWithPreInit(C);
7586 Record.writeEnum(value: C->getModifier());
7587 Record.AddStmt(S: C->getNumTasks());
7588 Record.AddSourceLocation(Loc: C->getModifierLoc());
7589 Record.AddSourceLocation(Loc: C->getLParenLoc());
7590}
7591
7592void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
7593 Record.AddStmt(S: C->getHint());
7594 Record.AddSourceLocation(Loc: C->getLParenLoc());
7595}
7596
7597void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
7598 VisitOMPClauseWithPreInit(C);
7599 Record.push_back(N: C->getDistScheduleKind());
7600 Record.AddStmt(S: C->getChunkSize());
7601 Record.AddSourceLocation(Loc: C->getLParenLoc());
7602 Record.AddSourceLocation(Loc: C->getDistScheduleKindLoc());
7603 Record.AddSourceLocation(Loc: C->getCommaLoc());
7604}
7605
7606void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
7607 Record.push_back(N: C->getDefaultmapKind());
7608 Record.push_back(N: C->getDefaultmapModifier());
7609 Record.AddSourceLocation(Loc: C->getLParenLoc());
7610 Record.AddSourceLocation(Loc: C->getDefaultmapModifierLoc());
7611 Record.AddSourceLocation(Loc: C->getDefaultmapKindLoc());
7612}
7613
7614void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
7615 Record.push_back(N: C->varlist_size());
7616 Record.push_back(N: C->getUniqueDeclarationsNum());
7617 Record.push_back(N: C->getTotalComponentListNum());
7618 Record.push_back(N: C->getTotalComponentsNum());
7619 Record.AddSourceLocation(Loc: C->getLParenLoc());
7620 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7621 Record.push_back(N: C->getMotionModifier(Cnt: I));
7622 Record.AddSourceLocation(Loc: C->getMotionModifierLoc(Cnt: I));
7623 }
7624 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
7625 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
7626 Record.AddSourceLocation(Loc: C->getColonLoc());
7627 for (auto *E : C->varlists())
7628 Record.AddStmt(S: E);
7629 for (auto *E : C->mapperlists())
7630 Record.AddStmt(S: E);
7631 for (auto *D : C->all_decls())
7632 Record.AddDeclRef(D);
7633 for (auto N : C->all_num_lists())
7634 Record.push_back(N);
7635 for (auto N : C->all_lists_sizes())
7636 Record.push_back(N);
7637 for (auto &M : C->all_components()) {
7638 Record.AddStmt(S: M.getAssociatedExpression());
7639 Record.writeBool(Value: M.isNonContiguous());
7640 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7641 }
7642}
7643
7644void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7645 Record.push_back(N: C->varlist_size());
7646 Record.push_back(N: C->getUniqueDeclarationsNum());
7647 Record.push_back(N: C->getTotalComponentListNum());
7648 Record.push_back(N: C->getTotalComponentsNum());
7649 Record.AddSourceLocation(Loc: C->getLParenLoc());
7650 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7651 Record.push_back(N: C->getMotionModifier(Cnt: I));
7652 Record.AddSourceLocation(Loc: C->getMotionModifierLoc(Cnt: I));
7653 }
7654 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
7655 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
7656 Record.AddSourceLocation(Loc: C->getColonLoc());
7657 for (auto *E : C->varlists())
7658 Record.AddStmt(S: E);
7659 for (auto *E : C->mapperlists())
7660 Record.AddStmt(S: E);
7661 for (auto *D : C->all_decls())
7662 Record.AddDeclRef(D);
7663 for (auto N : C->all_num_lists())
7664 Record.push_back(N);
7665 for (auto N : C->all_lists_sizes())
7666 Record.push_back(N);
7667 for (auto &M : C->all_components()) {
7668 Record.AddStmt(S: M.getAssociatedExpression());
7669 Record.writeBool(Value: M.isNonContiguous());
7670 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7671 }
7672}
7673
7674void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7675 Record.push_back(N: C->varlist_size());
7676 Record.push_back(N: C->getUniqueDeclarationsNum());
7677 Record.push_back(N: C->getTotalComponentListNum());
7678 Record.push_back(N: C->getTotalComponentsNum());
7679 Record.AddSourceLocation(Loc: C->getLParenLoc());
7680 for (auto *E : C->varlists())
7681 Record.AddStmt(S: E);
7682 for (auto *VE : C->private_copies())
7683 Record.AddStmt(S: VE);
7684 for (auto *VE : C->inits())
7685 Record.AddStmt(S: VE);
7686 for (auto *D : C->all_decls())
7687 Record.AddDeclRef(D);
7688 for (auto N : C->all_num_lists())
7689 Record.push_back(N);
7690 for (auto N : C->all_lists_sizes())
7691 Record.push_back(N);
7692 for (auto &M : C->all_components()) {
7693 Record.AddStmt(S: M.getAssociatedExpression());
7694 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7695 }
7696}
7697
7698void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
7699 Record.push_back(N: C->varlist_size());
7700 Record.push_back(N: C->getUniqueDeclarationsNum());
7701 Record.push_back(N: C->getTotalComponentListNum());
7702 Record.push_back(N: C->getTotalComponentsNum());
7703 Record.AddSourceLocation(Loc: C->getLParenLoc());
7704 for (auto *E : C->varlists())
7705 Record.AddStmt(S: E);
7706 for (auto *D : C->all_decls())
7707 Record.AddDeclRef(D);
7708 for (auto N : C->all_num_lists())
7709 Record.push_back(N);
7710 for (auto N : C->all_lists_sizes())
7711 Record.push_back(N);
7712 for (auto &M : C->all_components()) {
7713 Record.AddStmt(S: M.getAssociatedExpression());
7714 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7715 }
7716}
7717
7718void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7719 Record.push_back(N: C->varlist_size());
7720 Record.push_back(N: C->getUniqueDeclarationsNum());
7721 Record.push_back(N: C->getTotalComponentListNum());
7722 Record.push_back(N: C->getTotalComponentsNum());
7723 Record.AddSourceLocation(Loc: C->getLParenLoc());
7724 for (auto *E : C->varlists())
7725 Record.AddStmt(S: E);
7726 for (auto *D : C->all_decls())
7727 Record.AddDeclRef(D);
7728 for (auto N : C->all_num_lists())
7729 Record.push_back(N);
7730 for (auto N : C->all_lists_sizes())
7731 Record.push_back(N);
7732 for (auto &M : C->all_components()) {
7733 Record.AddStmt(S: M.getAssociatedExpression());
7734 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7735 }
7736}
7737
7738void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
7739 Record.push_back(N: C->varlist_size());
7740 Record.push_back(N: C->getUniqueDeclarationsNum());
7741 Record.push_back(N: C->getTotalComponentListNum());
7742 Record.push_back(N: C->getTotalComponentsNum());
7743 Record.AddSourceLocation(Loc: C->getLParenLoc());
7744 for (auto *E : C->varlists())
7745 Record.AddStmt(S: E);
7746 for (auto *D : C->all_decls())
7747 Record.AddDeclRef(D);
7748 for (auto N : C->all_num_lists())
7749 Record.push_back(N);
7750 for (auto N : C->all_lists_sizes())
7751 Record.push_back(N);
7752 for (auto &M : C->all_components()) {
7753 Record.AddStmt(S: M.getAssociatedExpression());
7754 Record.AddDeclRef(D: M.getAssociatedDeclaration());
7755 }
7756}
7757
7758void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
7759
7760void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7761 OMPUnifiedSharedMemoryClause *) {}
7762
7763void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
7764
7765void
7766OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7767}
7768
7769void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7770 OMPAtomicDefaultMemOrderClause *C) {
7771 Record.push_back(N: C->getAtomicDefaultMemOrderKind());
7772 Record.AddSourceLocation(Loc: C->getLParenLoc());
7773 Record.AddSourceLocation(Loc: C->getAtomicDefaultMemOrderKindKwLoc());
7774}
7775
7776void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
7777 Record.push_back(N: C->getAtKind());
7778 Record.AddSourceLocation(Loc: C->getLParenLoc());
7779 Record.AddSourceLocation(Loc: C->getAtKindKwLoc());
7780}
7781
7782void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
7783 Record.push_back(N: C->getSeverityKind());
7784 Record.AddSourceLocation(Loc: C->getLParenLoc());
7785 Record.AddSourceLocation(Loc: C->getSeverityKindKwLoc());
7786}
7787
7788void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
7789 Record.AddStmt(S: C->getMessageString());
7790 Record.AddSourceLocation(Loc: C->getLParenLoc());
7791}
7792
7793void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
7794 Record.push_back(N: C->varlist_size());
7795 Record.AddSourceLocation(Loc: C->getLParenLoc());
7796 for (auto *VE : C->varlists())
7797 Record.AddStmt(S: VE);
7798 for (auto *E : C->private_refs())
7799 Record.AddStmt(S: E);
7800}
7801
7802void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
7803 Record.push_back(N: C->varlist_size());
7804 Record.AddSourceLocation(Loc: C->getLParenLoc());
7805 for (auto *VE : C->varlists())
7806 Record.AddStmt(S: VE);
7807}
7808
7809void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
7810 Record.push_back(N: C->varlist_size());
7811 Record.AddSourceLocation(Loc: C->getLParenLoc());
7812 for (auto *VE : C->varlists())
7813 Record.AddStmt(S: VE);
7814}
7815
7816void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
7817 Record.writeEnum(value: C->getKind());
7818 Record.writeEnum(value: C->getModifier());
7819 Record.AddSourceLocation(Loc: C->getLParenLoc());
7820 Record.AddSourceLocation(Loc: C->getKindKwLoc());
7821 Record.AddSourceLocation(Loc: C->getModifierKwLoc());
7822}
7823
7824void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
7825 Record.push_back(N: C->getNumberOfAllocators());
7826 Record.AddSourceLocation(Loc: C->getLParenLoc());
7827 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
7828 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
7829 Record.AddStmt(S: Data.Allocator);
7830 Record.AddStmt(S: Data.AllocatorTraits);
7831 Record.AddSourceLocation(Loc: Data.LParenLoc);
7832 Record.AddSourceLocation(Loc: Data.RParenLoc);
7833 }
7834}
7835
7836void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
7837 Record.push_back(N: C->varlist_size());
7838 Record.AddSourceLocation(Loc: C->getLParenLoc());
7839 Record.AddStmt(S: C->getModifier());
7840 Record.AddSourceLocation(Loc: C->getColonLoc());
7841 for (Expr *E : C->varlists())
7842 Record.AddStmt(S: E);
7843}
7844
7845void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
7846 Record.writeEnum(value: C->getBindKind());
7847 Record.AddSourceLocation(Loc: C->getLParenLoc());
7848 Record.AddSourceLocation(Loc: C->getBindKindLoc());
7849}
7850
7851void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
7852 VisitOMPClauseWithPreInit(C);
7853 Record.AddStmt(S: C->getSize());
7854 Record.AddSourceLocation(Loc: C->getLParenLoc());
7855}
7856
7857void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
7858 Record.push_back(N: C->varlist_size());
7859 Record.push_back(N: C->getNumLoops());
7860 Record.AddSourceLocation(Loc: C->getLParenLoc());
7861 Record.push_back(N: C->getDependenceType());
7862 Record.AddSourceLocation(Loc: C->getDependenceLoc());
7863 Record.AddSourceLocation(Loc: C->getColonLoc());
7864 for (auto *VE : C->varlists())
7865 Record.AddStmt(S: VE);
7866 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
7867 Record.AddStmt(S: C->getLoopData(NumLoop: I));
7868}
7869
7870void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
7871 Record.AddAttributes(Attrs: C->getAttrs());
7872 Record.AddSourceLocation(Loc: C->getBeginLoc());
7873 Record.AddSourceLocation(Loc: C->getLParenLoc());
7874 Record.AddSourceLocation(Loc: C->getEndLoc());
7875}
7876
7877void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
7878
7879void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
7880 writeUInt32(Value: TI->Sets.size());
7881 for (const auto &Set : TI->Sets) {
7882 writeEnum(value: Set.Kind);
7883 writeUInt32(Value: Set.Selectors.size());
7884 for (const auto &Selector : Set.Selectors) {
7885 writeEnum(value: Selector.Kind);
7886 writeBool(Value: Selector.ScoreOrCondition);
7887 if (Selector.ScoreOrCondition)
7888 writeExprRef(value: Selector.ScoreOrCondition);
7889 writeUInt32(Value: Selector.Properties.size());
7890 for (const auto &Property : Selector.Properties)
7891 writeEnum(value: Property.Kind);
7892 }
7893 }
7894}
7895
7896void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
7897 if (!Data)
7898 return;
7899 writeUInt32(Value: Data->getNumClauses());
7900 writeUInt32(Value: Data->getNumChildren());
7901 writeBool(Value: Data->hasAssociatedStmt());
7902 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
7903 writeOMPClause(C: Data->getClauses()[I]);
7904 if (Data->hasAssociatedStmt())
7905 AddStmt(S: Data->getAssociatedStmt());
7906 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
7907 AddStmt(S: Data->getChildren()[I]);
7908}
7909
7910void ASTRecordWriter::writeOpenACCVarList(const OpenACCClauseWithVarList *C) {
7911 writeUInt32(Value: C->getVarList().size());
7912 for (Expr *E : C->getVarList())
7913 AddStmt(S: E);
7914}
7915
7916void ASTRecordWriter::writeOpenACCIntExprList(ArrayRef<Expr *> Exprs) {
7917 writeUInt32(Value: Exprs.size());
7918 for (Expr *E : Exprs)
7919 AddStmt(S: E);
7920}
7921
7922void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
7923 writeEnum(value: C->getClauseKind());
7924 writeSourceLocation(Loc: C->getBeginLoc());
7925 writeSourceLocation(Loc: C->getEndLoc());
7926
7927 switch (C->getClauseKind()) {
7928 case OpenACCClauseKind::Default: {
7929 const auto *DC = cast<OpenACCDefaultClause>(Val: C);
7930 writeSourceLocation(Loc: DC->getLParenLoc());
7931 writeEnum(value: DC->getDefaultClauseKind());
7932 return;
7933 }
7934 case OpenACCClauseKind::If: {
7935 const auto *IC = cast<OpenACCIfClause>(Val: C);
7936 writeSourceLocation(Loc: IC->getLParenLoc());
7937 AddStmt(S: const_cast<Expr*>(IC->getConditionExpr()));
7938 return;
7939 }
7940 case OpenACCClauseKind::Self: {
7941 const auto *SC = cast<OpenACCSelfClause>(Val: C);
7942 writeSourceLocation(Loc: SC->getLParenLoc());
7943 writeBool(Value: SC->hasConditionExpr());
7944 if (SC->hasConditionExpr())
7945 AddStmt(S: const_cast<Expr*>(SC->getConditionExpr()));
7946 return;
7947 }
7948 case OpenACCClauseKind::NumGangs: {
7949 const auto *NGC = cast<OpenACCNumGangsClause>(Val: C);
7950 writeSourceLocation(Loc: NGC->getLParenLoc());
7951 writeUInt32(Value: NGC->getIntExprs().size());
7952 for (Expr *E : NGC->getIntExprs())
7953 AddStmt(S: E);
7954 return;
7955 }
7956 case OpenACCClauseKind::NumWorkers: {
7957 const auto *NWC = cast<OpenACCNumWorkersClause>(Val: C);
7958 writeSourceLocation(Loc: NWC->getLParenLoc());
7959 AddStmt(S: const_cast<Expr*>(NWC->getIntExpr()));
7960 return;
7961 }
7962 case OpenACCClauseKind::VectorLength: {
7963 const auto *NWC = cast<OpenACCVectorLengthClause>(Val: C);
7964 writeSourceLocation(Loc: NWC->getLParenLoc());
7965 AddStmt(S: const_cast<Expr*>(NWC->getIntExpr()));
7966 return;
7967 }
7968 case OpenACCClauseKind::Private: {
7969 const auto *PC = cast<OpenACCPrivateClause>(Val: C);
7970 writeSourceLocation(Loc: PC->getLParenLoc());
7971 writeOpenACCVarList(C: PC);
7972 return;
7973 }
7974 case OpenACCClauseKind::FirstPrivate: {
7975 const auto *FPC = cast<OpenACCFirstPrivateClause>(Val: C);
7976 writeSourceLocation(Loc: FPC->getLParenLoc());
7977 writeOpenACCVarList(C: FPC);
7978 return;
7979 }
7980 case OpenACCClauseKind::Attach: {
7981 const auto *AC = cast<OpenACCAttachClause>(Val: C);
7982 writeSourceLocation(Loc: AC->getLParenLoc());
7983 writeOpenACCVarList(C: AC);
7984 return;
7985 }
7986 case OpenACCClauseKind::DevicePtr: {
7987 const auto *DPC = cast<OpenACCDevicePtrClause>(Val: C);
7988 writeSourceLocation(Loc: DPC->getLParenLoc());
7989 writeOpenACCVarList(C: DPC);
7990 return;
7991 }
7992 case OpenACCClauseKind::NoCreate: {
7993 const auto *NCC = cast<OpenACCNoCreateClause>(Val: C);
7994 writeSourceLocation(Loc: NCC->getLParenLoc());
7995 writeOpenACCVarList(C: NCC);
7996 return;
7997 }
7998 case OpenACCClauseKind::Present: {
7999 const auto *PC = cast<OpenACCPresentClause>(Val: C);
8000 writeSourceLocation(Loc: PC->getLParenLoc());
8001 writeOpenACCVarList(C: PC);
8002 return;
8003 }
8004 case OpenACCClauseKind::Copy:
8005 case OpenACCClauseKind::PCopy:
8006 case OpenACCClauseKind::PresentOrCopy: {
8007 const auto *CC = cast<OpenACCCopyClause>(Val: C);
8008 writeSourceLocation(Loc: CC->getLParenLoc());
8009 writeOpenACCVarList(C: CC);
8010 return;
8011 }
8012 case OpenACCClauseKind::CopyIn:
8013 case OpenACCClauseKind::PCopyIn:
8014 case OpenACCClauseKind::PresentOrCopyIn: {
8015 const auto *CIC = cast<OpenACCCopyInClause>(Val: C);
8016 writeSourceLocation(Loc: CIC->getLParenLoc());
8017 writeBool(Value: CIC->isReadOnly());
8018 writeOpenACCVarList(C: CIC);
8019 return;
8020 }
8021 case OpenACCClauseKind::CopyOut:
8022 case OpenACCClauseKind::PCopyOut:
8023 case OpenACCClauseKind::PresentOrCopyOut: {
8024 const auto *COC = cast<OpenACCCopyOutClause>(Val: C);
8025 writeSourceLocation(Loc: COC->getLParenLoc());
8026 writeBool(Value: COC->isZero());
8027 writeOpenACCVarList(C: COC);
8028 return;
8029 }
8030 case OpenACCClauseKind::Create:
8031 case OpenACCClauseKind::PCreate:
8032 case OpenACCClauseKind::PresentOrCreate: {
8033 const auto *CC = cast<OpenACCCreateClause>(Val: C);
8034 writeSourceLocation(Loc: CC->getLParenLoc());
8035 writeBool(Value: CC->isZero());
8036 writeOpenACCVarList(C: CC);
8037 return;
8038 }
8039 case OpenACCClauseKind::Async: {
8040 const auto *AC = cast<OpenACCAsyncClause>(Val: C);
8041 writeSourceLocation(Loc: AC->getLParenLoc());
8042 writeBool(Value: AC->hasIntExpr());
8043 if (AC->hasIntExpr())
8044 AddStmt(S: const_cast<Expr*>(AC->getIntExpr()));
8045 return;
8046 }
8047 case OpenACCClauseKind::Wait: {
8048 const auto *WC = cast<OpenACCWaitClause>(Val: C);
8049 writeSourceLocation(Loc: WC->getLParenLoc());
8050 writeBool(Value: WC->getDevNumExpr());
8051 if (Expr *DNE = WC->getDevNumExpr())
8052 AddStmt(S: DNE);
8053 writeSourceLocation(Loc: WC->getQueuesLoc());
8054
8055 writeOpenACCIntExprList(Exprs: WC->getQueueIdExprs());
8056 return;
8057 }
8058 case OpenACCClauseKind::DeviceType:
8059 case OpenACCClauseKind::DType: {
8060 const auto *DTC = cast<OpenACCDeviceTypeClause>(Val: C);
8061 writeSourceLocation(Loc: DTC->getLParenLoc());
8062 writeUInt32(Value: DTC->getArchitectures().size());
8063 for (const DeviceTypeArgument &Arg : DTC->getArchitectures()) {
8064 writeBool(Value: Arg.first);
8065 if (Arg.first)
8066 AddIdentifierRef(II: Arg.first);
8067 writeSourceLocation(Loc: Arg.second);
8068 }
8069 return;
8070 }
8071 case OpenACCClauseKind::Reduction: {
8072 const auto *RC = cast<OpenACCReductionClause>(Val: C);
8073 writeSourceLocation(Loc: RC->getLParenLoc());
8074 writeEnum(value: RC->getReductionOp());
8075 writeOpenACCVarList(C: RC);
8076 return;
8077 }
8078 case OpenACCClauseKind::Seq:
8079 case OpenACCClauseKind::Independent:
8080 case OpenACCClauseKind::Auto:
8081 // Nothing to do here, there is no additional information beyond the
8082 // begin/end loc and clause kind.
8083 return;
8084
8085 case OpenACCClauseKind::Finalize:
8086 case OpenACCClauseKind::IfPresent:
8087 case OpenACCClauseKind::Worker:
8088 case OpenACCClauseKind::Vector:
8089 case OpenACCClauseKind::NoHost:
8090 case OpenACCClauseKind::UseDevice:
8091 case OpenACCClauseKind::Delete:
8092 case OpenACCClauseKind::Detach:
8093 case OpenACCClauseKind::Device:
8094 case OpenACCClauseKind::DeviceResident:
8095 case OpenACCClauseKind::Host:
8096 case OpenACCClauseKind::Link:
8097 case OpenACCClauseKind::Collapse:
8098 case OpenACCClauseKind::Bind:
8099 case OpenACCClauseKind::DeviceNum:
8100 case OpenACCClauseKind::DefaultAsync:
8101 case OpenACCClauseKind::Tile:
8102 case OpenACCClauseKind::Gang:
8103 case OpenACCClauseKind::Invalid:
8104 llvm_unreachable("Clause serialization not yet implemented");
8105 }
8106 llvm_unreachable("Invalid Clause Kind");
8107}
8108
8109void ASTRecordWriter::writeOpenACCClauseList(
8110 ArrayRef<const OpenACCClause *> Clauses) {
8111 for (const OpenACCClause *Clause : Clauses)
8112 writeOpenACCClause(C: Clause);
8113}
8114