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