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