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