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