1 //===- ASTWriter.cpp - AST File Writer ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the ASTWriter class, which writes AST files.
11 //===----------------------------------------------------------------------===//
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "MultiOnDiskHashTable.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTUnresolvedSet.h"
18 #include "clang/AST/AbstractTypeWriter.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclContextInternals.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/LambdaCapture.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/OpenMPClause.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateName.h"
35 #include "clang/AST/Type.h"
36 #include "clang/AST/TypeLocVisitor.h"
37 #include "clang/Basic/Diagnostic.h"
38 #include "clang/Basic/DiagnosticOptions.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/FileSystemOptions.h"
41 #include "clang/Basic/IdentifierTable.h"
42 #include "clang/Basic/LLVM.h"
43 #include "clang/Basic/Lambda.h"
44 #include "clang/Basic/LangOptions.h"
45 #include "clang/Basic/Module.h"
46 #include "clang/Basic/ObjCRuntime.h"
47 #include "clang/Basic/OpenCLOptions.h"
48 #include "clang/Basic/SourceLocation.h"
49 #include "clang/Basic/SourceManager.h"
50 #include "clang/Basic/SourceManagerInternals.h"
51 #include "clang/Basic/Specifiers.h"
52 #include "clang/Basic/TargetInfo.h"
53 #include "clang/Basic/TargetOptions.h"
54 #include "clang/Basic/Version.h"
55 #include "clang/Lex/HeaderSearch.h"
56 #include "clang/Lex/HeaderSearchOptions.h"
57 #include "clang/Lex/MacroInfo.h"
58 #include "clang/Lex/ModuleMap.h"
59 #include "clang/Lex/PreprocessingRecord.h"
60 #include "clang/Lex/Preprocessor.h"
61 #include "clang/Lex/PreprocessorOptions.h"
62 #include "clang/Lex/Token.h"
63 #include "clang/Sema/IdentifierResolver.h"
64 #include "clang/Sema/ObjCMethodList.h"
65 #include "clang/Sema/Sema.h"
66 #include "clang/Sema/Weak.h"
67 #include "clang/Serialization/ASTBitCodes.h"
68 #include "clang/Serialization/ASTReader.h"
69 #include "clang/Serialization/ASTRecordWriter.h"
70 #include "clang/Serialization/InMemoryModuleCache.h"
71 #include "clang/Serialization/ModuleFile.h"
72 #include "clang/Serialization/ModuleFileExtension.h"
73 #include "clang/Serialization/SerializationDiagnostic.h"
74 #include "llvm/ADT/APFloat.h"
75 #include "llvm/ADT/APInt.h"
76 #include "llvm/ADT/APSInt.h"
77 #include "llvm/ADT/ArrayRef.h"
78 #include "llvm/ADT/DenseMap.h"
79 #include "llvm/ADT/Hashing.h"
80 #include "llvm/ADT/PointerIntPair.h"
81 #include "llvm/ADT/STLExtras.h"
82 #include "llvm/ADT/ScopeExit.h"
83 #include "llvm/ADT/SmallPtrSet.h"
84 #include "llvm/ADT/SmallString.h"
85 #include "llvm/ADT/SmallVector.h"
86 #include "llvm/ADT/StringMap.h"
87 #include "llvm/ADT/StringRef.h"
88 #include "llvm/Bitstream/BitCodes.h"
89 #include "llvm/Bitstream/BitstreamWriter.h"
90 #include "llvm/Support/Casting.h"
91 #include "llvm/Support/Compression.h"
92 #include "llvm/Support/DJB.h"
93 #include "llvm/Support/Endian.h"
94 #include "llvm/Support/EndianStream.h"
95 #include "llvm/Support/Error.h"
96 #include "llvm/Support/ErrorHandling.h"
97 #include "llvm/Support/LEB128.h"
98 #include "llvm/Support/MemoryBuffer.h"
99 #include "llvm/Support/OnDiskHashTable.h"
100 #include "llvm/Support/Path.h"
101 #include "llvm/Support/SHA1.h"
102 #include "llvm/Support/TimeProfiler.h"
103 #include "llvm/Support/VersionTuple.h"
104 #include "llvm/Support/raw_ostream.h"
119 using namespace clang
;
120 using namespace clang::serialization
;
122 template <typename T
, typename Allocator
>
123 static StringRef
bytes(const std::vector
<T
, Allocator
> &v
) {
124 if (v
.empty()) return StringRef();
125 return StringRef(reinterpret_cast<const char*>(&v
[0]),
126 sizeof(T
) * v
.size());
129 template <typename T
>
130 static StringRef
bytes(const SmallVectorImpl
<T
> &v
) {
131 return StringRef(reinterpret_cast<const char*>(v
.data()),
132 sizeof(T
) * v
.size());
135 static std::string
bytes(const std::vector
<bool> &V
) {
137 Str
.reserve(V
.size() / 8);
138 for (unsigned I
= 0, E
= V
.size(); I
< E
;) {
140 for (unsigned Bit
= 0; Bit
< 8 && I
< E
; ++Bit
, ++I
)
147 //===----------------------------------------------------------------------===//
148 // Type serialization
149 //===----------------------------------------------------------------------===//
151 static TypeCode
getTypeCodeForTypeClass(Type::TypeClass id
) {
153 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
154 case Type::CLASS_ID: return TYPE_##CODE_ID;
155 #include "clang/Serialization/TypeBitCodes.def"
157 llvm_unreachable("shouldn't be serializing a builtin type this way");
159 llvm_unreachable("bad type kind");
164 std::set
<const FileEntry
*> GetAffectingModuleMaps(const Preprocessor
&PP
,
165 Module
*RootModule
) {
166 SmallVector
<const Module
*> ModulesToProcess
{RootModule
};
168 const HeaderSearch
&HS
= PP
.getHeaderSearchInfo();
170 SmallVector
<OptionalFileEntryRef
, 16> FilesByUID
;
171 HS
.getFileMgr().GetUniqueIDMapping(FilesByUID
);
173 if (FilesByUID
.size() > HS
.header_file_size())
174 FilesByUID
.resize(HS
.header_file_size());
176 for (unsigned UID
= 0, LastUID
= FilesByUID
.size(); UID
!= LastUID
; ++UID
) {
177 OptionalFileEntryRef File
= FilesByUID
[UID
];
181 const HeaderFileInfo
*HFI
=
182 HS
.getExistingFileInfo(*File
, /*WantExternal*/ false);
183 if (!HFI
|| (HFI
->isModuleHeader
&& !HFI
->isCompilingModuleHeader
))
186 for (const auto &KH
: HS
.findResolvedModulesForHeader(*File
)) {
189 ModulesToProcess
.push_back(KH
.getModule());
193 const ModuleMap
&MM
= HS
.getModuleMap();
194 SourceManager
&SourceMgr
= PP
.getSourceManager();
196 std::set
<const FileEntry
*> ModuleMaps
{};
197 auto CollectIncludingModuleMaps
= [&](FileEntryRef F
) {
198 if (!ModuleMaps
.insert(F
).second
)
200 FileID FID
= SourceMgr
.translateFile(F
);
201 SourceLocation Loc
= SourceMgr
.getIncludeLoc(FID
);
202 // The include location of inferred module maps can point into the header
203 // file that triggered the inferring. Cut off the walk if that's the case.
204 while (Loc
.isValid() && isModuleMap(SourceMgr
.getFileCharacteristic(Loc
))) {
205 FID
= SourceMgr
.getFileID(Loc
);
206 if (!ModuleMaps
.insert(*SourceMgr
.getFileEntryRefForID(FID
)).second
)
208 Loc
= SourceMgr
.getIncludeLoc(FID
);
212 std::set
<const Module
*> ProcessedModules
;
213 auto CollectIncludingMapsFromAncestors
= [&](const Module
*M
) {
214 for (const Module
*Mod
= M
; Mod
; Mod
= Mod
->Parent
) {
215 if (!ProcessedModules
.insert(Mod
).second
)
217 // The containing module map is affecting, because it's being pointed
218 // into by Module::DefinitionLoc.
219 if (auto ModuleMapFile
= MM
.getContainingModuleMapFile(Mod
))
220 CollectIncludingModuleMaps(*ModuleMapFile
);
221 // For inferred modules, the module map that allowed inferring is not in
222 // the include chain of the virtual containing module map file. It did
223 // affect the compilation, though.
224 if (auto ModuleMapFile
= MM
.getModuleMapFileForUniquing(Mod
))
225 CollectIncludingModuleMaps(*ModuleMapFile
);
229 for (const Module
*CurrentModule
: ModulesToProcess
) {
230 CollectIncludingMapsFromAncestors(CurrentModule
);
231 for (const Module
*ImportedModule
: CurrentModule
->Imports
)
232 CollectIncludingMapsFromAncestors(ImportedModule
);
233 for (const Module
*UndeclaredModule
: CurrentModule
->UndeclaredUses
)
234 CollectIncludingMapsFromAncestors(UndeclaredModule
);
240 class ASTTypeWriter
{
242 ASTWriter::RecordData Record
;
243 ASTRecordWriter BasicWriter
;
246 ASTTypeWriter(ASTWriter
&Writer
)
247 : Writer(Writer
), BasicWriter(Writer
, Record
) {}
249 uint64_t write(QualType T
) {
250 if (T
.hasLocalNonFastQualifiers()) {
251 Qualifiers Qs
= T
.getLocalQualifiers();
252 BasicWriter
.writeQualType(T
.getLocalUnqualifiedType());
253 BasicWriter
.writeQualifiers(Qs
);
254 return BasicWriter
.Emit(TYPE_EXT_QUAL
, Writer
.getTypeExtQualAbbrev());
257 const Type
*typePtr
= T
.getTypePtr();
258 serialization::AbstractTypeWriter
<ASTRecordWriter
> atw(BasicWriter
);
260 return BasicWriter
.Emit(getTypeCodeForTypeClass(typePtr
->getTypeClass()),
265 class TypeLocWriter
: public TypeLocVisitor
<TypeLocWriter
> {
266 using LocSeq
= SourceLocationSequence
;
268 ASTRecordWriter
&Record
;
271 void addSourceLocation(SourceLocation Loc
) {
272 Record
.AddSourceLocation(Loc
, Seq
);
274 void addSourceRange(SourceRange Range
) { Record
.AddSourceRange(Range
, Seq
); }
277 TypeLocWriter(ASTRecordWriter
&Record
, LocSeq
*Seq
)
278 : Record(Record
), Seq(Seq
) {}
280 #define ABSTRACT_TYPELOC(CLASS, PARENT)
281 #define TYPELOC(CLASS, PARENT) \
282 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
283 #include "clang/AST/TypeLocNodes.def"
285 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc
);
286 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc
);
291 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL
) {
295 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL
) {
296 addSourceLocation(TL
.getBuiltinLoc());
297 if (TL
.needsExtraLocalData()) {
298 Record
.push_back(TL
.getWrittenTypeSpec());
299 Record
.push_back(static_cast<uint64_t>(TL
.getWrittenSignSpec()));
300 Record
.push_back(static_cast<uint64_t>(TL
.getWrittenWidthSpec()));
301 Record
.push_back(TL
.hasModeAttr());
305 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL
) {
306 addSourceLocation(TL
.getNameLoc());
309 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL
) {
310 addSourceLocation(TL
.getStarLoc());
313 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL
) {
317 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL
) {
321 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL
) {
322 addSourceLocation(TL
.getCaretLoc());
325 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL
) {
326 addSourceLocation(TL
.getAmpLoc());
329 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL
) {
330 addSourceLocation(TL
.getAmpAmpLoc());
333 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL
) {
334 addSourceLocation(TL
.getStarLoc());
335 Record
.AddTypeSourceInfo(TL
.getClassTInfo());
338 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL
) {
339 addSourceLocation(TL
.getLBracketLoc());
340 addSourceLocation(TL
.getRBracketLoc());
341 Record
.push_back(TL
.getSizeExpr() ? 1 : 0);
342 if (TL
.getSizeExpr())
343 Record
.AddStmt(TL
.getSizeExpr());
346 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL
) {
347 VisitArrayTypeLoc(TL
);
350 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL
) {
351 VisitArrayTypeLoc(TL
);
354 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL
) {
355 VisitArrayTypeLoc(TL
);
358 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
359 DependentSizedArrayTypeLoc TL
) {
360 VisitArrayTypeLoc(TL
);
363 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
364 DependentAddressSpaceTypeLoc TL
) {
365 addSourceLocation(TL
.getAttrNameLoc());
366 SourceRange range
= TL
.getAttrOperandParensRange();
367 addSourceLocation(range
.getBegin());
368 addSourceLocation(range
.getEnd());
369 Record
.AddStmt(TL
.getAttrExprOperand());
372 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
373 DependentSizedExtVectorTypeLoc TL
) {
374 addSourceLocation(TL
.getNameLoc());
377 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL
) {
378 addSourceLocation(TL
.getNameLoc());
381 void TypeLocWriter::VisitDependentVectorTypeLoc(
382 DependentVectorTypeLoc TL
) {
383 addSourceLocation(TL
.getNameLoc());
386 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL
) {
387 addSourceLocation(TL
.getNameLoc());
390 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL
) {
391 addSourceLocation(TL
.getAttrNameLoc());
392 SourceRange range
= TL
.getAttrOperandParensRange();
393 addSourceLocation(range
.getBegin());
394 addSourceLocation(range
.getEnd());
395 Record
.AddStmt(TL
.getAttrRowOperand());
396 Record
.AddStmt(TL
.getAttrColumnOperand());
399 void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
400 DependentSizedMatrixTypeLoc TL
) {
401 addSourceLocation(TL
.getAttrNameLoc());
402 SourceRange range
= TL
.getAttrOperandParensRange();
403 addSourceLocation(range
.getBegin());
404 addSourceLocation(range
.getEnd());
405 Record
.AddStmt(TL
.getAttrRowOperand());
406 Record
.AddStmt(TL
.getAttrColumnOperand());
409 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL
) {
410 addSourceLocation(TL
.getLocalRangeBegin());
411 addSourceLocation(TL
.getLParenLoc());
412 addSourceLocation(TL
.getRParenLoc());
413 addSourceRange(TL
.getExceptionSpecRange());
414 addSourceLocation(TL
.getLocalRangeEnd());
415 for (unsigned i
= 0, e
= TL
.getNumParams(); i
!= e
; ++i
)
416 Record
.AddDeclRef(TL
.getParam(i
));
419 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL
) {
420 VisitFunctionTypeLoc(TL
);
423 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL
) {
424 VisitFunctionTypeLoc(TL
);
427 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL
) {
428 addSourceLocation(TL
.getNameLoc());
431 void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL
) {
432 addSourceLocation(TL
.getNameLoc());
435 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL
) {
436 addSourceLocation(TL
.getNameLoc());
439 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL
) {
440 if (TL
.getNumProtocols()) {
441 addSourceLocation(TL
.getProtocolLAngleLoc());
442 addSourceLocation(TL
.getProtocolRAngleLoc());
444 for (unsigned i
= 0, e
= TL
.getNumProtocols(); i
!= e
; ++i
)
445 addSourceLocation(TL
.getProtocolLoc(i
));
448 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL
) {
449 addSourceLocation(TL
.getTypeofLoc());
450 addSourceLocation(TL
.getLParenLoc());
451 addSourceLocation(TL
.getRParenLoc());
454 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL
) {
455 addSourceLocation(TL
.getTypeofLoc());
456 addSourceLocation(TL
.getLParenLoc());
457 addSourceLocation(TL
.getRParenLoc());
458 Record
.AddTypeSourceInfo(TL
.getUnmodifiedTInfo());
461 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL
) {
462 addSourceLocation(TL
.getDecltypeLoc());
463 addSourceLocation(TL
.getRParenLoc());
466 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL
) {
467 addSourceLocation(TL
.getKWLoc());
468 addSourceLocation(TL
.getLParenLoc());
469 addSourceLocation(TL
.getRParenLoc());
470 Record
.AddTypeSourceInfo(TL
.getUnderlyingTInfo());
473 void ASTRecordWriter::AddConceptReference(const ConceptReference
*CR
) {
475 AddNestedNameSpecifierLoc(CR
->getNestedNameSpecifierLoc());
476 AddSourceLocation(CR
->getTemplateKWLoc());
477 AddDeclarationNameInfo(CR
->getConceptNameInfo());
478 AddDeclRef(CR
->getFoundDecl());
479 AddDeclRef(CR
->getNamedConcept());
480 push_back(CR
->getTemplateArgsAsWritten() != nullptr);
481 if (CR
->getTemplateArgsAsWritten())
482 AddASTTemplateArgumentListInfo(CR
->getTemplateArgsAsWritten());
485 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL
) {
486 addSourceLocation(TL
.getNameLoc());
487 auto *CR
= TL
.getConceptReference();
488 Record
.push_back(TL
.isConstrained() && CR
);
489 if (TL
.isConstrained() && CR
)
490 Record
.AddConceptReference(CR
);
491 Record
.push_back(TL
.isDecltypeAuto());
492 if (TL
.isDecltypeAuto())
493 addSourceLocation(TL
.getRParenLoc());
496 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
497 DeducedTemplateSpecializationTypeLoc TL
) {
498 addSourceLocation(TL
.getTemplateNameLoc());
501 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL
) {
502 addSourceLocation(TL
.getNameLoc());
505 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL
) {
506 addSourceLocation(TL
.getNameLoc());
509 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL
) {
510 Record
.AddAttr(TL
.getAttr());
513 void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL
) {
517 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL
) {
518 addSourceLocation(TL
.getNameLoc());
521 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
522 SubstTemplateTypeParmTypeLoc TL
) {
523 addSourceLocation(TL
.getNameLoc());
526 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
527 SubstTemplateTypeParmPackTypeLoc TL
) {
528 addSourceLocation(TL
.getNameLoc());
531 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
532 TemplateSpecializationTypeLoc TL
) {
533 addSourceLocation(TL
.getTemplateKeywordLoc());
534 addSourceLocation(TL
.getTemplateNameLoc());
535 addSourceLocation(TL
.getLAngleLoc());
536 addSourceLocation(TL
.getRAngleLoc());
537 for (unsigned i
= 0, e
= TL
.getNumArgs(); i
!= e
; ++i
)
538 Record
.AddTemplateArgumentLocInfo(TL
.getArgLoc(i
).getArgument().getKind(),
539 TL
.getArgLoc(i
).getLocInfo());
542 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL
) {
543 addSourceLocation(TL
.getLParenLoc());
544 addSourceLocation(TL
.getRParenLoc());
547 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL
) {
548 addSourceLocation(TL
.getExpansionLoc());
551 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL
) {
552 addSourceLocation(TL
.getElaboratedKeywordLoc());
553 Record
.AddNestedNameSpecifierLoc(TL
.getQualifierLoc());
556 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL
) {
557 addSourceLocation(TL
.getNameLoc());
560 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL
) {
561 addSourceLocation(TL
.getElaboratedKeywordLoc());
562 Record
.AddNestedNameSpecifierLoc(TL
.getQualifierLoc());
563 addSourceLocation(TL
.getNameLoc());
566 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
567 DependentTemplateSpecializationTypeLoc TL
) {
568 addSourceLocation(TL
.getElaboratedKeywordLoc());
569 Record
.AddNestedNameSpecifierLoc(TL
.getQualifierLoc());
570 addSourceLocation(TL
.getTemplateKeywordLoc());
571 addSourceLocation(TL
.getTemplateNameLoc());
572 addSourceLocation(TL
.getLAngleLoc());
573 addSourceLocation(TL
.getRAngleLoc());
574 for (unsigned I
= 0, E
= TL
.getNumArgs(); I
!= E
; ++I
)
575 Record
.AddTemplateArgumentLocInfo(TL
.getArgLoc(I
).getArgument().getKind(),
576 TL
.getArgLoc(I
).getLocInfo());
579 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL
) {
580 addSourceLocation(TL
.getEllipsisLoc());
583 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL
) {
584 addSourceLocation(TL
.getNameLoc());
585 addSourceLocation(TL
.getNameEndLoc());
588 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL
) {
589 Record
.push_back(TL
.hasBaseTypeAsWritten());
590 addSourceLocation(TL
.getTypeArgsLAngleLoc());
591 addSourceLocation(TL
.getTypeArgsRAngleLoc());
592 for (unsigned i
= 0, e
= TL
.getNumTypeArgs(); i
!= e
; ++i
)
593 Record
.AddTypeSourceInfo(TL
.getTypeArgTInfo(i
));
594 addSourceLocation(TL
.getProtocolLAngleLoc());
595 addSourceLocation(TL
.getProtocolRAngleLoc());
596 for (unsigned i
= 0, e
= TL
.getNumProtocols(); i
!= e
; ++i
)
597 addSourceLocation(TL
.getProtocolLoc(i
));
600 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL
) {
601 addSourceLocation(TL
.getStarLoc());
604 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL
) {
605 addSourceLocation(TL
.getKWLoc());
606 addSourceLocation(TL
.getLParenLoc());
607 addSourceLocation(TL
.getRParenLoc());
610 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL
) {
611 addSourceLocation(TL
.getKWLoc());
614 void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL
) {
615 addSourceLocation(TL
.getNameLoc());
617 void TypeLocWriter::VisitDependentBitIntTypeLoc(
618 clang::DependentBitIntTypeLoc TL
) {
619 addSourceLocation(TL
.getNameLoc());
622 void ASTWriter::WriteTypeAbbrevs() {
623 using namespace llvm
;
625 std::shared_ptr
<BitCodeAbbrev
> Abv
;
627 // Abbreviation for TYPE_EXT_QUAL
628 Abv
= std::make_shared
<BitCodeAbbrev
>();
629 Abv
->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL
));
630 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Type
631 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 3)); // Quals
632 TypeExtQualAbbrev
= Stream
.EmitAbbrev(std::move(Abv
));
635 //===----------------------------------------------------------------------===//
636 // ASTWriter Implementation
637 //===----------------------------------------------------------------------===//
639 static void EmitBlockID(unsigned ID
, const char *Name
,
640 llvm::BitstreamWriter
&Stream
,
641 ASTWriter::RecordDataImpl
&Record
) {
643 Record
.push_back(ID
);
644 Stream
.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID
, Record
);
646 // Emit the block name if present.
647 if (!Name
|| Name
[0] == 0)
651 Record
.push_back(*Name
++);
652 Stream
.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME
, Record
);
655 static void EmitRecordID(unsigned ID
, const char *Name
,
656 llvm::BitstreamWriter
&Stream
,
657 ASTWriter::RecordDataImpl
&Record
) {
659 Record
.push_back(ID
);
661 Record
.push_back(*Name
++);
662 Stream
.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME
, Record
);
665 static void AddStmtsExprs(llvm::BitstreamWriter
&Stream
,
666 ASTWriter::RecordDataImpl
&Record
) {
667 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
669 RECORD(STMT_NULL_PTR
);
670 RECORD(STMT_REF_PTR
);
672 RECORD(STMT_COMPOUND
);
674 RECORD(STMT_DEFAULT
);
676 RECORD(STMT_ATTRIBUTED
);
683 RECORD(STMT_INDIRECT_GOTO
);
684 RECORD(STMT_CONTINUE
);
690 RECORD(EXPR_PREDEFINED
);
691 RECORD(EXPR_DECL_REF
);
692 RECORD(EXPR_INTEGER_LITERAL
);
693 RECORD(EXPR_FIXEDPOINT_LITERAL
);
694 RECORD(EXPR_FLOATING_LITERAL
);
695 RECORD(EXPR_IMAGINARY_LITERAL
);
696 RECORD(EXPR_STRING_LITERAL
);
697 RECORD(EXPR_CHARACTER_LITERAL
);
699 RECORD(EXPR_PAREN_LIST
);
700 RECORD(EXPR_UNARY_OPERATOR
);
701 RECORD(EXPR_SIZEOF_ALIGN_OF
);
702 RECORD(EXPR_ARRAY_SUBSCRIPT
);
705 RECORD(EXPR_BINARY_OPERATOR
);
706 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR
);
707 RECORD(EXPR_CONDITIONAL_OPERATOR
);
708 RECORD(EXPR_IMPLICIT_CAST
);
709 RECORD(EXPR_CSTYLE_CAST
);
710 RECORD(EXPR_COMPOUND_LITERAL
);
711 RECORD(EXPR_EXT_VECTOR_ELEMENT
);
712 RECORD(EXPR_INIT_LIST
);
713 RECORD(EXPR_DESIGNATED_INIT
);
714 RECORD(EXPR_DESIGNATED_INIT_UPDATE
);
715 RECORD(EXPR_IMPLICIT_VALUE_INIT
);
716 RECORD(EXPR_NO_INIT
);
718 RECORD(EXPR_ADDR_LABEL
);
721 RECORD(EXPR_GNU_NULL
);
722 RECORD(EXPR_SHUFFLE_VECTOR
);
724 RECORD(EXPR_GENERIC_SELECTION
);
725 RECORD(EXPR_OBJC_STRING_LITERAL
);
726 RECORD(EXPR_OBJC_BOXED_EXPRESSION
);
727 RECORD(EXPR_OBJC_ARRAY_LITERAL
);
728 RECORD(EXPR_OBJC_DICTIONARY_LITERAL
);
729 RECORD(EXPR_OBJC_ENCODE
);
730 RECORD(EXPR_OBJC_SELECTOR_EXPR
);
731 RECORD(EXPR_OBJC_PROTOCOL_EXPR
);
732 RECORD(EXPR_OBJC_IVAR_REF_EXPR
);
733 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR
);
734 RECORD(EXPR_OBJC_KVC_REF_EXPR
);
735 RECORD(EXPR_OBJC_MESSAGE_EXPR
);
736 RECORD(STMT_OBJC_FOR_COLLECTION
);
737 RECORD(STMT_OBJC_CATCH
);
738 RECORD(STMT_OBJC_FINALLY
);
739 RECORD(STMT_OBJC_AT_TRY
);
740 RECORD(STMT_OBJC_AT_SYNCHRONIZED
);
741 RECORD(STMT_OBJC_AT_THROW
);
742 RECORD(EXPR_OBJC_BOOL_LITERAL
);
743 RECORD(STMT_CXX_CATCH
);
744 RECORD(STMT_CXX_TRY
);
745 RECORD(STMT_CXX_FOR_RANGE
);
746 RECORD(EXPR_CXX_OPERATOR_CALL
);
747 RECORD(EXPR_CXX_MEMBER_CALL
);
748 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR
);
749 RECORD(EXPR_CXX_CONSTRUCT
);
750 RECORD(EXPR_CXX_TEMPORARY_OBJECT
);
751 RECORD(EXPR_CXX_STATIC_CAST
);
752 RECORD(EXPR_CXX_DYNAMIC_CAST
);
753 RECORD(EXPR_CXX_REINTERPRET_CAST
);
754 RECORD(EXPR_CXX_CONST_CAST
);
755 RECORD(EXPR_CXX_ADDRSPACE_CAST
);
756 RECORD(EXPR_CXX_FUNCTIONAL_CAST
);
757 RECORD(EXPR_USER_DEFINED_LITERAL
);
758 RECORD(EXPR_CXX_STD_INITIALIZER_LIST
);
759 RECORD(EXPR_CXX_BOOL_LITERAL
);
760 RECORD(EXPR_CXX_PAREN_LIST_INIT
);
761 RECORD(EXPR_CXX_NULL_PTR_LITERAL
);
762 RECORD(EXPR_CXX_TYPEID_EXPR
);
763 RECORD(EXPR_CXX_TYPEID_TYPE
);
764 RECORD(EXPR_CXX_THIS
);
765 RECORD(EXPR_CXX_THROW
);
766 RECORD(EXPR_CXX_DEFAULT_ARG
);
767 RECORD(EXPR_CXX_DEFAULT_INIT
);
768 RECORD(EXPR_CXX_BIND_TEMPORARY
);
769 RECORD(EXPR_CXX_SCALAR_VALUE_INIT
);
770 RECORD(EXPR_CXX_NEW
);
771 RECORD(EXPR_CXX_DELETE
);
772 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR
);
773 RECORD(EXPR_EXPR_WITH_CLEANUPS
);
774 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER
);
775 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF
);
776 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT
);
777 RECORD(EXPR_CXX_UNRESOLVED_MEMBER
);
778 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP
);
779 RECORD(EXPR_CXX_EXPRESSION_TRAIT
);
780 RECORD(EXPR_CXX_NOEXCEPT
);
781 RECORD(EXPR_OPAQUE_VALUE
);
782 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR
);
783 RECORD(EXPR_TYPE_TRAIT
);
784 RECORD(EXPR_ARRAY_TYPE_TRAIT
);
785 RECORD(EXPR_PACK_EXPANSION
);
786 RECORD(EXPR_SIZEOF_PACK
);
787 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM
);
788 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK
);
789 RECORD(EXPR_FUNCTION_PARM_PACK
);
790 RECORD(EXPR_MATERIALIZE_TEMPORARY
);
791 RECORD(EXPR_CUDA_KERNEL_CALL
);
792 RECORD(EXPR_CXX_UUIDOF_EXPR
);
793 RECORD(EXPR_CXX_UUIDOF_TYPE
);
798 void ASTWriter::WriteBlockInfoBlock() {
800 Stream
.EnterBlockInfoBlock();
802 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
803 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
806 BLOCK(CONTROL_BLOCK
);
809 RECORD(MODULE_DIRECTORY
);
810 RECORD(MODULE_MAP_FILE
);
812 RECORD(ORIGINAL_FILE
);
813 RECORD(ORIGINAL_FILE_ID
);
814 RECORD(INPUT_FILE_OFFSETS
);
816 BLOCK(OPTIONS_BLOCK
);
817 RECORD(LANGUAGE_OPTIONS
);
818 RECORD(TARGET_OPTIONS
);
819 RECORD(FILE_SYSTEM_OPTIONS
);
820 RECORD(HEADER_SEARCH_OPTIONS
);
821 RECORD(PREPROCESSOR_OPTIONS
);
823 BLOCK(INPUT_FILES_BLOCK
);
825 RECORD(INPUT_FILE_HASH
);
827 // AST Top-Level Block.
831 RECORD(IDENTIFIER_OFFSET
);
832 RECORD(IDENTIFIER_TABLE
);
833 RECORD(EAGERLY_DESERIALIZED_DECLS
);
834 RECORD(MODULAR_CODEGEN_DECLS
);
835 RECORD(SPECIAL_TYPES
);
837 RECORD(TENTATIVE_DEFINITIONS
);
838 RECORD(SELECTOR_OFFSETS
);
840 RECORD(PP_COUNTER_VALUE
);
841 RECORD(SOURCE_LOCATION_OFFSETS
);
842 RECORD(EXT_VECTOR_DECLS
);
843 RECORD(UNUSED_FILESCOPED_DECLS
);
844 RECORD(PPD_ENTITIES_OFFSETS
);
846 RECORD(PPD_SKIPPED_RANGES
);
847 RECORD(REFERENCED_SELECTOR_POOL
);
848 RECORD(TU_UPDATE_LEXICAL
);
849 RECORD(SEMA_DECL_REFS
);
850 RECORD(WEAK_UNDECLARED_IDENTIFIERS
);
851 RECORD(PENDING_IMPLICIT_INSTANTIATIONS
);
852 RECORD(UPDATE_VISIBLE
);
853 RECORD(DECL_UPDATE_OFFSETS
);
854 RECORD(DECL_UPDATES
);
855 RECORD(CUDA_SPECIAL_DECL_REFS
);
856 RECORD(HEADER_SEARCH_TABLE
);
857 RECORD(FP_PRAGMA_OPTIONS
);
858 RECORD(OPENCL_EXTENSIONS
);
859 RECORD(OPENCL_EXTENSION_TYPES
);
860 RECORD(OPENCL_EXTENSION_DECLS
);
861 RECORD(DELEGATING_CTORS
);
862 RECORD(KNOWN_NAMESPACES
);
863 RECORD(MODULE_OFFSET_MAP
);
864 RECORD(SOURCE_MANAGER_LINE_TABLE
);
865 RECORD(OBJC_CATEGORIES_MAP
);
866 RECORD(FILE_SORTED_DECLS
);
867 RECORD(IMPORTED_MODULES
);
868 RECORD(OBJC_CATEGORIES
);
869 RECORD(MACRO_OFFSET
);
870 RECORD(INTERESTING_IDENTIFIERS
);
871 RECORD(UNDEFINED_BUT_USED
);
872 RECORD(LATE_PARSED_TEMPLATE
);
873 RECORD(OPTIMIZE_PRAGMA_OPTIONS
);
874 RECORD(MSSTRUCT_PRAGMA_OPTIONS
);
875 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS
);
876 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES
);
877 RECORD(DELETE_EXPRS_TO_ANALYZE
);
878 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH
);
879 RECORD(PP_CONDITIONAL_STACK
);
880 RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS
);
881 RECORD(PP_ASSUME_NONNULL_LOC
);
883 // SourceManager Block.
884 BLOCK(SOURCE_MANAGER_BLOCK
);
885 RECORD(SM_SLOC_FILE_ENTRY
);
886 RECORD(SM_SLOC_BUFFER_ENTRY
);
887 RECORD(SM_SLOC_BUFFER_BLOB
);
888 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED
);
889 RECORD(SM_SLOC_EXPANSION_ENTRY
);
891 // Preprocessor Block.
892 BLOCK(PREPROCESSOR_BLOCK
);
893 RECORD(PP_MACRO_DIRECTIVE_HISTORY
);
894 RECORD(PP_MACRO_FUNCTION_LIKE
);
895 RECORD(PP_MACRO_OBJECT_LIKE
);
896 RECORD(PP_MODULE_MACRO
);
900 BLOCK(SUBMODULE_BLOCK
);
901 RECORD(SUBMODULE_METADATA
);
902 RECORD(SUBMODULE_DEFINITION
);
903 RECORD(SUBMODULE_UMBRELLA_HEADER
);
904 RECORD(SUBMODULE_HEADER
);
905 RECORD(SUBMODULE_TOPHEADER
);
906 RECORD(SUBMODULE_UMBRELLA_DIR
);
907 RECORD(SUBMODULE_IMPORTS
);
908 RECORD(SUBMODULE_AFFECTING_MODULES
);
909 RECORD(SUBMODULE_EXPORTS
);
910 RECORD(SUBMODULE_REQUIRES
);
911 RECORD(SUBMODULE_EXCLUDED_HEADER
);
912 RECORD(SUBMODULE_LINK_LIBRARY
);
913 RECORD(SUBMODULE_CONFIG_MACRO
);
914 RECORD(SUBMODULE_CONFLICT
);
915 RECORD(SUBMODULE_PRIVATE_HEADER
);
916 RECORD(SUBMODULE_TEXTUAL_HEADER
);
917 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER
);
918 RECORD(SUBMODULE_INITIALIZERS
);
919 RECORD(SUBMODULE_EXPORT_AS
);
922 BLOCK(COMMENTS_BLOCK
);
923 RECORD(COMMENTS_RAW_COMMENT
);
925 // Decls and Types block.
926 BLOCK(DECLTYPES_BLOCK
);
927 RECORD(TYPE_EXT_QUAL
);
928 RECORD(TYPE_COMPLEX
);
929 RECORD(TYPE_POINTER
);
930 RECORD(TYPE_BLOCK_POINTER
);
931 RECORD(TYPE_LVALUE_REFERENCE
);
932 RECORD(TYPE_RVALUE_REFERENCE
);
933 RECORD(TYPE_MEMBER_POINTER
);
934 RECORD(TYPE_CONSTANT_ARRAY
);
935 RECORD(TYPE_INCOMPLETE_ARRAY
);
936 RECORD(TYPE_VARIABLE_ARRAY
);
938 RECORD(TYPE_EXT_VECTOR
);
939 RECORD(TYPE_FUNCTION_NO_PROTO
);
940 RECORD(TYPE_FUNCTION_PROTO
);
941 RECORD(TYPE_TYPEDEF
);
942 RECORD(TYPE_TYPEOF_EXPR
);
946 RECORD(TYPE_OBJC_INTERFACE
);
947 RECORD(TYPE_OBJC_OBJECT_POINTER
);
948 RECORD(TYPE_DECLTYPE
);
949 RECORD(TYPE_ELABORATED
);
950 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM
);
951 RECORD(TYPE_UNRESOLVED_USING
);
952 RECORD(TYPE_INJECTED_CLASS_NAME
);
953 RECORD(TYPE_OBJC_OBJECT
);
954 RECORD(TYPE_TEMPLATE_TYPE_PARM
);
955 RECORD(TYPE_TEMPLATE_SPECIALIZATION
);
956 RECORD(TYPE_DEPENDENT_NAME
);
957 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION
);
958 RECORD(TYPE_DEPENDENT_SIZED_ARRAY
);
960 RECORD(TYPE_MACRO_QUALIFIED
);
961 RECORD(TYPE_PACK_EXPANSION
);
962 RECORD(TYPE_ATTRIBUTED
);
963 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK
);
965 RECORD(TYPE_UNARY_TRANSFORM
);
967 RECORD(TYPE_DECAYED
);
968 RECORD(TYPE_ADJUSTED
);
969 RECORD(TYPE_OBJC_TYPE_PARAM
);
970 RECORD(LOCAL_REDECLARATIONS
);
971 RECORD(DECL_TYPEDEF
);
972 RECORD(DECL_TYPEALIAS
);
975 RECORD(DECL_ENUM_CONSTANT
);
976 RECORD(DECL_FUNCTION
);
977 RECORD(DECL_OBJC_METHOD
);
978 RECORD(DECL_OBJC_INTERFACE
);
979 RECORD(DECL_OBJC_PROTOCOL
);
980 RECORD(DECL_OBJC_IVAR
);
981 RECORD(DECL_OBJC_AT_DEFS_FIELD
);
982 RECORD(DECL_OBJC_CATEGORY
);
983 RECORD(DECL_OBJC_CATEGORY_IMPL
);
984 RECORD(DECL_OBJC_IMPLEMENTATION
);
985 RECORD(DECL_OBJC_COMPATIBLE_ALIAS
);
986 RECORD(DECL_OBJC_PROPERTY
);
987 RECORD(DECL_OBJC_PROPERTY_IMPL
);
989 RECORD(DECL_MS_PROPERTY
);
991 RECORD(DECL_IMPLICIT_PARAM
);
992 RECORD(DECL_PARM_VAR
);
993 RECORD(DECL_FILE_SCOPE_ASM
);
995 RECORD(DECL_CONTEXT_LEXICAL
);
996 RECORD(DECL_CONTEXT_VISIBLE
);
997 RECORD(DECL_NAMESPACE
);
998 RECORD(DECL_NAMESPACE_ALIAS
);
1000 RECORD(DECL_USING_SHADOW
);
1001 RECORD(DECL_USING_DIRECTIVE
);
1002 RECORD(DECL_UNRESOLVED_USING_VALUE
);
1003 RECORD(DECL_UNRESOLVED_USING_TYPENAME
);
1004 RECORD(DECL_LINKAGE_SPEC
);
1005 RECORD(DECL_CXX_RECORD
);
1006 RECORD(DECL_CXX_METHOD
);
1007 RECORD(DECL_CXX_CONSTRUCTOR
);
1008 RECORD(DECL_CXX_DESTRUCTOR
);
1009 RECORD(DECL_CXX_CONVERSION
);
1010 RECORD(DECL_ACCESS_SPEC
);
1011 RECORD(DECL_FRIEND
);
1012 RECORD(DECL_FRIEND_TEMPLATE
);
1013 RECORD(DECL_CLASS_TEMPLATE
);
1014 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION
);
1015 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
);
1016 RECORD(DECL_VAR_TEMPLATE
);
1017 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION
);
1018 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION
);
1019 RECORD(DECL_FUNCTION_TEMPLATE
);
1020 RECORD(DECL_TEMPLATE_TYPE_PARM
);
1021 RECORD(DECL_NON_TYPE_TEMPLATE_PARM
);
1022 RECORD(DECL_TEMPLATE_TEMPLATE_PARM
);
1023 RECORD(DECL_CONCEPT
);
1024 RECORD(DECL_REQUIRES_EXPR_BODY
);
1025 RECORD(DECL_TYPE_ALIAS_TEMPLATE
);
1026 RECORD(DECL_STATIC_ASSERT
);
1027 RECORD(DECL_CXX_BASE_SPECIFIERS
);
1028 RECORD(DECL_CXX_CTOR_INITIALIZERS
);
1029 RECORD(DECL_INDIRECTFIELD
);
1030 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK
);
1031 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK
);
1032 RECORD(DECL_IMPORT
);
1033 RECORD(DECL_OMP_THREADPRIVATE
);
1035 RECORD(DECL_OBJC_TYPE_PARAM
);
1036 RECORD(DECL_OMP_CAPTUREDEXPR
);
1037 RECORD(DECL_PRAGMA_COMMENT
);
1038 RECORD(DECL_PRAGMA_DETECT_MISMATCH
);
1039 RECORD(DECL_OMP_DECLARE_REDUCTION
);
1040 RECORD(DECL_OMP_ALLOCATE
);
1041 RECORD(DECL_HLSL_BUFFER
);
1043 // Statements and Exprs can occur in the Decls and Types block.
1044 AddStmtsExprs(Stream
, Record
);
1046 BLOCK(PREPROCESSOR_DETAIL_BLOCK
);
1047 RECORD(PPD_MACRO_EXPANSION
);
1048 RECORD(PPD_MACRO_DEFINITION
);
1049 RECORD(PPD_INCLUSION_DIRECTIVE
);
1051 // Decls and Types block.
1052 BLOCK(EXTENSION_BLOCK
);
1053 RECORD(EXTENSION_METADATA
);
1055 BLOCK(UNHASHED_CONTROL_BLOCK
);
1057 RECORD(AST_BLOCK_HASH
);
1058 RECORD(DIAGNOSTIC_OPTIONS
);
1059 RECORD(HEADER_SEARCH_PATHS
);
1060 RECORD(DIAG_PRAGMA_MAPPINGS
);
1067 /// Prepares a path for being written to an AST file by converting it
1068 /// to an absolute path and removing nested './'s.
1070 /// \return \c true if the path was changed.
1071 static bool cleanPathForOutput(FileManager
&FileMgr
,
1072 SmallVectorImpl
<char> &Path
) {
1073 bool Changed
= FileMgr
.makeAbsolutePath(Path
);
1074 return Changed
| llvm::sys::path::remove_dots(Path
);
1077 /// Adjusts the given filename to only write out the portion of the
1078 /// filename that is not part of the system root directory.
1080 /// \param Filename the file name to adjust.
1082 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1083 /// the returned filename will be adjusted by this root directory.
1085 /// \returns either the original filename (if it needs no adjustment) or the
1086 /// adjusted filename (which points into the @p Filename parameter).
1088 adjustFilenameForRelocatableAST(const char *Filename
, StringRef BaseDir
) {
1089 assert(Filename
&& "No file name to adjust?");
1091 if (BaseDir
.empty())
1094 // Verify that the filename and the system root have the same prefix.
1096 for (; Filename
[Pos
] && Pos
< BaseDir
.size(); ++Pos
)
1097 if (Filename
[Pos
] != BaseDir
[Pos
])
1098 return Filename
; // Prefixes don't match.
1100 // We hit the end of the filename before we hit the end of the system root.
1104 // If there's not a path separator at the end of the base directory nor
1105 // immediately after it, then this isn't within the base directory.
1106 if (!llvm::sys::path::is_separator(Filename
[Pos
])) {
1107 if (!llvm::sys::path::is_separator(BaseDir
.back()))
1110 // If the file name has a '/' at the current position, skip over the '/'.
1111 // We distinguish relative paths from absolute paths by the
1112 // absence of '/' at the beginning of relative paths.
1114 // FIXME: This is wrong. We distinguish them by asking if the path is
1115 // absolute, which isn't the same thing. And there might be multiple '/'s
1116 // in a row. Use a better mechanism to indicate whether we have emitted an
1117 // absolute or relative path.
1121 return Filename
+ Pos
;
1124 std::pair
<ASTFileSignature
, ASTFileSignature
>
1125 ASTWriter::createSignature() const {
1126 StringRef
AllBytes(Buffer
.data(), Buffer
.size());
1129 Hasher
.update(AllBytes
.slice(ASTBlockRange
.first
, ASTBlockRange
.second
));
1130 ASTFileSignature ASTBlockHash
= ASTFileSignature::create(Hasher
.result());
1132 // Add the remaining bytes:
1133 // 1. Before the unhashed control block.
1134 Hasher
.update(AllBytes
.slice(0, UnhashedControlBlockRange
.first
));
1135 // 2. Between the unhashed control block and the AST block.
1137 AllBytes
.slice(UnhashedControlBlockRange
.second
, ASTBlockRange
.first
));
1138 // 3. After the AST block.
1139 Hasher
.update(AllBytes
.slice(ASTBlockRange
.second
, StringRef::npos
));
1140 ASTFileSignature Signature
= ASTFileSignature::create(Hasher
.result());
1142 return std::make_pair(ASTBlockHash
, Signature
);
1145 ASTFileSignature
ASTWriter::backpatchSignature() {
1146 if (!WritingModule
||
1147 !PP
->getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent
)
1150 // For implicit modules, write the hash of the PCM as its signature.
1152 auto BackpatchSignatureAt
= [&](const ASTFileSignature
&S
, uint64_t BitNo
) {
1153 for (uint8_t Byte
: S
) {
1154 Stream
.BackpatchByte(BitNo
, Byte
);
1159 ASTFileSignature ASTBlockHash
;
1160 ASTFileSignature Signature
;
1161 std::tie(ASTBlockHash
, Signature
) = createSignature();
1163 BackpatchSignatureAt(ASTBlockHash
, ASTBlockHashOffset
);
1164 BackpatchSignatureAt(Signature
, SignatureOffset
);
1169 void ASTWriter::writeUnhashedControlBlock(Preprocessor
&PP
,
1170 ASTContext
&Context
) {
1171 using namespace llvm
;
1173 // Flush first to prepare the PCM hash (signature).
1174 Stream
.FlushToWord();
1175 UnhashedControlBlockRange
.first
= Stream
.GetCurrentBitNo() >> 3;
1177 // Enter the block and prepare to write records.
1179 Stream
.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID
, 5);
1181 // For implicit modules, write the hash of the PCM as its signature.
1182 if (WritingModule
&&
1183 PP
.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent
) {
1184 // At this point, we don't know the actual signature of the file or the AST
1185 // block - we're only able to compute those at the end of the serialization
1186 // process. Let's store dummy signatures for now, and replace them with the
1187 // real ones later on.
1188 // The bitstream VBR-encodes record elements, which makes backpatching them
1189 // really difficult. Let's store the signatures as blobs instead - they are
1190 // guaranteed to be word-aligned, and we control their format/encoding.
1191 auto Dummy
= ASTFileSignature::createDummy();
1192 SmallString
<128> Blob
{Dummy
.begin(), Dummy
.end()};
1194 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1195 Abbrev
->Add(BitCodeAbbrevOp(AST_BLOCK_HASH
));
1196 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
1197 unsigned ASTBlockHashAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
1199 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1200 Abbrev
->Add(BitCodeAbbrevOp(SIGNATURE
));
1201 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
1202 unsigned SignatureAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
1204 Record
.push_back(AST_BLOCK_HASH
);
1205 Stream
.EmitRecordWithBlob(ASTBlockHashAbbrev
, Record
, Blob
);
1206 ASTBlockHashOffset
= Stream
.GetCurrentBitNo() - Blob
.size() * 8;
1209 Record
.push_back(SIGNATURE
);
1210 Stream
.EmitRecordWithBlob(SignatureAbbrev
, Record
, Blob
);
1211 SignatureOffset
= Stream
.GetCurrentBitNo() - Blob
.size() * 8;
1215 const auto &HSOpts
= PP
.getHeaderSearchInfo().getHeaderSearchOpts();
1217 // Diagnostic options.
1218 const auto &Diags
= Context
.getDiagnostics();
1219 const DiagnosticOptions
&DiagOpts
= Diags
.getDiagnosticOptions();
1220 if (!HSOpts
.ModulesSkipDiagnosticOptions
) {
1221 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1222 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1223 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1224 #include "clang/Basic/DiagnosticOptions.def"
1225 Record
.push_back(DiagOpts
.Warnings
.size());
1226 for (unsigned I
= 0, N
= DiagOpts
.Warnings
.size(); I
!= N
; ++I
)
1227 AddString(DiagOpts
.Warnings
[I
], Record
);
1228 Record
.push_back(DiagOpts
.Remarks
.size());
1229 for (unsigned I
= 0, N
= DiagOpts
.Remarks
.size(); I
!= N
; ++I
)
1230 AddString(DiagOpts
.Remarks
[I
], Record
);
1231 // Note: we don't serialize the log or serialization file names, because
1232 // they are generally transient files and will almost always be overridden.
1233 Stream
.EmitRecord(DIAGNOSTIC_OPTIONS
, Record
);
1237 // Header search paths.
1238 if (!HSOpts
.ModulesSkipHeaderSearchPaths
) {
1240 Record
.push_back(HSOpts
.UserEntries
.size());
1241 for (unsigned I
= 0, N
= HSOpts
.UserEntries
.size(); I
!= N
; ++I
) {
1242 const HeaderSearchOptions::Entry
&Entry
= HSOpts
.UserEntries
[I
];
1243 AddString(Entry
.Path
, Record
);
1244 Record
.push_back(static_cast<unsigned>(Entry
.Group
));
1245 Record
.push_back(Entry
.IsFramework
);
1246 Record
.push_back(Entry
.IgnoreSysRoot
);
1249 // System header prefixes.
1250 Record
.push_back(HSOpts
.SystemHeaderPrefixes
.size());
1251 for (unsigned I
= 0, N
= HSOpts
.SystemHeaderPrefixes
.size(); I
!= N
; ++I
) {
1252 AddString(HSOpts
.SystemHeaderPrefixes
[I
].Prefix
, Record
);
1253 Record
.push_back(HSOpts
.SystemHeaderPrefixes
[I
].IsSystemHeader
);
1256 // VFS overlay files.
1257 Record
.push_back(HSOpts
.VFSOverlayFiles
.size());
1258 for (StringRef VFSOverlayFile
: HSOpts
.VFSOverlayFiles
)
1259 AddString(VFSOverlayFile
, Record
);
1261 Stream
.EmitRecord(HEADER_SEARCH_PATHS
, Record
);
1264 if (!HSOpts
.ModulesSkipPragmaDiagnosticMappings
)
1265 WritePragmaDiagnosticMappings(Diags
, /* isModule = */ WritingModule
);
1267 // Header search entry usage.
1268 auto HSEntryUsage
= PP
.getHeaderSearchInfo().computeUserEntryUsage();
1269 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1270 Abbrev
->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE
));
1271 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // Number of bits.
1272 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Bit vector.
1273 unsigned HSUsageAbbrevCode
= Stream
.EmitAbbrev(std::move(Abbrev
));
1275 RecordData::value_type Record
[] = {HEADER_SEARCH_ENTRY_USAGE
,
1276 HSEntryUsage
.size()};
1277 Stream
.EmitRecordWithBlob(HSUsageAbbrevCode
, Record
, bytes(HSEntryUsage
));
1280 // Leave the options block.
1282 UnhashedControlBlockRange
.second
= Stream
.GetCurrentBitNo() >> 3;
1285 /// Write the control block.
1286 void ASTWriter::WriteControlBlock(Preprocessor
&PP
, ASTContext
&Context
,
1287 StringRef isysroot
) {
1288 using namespace llvm
;
1290 Stream
.EnterSubblock(CONTROL_BLOCK_ID
, 5);
1294 auto MetadataAbbrev
= std::make_shared
<BitCodeAbbrev
>();
1295 MetadataAbbrev
->Add(BitCodeAbbrevOp(METADATA
));
1296 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 16)); // Major
1297 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 16)); // Minor
1298 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 16)); // Clang maj.
1299 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 16)); // Clang min.
1300 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Relocatable
1301 // Standard C++ module
1302 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1));
1303 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Timestamps
1304 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Errors
1305 MetadataAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // SVN branch/tag
1306 unsigned MetadataAbbrevCode
= Stream
.EmitAbbrev(std::move(MetadataAbbrev
));
1307 assert((!WritingModule
|| isysroot
.empty()) &&
1308 "writing module as a relocatable PCH?");
1310 RecordData::value_type Record
[] = {METADATA
,
1313 CLANG_VERSION_MAJOR
,
1314 CLANG_VERSION_MINOR
,
1316 isWritingStdCXXNamedModules(),
1318 ASTHasCompilerErrors
};
1319 Stream
.EmitRecordWithBlob(MetadataAbbrevCode
, Record
,
1320 getClangFullRepositoryVersion());
1323 if (WritingModule
) {
1325 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1326 Abbrev
->Add(BitCodeAbbrevOp(MODULE_NAME
));
1327 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
1328 unsigned AbbrevCode
= Stream
.EmitAbbrev(std::move(Abbrev
));
1329 RecordData::value_type Record
[] = {MODULE_NAME
};
1330 Stream
.EmitRecordWithBlob(AbbrevCode
, Record
, WritingModule
->Name
);
1333 if (WritingModule
&& WritingModule
->Directory
) {
1334 SmallString
<128> BaseDir
;
1335 if (PP
.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd
) {
1336 // Use the current working directory as the base path for all inputs.
1338 Context
.getSourceManager().getFileManager().getOptionalDirectoryRef(
1340 BaseDir
.assign(CWD
->getName());
1342 BaseDir
.assign(WritingModule
->Directory
->getName());
1344 cleanPathForOutput(Context
.getSourceManager().getFileManager(), BaseDir
);
1346 // If the home of the module is the current working directory, then we
1347 // want to pick up the cwd of the build process loading the module, not
1348 // our cwd, when we load this module.
1349 if (!PP
.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd
&&
1350 (!PP
.getHeaderSearchInfo()
1351 .getHeaderSearchOpts()
1352 .ModuleMapFileHomeIsCwd
||
1353 WritingModule
->Directory
->getName() != StringRef("."))) {
1354 // Module directory.
1355 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1356 Abbrev
->Add(BitCodeAbbrevOp(MODULE_DIRECTORY
));
1357 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Directory
1358 unsigned AbbrevCode
= Stream
.EmitAbbrev(std::move(Abbrev
));
1360 RecordData::value_type Record
[] = {MODULE_DIRECTORY
};
1361 Stream
.EmitRecordWithBlob(AbbrevCode
, Record
, BaseDir
);
1364 // Write out all other paths relative to the base directory if possible.
1365 BaseDirectory
.assign(BaseDir
.begin(), BaseDir
.end());
1366 } else if (!isysroot
.empty()) {
1367 // Write out paths relative to the sysroot if possible.
1368 BaseDirectory
= std::string(isysroot
);
1372 if (WritingModule
&& WritingModule
->Kind
== Module::ModuleMapModule
) {
1375 auto &Map
= PP
.getHeaderSearchInfo().getModuleMap();
1376 AddPath(WritingModule
->PresumedModuleMapFile
.empty()
1377 ? Map
.getModuleMapFileForUniquing(WritingModule
)
1378 ->getNameAsRequested()
1379 : StringRef(WritingModule
->PresumedModuleMapFile
),
1382 // Additional module map files.
1383 if (auto *AdditionalModMaps
=
1384 Map
.getAdditionalModuleMapFiles(WritingModule
)) {
1385 Record
.push_back(AdditionalModMaps
->size());
1386 SmallVector
<FileEntryRef
, 1> ModMaps(AdditionalModMaps
->begin(),
1387 AdditionalModMaps
->end());
1388 llvm::sort(ModMaps
, [](FileEntryRef A
, FileEntryRef B
) {
1389 return A
.getName() < B
.getName();
1391 for (FileEntryRef F
: ModMaps
)
1392 AddPath(F
.getName(), Record
);
1394 Record
.push_back(0);
1397 Stream
.EmitRecord(MODULE_MAP_FILE
, Record
);
1402 serialization::ModuleManager
&Mgr
= Chain
->getModuleManager();
1405 for (ModuleFile
&M
: Mgr
) {
1406 // Skip modules that weren't directly imported.
1407 if (!M
.isDirectlyImported())
1410 Record
.push_back((unsigned)M
.Kind
); // FIXME: Stable encoding
1411 Record
.push_back(M
.StandardCXXModule
);
1412 AddSourceLocation(M
.ImportLoc
, Record
);
1414 // If we have calculated signature, there is no need to store
1415 // the size or timestamp.
1416 Record
.push_back(M
.Signature
? 0 : M
.File
.getSize());
1417 Record
.push_back(M
.Signature
? 0 : getTimestampForOutput(M
.File
));
1419 llvm::append_range(Record
, M
.Signature
);
1421 AddString(M
.ModuleName
, Record
);
1422 AddPath(M
.FileName
, Record
);
1424 Stream
.EmitRecord(IMPORTS
, Record
);
1427 // Write the options block.
1428 Stream
.EnterSubblock(OPTIONS_BLOCK_ID
, 4);
1430 // Language options.
1432 const LangOptions
&LangOpts
= Context
.getLangOpts();
1433 #define LANGOPT(Name, Bits, Default, Description) \
1434 Record.push_back(LangOpts.Name);
1435 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1436 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1437 #include "clang/Basic/LangOptions.def"
1438 #define SANITIZER(NAME, ID) \
1439 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1440 #include "clang/Basic/Sanitizers.def"
1442 Record
.push_back(LangOpts
.ModuleFeatures
.size());
1443 for (StringRef Feature
: LangOpts
.ModuleFeatures
)
1444 AddString(Feature
, Record
);
1446 Record
.push_back((unsigned) LangOpts
.ObjCRuntime
.getKind());
1447 AddVersionTuple(LangOpts
.ObjCRuntime
.getVersion(), Record
);
1449 AddString(LangOpts
.CurrentModule
, Record
);
1452 Record
.push_back(LangOpts
.CommentOpts
.BlockCommandNames
.size());
1453 for (const auto &I
: LangOpts
.CommentOpts
.BlockCommandNames
) {
1454 AddString(I
, Record
);
1456 Record
.push_back(LangOpts
.CommentOpts
.ParseAllComments
);
1458 // OpenMP offloading options.
1459 Record
.push_back(LangOpts
.OMPTargetTriples
.size());
1460 for (auto &T
: LangOpts
.OMPTargetTriples
)
1461 AddString(T
.getTriple(), Record
);
1463 AddString(LangOpts
.OMPHostIRFile
, Record
);
1465 Stream
.EmitRecord(LANGUAGE_OPTIONS
, Record
);
1469 const TargetInfo
&Target
= Context
.getTargetInfo();
1470 const TargetOptions
&TargetOpts
= Target
.getTargetOpts();
1471 AddString(TargetOpts
.Triple
, Record
);
1472 AddString(TargetOpts
.CPU
, Record
);
1473 AddString(TargetOpts
.TuneCPU
, Record
);
1474 AddString(TargetOpts
.ABI
, Record
);
1475 Record
.push_back(TargetOpts
.FeaturesAsWritten
.size());
1476 for (unsigned I
= 0, N
= TargetOpts
.FeaturesAsWritten
.size(); I
!= N
; ++I
) {
1477 AddString(TargetOpts
.FeaturesAsWritten
[I
], Record
);
1479 Record
.push_back(TargetOpts
.Features
.size());
1480 for (unsigned I
= 0, N
= TargetOpts
.Features
.size(); I
!= N
; ++I
) {
1481 AddString(TargetOpts
.Features
[I
], Record
);
1483 Stream
.EmitRecord(TARGET_OPTIONS
, Record
);
1485 // File system options.
1487 const FileSystemOptions
&FSOpts
=
1488 Context
.getSourceManager().getFileManager().getFileSystemOpts();
1489 AddString(FSOpts
.WorkingDir
, Record
);
1490 Stream
.EmitRecord(FILE_SYSTEM_OPTIONS
, Record
);
1492 // Header search options.
1494 const HeaderSearchOptions
&HSOpts
=
1495 PP
.getHeaderSearchInfo().getHeaderSearchOpts();
1497 AddString(HSOpts
.Sysroot
, Record
);
1498 AddString(HSOpts
.ResourceDir
, Record
);
1499 AddString(HSOpts
.ModuleCachePath
, Record
);
1500 AddString(HSOpts
.ModuleUserBuildPath
, Record
);
1501 Record
.push_back(HSOpts
.DisableModuleHash
);
1502 Record
.push_back(HSOpts
.ImplicitModuleMaps
);
1503 Record
.push_back(HSOpts
.ModuleMapFileHomeIsCwd
);
1504 Record
.push_back(HSOpts
.EnablePrebuiltImplicitModules
);
1505 Record
.push_back(HSOpts
.UseBuiltinIncludes
);
1506 Record
.push_back(HSOpts
.UseStandardSystemIncludes
);
1507 Record
.push_back(HSOpts
.UseStandardCXXIncludes
);
1508 Record
.push_back(HSOpts
.UseLibcxx
);
1509 // Write out the specific module cache path that contains the module files.
1510 AddString(PP
.getHeaderSearchInfo().getModuleCachePath(), Record
);
1511 Stream
.EmitRecord(HEADER_SEARCH_OPTIONS
, Record
);
1513 // Preprocessor options.
1515 const PreprocessorOptions
&PPOpts
= PP
.getPreprocessorOpts();
1517 // If we're building an implicit module with a context hash, the importer is
1518 // guaranteed to have the same macros defined on the command line. Skip
1520 bool SkipMacros
= BuildingImplicitModule
&& !HSOpts
.DisableModuleHash
;
1521 bool WriteMacros
= !SkipMacros
;
1522 Record
.push_back(WriteMacros
);
1524 // Macro definitions.
1525 Record
.push_back(PPOpts
.Macros
.size());
1526 for (unsigned I
= 0, N
= PPOpts
.Macros
.size(); I
!= N
; ++I
) {
1527 AddString(PPOpts
.Macros
[I
].first
, Record
);
1528 Record
.push_back(PPOpts
.Macros
[I
].second
);
1533 Record
.push_back(PPOpts
.Includes
.size());
1534 for (unsigned I
= 0, N
= PPOpts
.Includes
.size(); I
!= N
; ++I
)
1535 AddString(PPOpts
.Includes
[I
], Record
);
1538 Record
.push_back(PPOpts
.MacroIncludes
.size());
1539 for (unsigned I
= 0, N
= PPOpts
.MacroIncludes
.size(); I
!= N
; ++I
)
1540 AddString(PPOpts
.MacroIncludes
[I
], Record
);
1542 Record
.push_back(PPOpts
.UsePredefines
);
1543 // Detailed record is important since it is used for the module cache hash.
1544 Record
.push_back(PPOpts
.DetailedRecord
);
1545 AddString(PPOpts
.ImplicitPCHInclude
, Record
);
1546 Record
.push_back(static_cast<unsigned>(PPOpts
.ObjCXXARCStandardLibrary
));
1547 Stream
.EmitRecord(PREPROCESSOR_OPTIONS
, Record
);
1549 // Leave the options block.
1552 // Original file name and file ID
1553 SourceManager
&SM
= Context
.getSourceManager();
1554 if (auto MainFile
= SM
.getFileEntryRefForID(SM
.getMainFileID())) {
1555 auto FileAbbrev
= std::make_shared
<BitCodeAbbrev
>();
1556 FileAbbrev
->Add(BitCodeAbbrevOp(ORIGINAL_FILE
));
1557 FileAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // File ID
1558 FileAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // File name
1559 unsigned FileAbbrevCode
= Stream
.EmitAbbrev(std::move(FileAbbrev
));
1562 Record
.push_back(ORIGINAL_FILE
);
1563 AddFileID(SM
.getMainFileID(), Record
);
1564 EmitRecordWithPath(FileAbbrevCode
, Record
, MainFile
->getName());
1568 AddFileID(SM
.getMainFileID(), Record
);
1569 Stream
.EmitRecord(ORIGINAL_FILE_ID
, Record
);
1571 WriteInputFiles(Context
.SourceMgr
,
1572 PP
.getHeaderSearchInfo().getHeaderSearchOpts());
1579 struct InputFileEntry
{
1583 bool BufferOverridden
;
1586 uint32_t ContentHash
[2];
1588 InputFileEntry(FileEntryRef File
) : File(File
) {}
1593 void ASTWriter::WriteInputFiles(SourceManager
&SourceMgr
,
1594 HeaderSearchOptions
&HSOpts
) {
1595 using namespace llvm
;
1597 Stream
.EnterSubblock(INPUT_FILES_BLOCK_ID
, 4);
1599 // Create input-file abbreviation.
1600 auto IFAbbrev
= std::make_shared
<BitCodeAbbrev
>();
1601 IFAbbrev
->Add(BitCodeAbbrevOp(INPUT_FILE
));
1602 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // ID
1603 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 12)); // Size
1604 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 32)); // Modification time
1605 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Overridden
1606 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Transient
1607 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Top-level
1608 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Module map
1609 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 16)); // Name as req. len
1610 IFAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name as req. + name
1611 unsigned IFAbbrevCode
= Stream
.EmitAbbrev(std::move(IFAbbrev
));
1613 // Create input file hash abbreviation.
1614 auto IFHAbbrev
= std::make_shared
<BitCodeAbbrev
>();
1615 IFHAbbrev
->Add(BitCodeAbbrevOp(INPUT_FILE_HASH
));
1616 IFHAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
1617 IFHAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
1618 unsigned IFHAbbrevCode
= Stream
.EmitAbbrev(std::move(IFHAbbrev
));
1620 uint64_t InputFilesOffsetBase
= Stream
.GetCurrentBitNo();
1622 // Get all ContentCache objects for files.
1623 std::vector
<InputFileEntry
> UserFiles
;
1624 std::vector
<InputFileEntry
> SystemFiles
;
1625 for (unsigned I
= 1, N
= SourceMgr
.local_sloc_entry_size(); I
!= N
; ++I
) {
1626 // Get this source location entry.
1627 const SrcMgr::SLocEntry
*SLoc
= &SourceMgr
.getLocalSLocEntry(I
);
1628 assert(&SourceMgr
.getSLocEntry(FileID::get(I
)) == SLoc
);
1630 // We only care about file entries that were not overridden.
1631 if (!SLoc
->isFile())
1633 const SrcMgr::FileInfo
&File
= SLoc
->getFile();
1634 const SrcMgr::ContentCache
*Cache
= &File
.getContentCache();
1635 if (!Cache
->OrigEntry
)
1638 // Do not emit input files that do not affect current module.
1639 if (!IsSLocAffecting
[I
])
1642 InputFileEntry
Entry(*Cache
->OrigEntry
);
1643 Entry
.IsSystemFile
= isSystem(File
.getFileCharacteristic());
1644 Entry
.IsTransient
= Cache
->IsTransient
;
1645 Entry
.BufferOverridden
= Cache
->BufferOverridden
;
1646 Entry
.IsTopLevel
= File
.getIncludeLoc().isInvalid();
1647 Entry
.IsModuleMap
= isModuleMap(File
.getFileCharacteristic());
1649 auto ContentHash
= hash_code(-1);
1650 if (PP
->getHeaderSearchInfo()
1651 .getHeaderSearchOpts()
1652 .ValidateASTInputFilesContent
) {
1653 auto MemBuff
= Cache
->getBufferIfLoaded();
1655 ContentHash
= hash_value(MemBuff
->getBuffer());
1657 PP
->Diag(SourceLocation(), diag::err_module_unable_to_hash_content
)
1658 << Entry
.File
.getName();
1660 auto CH
= llvm::APInt(64, ContentHash
);
1661 Entry
.ContentHash
[0] =
1662 static_cast<uint32_t>(CH
.getLoBits(32).getZExtValue());
1663 Entry
.ContentHash
[1] =
1664 static_cast<uint32_t>(CH
.getHiBits(32).getZExtValue());
1666 if (Entry
.IsSystemFile
)
1667 SystemFiles
.push_back(Entry
);
1669 UserFiles
.push_back(Entry
);
1672 // User files go at the front, system files at the back.
1673 auto SortedFiles
= llvm::concat
<InputFileEntry
>(std::move(UserFiles
),
1674 std::move(SystemFiles
));
1676 unsigned UserFilesNum
= 0;
1677 // Write out all of the input files.
1678 std::vector
<uint64_t> InputFileOffsets
;
1679 for (const auto &Entry
: SortedFiles
) {
1680 uint32_t &InputFileID
= InputFileIDs
[Entry
.File
];
1681 if (InputFileID
!= 0)
1682 continue; // already recorded this file.
1684 // Record this entry's offset.
1685 InputFileOffsets
.push_back(Stream
.GetCurrentBitNo() - InputFilesOffsetBase
);
1687 InputFileID
= InputFileOffsets
.size();
1689 if (!Entry
.IsSystemFile
)
1692 // Emit size/modification time for this file.
1693 // And whether this file was overridden.
1695 SmallString
<128> NameAsRequested
= Entry
.File
.getNameAsRequested();
1696 SmallString
<128> Name
= Entry
.File
.getName();
1698 PreparePathForOutput(NameAsRequested
);
1699 PreparePathForOutput(Name
);
1701 if (Name
== NameAsRequested
)
1704 RecordData::value_type Record
[] = {
1706 InputFileOffsets
.size(),
1707 (uint64_t)Entry
.File
.getSize(),
1708 (uint64_t)getTimestampForOutput(Entry
.File
),
1709 Entry
.BufferOverridden
,
1713 NameAsRequested
.size()};
1715 Stream
.EmitRecordWithBlob(IFAbbrevCode
, Record
,
1716 (NameAsRequested
+ Name
).str());
1719 // Emit content hash for this file.
1721 RecordData::value_type Record
[] = {INPUT_FILE_HASH
, Entry
.ContentHash
[0],
1722 Entry
.ContentHash
[1]};
1723 Stream
.EmitRecordWithAbbrev(IFHAbbrevCode
, Record
);
1729 // Create input file offsets abbreviation.
1730 auto OffsetsAbbrev
= std::make_shared
<BitCodeAbbrev
>();
1731 OffsetsAbbrev
->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS
));
1732 OffsetsAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // # input files
1733 OffsetsAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // # non-system
1735 OffsetsAbbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Array
1736 unsigned OffsetsAbbrevCode
= Stream
.EmitAbbrev(std::move(OffsetsAbbrev
));
1738 // Write input file offsets.
1739 RecordData::value_type Record
[] = {INPUT_FILE_OFFSETS
,
1740 InputFileOffsets
.size(), UserFilesNum
};
1741 Stream
.EmitRecordWithBlob(OffsetsAbbrevCode
, Record
, bytes(InputFileOffsets
));
1744 //===----------------------------------------------------------------------===//
1745 // Source Manager Serialization
1746 //===----------------------------------------------------------------------===//
1748 /// Create an abbreviation for the SLocEntry that refers to a
1750 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter
&Stream
) {
1751 using namespace llvm
;
1753 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1754 Abbrev
->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY
));
1755 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Offset
1756 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Include location
1757 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 3)); // Characteristic
1758 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Line directives
1759 // FileEntry fields.
1760 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Input File ID
1761 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // NumCreatedFIDs
1762 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 24)); // FirstDeclIndex
1763 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // NumDecls
1764 return Stream
.EmitAbbrev(std::move(Abbrev
));
1767 /// Create an abbreviation for the SLocEntry that refers to a
1769 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter
&Stream
) {
1770 using namespace llvm
;
1772 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1773 Abbrev
->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY
));
1774 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Offset
1775 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Include location
1776 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 3)); // Characteristic
1777 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Line directives
1778 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Buffer name blob
1779 return Stream
.EmitAbbrev(std::move(Abbrev
));
1782 /// Create an abbreviation for the SLocEntry that refers to a
1784 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter
&Stream
,
1786 using namespace llvm
;
1788 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1789 Abbrev
->Add(BitCodeAbbrevOp(Compressed
? SM_SLOC_BUFFER_BLOB_COMPRESSED
1790 : SM_SLOC_BUFFER_BLOB
));
1792 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Uncompressed size
1793 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Blob
1794 return Stream
.EmitAbbrev(std::move(Abbrev
));
1797 /// Create an abbreviation for the SLocEntry that refers to a macro
1799 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter
&Stream
) {
1800 using namespace llvm
;
1802 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
1803 Abbrev
->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY
));
1804 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Offset
1805 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Spelling location
1806 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Start location
1807 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // End location
1808 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // Is token range
1809 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Token length
1810 return Stream
.EmitAbbrev(std::move(Abbrev
));
1813 /// Emit key length and data length as ULEB-encoded data, and return them as a
1815 static std::pair
<unsigned, unsigned>
1816 emitULEBKeyDataLength(unsigned KeyLen
, unsigned DataLen
, raw_ostream
&Out
) {
1817 llvm::encodeULEB128(KeyLen
, Out
);
1818 llvm::encodeULEB128(DataLen
, Out
);
1819 return std::make_pair(KeyLen
, DataLen
);
1824 // Trait used for the on-disk hash table of header search information.
1825 class HeaderFileInfoTrait
{
1828 // Keep track of the framework names we've used during serialization.
1829 SmallString
<128> FrameworkStringData
;
1830 llvm::StringMap
<unsigned> FrameworkNameOffset
;
1833 HeaderFileInfoTrait(ASTWriter
&Writer
) : Writer(Writer
) {}
1840 using key_type_ref
= const key_type
&;
1842 using UnresolvedModule
=
1843 llvm::PointerIntPair
<Module
*, 2, ModuleMap::ModuleHeaderRole
>;
1846 const HeaderFileInfo
&HFI
;
1847 bool AlreadyIncluded
;
1848 ArrayRef
<ModuleMap::KnownHeader
> KnownHeaders
;
1849 UnresolvedModule Unresolved
;
1851 using data_type_ref
= const data_type
&;
1853 using hash_value_type
= unsigned;
1854 using offset_type
= unsigned;
1856 hash_value_type
ComputeHash(key_type_ref key
) {
1857 // The hash is based only on size/time of the file, so that the reader can
1858 // match even when symlinking or excess path elements ("foo/../", "../")
1859 // change the form of the name. However, complete path is still the key.
1860 return llvm::hash_combine(key
.Size
, key
.ModTime
);
1863 std::pair
<unsigned, unsigned>
1864 EmitKeyDataLength(raw_ostream
& Out
, key_type_ref key
, data_type_ref Data
) {
1865 unsigned KeyLen
= key
.Filename
.size() + 1 + 8 + 8;
1866 unsigned DataLen
= 1 + 4 + 4;
1867 for (auto ModInfo
: Data
.KnownHeaders
)
1868 if (Writer
.getLocalOrImportedSubmoduleID(ModInfo
.getModule()))
1870 if (Data
.Unresolved
.getPointer())
1872 return emitULEBKeyDataLength(KeyLen
, DataLen
, Out
);
1875 void EmitKey(raw_ostream
& Out
, key_type_ref key
, unsigned KeyLen
) {
1876 using namespace llvm::support
;
1878 endian::Writer
LE(Out
, llvm::endianness::little
);
1879 LE
.write
<uint64_t>(key
.Size
);
1881 LE
.write
<uint64_t>(key
.ModTime
);
1883 Out
.write(key
.Filename
.data(), KeyLen
);
1886 void EmitData(raw_ostream
&Out
, key_type_ref key
,
1887 data_type_ref Data
, unsigned DataLen
) {
1888 using namespace llvm::support
;
1890 endian::Writer
LE(Out
, llvm::endianness::little
);
1891 uint64_t Start
= Out
.tell(); (void)Start
;
1893 unsigned char Flags
= (Data
.AlreadyIncluded
<< 6)
1894 | (Data
.HFI
.isImport
<< 5)
1895 | (Data
.HFI
.isPragmaOnce
<< 4)
1896 | (Data
.HFI
.DirInfo
<< 1)
1897 | Data
.HFI
.IndexHeaderMapHeader
;
1898 LE
.write
<uint8_t>(Flags
);
1900 if (!Data
.HFI
.ControllingMacro
)
1901 LE
.write
<uint32_t>(Data
.HFI
.ControllingMacroID
);
1903 LE
.write
<uint32_t>(Writer
.getIdentifierRef(Data
.HFI
.ControllingMacro
));
1905 unsigned Offset
= 0;
1906 if (!Data
.HFI
.Framework
.empty()) {
1907 // If this header refers into a framework, save the framework name.
1908 llvm::StringMap
<unsigned>::iterator Pos
1909 = FrameworkNameOffset
.find(Data
.HFI
.Framework
);
1910 if (Pos
== FrameworkNameOffset
.end()) {
1911 Offset
= FrameworkStringData
.size() + 1;
1912 FrameworkStringData
.append(Data
.HFI
.Framework
);
1913 FrameworkStringData
.push_back(0);
1915 FrameworkNameOffset
[Data
.HFI
.Framework
] = Offset
;
1917 Offset
= Pos
->second
;
1919 LE
.write
<uint32_t>(Offset
);
1921 auto EmitModule
= [&](Module
*M
, ModuleMap::ModuleHeaderRole Role
) {
1922 if (uint32_t ModID
= Writer
.getLocalOrImportedSubmoduleID(M
)) {
1923 uint32_t Value
= (ModID
<< 3) | (unsigned)Role
;
1924 assert((Value
>> 3) == ModID
&& "overflow in header module info");
1925 LE
.write
<uint32_t>(Value
);
1929 for (auto ModInfo
: Data
.KnownHeaders
)
1930 EmitModule(ModInfo
.getModule(), ModInfo
.getRole());
1931 if (Data
.Unresolved
.getPointer())
1932 EmitModule(Data
.Unresolved
.getPointer(), Data
.Unresolved
.getInt());
1934 assert(Out
.tell() - Start
== DataLen
&& "Wrong data length");
1937 const char *strings_begin() const { return FrameworkStringData
.begin(); }
1938 const char *strings_end() const { return FrameworkStringData
.end(); }
1943 /// Write the header search block for the list of files that
1945 /// \param HS The header search structure to save.
1946 void ASTWriter::WriteHeaderSearch(const HeaderSearch
&HS
) {
1947 HeaderFileInfoTrait
GeneratorTrait(*this);
1948 llvm::OnDiskChainedHashTableGenerator
<HeaderFileInfoTrait
> Generator
;
1949 SmallVector
<const char *, 4> SavedStrings
;
1950 unsigned NumHeaderSearchEntries
= 0;
1952 // Find all unresolved headers for the current module. We generally will
1953 // have resolved them before we get here, but not necessarily: we might be
1954 // compiling a preprocessed module, where there is no requirement for the
1955 // original files to exist any more.
1956 const HeaderFileInfo Empty
; // So we can take a reference.
1957 if (WritingModule
) {
1958 llvm::SmallVector
<Module
*, 16> Worklist(1, WritingModule
);
1959 while (!Worklist
.empty()) {
1960 Module
*M
= Worklist
.pop_back_val();
1961 // We don't care about headers in unimportable submodules.
1962 if (M
->isUnimportable())
1965 // Map to disk files where possible, to pick up any missing stat
1966 // information. This also means we don't need to check the unresolved
1967 // headers list when emitting resolved headers in the first loop below.
1968 // FIXME: It'd be preferable to avoid doing this if we were given
1969 // sufficient stat information in the module map.
1970 HS
.getModuleMap().resolveHeaderDirectives(M
, /*File=*/std::nullopt
);
1972 // If the file didn't exist, we can still create a module if we were given
1973 // enough information in the module map.
1974 for (const auto &U
: M
->MissingHeaders
) {
1975 // Check that we were given enough information to build a module
1976 // without this file existing on disk.
1977 if (!U
.Size
|| (!U
.ModTime
&& IncludeTimestamps
)) {
1978 PP
->Diag(U
.FileNameLoc
, diag::err_module_no_size_mtime_for_header
)
1979 << WritingModule
->getFullModuleName() << U
.Size
.has_value()
1984 // Form the effective relative pathname for the file.
1985 SmallString
<128> Filename(M
->Directory
->getName());
1986 llvm::sys::path::append(Filename
, U
.FileName
);
1987 PreparePathForOutput(Filename
);
1989 StringRef FilenameDup
= strdup(Filename
.c_str());
1990 SavedStrings
.push_back(FilenameDup
.data());
1992 HeaderFileInfoTrait::key_type Key
= {
1993 FilenameDup
, *U
.Size
, IncludeTimestamps
? *U
.ModTime
: 0};
1994 HeaderFileInfoTrait::data_type Data
= {
1995 Empty
, false, {}, {M
, ModuleMap::headerKindToRole(U
.Kind
)}};
1996 // FIXME: Deal with cases where there are multiple unresolved header
1997 // directives in different submodules for the same header.
1998 Generator
.insert(Key
, Data
, GeneratorTrait
);
1999 ++NumHeaderSearchEntries
;
2001 auto SubmodulesRange
= M
->submodules();
2002 Worklist
.append(SubmodulesRange
.begin(), SubmodulesRange
.end());
2006 SmallVector
<OptionalFileEntryRef
, 16> FilesByUID
;
2007 HS
.getFileMgr().GetUniqueIDMapping(FilesByUID
);
2009 if (FilesByUID
.size() > HS
.header_file_size())
2010 FilesByUID
.resize(HS
.header_file_size());
2012 for (unsigned UID
= 0, LastUID
= FilesByUID
.size(); UID
!= LastUID
; ++UID
) {
2013 OptionalFileEntryRef File
= FilesByUID
[UID
];
2017 // Get the file info. This will load info from the external source if
2018 // necessary. Skip emitting this file if we have no information on it
2019 // as a header file (in which case HFI will be null) or if it hasn't
2020 // changed since it was loaded. Also skip it if it's for a modular header
2021 // from a different module; in that case, we rely on the module(s)
2022 // containing the header to provide this information.
2023 const HeaderFileInfo
*HFI
=
2024 HS
.getExistingFileInfo(*File
, /*WantExternal*/!Chain
);
2025 if (!HFI
|| (HFI
->isModuleHeader
&& !HFI
->isCompilingModuleHeader
))
2028 // Massage the file path into an appropriate form.
2029 StringRef Filename
= File
->getName();
2030 SmallString
<128> FilenameTmp(Filename
);
2031 if (PreparePathForOutput(FilenameTmp
)) {
2032 // If we performed any translation on the file name at all, we need to
2033 // save this string, since the generator will refer to it later.
2034 Filename
= StringRef(strdup(FilenameTmp
.c_str()));
2035 SavedStrings
.push_back(Filename
.data());
2038 bool Included
= PP
->alreadyIncluded(*File
);
2040 HeaderFileInfoTrait::key_type Key
= {
2041 Filename
, File
->getSize(), getTimestampForOutput(*File
)
2043 HeaderFileInfoTrait::data_type Data
= {
2044 *HFI
, Included
, HS
.getModuleMap().findResolvedModulesForHeader(*File
), {}
2046 Generator
.insert(Key
, Data
, GeneratorTrait
);
2047 ++NumHeaderSearchEntries
;
2050 // Create the on-disk hash table in a buffer.
2051 SmallString
<4096> TableData
;
2052 uint32_t BucketOffset
;
2054 using namespace llvm::support
;
2056 llvm::raw_svector_ostream
Out(TableData
);
2057 // Make sure that no bucket is at offset 0
2058 endian::write
<uint32_t>(Out
, 0, llvm::endianness::little
);
2059 BucketOffset
= Generator
.Emit(Out
, GeneratorTrait
);
2062 // Create a blob abbreviation
2063 using namespace llvm
;
2065 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2066 Abbrev
->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE
));
2067 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
2068 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
2069 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
2070 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
2071 unsigned TableAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2073 // Write the header search table
2074 RecordData::value_type Record
[] = {HEADER_SEARCH_TABLE
, BucketOffset
,
2075 NumHeaderSearchEntries
, TableData
.size()};
2076 TableData
.append(GeneratorTrait
.strings_begin(),GeneratorTrait
.strings_end());
2077 Stream
.EmitRecordWithBlob(TableAbbrev
, Record
, TableData
);
2079 // Free all of the strings we had to duplicate.
2080 for (unsigned I
= 0, N
= SavedStrings
.size(); I
!= N
; ++I
)
2081 free(const_cast<char *>(SavedStrings
[I
]));
2084 static void emitBlob(llvm::BitstreamWriter
&Stream
, StringRef Blob
,
2085 unsigned SLocBufferBlobCompressedAbbrv
,
2086 unsigned SLocBufferBlobAbbrv
) {
2087 using RecordDataType
= ASTWriter::RecordData::value_type
;
2089 // Compress the buffer if possible. We expect that almost all PCM
2090 // consumers will not want its contents.
2091 SmallVector
<uint8_t, 0> CompressedBuffer
;
2092 if (llvm::compression::zstd::isAvailable()) {
2093 llvm::compression::zstd::compress(
2094 llvm::arrayRefFromStringRef(Blob
.drop_back(1)), CompressedBuffer
, 9);
2095 RecordDataType Record
[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED
, Blob
.size() - 1};
2096 Stream
.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv
, Record
,
2097 llvm::toStringRef(CompressedBuffer
));
2100 if (llvm::compression::zlib::isAvailable()) {
2101 llvm::compression::zlib::compress(
2102 llvm::arrayRefFromStringRef(Blob
.drop_back(1)), CompressedBuffer
);
2103 RecordDataType Record
[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED
, Blob
.size() - 1};
2104 Stream
.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv
, Record
,
2105 llvm::toStringRef(CompressedBuffer
));
2109 RecordDataType Record
[] = {SM_SLOC_BUFFER_BLOB
};
2110 Stream
.EmitRecordWithBlob(SLocBufferBlobAbbrv
, Record
, Blob
);
2113 /// Writes the block containing the serialized form of the
2116 /// TODO: We should probably use an on-disk hash table (stored in a
2117 /// blob), indexed based on the file name, so that we only create
2118 /// entries for files that we actually need. In the common case (no
2119 /// errors), we probably won't have to create file entries for any of
2120 /// the files in the AST.
2121 void ASTWriter::WriteSourceManagerBlock(SourceManager
&SourceMgr
,
2122 const Preprocessor
&PP
) {
2125 // Enter the source manager block.
2126 Stream
.EnterSubblock(SOURCE_MANAGER_BLOCK_ID
, 4);
2127 const uint64_t SourceManagerBlockOffset
= Stream
.GetCurrentBitNo();
2129 // Abbreviations for the various kinds of source-location entries.
2130 unsigned SLocFileAbbrv
= CreateSLocFileAbbrev(Stream
);
2131 unsigned SLocBufferAbbrv
= CreateSLocBufferAbbrev(Stream
);
2132 unsigned SLocBufferBlobAbbrv
= CreateSLocBufferBlobAbbrev(Stream
, false);
2133 unsigned SLocBufferBlobCompressedAbbrv
=
2134 CreateSLocBufferBlobAbbrev(Stream
, true);
2135 unsigned SLocExpansionAbbrv
= CreateSLocExpansionAbbrev(Stream
);
2137 // Write out the source location entry table. We skip the first
2138 // entry, which is always the same dummy entry.
2139 std::vector
<uint32_t> SLocEntryOffsets
;
2140 uint64_t SLocEntryOffsetsBase
= Stream
.GetCurrentBitNo();
2141 SLocEntryOffsets
.reserve(SourceMgr
.local_sloc_entry_size() - 1);
2142 for (unsigned I
= 1, N
= SourceMgr
.local_sloc_entry_size();
2144 // Get this source location entry.
2145 const SrcMgr::SLocEntry
*SLoc
= &SourceMgr
.getLocalSLocEntry(I
);
2146 FileID FID
= FileID::get(I
);
2147 assert(&SourceMgr
.getSLocEntry(FID
) == SLoc
);
2149 // Record the offset of this source-location entry.
2150 uint64_t Offset
= Stream
.GetCurrentBitNo() - SLocEntryOffsetsBase
;
2151 assert((Offset
>> 32) == 0 && "SLocEntry offset too large");
2153 // Figure out which record code to use.
2155 if (SLoc
->isFile()) {
2156 const SrcMgr::ContentCache
*Cache
= &SLoc
->getFile().getContentCache();
2157 if (Cache
->OrigEntry
) {
2158 Code
= SM_SLOC_FILE_ENTRY
;
2160 Code
= SM_SLOC_BUFFER_ENTRY
;
2162 Code
= SM_SLOC_EXPANSION_ENTRY
;
2164 Record
.push_back(Code
);
2166 if (SLoc
->isFile()) {
2167 const SrcMgr::FileInfo
&File
= SLoc
->getFile();
2168 const SrcMgr::ContentCache
*Content
= &File
.getContentCache();
2169 // Do not emit files that were not listed as inputs.
2170 if (!IsSLocAffecting
[I
])
2172 SLocEntryOffsets
.push_back(Offset
);
2173 // Starting offset of this entry within this module, so skip the dummy.
2174 Record
.push_back(getAdjustedOffset(SLoc
->getOffset()) - 2);
2175 AddSourceLocation(File
.getIncludeLoc(), Record
);
2176 Record
.push_back(File
.getFileCharacteristic()); // FIXME: stable encoding
2177 Record
.push_back(File
.hasLineDirectives());
2179 bool EmitBlob
= false;
2180 if (Content
->OrigEntry
) {
2181 assert(Content
->OrigEntry
== Content
->ContentsEntry
&&
2182 "Writing to AST an overridden file is not supported");
2184 // The source location entry is a file. Emit input file ID.
2185 assert(InputFileIDs
[*Content
->OrigEntry
] != 0 && "Missed file entry");
2186 Record
.push_back(InputFileIDs
[*Content
->OrigEntry
]);
2188 Record
.push_back(getAdjustedNumCreatedFIDs(FID
));
2190 FileDeclIDsTy::iterator FDI
= FileDeclIDs
.find(FID
);
2191 if (FDI
!= FileDeclIDs
.end()) {
2192 Record
.push_back(FDI
->second
->FirstDeclIndex
);
2193 Record
.push_back(FDI
->second
->DeclIDs
.size());
2195 Record
.push_back(0);
2196 Record
.push_back(0);
2199 Stream
.EmitRecordWithAbbrev(SLocFileAbbrv
, Record
);
2201 if (Content
->BufferOverridden
|| Content
->IsTransient
)
2204 // The source location entry is a buffer. The blob associated
2205 // with this entry contains the contents of the buffer.
2207 // We add one to the size so that we capture the trailing NULL
2208 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2209 // the reader side).
2210 std::optional
<llvm::MemoryBufferRef
> Buffer
=
2211 Content
->getBufferOrNone(PP
.getDiagnostics(), PP
.getFileManager());
2212 StringRef Name
= Buffer
? Buffer
->getBufferIdentifier() : "";
2213 Stream
.EmitRecordWithBlob(SLocBufferAbbrv
, Record
,
2214 StringRef(Name
.data(), Name
.size() + 1));
2219 // Include the implicit terminating null character in the on-disk buffer
2220 // if we're writing it uncompressed.
2221 std::optional
<llvm::MemoryBufferRef
> Buffer
=
2222 Content
->getBufferOrNone(PP
.getDiagnostics(), PP
.getFileManager());
2224 Buffer
= llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2225 StringRef
Blob(Buffer
->getBufferStart(), Buffer
->getBufferSize() + 1);
2226 emitBlob(Stream
, Blob
, SLocBufferBlobCompressedAbbrv
,
2227 SLocBufferBlobAbbrv
);
2230 // The source location entry is a macro expansion.
2231 const SrcMgr::ExpansionInfo
&Expansion
= SLoc
->getExpansion();
2232 SLocEntryOffsets
.push_back(Offset
);
2233 // Starting offset of this entry within this module, so skip the dummy.
2234 Record
.push_back(getAdjustedOffset(SLoc
->getOffset()) - 2);
2236 AddSourceLocation(Expansion
.getSpellingLoc(), Record
, Seq
);
2237 AddSourceLocation(Expansion
.getExpansionLocStart(), Record
, Seq
);
2238 AddSourceLocation(Expansion
.isMacroArgExpansion()
2240 : Expansion
.getExpansionLocEnd(),
2242 Record
.push_back(Expansion
.isExpansionTokenRange());
2244 // Compute the token length for this macro expansion.
2245 SourceLocation::UIntTy NextOffset
= SourceMgr
.getNextLocalOffset();
2247 NextOffset
= SourceMgr
.getLocalSLocEntry(I
+ 1).getOffset();
2248 Record
.push_back(getAdjustedOffset(NextOffset
- SLoc
->getOffset()) - 1);
2249 Stream
.EmitRecordWithAbbrev(SLocExpansionAbbrv
, Record
);
2255 if (SLocEntryOffsets
.empty())
2258 // Write the source-location offsets table into the AST block. This
2259 // table is used for lazily loading source-location information.
2260 using namespace llvm
;
2262 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2263 Abbrev
->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS
));
2264 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 16)); // # of slocs
2265 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 16)); // total size
2266 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 32)); // base offset
2267 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // offsets
2268 unsigned SLocOffsetsAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2270 RecordData::value_type Record
[] = {
2271 SOURCE_LOCATION_OFFSETS
, SLocEntryOffsets
.size(),
2272 getAdjustedOffset(SourceMgr
.getNextLocalOffset()) - 1 /* skip dummy */,
2273 SLocEntryOffsetsBase
- SourceManagerBlockOffset
};
2274 Stream
.EmitRecordWithBlob(SLocOffsetsAbbrev
, Record
,
2275 bytes(SLocEntryOffsets
));
2278 // Write the line table. It depends on remapping working, so it must come
2279 // after the source location offsets.
2280 if (SourceMgr
.hasLineTable()) {
2281 LineTableInfo
&LineTable
= SourceMgr
.getLineTable();
2285 // Emit the needed file names.
2286 llvm::DenseMap
<int, int> FilenameMap
;
2287 FilenameMap
[-1] = -1; // For unspecified filenames.
2288 for (const auto &L
: LineTable
) {
2291 for (auto &LE
: L
.second
) {
2292 if (FilenameMap
.insert(std::make_pair(LE
.FilenameID
,
2293 FilenameMap
.size() - 1)).second
)
2294 AddPath(LineTable
.getFilename(LE
.FilenameID
), Record
);
2297 Record
.push_back(0);
2299 // Emit the line entries
2300 for (const auto &L
: LineTable
) {
2301 // Only emit entries for local files.
2305 AddFileID(L
.first
, Record
);
2307 // Emit the line entries
2308 Record
.push_back(L
.second
.size());
2309 for (const auto &LE
: L
.second
) {
2310 Record
.push_back(LE
.FileOffset
);
2311 Record
.push_back(LE
.LineNo
);
2312 Record
.push_back(FilenameMap
[LE
.FilenameID
]);
2313 Record
.push_back((unsigned)LE
.FileKind
);
2314 Record
.push_back(LE
.IncludeOffset
);
2318 Stream
.EmitRecord(SOURCE_MANAGER_LINE_TABLE
, Record
);
2322 //===----------------------------------------------------------------------===//
2323 // Preprocessor Serialization
2324 //===----------------------------------------------------------------------===//
2326 static bool shouldIgnoreMacro(MacroDirective
*MD
, bool IsModule
,
2327 const Preprocessor
&PP
) {
2328 if (MacroInfo
*MI
= MD
->getMacroInfo())
2329 if (MI
->isBuiltinMacro())
2333 SourceLocation Loc
= MD
->getLocation();
2334 if (Loc
.isInvalid())
2336 if (PP
.getSourceManager().getFileID(Loc
) == PP
.getPredefinesFileID())
2343 /// Writes the block containing the serialized form of the
2345 void ASTWriter::WritePreprocessor(const Preprocessor
&PP
, bool IsModule
) {
2346 uint64_t MacroOffsetsBase
= Stream
.GetCurrentBitNo();
2348 PreprocessingRecord
*PPRec
= PP
.getPreprocessingRecord();
2350 WritePreprocessorDetail(*PPRec
, MacroOffsetsBase
);
2353 RecordData ModuleMacroRecord
;
2355 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2356 if (PP
.getCounterValue() != 0) {
2357 RecordData::value_type Record
[] = {PP
.getCounterValue()};
2358 Stream
.EmitRecord(PP_COUNTER_VALUE
, Record
);
2361 // If we have a recorded #pragma assume_nonnull, remember it so it can be
2362 // replayed when the preamble terminates into the main file.
2363 SourceLocation AssumeNonNullLoc
=
2364 PP
.getPreambleRecordedPragmaAssumeNonNullLoc();
2365 if (AssumeNonNullLoc
.isValid()) {
2366 assert(PP
.isRecordingPreamble());
2367 AddSourceLocation(AssumeNonNullLoc
, Record
);
2368 Stream
.EmitRecord(PP_ASSUME_NONNULL_LOC
, Record
);
2372 if (PP
.isRecordingPreamble() && PP
.hasRecordedPreamble()) {
2374 auto SkipInfo
= PP
.getPreambleSkipInfo();
2376 Record
.push_back(true);
2377 AddSourceLocation(SkipInfo
->HashTokenLoc
, Record
);
2378 AddSourceLocation(SkipInfo
->IfTokenLoc
, Record
);
2379 Record
.push_back(SkipInfo
->FoundNonSkipPortion
);
2380 Record
.push_back(SkipInfo
->FoundElse
);
2381 AddSourceLocation(SkipInfo
->ElseLoc
, Record
);
2383 Record
.push_back(false);
2385 for (const auto &Cond
: PP
.getPreambleConditionalStack()) {
2386 AddSourceLocation(Cond
.IfLoc
, Record
);
2387 Record
.push_back(Cond
.WasSkipping
);
2388 Record
.push_back(Cond
.FoundNonSkip
);
2389 Record
.push_back(Cond
.FoundElse
);
2391 Stream
.EmitRecord(PP_CONDITIONAL_STACK
, Record
);
2395 // Enter the preprocessor block.
2396 Stream
.EnterSubblock(PREPROCESSOR_BLOCK_ID
, 3);
2398 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2399 // FIXME: Include a location for the use, and say which one was used.
2400 if (PP
.SawDateOrTime())
2401 PP
.Diag(SourceLocation(), diag::warn_module_uses_date_time
) << IsModule
;
2403 // Loop over all the macro directives that are live at the end of the file,
2404 // emitting each to the PP section.
2406 // Construct the list of identifiers with macro directives that need to be
2408 SmallVector
<const IdentifierInfo
*, 128> MacroIdentifiers
;
2409 // It is meaningless to emit macros for named modules. It only wastes times
2411 if (!isWritingStdCXXNamedModules())
2412 for (auto &Id
: PP
.getIdentifierTable())
2413 if (Id
.second
->hadMacroDefinition() &&
2414 (!Id
.second
->isFromAST() ||
2415 Id
.second
->hasChangedSinceDeserialization()))
2416 MacroIdentifiers
.push_back(Id
.second
);
2417 // Sort the set of macro definitions that need to be serialized by the
2418 // name of the macro, to provide a stable ordering.
2419 llvm::sort(MacroIdentifiers
, llvm::deref
<std::less
<>>());
2421 // Emit the macro directives as a list and associate the offset with the
2422 // identifier they belong to.
2423 for (const IdentifierInfo
*Name
: MacroIdentifiers
) {
2424 MacroDirective
*MD
= PP
.getLocalMacroDirectiveHistory(Name
);
2425 uint64_t StartOffset
= Stream
.GetCurrentBitNo() - MacroOffsetsBase
;
2426 assert((StartOffset
>> 32) == 0 && "Macro identifiers offset too large");
2428 // Write out any exported module macros.
2429 bool EmittedModuleMacros
= false;
2430 // C+=20 Header Units are compiled module interfaces, but they preserve
2431 // macros that are live (i.e. have a defined value) at the end of the
2432 // compilation. So when writing a header unit, we preserve only the final
2433 // value of each macro (and discard any that are undefined). Header units
2434 // do not have sub-modules (although they might import other header units).
2435 // PCH files, conversely, retain the history of each macro's define/undef
2436 // and of leaf macros in sub modules.
2437 if (IsModule
&& WritingModule
->isHeaderUnit()) {
2438 // This is for the main TU when it is a C++20 header unit.
2439 // We preserve the final state of defined macros, and we do not emit ones
2440 // that are undefined.
2441 if (!MD
|| shouldIgnoreMacro(MD
, IsModule
, PP
) ||
2442 MD
->getKind() == MacroDirective::MD_Undefine
)
2444 AddSourceLocation(MD
->getLocation(), Record
);
2445 Record
.push_back(MD
->getKind());
2446 if (auto *DefMD
= dyn_cast
<DefMacroDirective
>(MD
)) {
2447 Record
.push_back(getMacroRef(DefMD
->getInfo(), Name
));
2448 } else if (auto *VisMD
= dyn_cast
<VisibilityMacroDirective
>(MD
)) {
2449 Record
.push_back(VisMD
->isPublic());
2451 ModuleMacroRecord
.push_back(getSubmoduleID(WritingModule
));
2452 ModuleMacroRecord
.push_back(getMacroRef(MD
->getMacroInfo(), Name
));
2453 Stream
.EmitRecord(PP_MODULE_MACRO
, ModuleMacroRecord
);
2454 ModuleMacroRecord
.clear();
2455 EmittedModuleMacros
= true;
2457 // Emit the macro directives in reverse source order.
2458 for (; MD
; MD
= MD
->getPrevious()) {
2459 // Once we hit an ignored macro, we're done: the rest of the chain
2460 // will all be ignored macros.
2461 if (shouldIgnoreMacro(MD
, IsModule
, PP
))
2463 AddSourceLocation(MD
->getLocation(), Record
);
2464 Record
.push_back(MD
->getKind());
2465 if (auto *DefMD
= dyn_cast
<DefMacroDirective
>(MD
)) {
2466 Record
.push_back(getMacroRef(DefMD
->getInfo(), Name
));
2467 } else if (auto *VisMD
= dyn_cast
<VisibilityMacroDirective
>(MD
)) {
2468 Record
.push_back(VisMD
->isPublic());
2472 // We write out exported module macros for PCH as well.
2473 auto Leafs
= PP
.getLeafModuleMacros(Name
);
2474 SmallVector
<ModuleMacro
*, 8> Worklist(Leafs
.begin(), Leafs
.end());
2475 llvm::DenseMap
<ModuleMacro
*, unsigned> Visits
;
2476 while (!Worklist
.empty()) {
2477 auto *Macro
= Worklist
.pop_back_val();
2479 // Emit a record indicating this submodule exports this macro.
2480 ModuleMacroRecord
.push_back(getSubmoduleID(Macro
->getOwningModule()));
2481 ModuleMacroRecord
.push_back(getMacroRef(Macro
->getMacroInfo(), Name
));
2482 for (auto *M
: Macro
->overrides())
2483 ModuleMacroRecord
.push_back(getSubmoduleID(M
->getOwningModule()));
2485 Stream
.EmitRecord(PP_MODULE_MACRO
, ModuleMacroRecord
);
2486 ModuleMacroRecord
.clear();
2488 // Enqueue overridden macros once we've visited all their ancestors.
2489 for (auto *M
: Macro
->overrides())
2490 if (++Visits
[M
] == M
->getNumOverridingMacros())
2491 Worklist
.push_back(M
);
2493 EmittedModuleMacros
= true;
2496 if (Record
.empty() && !EmittedModuleMacros
)
2499 IdentMacroDirectivesOffsetMap
[Name
] = StartOffset
;
2500 Stream
.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY
, Record
);
2504 /// Offsets of each of the macros into the bitstream, indexed by
2505 /// the local macro ID
2507 /// For each identifier that is associated with a macro, this map
2508 /// provides the offset into the bitstream where that macro is
2510 std::vector
<uint32_t> MacroOffsets
;
2512 for (unsigned I
= 0, N
= MacroInfosToEmit
.size(); I
!= N
; ++I
) {
2513 const IdentifierInfo
*Name
= MacroInfosToEmit
[I
].Name
;
2514 MacroInfo
*MI
= MacroInfosToEmit
[I
].MI
;
2515 MacroID ID
= MacroInfosToEmit
[I
].ID
;
2517 if (ID
< FirstMacroID
) {
2518 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2522 // Record the local offset of this macro.
2523 unsigned Index
= ID
- FirstMacroID
;
2524 if (Index
>= MacroOffsets
.size())
2525 MacroOffsets
.resize(Index
+ 1);
2527 uint64_t Offset
= Stream
.GetCurrentBitNo() - MacroOffsetsBase
;
2528 assert((Offset
>> 32) == 0 && "Macro offset too large");
2529 MacroOffsets
[Index
] = Offset
;
2531 AddIdentifierRef(Name
, Record
);
2532 AddSourceLocation(MI
->getDefinitionLoc(), Record
);
2533 AddSourceLocation(MI
->getDefinitionEndLoc(), Record
);
2534 Record
.push_back(MI
->isUsed());
2535 Record
.push_back(MI
->isUsedForHeaderGuard());
2536 Record
.push_back(MI
->getNumTokens());
2538 if (MI
->isObjectLike()) {
2539 Code
= PP_MACRO_OBJECT_LIKE
;
2541 Code
= PP_MACRO_FUNCTION_LIKE
;
2543 Record
.push_back(MI
->isC99Varargs());
2544 Record
.push_back(MI
->isGNUVarargs());
2545 Record
.push_back(MI
->hasCommaPasting());
2546 Record
.push_back(MI
->getNumParams());
2547 for (const IdentifierInfo
*Param
: MI
->params())
2548 AddIdentifierRef(Param
, Record
);
2551 // If we have a detailed preprocessing record, record the macro definition
2552 // ID that corresponds to this macro.
2554 Record
.push_back(MacroDefinitions
[PPRec
->findMacroDefinition(MI
)]);
2556 Stream
.EmitRecord(Code
, Record
);
2559 // Emit the tokens array.
2560 for (unsigned TokNo
= 0, e
= MI
->getNumTokens(); TokNo
!= e
; ++TokNo
) {
2561 // Note that we know that the preprocessor does not have any annotation
2562 // tokens in it because they are created by the parser, and thus can't
2563 // be in a macro definition.
2564 const Token
&Tok
= MI
->getReplacementToken(TokNo
);
2565 AddToken(Tok
, Record
);
2566 Stream
.EmitRecord(PP_TOKEN
, Record
);
2574 // Write the offsets table for macro IDs.
2575 using namespace llvm
;
2577 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2578 Abbrev
->Add(BitCodeAbbrevOp(MACRO_OFFSET
));
2579 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // # of macros
2580 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // first ID
2581 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 32)); // base offset
2582 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
2584 unsigned MacroOffsetAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2586 RecordData::value_type Record
[] = {MACRO_OFFSET
, MacroOffsets
.size(),
2587 FirstMacroID
- NUM_PREDEF_MACRO_IDS
,
2588 MacroOffsetsBase
- ASTBlockStartOffset
};
2589 Stream
.EmitRecordWithBlob(MacroOffsetAbbrev
, Record
, bytes(MacroOffsets
));
2593 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord
&PPRec
,
2594 uint64_t MacroOffsetsBase
) {
2595 if (PPRec
.local_begin() == PPRec
.local_end())
2598 SmallVector
<PPEntityOffset
, 64> PreprocessedEntityOffsets
;
2600 // Enter the preprocessor block.
2601 Stream
.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID
, 3);
2603 // If the preprocessor has a preprocessing record, emit it.
2604 unsigned NumPreprocessingRecords
= 0;
2605 using namespace llvm
;
2607 // Set up the abbreviation for
2608 unsigned InclusionAbbrev
= 0;
2610 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2611 Abbrev
->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE
));
2612 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // filename length
2613 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // in quotes
2614 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 2)); // kind
2615 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // imported module
2616 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
2617 InclusionAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2620 unsigned FirstPreprocessorEntityID
2621 = (Chain
? PPRec
.getNumLoadedPreprocessedEntities() : 0)
2622 + NUM_PREDEF_PP_ENTITY_IDS
;
2623 unsigned NextPreprocessorEntityID
= FirstPreprocessorEntityID
;
2625 for (PreprocessingRecord::iterator E
= PPRec
.local_begin(),
2626 EEnd
= PPRec
.local_end();
2628 (void)++E
, ++NumPreprocessingRecords
, ++NextPreprocessorEntityID
) {
2631 uint64_t Offset
= Stream
.GetCurrentBitNo() - MacroOffsetsBase
;
2632 assert((Offset
>> 32) == 0 && "Preprocessed entity offset too large");
2633 PreprocessedEntityOffsets
.push_back(
2634 PPEntityOffset(getAdjustedRange((*E
)->getSourceRange()), Offset
));
2636 if (auto *MD
= dyn_cast
<MacroDefinitionRecord
>(*E
)) {
2637 // Record this macro definition's ID.
2638 MacroDefinitions
[MD
] = NextPreprocessorEntityID
;
2640 AddIdentifierRef(MD
->getName(), Record
);
2641 Stream
.EmitRecord(PPD_MACRO_DEFINITION
, Record
);
2645 if (auto *ME
= dyn_cast
<MacroExpansion
>(*E
)) {
2646 Record
.push_back(ME
->isBuiltinMacro());
2647 if (ME
->isBuiltinMacro())
2648 AddIdentifierRef(ME
->getName(), Record
);
2650 Record
.push_back(MacroDefinitions
[ME
->getDefinition()]);
2651 Stream
.EmitRecord(PPD_MACRO_EXPANSION
, Record
);
2655 if (auto *ID
= dyn_cast
<InclusionDirective
>(*E
)) {
2656 Record
.push_back(PPD_INCLUSION_DIRECTIVE
);
2657 Record
.push_back(ID
->getFileName().size());
2658 Record
.push_back(ID
->wasInQuotes());
2659 Record
.push_back(static_cast<unsigned>(ID
->getKind()));
2660 Record
.push_back(ID
->importedModule());
2661 SmallString
<64> Buffer
;
2662 Buffer
+= ID
->getFileName();
2663 // Check that the FileEntry is not null because it was not resolved and
2664 // we create a PCH even with compiler errors.
2666 Buffer
+= ID
->getFile()->getName();
2667 Stream
.EmitRecordWithBlob(InclusionAbbrev
, Record
, Buffer
);
2671 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2675 // Write the offsets table for the preprocessing record.
2676 if (NumPreprocessingRecords
> 0) {
2677 assert(PreprocessedEntityOffsets
.size() == NumPreprocessingRecords
);
2679 // Write the offsets table for identifier IDs.
2680 using namespace llvm
;
2682 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2683 Abbrev
->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS
));
2684 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // first pp entity
2685 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
2686 unsigned PPEOffsetAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2688 RecordData::value_type Record
[] = {PPD_ENTITIES_OFFSETS
,
2689 FirstPreprocessorEntityID
-
2690 NUM_PREDEF_PP_ENTITY_IDS
};
2691 Stream
.EmitRecordWithBlob(PPEOffsetAbbrev
, Record
,
2692 bytes(PreprocessedEntityOffsets
));
2695 // Write the skipped region table for the preprocessing record.
2696 ArrayRef
<SourceRange
> SkippedRanges
= PPRec
.getSkippedRanges();
2697 if (SkippedRanges
.size() > 0) {
2698 std::vector
<PPSkippedRange
> SerializedSkippedRanges
;
2699 SerializedSkippedRanges
.reserve(SkippedRanges
.size());
2700 for (auto const& Range
: SkippedRanges
)
2701 SerializedSkippedRanges
.emplace_back(Range
);
2703 using namespace llvm
;
2704 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2705 Abbrev
->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES
));
2706 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
2707 unsigned PPESkippedRangeAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2710 Record
.push_back(PPD_SKIPPED_RANGES
);
2711 Stream
.EmitRecordWithBlob(PPESkippedRangeAbbrev
, Record
,
2712 bytes(SerializedSkippedRanges
));
2716 unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module
*Mod
) {
2720 auto Known
= SubmoduleIDs
.find(Mod
);
2721 if (Known
!= SubmoduleIDs
.end())
2722 return Known
->second
;
2724 auto *Top
= Mod
->getTopLevelModule();
2725 if (Top
!= WritingModule
&&
2726 (getLangOpts().CompilingPCH
||
2727 !Top
->fullModuleNameIs(StringRef(getLangOpts().CurrentModule
))))
2730 return SubmoduleIDs
[Mod
] = NextSubmoduleID
++;
2733 unsigned ASTWriter::getSubmoduleID(Module
*Mod
) {
2734 unsigned ID
= getLocalOrImportedSubmoduleID(Mod
);
2735 // FIXME: This can easily happen, if we have a reference to a submodule that
2736 // did not result in us loading a module file for that submodule. For
2737 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2738 // assert((ID || !Mod) &&
2739 // "asked for module ID for non-local, non-imported module");
2743 /// Compute the number of modules within the given tree (including the
2745 static unsigned getNumberOfModules(Module
*Mod
) {
2746 unsigned ChildModules
= 0;
2747 for (auto *Submodule
: Mod
->submodules())
2748 ChildModules
+= getNumberOfModules(Submodule
);
2750 return ChildModules
+ 1;
2753 void ASTWriter::WriteSubmodules(Module
*WritingModule
) {
2754 // Enter the submodule description block.
2755 Stream
.EnterSubblock(SUBMODULE_BLOCK_ID
, /*bits for abbreviations*/5);
2757 // Write the abbreviations needed for the submodules block.
2758 using namespace llvm
;
2760 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2761 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION
));
2762 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // ID
2763 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Parent
2764 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 4)); // Kind
2765 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 8)); // Definition location
2766 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // IsFramework
2767 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // IsExplicit
2768 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // IsSystem
2769 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // IsExternC
2770 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // InferSubmodules...
2771 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // InferExplicit...
2772 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // InferExportWild...
2773 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // ConfigMacrosExh...
2774 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // ModuleMapIsPriv...
2775 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // NamedModuleHasN...
2776 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2777 unsigned DefinitionAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2779 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2780 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER
));
2781 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2782 unsigned UmbrellaAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2784 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2785 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_HEADER
));
2786 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2787 unsigned HeaderAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2789 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2790 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER
));
2791 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2792 unsigned TopHeaderAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2794 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2795 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR
));
2796 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2797 unsigned UmbrellaDirAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2799 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2800 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES
));
2801 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // State
2802 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Feature
2803 unsigned RequiresAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2805 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2806 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER
));
2807 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2808 unsigned ExcludedHeaderAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2810 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2811 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER
));
2812 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2813 unsigned TextualHeaderAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2815 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2816 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER
));
2817 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2818 unsigned PrivateHeaderAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2820 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2821 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER
));
2822 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2823 unsigned PrivateTextualHeaderAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2825 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2826 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY
));
2827 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 1)); // IsFramework
2828 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Name
2829 unsigned LinkLibraryAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2831 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2832 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO
));
2833 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Macro name
2834 unsigned ConfigMacroAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2836 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2837 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT
));
2838 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Other module
2839 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Message
2840 unsigned ConflictAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2842 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
2843 Abbrev
->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS
));
2844 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // Macro name
2845 unsigned ExportAsAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
2847 // Write the submodule metadata block.
2848 RecordData::value_type Record
[] = {
2849 getNumberOfModules(WritingModule
),
2850 FirstSubmoduleID
- NUM_PREDEF_SUBMODULE_IDS
};
2851 Stream
.EmitRecord(SUBMODULE_METADATA
, Record
);
2853 // Write all of the submodules.
2854 std::queue
<Module
*> Q
;
2855 Q
.push(WritingModule
);
2856 while (!Q
.empty()) {
2857 Module
*Mod
= Q
.front();
2859 unsigned ID
= getSubmoduleID(Mod
);
2861 uint64_t ParentID
= 0;
2863 assert(SubmoduleIDs
[Mod
->Parent
] && "Submodule parent not written?");
2864 ParentID
= SubmoduleIDs
[Mod
->Parent
];
2867 uint64_t DefinitionLoc
=
2868 SourceLocationEncoding::encode(getAdjustedLocation(Mod
->DefinitionLoc
));
2870 // Emit the definition of the block.
2872 RecordData::value_type Record
[] = {SUBMODULE_DEFINITION
,
2875 (RecordData::value_type
)Mod
->Kind
,
2881 Mod
->InferSubmodules
,
2882 Mod
->InferExplicitSubmodules
,
2883 Mod
->InferExportWildcard
,
2884 Mod
->ConfigMacrosExhaustive
,
2885 Mod
->ModuleMapIsPrivate
,
2886 Mod
->NamedModuleHasInit
};
2887 Stream
.EmitRecordWithBlob(DefinitionAbbrev
, Record
, Mod
->Name
);
2890 // Emit the requirements.
2891 for (const auto &R
: Mod
->Requirements
) {
2892 RecordData::value_type Record
[] = {SUBMODULE_REQUIRES
, R
.second
};
2893 Stream
.EmitRecordWithBlob(RequiresAbbrev
, Record
, R
.first
);
2896 // Emit the umbrella header, if there is one.
2897 if (std::optional
<Module::Header
> UmbrellaHeader
=
2898 Mod
->getUmbrellaHeaderAsWritten()) {
2899 RecordData::value_type Record
[] = {SUBMODULE_UMBRELLA_HEADER
};
2900 Stream
.EmitRecordWithBlob(UmbrellaAbbrev
, Record
,
2901 UmbrellaHeader
->NameAsWritten
);
2902 } else if (std::optional
<Module::DirectoryName
> UmbrellaDir
=
2903 Mod
->getUmbrellaDirAsWritten()) {
2904 RecordData::value_type Record
[] = {SUBMODULE_UMBRELLA_DIR
};
2905 Stream
.EmitRecordWithBlob(UmbrellaDirAbbrev
, Record
,
2906 UmbrellaDir
->NameAsWritten
);
2909 // Emit the headers.
2911 unsigned RecordKind
;
2913 Module::HeaderKind HeaderKind
;
2915 {SUBMODULE_HEADER
, HeaderAbbrev
, Module::HK_Normal
},
2916 {SUBMODULE_TEXTUAL_HEADER
, TextualHeaderAbbrev
, Module::HK_Textual
},
2917 {SUBMODULE_PRIVATE_HEADER
, PrivateHeaderAbbrev
, Module::HK_Private
},
2918 {SUBMODULE_PRIVATE_TEXTUAL_HEADER
, PrivateTextualHeaderAbbrev
,
2919 Module::HK_PrivateTextual
},
2920 {SUBMODULE_EXCLUDED_HEADER
, ExcludedHeaderAbbrev
, Module::HK_Excluded
}
2922 for (auto &HL
: HeaderLists
) {
2923 RecordData::value_type Record
[] = {HL
.RecordKind
};
2924 for (auto &H
: Mod
->Headers
[HL
.HeaderKind
])
2925 Stream
.EmitRecordWithBlob(HL
.Abbrev
, Record
, H
.NameAsWritten
);
2928 // Emit the top headers.
2930 RecordData::value_type Record
[] = {SUBMODULE_TOPHEADER
};
2931 for (FileEntryRef H
: Mod
->getTopHeaders(PP
->getFileManager())) {
2932 SmallString
<128> HeaderName(H
.getName());
2933 PreparePathForOutput(HeaderName
);
2934 Stream
.EmitRecordWithBlob(TopHeaderAbbrev
, Record
, HeaderName
);
2938 // Emit the imports.
2939 if (!Mod
->Imports
.empty()) {
2941 for (auto *I
: Mod
->Imports
)
2942 Record
.push_back(getSubmoduleID(I
));
2943 Stream
.EmitRecord(SUBMODULE_IMPORTS
, Record
);
2946 // Emit the modules affecting compilation that were not imported.
2947 if (!Mod
->AffectingClangModules
.empty()) {
2949 for (auto *I
: Mod
->AffectingClangModules
)
2950 Record
.push_back(getSubmoduleID(I
));
2951 Stream
.EmitRecord(SUBMODULE_AFFECTING_MODULES
, Record
);
2954 // Emit the exports.
2955 if (!Mod
->Exports
.empty()) {
2957 for (const auto &E
: Mod
->Exports
) {
2958 // FIXME: This may fail; we don't require that all exported modules
2959 // are local or imported.
2960 Record
.push_back(getSubmoduleID(E
.getPointer()));
2961 Record
.push_back(E
.getInt());
2963 Stream
.EmitRecord(SUBMODULE_EXPORTS
, Record
);
2966 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2967 // Might be unnecessary as use declarations are only used to build the
2970 // TODO: Consider serializing undeclared uses of modules.
2972 // Emit the link libraries.
2973 for (const auto &LL
: Mod
->LinkLibraries
) {
2974 RecordData::value_type Record
[] = {SUBMODULE_LINK_LIBRARY
,
2976 Stream
.EmitRecordWithBlob(LinkLibraryAbbrev
, Record
, LL
.Library
);
2979 // Emit the conflicts.
2980 for (const auto &C
: Mod
->Conflicts
) {
2981 // FIXME: This may fail; we don't require that all conflicting modules
2982 // are local or imported.
2983 RecordData::value_type Record
[] = {SUBMODULE_CONFLICT
,
2984 getSubmoduleID(C
.Other
)};
2985 Stream
.EmitRecordWithBlob(ConflictAbbrev
, Record
, C
.Message
);
2988 // Emit the configuration macros.
2989 for (const auto &CM
: Mod
->ConfigMacros
) {
2990 RecordData::value_type Record
[] = {SUBMODULE_CONFIG_MACRO
};
2991 Stream
.EmitRecordWithBlob(ConfigMacroAbbrev
, Record
, CM
);
2994 // Emit the initializers, if any.
2996 for (Decl
*D
: Context
->getModuleInitializers(Mod
))
2997 Inits
.push_back(GetDeclRef(D
));
2999 Stream
.EmitRecord(SUBMODULE_INITIALIZERS
, Inits
);
3001 // Emit the name of the re-exported module, if any.
3002 if (!Mod
->ExportAsModule
.empty()) {
3003 RecordData::value_type Record
[] = {SUBMODULE_EXPORT_AS
};
3004 Stream
.EmitRecordWithBlob(ExportAsAbbrev
, Record
, Mod
->ExportAsModule
);
3007 // Queue up the submodules of this module.
3008 for (auto *M
: Mod
->submodules())
3014 assert((NextSubmoduleID
- FirstSubmoduleID
==
3015 getNumberOfModules(WritingModule
)) &&
3016 "Wrong # of submodules; found a reference to a non-local, "
3017 "non-imported submodule?");
3020 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine
&Diag
,
3022 llvm::SmallDenseMap
<const DiagnosticsEngine::DiagState
*, unsigned, 64>
3024 unsigned CurrID
= 0;
3027 auto EncodeDiagStateFlags
=
3028 [](const DiagnosticsEngine::DiagState
*DS
) -> unsigned {
3029 unsigned Result
= (unsigned)DS
->ExtBehavior
;
3031 {(unsigned)DS
->IgnoreAllWarnings
, (unsigned)DS
->EnableAllWarnings
,
3032 (unsigned)DS
->WarningsAsErrors
, (unsigned)DS
->ErrorsAsFatal
,
3033 (unsigned)DS
->SuppressSystemWarnings
})
3034 Result
= (Result
<< 1) | Val
;
3038 unsigned Flags
= EncodeDiagStateFlags(Diag
.DiagStatesByLoc
.FirstDiagState
);
3039 Record
.push_back(Flags
);
3041 auto AddDiagState
= [&](const DiagnosticsEngine::DiagState
*State
,
3042 bool IncludeNonPragmaStates
) {
3043 // Ensure that the diagnostic state wasn't modified since it was created.
3044 // We will not correctly round-trip this information otherwise.
3045 assert(Flags
== EncodeDiagStateFlags(State
) &&
3046 "diag state flags vary in single AST file");
3048 // If we ever serialize non-pragma mappings outside the initial state, the
3049 // code below will need to consider more than getDefaultMapping.
3050 assert(!IncludeNonPragmaStates
||
3051 State
== Diag
.DiagStatesByLoc
.FirstDiagState
);
3053 unsigned &DiagStateID
= DiagStateIDMap
[State
];
3054 Record
.push_back(DiagStateID
);
3056 if (DiagStateID
== 0) {
3057 DiagStateID
= ++CurrID
;
3058 SmallVector
<std::pair
<unsigned, DiagnosticMapping
>> Mappings
;
3060 // Add a placeholder for the number of mappings.
3061 auto SizeIdx
= Record
.size();
3062 Record
.emplace_back();
3063 for (const auto &I
: *State
) {
3064 // Maybe skip non-pragmas.
3065 if (!I
.second
.isPragma() && !IncludeNonPragmaStates
)
3067 // Skip default mappings. We have a mapping for every diagnostic ever
3068 // emitted, regardless of whether it was customized.
3069 if (!I
.second
.isPragma() &&
3070 I
.second
== DiagnosticIDs::getDefaultMapping(I
.first
))
3072 Mappings
.push_back(I
);
3075 // Sort by diag::kind for deterministic output.
3076 llvm::sort(Mappings
, [](const auto &LHS
, const auto &RHS
) {
3077 return LHS
.first
< RHS
.first
;
3080 for (const auto &I
: Mappings
) {
3081 Record
.push_back(I
.first
);
3082 Record
.push_back(I
.second
.serialize());
3084 // Update the placeholder.
3085 Record
[SizeIdx
] = (Record
.size() - SizeIdx
) / 2;
3089 AddDiagState(Diag
.DiagStatesByLoc
.FirstDiagState
, isModule
);
3091 // Reserve a spot for the number of locations with state transitions.
3092 auto NumLocationsIdx
= Record
.size();
3093 Record
.emplace_back();
3095 // Emit the state transitions.
3096 unsigned NumLocations
= 0;
3097 for (auto &FileIDAndFile
: Diag
.DiagStatesByLoc
.Files
) {
3098 if (!FileIDAndFile
.first
.isValid() ||
3099 !FileIDAndFile
.second
.HasLocalTransitions
)
3103 SourceLocation Loc
= Diag
.SourceMgr
->getComposedLoc(FileIDAndFile
.first
, 0);
3104 assert(!Loc
.isInvalid() && "start loc for valid FileID is invalid");
3105 AddSourceLocation(Loc
, Record
);
3107 Record
.push_back(FileIDAndFile
.second
.StateTransitions
.size());
3108 for (auto &StatePoint
: FileIDAndFile
.second
.StateTransitions
) {
3109 Record
.push_back(getAdjustedOffset(StatePoint
.Offset
));
3110 AddDiagState(StatePoint
.State
, false);
3114 // Backpatch the number of locations.
3115 Record
[NumLocationsIdx
] = NumLocations
;
3117 // Emit CurDiagStateLoc. Do it last in order to match source order.
3119 // This also protects against a hypothetical corner case with simulating
3120 // -Werror settings for implicit modules in the ASTReader, where reading
3121 // CurDiagState out of context could change whether warning pragmas are
3122 // treated as errors.
3123 AddSourceLocation(Diag
.DiagStatesByLoc
.CurDiagStateLoc
, Record
);
3124 AddDiagState(Diag
.DiagStatesByLoc
.CurDiagState
, false);
3126 Stream
.EmitRecord(DIAG_PRAGMA_MAPPINGS
, Record
);
3129 //===----------------------------------------------------------------------===//
3130 // Type Serialization
3131 //===----------------------------------------------------------------------===//
3133 /// Write the representation of a type to the AST stream.
3134 void ASTWriter::WriteType(QualType T
) {
3135 TypeIdx
&IdxRef
= TypeIdxs
[T
];
3136 if (IdxRef
.getIndex() == 0) // we haven't seen this type before.
3137 IdxRef
= TypeIdx(NextTypeID
++);
3138 TypeIdx Idx
= IdxRef
;
3140 assert(Idx
.getIndex() >= FirstTypeID
&& "Re-writing a type from a prior AST");
3142 // Emit the type's representation.
3143 uint64_t Offset
= ASTTypeWriter(*this).write(T
) - DeclTypesBlockStartOffset
;
3145 // Record the offset for this type.
3146 unsigned Index
= Idx
.getIndex() - FirstTypeID
;
3147 if (TypeOffsets
.size() == Index
)
3148 TypeOffsets
.emplace_back(Offset
);
3149 else if (TypeOffsets
.size() < Index
) {
3150 TypeOffsets
.resize(Index
+ 1);
3151 TypeOffsets
[Index
].setBitOffset(Offset
);
3153 llvm_unreachable("Types emitted in wrong order");
3157 //===----------------------------------------------------------------------===//
3158 // Declaration Serialization
3159 //===----------------------------------------------------------------------===//
3161 /// Write the block containing all of the declaration IDs
3162 /// lexically declared within the given DeclContext.
3164 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3165 /// bitstream, or 0 if no block was written.
3166 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext
&Context
,
3168 if (DC
->decls_empty())
3171 uint64_t Offset
= Stream
.GetCurrentBitNo();
3172 SmallVector
<uint32_t, 128> KindDeclPairs
;
3173 for (const auto *D
: DC
->decls()) {
3174 KindDeclPairs
.push_back(D
->getKind());
3175 KindDeclPairs
.push_back(GetDeclRef(D
));
3178 ++NumLexicalDeclContexts
;
3179 RecordData::value_type Record
[] = {DECL_CONTEXT_LEXICAL
};
3180 Stream
.EmitRecordWithBlob(DeclContextLexicalAbbrev
, Record
,
3181 bytes(KindDeclPairs
));
3185 void ASTWriter::WriteTypeDeclOffsets() {
3186 using namespace llvm
;
3188 // Write the type offsets array
3189 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3190 Abbrev
->Add(BitCodeAbbrevOp(TYPE_OFFSET
));
3191 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // # of types
3192 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // base type index
3193 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // types block
3194 unsigned TypeOffsetAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
3196 RecordData::value_type Record
[] = {TYPE_OFFSET
, TypeOffsets
.size(),
3197 FirstTypeID
- NUM_PREDEF_TYPE_IDS
};
3198 Stream
.EmitRecordWithBlob(TypeOffsetAbbrev
, Record
, bytes(TypeOffsets
));
3201 // Write the declaration offsets array
3202 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3203 Abbrev
->Add(BitCodeAbbrevOp(DECL_OFFSET
));
3204 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // # of declarations
3205 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // base decl ID
3206 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
)); // declarations block
3207 unsigned DeclOffsetAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
3209 RecordData::value_type Record
[] = {DECL_OFFSET
, DeclOffsets
.size(),
3210 FirstDeclID
- NUM_PREDEF_DECL_IDS
};
3211 Stream
.EmitRecordWithBlob(DeclOffsetAbbrev
, Record
, bytes(DeclOffsets
));
3215 void ASTWriter::WriteFileDeclIDsMap() {
3216 using namespace llvm
;
3218 SmallVector
<std::pair
<FileID
, DeclIDInFileInfo
*>, 64> SortedFileDeclIDs
;
3219 SortedFileDeclIDs
.reserve(FileDeclIDs
.size());
3220 for (const auto &P
: FileDeclIDs
)
3221 SortedFileDeclIDs
.push_back(std::make_pair(P
.first
, P
.second
.get()));
3222 llvm::sort(SortedFileDeclIDs
, llvm::less_first());
3224 // Join the vectors of DeclIDs from all files.
3225 SmallVector
<DeclID
, 256> FileGroupedDeclIDs
;
3226 for (auto &FileDeclEntry
: SortedFileDeclIDs
) {
3227 DeclIDInFileInfo
&Info
= *FileDeclEntry
.second
;
3228 Info
.FirstDeclIndex
= FileGroupedDeclIDs
.size();
3229 llvm::stable_sort(Info
.DeclIDs
);
3230 for (auto &LocDeclEntry
: Info
.DeclIDs
)
3231 FileGroupedDeclIDs
.push_back(LocDeclEntry
.second
);
3234 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3235 Abbrev
->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS
));
3236 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
3237 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
3238 unsigned AbbrevCode
= Stream
.EmitAbbrev(std::move(Abbrev
));
3239 RecordData::value_type Record
[] = {FILE_SORTED_DECLS
,
3240 FileGroupedDeclIDs
.size()};
3241 Stream
.EmitRecordWithBlob(AbbrevCode
, Record
, bytes(FileGroupedDeclIDs
));
3244 void ASTWriter::WriteComments() {
3245 Stream
.EnterSubblock(COMMENTS_BLOCK_ID
, 3);
3246 auto _
= llvm::make_scope_exit([this] { Stream
.ExitBlock(); });
3247 if (!PP
->getPreprocessorOpts().WriteCommentListToPCH
)
3250 // Don't write comments to BMI to reduce the size of BMI.
3251 // If language services (e.g., clangd) want such abilities,
3252 // we can offer a special option then.
3253 if (isWritingStdCXXNamedModules())
3257 for (const auto &FO
: Context
->Comments
.OrderedComments
) {
3258 for (const auto &OC
: FO
.second
) {
3259 const RawComment
*I
= OC
.second
;
3261 AddSourceRange(I
->getSourceRange(), Record
);
3262 Record
.push_back(I
->getKind());
3263 Record
.push_back(I
->isTrailingComment());
3264 Record
.push_back(I
->isAlmostTrailingComment());
3265 Stream
.EmitRecord(COMMENTS_RAW_COMMENT
, Record
);
3270 //===----------------------------------------------------------------------===//
3271 // Global Method Pool and Selector Serialization
3272 //===----------------------------------------------------------------------===//
3276 // Trait used for the on-disk hash table used in the method pool.
3277 class ASTMethodPoolTrait
{
3281 using key_type
= Selector
;
3282 using key_type_ref
= key_type
;
3286 ObjCMethodList Instance
, Factory
;
3288 using data_type_ref
= const data_type
&;
3290 using hash_value_type
= unsigned;
3291 using offset_type
= unsigned;
3293 explicit ASTMethodPoolTrait(ASTWriter
&Writer
) : Writer(Writer
) {}
3295 static hash_value_type
ComputeHash(Selector Sel
) {
3296 return serialization::ComputeHash(Sel
);
3299 std::pair
<unsigned, unsigned>
3300 EmitKeyDataLength(raw_ostream
& Out
, Selector Sel
,
3301 data_type_ref Methods
) {
3302 unsigned KeyLen
= 2 + (Sel
.getNumArgs()? Sel
.getNumArgs() * 4 : 4);
3303 unsigned DataLen
= 4 + 2 + 2; // 2 bytes for each of the method counts
3304 for (const ObjCMethodList
*Method
= &Methods
.Instance
; Method
;
3305 Method
= Method
->getNext())
3306 if (ShouldWriteMethodListNode(Method
))
3308 for (const ObjCMethodList
*Method
= &Methods
.Factory
; Method
;
3309 Method
= Method
->getNext())
3310 if (ShouldWriteMethodListNode(Method
))
3312 return emitULEBKeyDataLength(KeyLen
, DataLen
, Out
);
3315 void EmitKey(raw_ostream
& Out
, Selector Sel
, unsigned) {
3316 using namespace llvm::support
;
3318 endian::Writer
LE(Out
, llvm::endianness::little
);
3319 uint64_t Start
= Out
.tell();
3320 assert((Start
>> 32) == 0 && "Selector key offset too large");
3321 Writer
.SetSelectorOffset(Sel
, Start
);
3322 unsigned N
= Sel
.getNumArgs();
3323 LE
.write
<uint16_t>(N
);
3326 for (unsigned I
= 0; I
!= N
; ++I
)
3328 Writer
.getIdentifierRef(Sel
.getIdentifierInfoForSlot(I
)));
3331 void EmitData(raw_ostream
& Out
, key_type_ref
,
3332 data_type_ref Methods
, unsigned DataLen
) {
3333 using namespace llvm::support
;
3335 endian::Writer
LE(Out
, llvm::endianness::little
);
3336 uint64_t Start
= Out
.tell(); (void)Start
;
3337 LE
.write
<uint32_t>(Methods
.ID
);
3338 unsigned NumInstanceMethods
= 0;
3339 for (const ObjCMethodList
*Method
= &Methods
.Instance
; Method
;
3340 Method
= Method
->getNext())
3341 if (ShouldWriteMethodListNode(Method
))
3342 ++NumInstanceMethods
;
3344 unsigned NumFactoryMethods
= 0;
3345 for (const ObjCMethodList
*Method
= &Methods
.Factory
; Method
;
3346 Method
= Method
->getNext())
3347 if (ShouldWriteMethodListNode(Method
))
3348 ++NumFactoryMethods
;
3350 unsigned InstanceBits
= Methods
.Instance
.getBits();
3351 assert(InstanceBits
< 4);
3352 unsigned InstanceHasMoreThanOneDeclBit
=
3353 Methods
.Instance
.hasMoreThanOneDecl();
3354 unsigned FullInstanceBits
= (NumInstanceMethods
<< 3) |
3355 (InstanceHasMoreThanOneDeclBit
<< 2) |
3357 unsigned FactoryBits
= Methods
.Factory
.getBits();
3358 assert(FactoryBits
< 4);
3359 unsigned FactoryHasMoreThanOneDeclBit
=
3360 Methods
.Factory
.hasMoreThanOneDecl();
3361 unsigned FullFactoryBits
= (NumFactoryMethods
<< 3) |
3362 (FactoryHasMoreThanOneDeclBit
<< 2) |
3364 LE
.write
<uint16_t>(FullInstanceBits
);
3365 LE
.write
<uint16_t>(FullFactoryBits
);
3366 for (const ObjCMethodList
*Method
= &Methods
.Instance
; Method
;
3367 Method
= Method
->getNext())
3368 if (ShouldWriteMethodListNode(Method
))
3369 LE
.write
<uint32_t>(Writer
.getDeclID(Method
->getMethod()));
3370 for (const ObjCMethodList
*Method
= &Methods
.Factory
; Method
;
3371 Method
= Method
->getNext())
3372 if (ShouldWriteMethodListNode(Method
))
3373 LE
.write
<uint32_t>(Writer
.getDeclID(Method
->getMethod()));
3375 assert(Out
.tell() - Start
== DataLen
&& "Data length is wrong");
3379 static bool ShouldWriteMethodListNode(const ObjCMethodList
*Node
) {
3380 return (Node
->getMethod() && !Node
->getMethod()->isFromASTFile());
3386 /// Write ObjC data: selectors and the method pool.
3388 /// The method pool contains both instance and factory methods, stored
3389 /// in an on-disk hash table indexed by the selector. The hash table also
3390 /// contains an empty entry for every other selector known to Sema.
3391 void ASTWriter::WriteSelectors(Sema
&SemaRef
) {
3392 using namespace llvm
;
3394 // Do we have to do anything at all?
3395 if (SemaRef
.MethodPool
.empty() && SelectorIDs
.empty())
3397 unsigned NumTableEntries
= 0;
3398 // Create and write out the blob that contains selectors and the method pool.
3400 llvm::OnDiskChainedHashTableGenerator
<ASTMethodPoolTrait
> Generator
;
3401 ASTMethodPoolTrait
Trait(*this);
3403 // Create the on-disk hash table representation. We walk through every
3404 // selector we've seen and look it up in the method pool.
3405 SelectorOffsets
.resize(NextSelectorID
- FirstSelectorID
);
3406 for (auto &SelectorAndID
: SelectorIDs
) {
3407 Selector S
= SelectorAndID
.first
;
3408 SelectorID ID
= SelectorAndID
.second
;
3409 Sema::GlobalMethodPool::iterator F
= SemaRef
.MethodPool
.find(S
);
3410 ASTMethodPoolTrait::data_type Data
= {
3415 if (F
!= SemaRef
.MethodPool
.end()) {
3416 Data
.Instance
= F
->second
.first
;
3417 Data
.Factory
= F
->second
.second
;
3419 // Only write this selector if it's not in an existing AST or something
3421 if (Chain
&& ID
< FirstSelectorID
) {
3422 // Selector already exists. Did it change?
3423 bool changed
= false;
3424 for (ObjCMethodList
*M
= &Data
.Instance
; M
&& M
->getMethod();
3426 if (!M
->getMethod()->isFromASTFile()) {
3432 for (ObjCMethodList
*M
= &Data
.Factory
; M
&& M
->getMethod();
3434 if (!M
->getMethod()->isFromASTFile()) {
3442 } else if (Data
.Instance
.getMethod() || Data
.Factory
.getMethod()) {
3443 // A new method pool entry.
3446 Generator
.insert(S
, Data
, Trait
);
3449 // Create the on-disk hash table in a buffer.
3450 SmallString
<4096> MethodPool
;
3451 uint32_t BucketOffset
;
3453 using namespace llvm::support
;
3455 ASTMethodPoolTrait
Trait(*this);
3456 llvm::raw_svector_ostream
Out(MethodPool
);
3457 // Make sure that no bucket is at offset 0
3458 endian::write
<uint32_t>(Out
, 0, llvm::endianness::little
);
3459 BucketOffset
= Generator
.Emit(Out
, Trait
);
3462 // Create a blob abbreviation
3463 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3464 Abbrev
->Add(BitCodeAbbrevOp(METHOD_POOL
));
3465 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
3466 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
3467 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
3468 unsigned MethodPoolAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
3470 // Write the method pool
3472 RecordData::value_type Record
[] = {METHOD_POOL
, BucketOffset
,
3474 Stream
.EmitRecordWithBlob(MethodPoolAbbrev
, Record
, MethodPool
);
3477 // Create a blob abbreviation for the selector table offsets.
3478 Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3479 Abbrev
->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS
));
3480 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // size
3481 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // first ID
3482 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
3483 unsigned SelectorOffsetAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
3485 // Write the selector offsets table.
3487 RecordData::value_type Record
[] = {
3488 SELECTOR_OFFSETS
, SelectorOffsets
.size(),
3489 FirstSelectorID
- NUM_PREDEF_SELECTOR_IDS
};
3490 Stream
.EmitRecordWithBlob(SelectorOffsetAbbrev
, Record
,
3491 bytes(SelectorOffsets
));
3496 /// Write the selectors referenced in @selector expression into AST file.
3497 void ASTWriter::WriteReferencedSelectorsPool(Sema
&SemaRef
) {
3498 using namespace llvm
;
3500 if (SemaRef
.ReferencedSelectors
.empty())
3504 ASTRecordWriter
Writer(*this, Record
);
3506 // Note: this writes out all references even for a dependent AST. But it is
3507 // very tricky to fix, and given that @selector shouldn't really appear in
3508 // headers, probably not worth it. It's not a correctness issue.
3509 for (auto &SelectorAndLocation
: SemaRef
.ReferencedSelectors
) {
3510 Selector Sel
= SelectorAndLocation
.first
;
3511 SourceLocation Loc
= SelectorAndLocation
.second
;
3512 Writer
.AddSelectorRef(Sel
);
3513 Writer
.AddSourceLocation(Loc
);
3515 Writer
.Emit(REFERENCED_SELECTOR_POOL
);
3518 //===----------------------------------------------------------------------===//
3519 // Identifier Table Serialization
3520 //===----------------------------------------------------------------------===//
3522 /// Determine the declaration that should be put into the name lookup table to
3523 /// represent the given declaration in this module. This is usually D itself,
3524 /// but if D was imported and merged into a local declaration, we want the most
3525 /// recent local declaration instead. The chosen declaration will be the most
3526 /// recent declaration in any module that imports this one.
3527 static NamedDecl
*getDeclForLocalLookup(const LangOptions
&LangOpts
,
3529 if (!LangOpts
.Modules
|| !D
->isFromASTFile())
3532 if (Decl
*Redecl
= D
->getPreviousDecl()) {
3533 // For Redeclarable decls, a prior declaration might be local.
3534 for (; Redecl
; Redecl
= Redecl
->getPreviousDecl()) {
3535 // If we find a local decl, we're done.
3536 if (!Redecl
->isFromASTFile()) {
3537 // Exception: in very rare cases (for injected-class-names), not all
3538 // redeclarations are in the same semantic context. Skip ones in a
3539 // different context. They don't go in this lookup table at all.
3540 if (!Redecl
->getDeclContext()->getRedeclContext()->Equals(
3541 D
->getDeclContext()->getRedeclContext()))
3543 return cast
<NamedDecl
>(Redecl
);
3546 // If we find a decl from a (chained-)PCH stop since we won't find a
3548 if (Redecl
->getOwningModuleID() == 0)
3551 } else if (Decl
*First
= D
->getCanonicalDecl()) {
3552 // For Mergeable decls, the first decl might be local.
3553 if (!First
->isFromASTFile())
3554 return cast
<NamedDecl
>(First
);
3557 // All declarations are imported. Our most recent declaration will also be
3558 // the most recent one in anyone who imports us.
3564 class ASTIdentifierTableTrait
{
3567 IdentifierResolver
&IdResolver
;
3570 ASTWriter::RecordData
*InterestingIdentifierOffsets
;
3572 /// Determines whether this is an "interesting" identifier that needs a
3573 /// full IdentifierInfo structure written into the hash table. Notably, this
3574 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3576 bool isInterestingIdentifier(const IdentifierInfo
*II
, uint64_t MacroOffset
) {
3577 if (MacroOffset
|| II
->isPoisoned() ||
3578 (!IsModule
&& II
->getObjCOrBuiltinID()) ||
3579 II
->hasRevertedTokenIDToIdentifier() ||
3580 (NeedDecls
&& II
->getFETokenInfo()))
3587 using key_type
= IdentifierInfo
*;
3588 using key_type_ref
= key_type
;
3590 using data_type
= IdentID
;
3591 using data_type_ref
= data_type
;
3593 using hash_value_type
= unsigned;
3594 using offset_type
= unsigned;
3596 ASTIdentifierTableTrait(ASTWriter
&Writer
, Preprocessor
&PP
,
3597 IdentifierResolver
&IdResolver
, bool IsModule
,
3598 ASTWriter::RecordData
*InterestingIdentifierOffsets
)
3599 : Writer(Writer
), PP(PP
), IdResolver(IdResolver
), IsModule(IsModule
),
3600 NeedDecls(!IsModule
|| !Writer
.getLangOpts().CPlusPlus
),
3601 InterestingIdentifierOffsets(InterestingIdentifierOffsets
) {}
3603 bool needDecls() const { return NeedDecls
; }
3605 static hash_value_type
ComputeHash(const IdentifierInfo
* II
) {
3606 return llvm::djbHash(II
->getName());
3609 bool isInterestingIdentifier(const IdentifierInfo
*II
) {
3610 auto MacroOffset
= Writer
.getMacroDirectivesOffset(II
);
3611 return isInterestingIdentifier(II
, MacroOffset
);
3614 bool isInterestingNonMacroIdentifier(const IdentifierInfo
*II
) {
3615 return isInterestingIdentifier(II
, 0);
3618 std::pair
<unsigned, unsigned>
3619 EmitKeyDataLength(raw_ostream
& Out
, IdentifierInfo
* II
, IdentID ID
) {
3620 // Record the location of the identifier data. This is used when generating
3621 // the mapping from persistent IDs to strings.
3622 Writer
.SetIdentifierOffset(II
, Out
.tell());
3624 auto MacroOffset
= Writer
.getMacroDirectivesOffset(II
);
3626 // Emit the offset of the key/data length information to the interesting
3627 // identifiers table if necessary.
3628 if (InterestingIdentifierOffsets
&&
3629 isInterestingIdentifier(II
, MacroOffset
))
3630 InterestingIdentifierOffsets
->push_back(Out
.tell());
3632 unsigned KeyLen
= II
->getLength() + 1;
3633 unsigned DataLen
= 4; // 4 bytes for the persistent ID << 1
3634 if (isInterestingIdentifier(II
, MacroOffset
)) {
3635 DataLen
+= 2; // 2 bytes for builtin ID
3636 DataLen
+= 2; // 2 bytes for flags
3638 DataLen
+= 4; // MacroDirectives offset.
3641 DataLen
+= std::distance(IdResolver
.begin(II
), IdResolver
.end()) * 4;
3643 return emitULEBKeyDataLength(KeyLen
, DataLen
, Out
);
3646 void EmitKey(raw_ostream
& Out
, const IdentifierInfo
* II
,
3648 Out
.write(II
->getNameStart(), KeyLen
);
3651 void EmitData(raw_ostream
& Out
, IdentifierInfo
* II
,
3652 IdentID ID
, unsigned) {
3653 using namespace llvm::support
;
3655 endian::Writer
LE(Out
, llvm::endianness::little
);
3657 auto MacroOffset
= Writer
.getMacroDirectivesOffset(II
);
3658 if (!isInterestingIdentifier(II
, MacroOffset
)) {
3659 LE
.write
<uint32_t>(ID
<< 1);
3663 LE
.write
<uint32_t>((ID
<< 1) | 0x01);
3664 uint32_t Bits
= (uint32_t)II
->getObjCOrBuiltinID();
3665 assert((Bits
& 0xffff) == Bits
&& "ObjCOrBuiltinID too big for ASTReader.");
3666 LE
.write
<uint16_t>(Bits
);
3668 bool HadMacroDefinition
= MacroOffset
!= 0;
3669 Bits
= (Bits
<< 1) | unsigned(HadMacroDefinition
);
3670 Bits
= (Bits
<< 1) | unsigned(II
->isExtensionToken());
3671 Bits
= (Bits
<< 1) | unsigned(II
->isPoisoned());
3672 Bits
= (Bits
<< 1) | unsigned(II
->hasRevertedTokenIDToIdentifier());
3673 Bits
= (Bits
<< 1) | unsigned(II
->isCPlusPlusOperatorKeyword());
3674 LE
.write
<uint16_t>(Bits
);
3676 if (HadMacroDefinition
)
3677 LE
.write
<uint32_t>(MacroOffset
);
3680 // Emit the declaration IDs in reverse order, because the
3681 // IdentifierResolver provides the declarations as they would be
3682 // visible (e.g., the function "stat" would come before the struct
3683 // "stat"), but the ASTReader adds declarations to the end of the list
3684 // (so we need to see the struct "stat" before the function "stat").
3685 // Only emit declarations that aren't from a chained PCH, though.
3686 SmallVector
<NamedDecl
*, 16> Decls(IdResolver
.decls(II
));
3687 for (NamedDecl
*D
: llvm::reverse(Decls
))
3689 Writer
.getDeclID(getDeclForLocalLookup(PP
.getLangOpts(), D
)));
3696 /// Write the identifier table into the AST file.
3698 /// The identifier table consists of a blob containing string data
3699 /// (the actual identifiers themselves) and a separate "offsets" index
3700 /// that maps identifier IDs to locations within the blob.
3701 void ASTWriter::WriteIdentifierTable(Preprocessor
&PP
,
3702 IdentifierResolver
&IdResolver
,
3704 using namespace llvm
;
3706 RecordData InterestingIdents
;
3708 // Create and write out the blob that contains the identifier
3711 llvm::OnDiskChainedHashTableGenerator
<ASTIdentifierTableTrait
> Generator
;
3712 ASTIdentifierTableTrait
Trait(*this, PP
, IdResolver
, IsModule
,
3713 IsModule
? &InterestingIdents
: nullptr);
3715 // Look for any identifiers that were named while processing the
3716 // headers, but are otherwise not needed. We add these to the hash
3717 // table to enable checking of the predefines buffer in the case
3718 // where the user adds new macro definitions when building the AST
3720 SmallVector
<const IdentifierInfo
*, 128> IIs
;
3721 for (const auto &ID
: PP
.getIdentifierTable())
3722 if (Trait
.isInterestingNonMacroIdentifier(ID
.second
))
3723 IIs
.push_back(ID
.second
);
3724 // Sort the identifiers lexicographically before getting the references so
3725 // that their order is stable.
3726 llvm::sort(IIs
, llvm::deref
<std::less
<>>());
3727 for (const IdentifierInfo
*II
: IIs
)
3728 getIdentifierRef(II
);
3730 // Create the on-disk hash table representation. We only store offsets
3731 // for identifiers that appear here for the first time.
3732 IdentifierOffsets
.resize(NextIdentID
- FirstIdentID
);
3733 for (auto IdentIDPair
: IdentifierIDs
) {
3734 auto *II
= const_cast<IdentifierInfo
*>(IdentIDPair
.first
);
3735 IdentID ID
= IdentIDPair
.second
;
3736 assert(II
&& "NULL identifier in identifier table");
3737 // Write out identifiers if either the ID is local or the identifier has
3738 // changed since it was loaded.
3739 if (ID
>= FirstIdentID
|| !Chain
|| !II
->isFromAST()
3740 || II
->hasChangedSinceDeserialization() ||
3741 (Trait
.needDecls() &&
3742 II
->hasFETokenInfoChangedSinceDeserialization()))
3743 Generator
.insert(II
, ID
, Trait
);
3746 // Create the on-disk hash table in a buffer.
3747 SmallString
<4096> IdentifierTable
;
3748 uint32_t BucketOffset
;
3750 using namespace llvm::support
;
3752 llvm::raw_svector_ostream
Out(IdentifierTable
);
3753 // Make sure that no bucket is at offset 0
3754 endian::write
<uint32_t>(Out
, 0, llvm::endianness::little
);
3755 BucketOffset
= Generator
.Emit(Out
, Trait
);
3758 // Create a blob abbreviation
3759 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3760 Abbrev
->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE
));
3761 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
3762 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
3763 unsigned IDTableAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
3765 // Write the identifier table
3766 RecordData::value_type Record
[] = {IDENTIFIER_TABLE
, BucketOffset
};
3767 Stream
.EmitRecordWithBlob(IDTableAbbrev
, Record
, IdentifierTable
);
3770 // Write the offsets table for identifier IDs.
3771 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
3772 Abbrev
->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET
));
3773 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // # of identifiers
3774 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32)); // first ID
3775 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
3776 unsigned IdentifierOffsetAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
3779 for (unsigned I
= 0, N
= IdentifierOffsets
.size(); I
!= N
; ++I
)
3780 assert(IdentifierOffsets
[I
] && "Missing identifier offset?");
3783 RecordData::value_type Record
[] = {IDENTIFIER_OFFSET
,
3784 IdentifierOffsets
.size(),
3785 FirstIdentID
- NUM_PREDEF_IDENT_IDS
};
3786 Stream
.EmitRecordWithBlob(IdentifierOffsetAbbrev
, Record
,
3787 bytes(IdentifierOffsets
));
3789 // In C++, write the list of interesting identifiers (those that are
3790 // defined as macros, poisoned, or similar unusual things).
3791 if (!InterestingIdents
.empty())
3792 Stream
.EmitRecord(INTERESTING_IDENTIFIERS
, InterestingIdents
);
3795 //===----------------------------------------------------------------------===//
3796 // DeclContext's Name Lookup Table Serialization
3797 //===----------------------------------------------------------------------===//
3801 // Trait used for the on-disk hash table used in the method pool.
3802 class ASTDeclContextNameLookupTrait
{
3804 llvm::SmallVector
<DeclID
, 64> DeclIDs
;
3807 using key_type
= DeclarationNameKey
;
3808 using key_type_ref
= key_type
;
3810 /// A start and end index into DeclIDs, representing a sequence of decls.
3811 using data_type
= std::pair
<unsigned, unsigned>;
3812 using data_type_ref
= const data_type
&;
3814 using hash_value_type
= unsigned;
3815 using offset_type
= unsigned;
3817 explicit ASTDeclContextNameLookupTrait(ASTWriter
&Writer
) : Writer(Writer
) {}
3819 template<typename Coll
>
3820 data_type
getData(const Coll
&Decls
) {
3821 unsigned Start
= DeclIDs
.size();
3822 for (NamedDecl
*D
: Decls
) {
3824 Writer
.GetDeclRef(getDeclForLocalLookup(Writer
.getLangOpts(), D
)));
3826 return std::make_pair(Start
, DeclIDs
.size());
3829 data_type
ImportData(const reader::ASTDeclContextNameLookupTrait::data_type
&FromReader
) {
3830 unsigned Start
= DeclIDs
.size();
3831 llvm::append_range(DeclIDs
, FromReader
);
3832 return std::make_pair(Start
, DeclIDs
.size());
3835 static bool EqualKey(key_type_ref a
, key_type_ref b
) {
3839 hash_value_type
ComputeHash(DeclarationNameKey Name
) {
3840 return Name
.getHash();
3843 void EmitFileRef(raw_ostream
&Out
, ModuleFile
*F
) const {
3844 assert(Writer
.hasChain() &&
3845 "have reference to loaded module file but no chain?");
3847 using namespace llvm::support
;
3849 endian::write
<uint32_t>(Out
, Writer
.getChain()->getModuleFileID(F
),
3850 llvm::endianness::little
);
3853 std::pair
<unsigned, unsigned> EmitKeyDataLength(raw_ostream
&Out
,
3854 DeclarationNameKey Name
,
3855 data_type_ref Lookup
) {
3856 unsigned KeyLen
= 1;
3857 switch (Name
.getKind()) {
3858 case DeclarationName::Identifier
:
3859 case DeclarationName::ObjCZeroArgSelector
:
3860 case DeclarationName::ObjCOneArgSelector
:
3861 case DeclarationName::ObjCMultiArgSelector
:
3862 case DeclarationName::CXXLiteralOperatorName
:
3863 case DeclarationName::CXXDeductionGuideName
:
3866 case DeclarationName::CXXOperatorName
:
3869 case DeclarationName::CXXConstructorName
:
3870 case DeclarationName::CXXDestructorName
:
3871 case DeclarationName::CXXConversionFunctionName
:
3872 case DeclarationName::CXXUsingDirective
:
3876 // 4 bytes for each DeclID.
3877 unsigned DataLen
= 4 * (Lookup
.second
- Lookup
.first
);
3879 return emitULEBKeyDataLength(KeyLen
, DataLen
, Out
);
3882 void EmitKey(raw_ostream
&Out
, DeclarationNameKey Name
, unsigned) {
3883 using namespace llvm::support
;
3885 endian::Writer
LE(Out
, llvm::endianness::little
);
3886 LE
.write
<uint8_t>(Name
.getKind());
3887 switch (Name
.getKind()) {
3888 case DeclarationName::Identifier
:
3889 case DeclarationName::CXXLiteralOperatorName
:
3890 case DeclarationName::CXXDeductionGuideName
:
3891 LE
.write
<uint32_t>(Writer
.getIdentifierRef(Name
.getIdentifier()));
3893 case DeclarationName::ObjCZeroArgSelector
:
3894 case DeclarationName::ObjCOneArgSelector
:
3895 case DeclarationName::ObjCMultiArgSelector
:
3896 LE
.write
<uint32_t>(Writer
.getSelectorRef(Name
.getSelector()));
3898 case DeclarationName::CXXOperatorName
:
3899 assert(Name
.getOperatorKind() < NUM_OVERLOADED_OPERATORS
&&
3900 "Invalid operator?");
3901 LE
.write
<uint8_t>(Name
.getOperatorKind());
3903 case DeclarationName::CXXConstructorName
:
3904 case DeclarationName::CXXDestructorName
:
3905 case DeclarationName::CXXConversionFunctionName
:
3906 case DeclarationName::CXXUsingDirective
:
3910 llvm_unreachable("Invalid name kind?");
3913 void EmitData(raw_ostream
&Out
, key_type_ref
, data_type Lookup
,
3915 using namespace llvm::support
;
3917 endian::Writer
LE(Out
, llvm::endianness::little
);
3918 uint64_t Start
= Out
.tell(); (void)Start
;
3919 for (unsigned I
= Lookup
.first
, N
= Lookup
.second
; I
!= N
; ++I
)
3920 LE
.write
<uint32_t>(DeclIDs
[I
]);
3921 assert(Out
.tell() - Start
== DataLen
&& "Data length is wrong");
3927 bool ASTWriter::isLookupResultExternal(StoredDeclsList
&Result
,
3929 return Result
.hasExternalDecls() &&
3930 DC
->hasNeedToReconcileExternalVisibleStorage();
3933 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList
&Result
,
3935 for (auto *D
: Result
.getLookupResult())
3936 if (!getDeclForLocalLookup(getLangOpts(), D
)->isFromASTFile())
3943 ASTWriter::GenerateNameLookupTable(const DeclContext
*ConstDC
,
3944 llvm::SmallVectorImpl
<char> &LookupTable
) {
3945 assert(!ConstDC
->hasLazyLocalLexicalLookups() &&
3946 !ConstDC
->hasLazyExternalLexicalLookups() &&
3947 "must call buildLookups first");
3949 // FIXME: We need to build the lookups table, which is logically const.
3950 auto *DC
= const_cast<DeclContext
*>(ConstDC
);
3951 assert(DC
== DC
->getPrimaryContext() && "only primary DC has lookup table");
3953 // Create the on-disk hash table representation.
3954 MultiOnDiskHashTableGenerator
<reader::ASTDeclContextNameLookupTrait
,
3955 ASTDeclContextNameLookupTrait
> Generator
;
3956 ASTDeclContextNameLookupTrait
Trait(*this);
3958 // The first step is to collect the declaration names which we need to
3959 // serialize into the name lookup table, and to collect them in a stable
3961 SmallVector
<DeclarationName
, 16> Names
;
3963 // We also build up small sets of the constructor and conversion function
3964 // names which are visible.
3965 llvm::SmallPtrSet
<DeclarationName
, 8> ConstructorNameSet
, ConversionNameSet
;
3967 for (auto &Lookup
: *DC
->buildLookup()) {
3968 auto &Name
= Lookup
.first
;
3969 auto &Result
= Lookup
.second
;
3971 // If there are no local declarations in our lookup result, we
3972 // don't need to write an entry for the name at all. If we can't
3973 // write out a lookup set without performing more deserialization,
3974 // just skip this entry.
3975 if (isLookupResultExternal(Result
, DC
) &&
3976 isLookupResultEntirelyExternal(Result
, DC
))
3979 // We also skip empty results. If any of the results could be external and
3980 // the currently available results are empty, then all of the results are
3981 // external and we skip it above. So the only way we get here with an empty
3982 // results is when no results could have been external *and* we have
3983 // external results.
3985 // FIXME: While we might want to start emitting on-disk entries for negative
3986 // lookups into a decl context as an optimization, today we *have* to skip
3987 // them because there are names with empty lookup results in decl contexts
3988 // which we can't emit in any stable ordering: we lookup constructors and
3989 // conversion functions in the enclosing namespace scope creating empty
3990 // results for them. This in almost certainly a bug in Clang's name lookup,
3991 // but that is likely to be hard or impossible to fix and so we tolerate it
3992 // here by omitting lookups with empty results.
3993 if (Lookup
.second
.getLookupResult().empty())
3996 switch (Lookup
.first
.getNameKind()) {
3998 Names
.push_back(Lookup
.first
);
4001 case DeclarationName::CXXConstructorName
:
4002 assert(isa
<CXXRecordDecl
>(DC
) &&
4003 "Cannot have a constructor name outside of a class!");
4004 ConstructorNameSet
.insert(Name
);
4007 case DeclarationName::CXXConversionFunctionName
:
4008 assert(isa
<CXXRecordDecl
>(DC
) &&
4009 "Cannot have a conversion function name outside of a class!");
4010 ConversionNameSet
.insert(Name
);
4015 // Sort the names into a stable order.
4018 if (auto *D
= dyn_cast
<CXXRecordDecl
>(DC
)) {
4019 // We need to establish an ordering of constructor and conversion function
4020 // names, and they don't have an intrinsic ordering.
4022 // First we try the easy case by forming the current context's constructor
4023 // name and adding that name first. This is a very useful optimization to
4024 // avoid walking the lexical declarations in many cases, and it also
4025 // handles the only case where a constructor name can come from some other
4026 // lexical context -- when that name is an implicit constructor merged from
4027 // another declaration in the redecl chain. Any non-implicit constructor or
4028 // conversion function which doesn't occur in all the lexical contexts
4029 // would be an ODR violation.
4030 auto ImplicitCtorName
= Context
->DeclarationNames
.getCXXConstructorName(
4031 Context
->getCanonicalType(Context
->getRecordType(D
)));
4032 if (ConstructorNameSet
.erase(ImplicitCtorName
))
4033 Names
.push_back(ImplicitCtorName
);
4035 // If we still have constructors or conversion functions, we walk all the
4036 // names in the decl and add the constructors and conversion functions
4037 // which are visible in the order they lexically occur within the context.
4038 if (!ConstructorNameSet
.empty() || !ConversionNameSet
.empty())
4039 for (Decl
*ChildD
: cast
<CXXRecordDecl
>(DC
)->decls())
4040 if (auto *ChildND
= dyn_cast
<NamedDecl
>(ChildD
)) {
4041 auto Name
= ChildND
->getDeclName();
4042 switch (Name
.getNameKind()) {
4046 case DeclarationName::CXXConstructorName
:
4047 if (ConstructorNameSet
.erase(Name
))
4048 Names
.push_back(Name
);
4051 case DeclarationName::CXXConversionFunctionName
:
4052 if (ConversionNameSet
.erase(Name
))
4053 Names
.push_back(Name
);
4057 if (ConstructorNameSet
.empty() && ConversionNameSet
.empty())
4061 assert(ConstructorNameSet
.empty() && "Failed to find all of the visible "
4062 "constructors by walking all the "
4063 "lexical members of the context.");
4064 assert(ConversionNameSet
.empty() && "Failed to find all of the visible "
4065 "conversion functions by walking all "
4066 "the lexical members of the context.");
4069 // Next we need to do a lookup with each name into this decl context to fully
4070 // populate any results from external sources. We don't actually use the
4071 // results of these lookups because we only want to use the results after all
4072 // results have been loaded and the pointers into them will be stable.
4073 for (auto &Name
: Names
)
4076 // Now we need to insert the results for each name into the hash table. For
4077 // constructor names and conversion function names, we actually need to merge
4078 // all of the results for them into one list of results each and insert
4080 SmallVector
<NamedDecl
*, 8> ConstructorDecls
;
4081 SmallVector
<NamedDecl
*, 8> ConversionDecls
;
4083 // Now loop over the names, either inserting them or appending for the two
4085 for (auto &Name
: Names
) {
4086 DeclContext::lookup_result Result
= DC
->noload_lookup(Name
);
4088 switch (Name
.getNameKind()) {
4090 Generator
.insert(Name
, Trait
.getData(Result
), Trait
);
4093 case DeclarationName::CXXConstructorName
:
4094 ConstructorDecls
.append(Result
.begin(), Result
.end());
4097 case DeclarationName::CXXConversionFunctionName
:
4098 ConversionDecls
.append(Result
.begin(), Result
.end());
4103 // Handle our two special cases if we ended up having any. We arbitrarily use
4104 // the first declaration's name here because the name itself isn't part of
4105 // the key, only the kind of name is used.
4106 if (!ConstructorDecls
.empty())
4107 Generator
.insert(ConstructorDecls
.front()->getDeclName(),
4108 Trait
.getData(ConstructorDecls
), Trait
);
4109 if (!ConversionDecls
.empty())
4110 Generator
.insert(ConversionDecls
.front()->getDeclName(),
4111 Trait
.getData(ConversionDecls
), Trait
);
4113 // Create the on-disk hash table. Also emit the existing imported and
4114 // merged table if there is one.
4115 auto *Lookups
= Chain
? Chain
->getLoadedLookupTables(DC
) : nullptr;
4116 Generator
.emit(LookupTable
, Trait
, Lookups
? &Lookups
->Table
: nullptr);
4119 /// Write the block containing all of the declaration IDs
4120 /// visible from the given DeclContext.
4122 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4123 /// bitstream, or 0 if no block was written.
4124 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext
&Context
,
4126 // If we imported a key declaration of this namespace, write the visible
4127 // lookup results as an update record for it rather than including them
4128 // on this declaration. We will only look at key declarations on reload.
4129 if (isa
<NamespaceDecl
>(DC
) && Chain
&&
4130 Chain
->getKeyDeclaration(cast
<Decl
>(DC
))->isFromASTFile()) {
4131 // Only do this once, for the first local declaration of the namespace.
4132 for (auto *Prev
= cast
<NamespaceDecl
>(DC
)->getPreviousDecl(); Prev
;
4133 Prev
= Prev
->getPreviousDecl())
4134 if (!Prev
->isFromASTFile())
4137 // Note that we need to emit an update record for the primary context.
4138 UpdatedDeclContexts
.insert(DC
->getPrimaryContext());
4140 // Make sure all visible decls are written. They will be recorded later. We
4141 // do this using a side data structure so we can sort the names into
4142 // a deterministic order.
4143 StoredDeclsMap
*Map
= DC
->getPrimaryContext()->buildLookup();
4144 SmallVector
<std::pair
<DeclarationName
, DeclContext::lookup_result
>, 16>
4147 LookupResults
.reserve(Map
->size());
4148 for (auto &Entry
: *Map
)
4149 LookupResults
.push_back(
4150 std::make_pair(Entry
.first
, Entry
.second
.getLookupResult()));
4153 llvm::sort(LookupResults
, llvm::less_first());
4154 for (auto &NameAndResult
: LookupResults
) {
4155 DeclarationName Name
= NameAndResult
.first
;
4156 DeclContext::lookup_result Result
= NameAndResult
.second
;
4157 if (Name
.getNameKind() == DeclarationName::CXXConstructorName
||
4158 Name
.getNameKind() == DeclarationName::CXXConversionFunctionName
) {
4159 // We have to work around a name lookup bug here where negative lookup
4160 // results for these names get cached in namespace lookup tables (these
4161 // names should never be looked up in a namespace).
4162 assert(Result
.empty() && "Cannot have a constructor or conversion "
4163 "function name in a namespace!");
4167 for (NamedDecl
*ND
: Result
)
4168 if (!ND
->isFromASTFile())
4175 if (DC
->getPrimaryContext() != DC
)
4178 // Skip contexts which don't support name lookup.
4179 if (!DC
->isLookupContext())
4182 // If not in C++, we perform name lookup for the translation unit via the
4183 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4184 if (DC
->isTranslationUnit() && !Context
.getLangOpts().CPlusPlus
)
4187 // Serialize the contents of the mapping used for lookup. Note that,
4188 // although we have two very different code paths, the serialized
4189 // representation is the same for both cases: a declaration name,
4190 // followed by a size, followed by references to the visible
4191 // declarations that have that name.
4192 uint64_t Offset
= Stream
.GetCurrentBitNo();
4193 StoredDeclsMap
*Map
= DC
->buildLookup();
4194 if (!Map
|| Map
->empty())
4197 // Create the on-disk hash table in a buffer.
4198 SmallString
<4096> LookupTable
;
4199 GenerateNameLookupTable(DC
, LookupTable
);
4201 // Write the lookup table
4202 RecordData::value_type Record
[] = {DECL_CONTEXT_VISIBLE
};
4203 Stream
.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev
, Record
,
4205 ++NumVisibleDeclContexts
;
4209 /// Write an UPDATE_VISIBLE block for the given context.
4211 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4212 /// DeclContext in a dependent AST file. As such, they only exist for the TU
4213 /// (in C++), for namespaces, and for classes with forward-declared unscoped
4214 /// enumeration members (in C++11).
4215 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext
*DC
) {
4216 StoredDeclsMap
*Map
= DC
->getLookupPtr();
4217 if (!Map
|| Map
->empty())
4220 // Create the on-disk hash table in a buffer.
4221 SmallString
<4096> LookupTable
;
4222 GenerateNameLookupTable(DC
, LookupTable
);
4224 // If we're updating a namespace, select a key declaration as the key for the
4225 // update record; those are the only ones that will be checked on reload.
4226 if (isa
<NamespaceDecl
>(DC
))
4227 DC
= cast
<DeclContext
>(Chain
->getKeyDeclaration(cast
<Decl
>(DC
)));
4229 // Write the lookup table
4230 RecordData::value_type Record
[] = {UPDATE_VISIBLE
, getDeclID(cast
<Decl
>(DC
))};
4231 Stream
.EmitRecordWithBlob(UpdateVisibleAbbrev
, Record
, LookupTable
);
4234 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4235 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride
&Opts
) {
4236 RecordData::value_type Record
[] = {Opts
.getAsOpaqueInt()};
4237 Stream
.EmitRecord(FP_PRAGMA_OPTIONS
, Record
);
4240 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4241 void ASTWriter::WriteOpenCLExtensions(Sema
&SemaRef
) {
4242 if (!SemaRef
.Context
.getLangOpts().OpenCL
)
4245 const OpenCLOptions
&Opts
= SemaRef
.getOpenCLOptions();
4247 for (const auto &I
:Opts
.OptMap
) {
4248 AddString(I
.getKey(), Record
);
4249 auto V
= I
.getValue();
4250 Record
.push_back(V
.Supported
? 1 : 0);
4251 Record
.push_back(V
.Enabled
? 1 : 0);
4252 Record
.push_back(V
.WithPragma
? 1 : 0);
4253 Record
.push_back(V
.Avail
);
4254 Record
.push_back(V
.Core
);
4255 Record
.push_back(V
.Opt
);
4257 Stream
.EmitRecord(OPENCL_EXTENSIONS
, Record
);
4259 void ASTWriter::WriteCUDAPragmas(Sema
&SemaRef
) {
4260 if (SemaRef
.ForceCUDAHostDeviceDepth
> 0) {
4261 RecordData::value_type Record
[] = {SemaRef
.ForceCUDAHostDeviceDepth
};
4262 Stream
.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH
, Record
);
4266 void ASTWriter::WriteObjCCategories() {
4267 SmallVector
<ObjCCategoriesInfo
, 2> CategoriesMap
;
4268 RecordData Categories
;
4270 for (unsigned I
= 0, N
= ObjCClassesWithCategories
.size(); I
!= N
; ++I
) {
4272 unsigned StartIndex
= Categories
.size();
4274 ObjCInterfaceDecl
*Class
= ObjCClassesWithCategories
[I
];
4276 // Allocate space for the size.
4277 Categories
.push_back(0);
4279 // Add the categories.
4280 for (ObjCInterfaceDecl::known_categories_iterator
4281 Cat
= Class
->known_categories_begin(),
4282 CatEnd
= Class
->known_categories_end();
4283 Cat
!= CatEnd
; ++Cat
, ++Size
) {
4284 assert(getDeclID(*Cat
) != 0 && "Bogus category");
4285 AddDeclRef(*Cat
, Categories
);
4289 Categories
[StartIndex
] = Size
;
4291 // Record this interface -> category map.
4292 ObjCCategoriesInfo CatInfo
= { getDeclID(Class
), StartIndex
};
4293 CategoriesMap
.push_back(CatInfo
);
4296 // Sort the categories map by the definition ID, since the reader will be
4297 // performing binary searches on this information.
4298 llvm::array_pod_sort(CategoriesMap
.begin(), CategoriesMap
.end());
4300 // Emit the categories map.
4301 using namespace llvm
;
4303 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
4304 Abbrev
->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP
));
4305 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // # of entries
4306 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
4307 unsigned AbbrevID
= Stream
.EmitAbbrev(std::move(Abbrev
));
4309 RecordData::value_type Record
[] = {OBJC_CATEGORIES_MAP
, CategoriesMap
.size()};
4310 Stream
.EmitRecordWithBlob(AbbrevID
, Record
,
4311 reinterpret_cast<char *>(CategoriesMap
.data()),
4312 CategoriesMap
.size() * sizeof(ObjCCategoriesInfo
));
4314 // Emit the category lists.
4315 Stream
.EmitRecord(OBJC_CATEGORIES
, Categories
);
4318 void ASTWriter::WriteLateParsedTemplates(Sema
&SemaRef
) {
4319 Sema::LateParsedTemplateMapT
&LPTMap
= SemaRef
.LateParsedTemplateMap
;
4325 for (auto &LPTMapEntry
: LPTMap
) {
4326 const FunctionDecl
*FD
= LPTMapEntry
.first
;
4327 LateParsedTemplate
&LPT
= *LPTMapEntry
.second
;
4328 AddDeclRef(FD
, Record
);
4329 AddDeclRef(LPT
.D
, Record
);
4330 Record
.push_back(LPT
.FPO
.getAsOpaqueInt());
4331 Record
.push_back(LPT
.Toks
.size());
4333 for (const auto &Tok
: LPT
.Toks
) {
4334 AddToken(Tok
, Record
);
4337 Stream
.EmitRecord(LATE_PARSED_TEMPLATE
, Record
);
4340 /// Write the state of 'pragma clang optimize' at the end of the module.
4341 void ASTWriter::WriteOptimizePragmaOptions(Sema
&SemaRef
) {
4343 SourceLocation PragmaLoc
= SemaRef
.getOptimizeOffPragmaLocation();
4344 AddSourceLocation(PragmaLoc
, Record
);
4345 Stream
.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS
, Record
);
4348 /// Write the state of 'pragma ms_struct' at the end of the module.
4349 void ASTWriter::WriteMSStructPragmaOptions(Sema
&SemaRef
) {
4351 Record
.push_back(SemaRef
.MSStructPragmaOn
? PMSST_ON
: PMSST_OFF
);
4352 Stream
.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS
, Record
);
4355 /// Write the state of 'pragma pointers_to_members' at the end of the
4357 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema
&SemaRef
) {
4359 Record
.push_back(SemaRef
.MSPointerToMemberRepresentationMethod
);
4360 AddSourceLocation(SemaRef
.ImplicitMSInheritanceAttrLoc
, Record
);
4361 Stream
.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS
, Record
);
4364 /// Write the state of 'pragma align/pack' at the end of the module.
4365 void ASTWriter::WritePackPragmaOptions(Sema
&SemaRef
) {
4366 // Don't serialize pragma align/pack state for modules, since it should only
4367 // take effect on a per-submodule basis.
4372 AddAlignPackInfo(SemaRef
.AlignPackStack
.CurrentValue
, Record
);
4373 AddSourceLocation(SemaRef
.AlignPackStack
.CurrentPragmaLocation
, Record
);
4374 Record
.push_back(SemaRef
.AlignPackStack
.Stack
.size());
4375 for (const auto &StackEntry
: SemaRef
.AlignPackStack
.Stack
) {
4376 AddAlignPackInfo(StackEntry
.Value
, Record
);
4377 AddSourceLocation(StackEntry
.PragmaLocation
, Record
);
4378 AddSourceLocation(StackEntry
.PragmaPushLocation
, Record
);
4379 AddString(StackEntry
.StackSlotLabel
, Record
);
4381 Stream
.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS
, Record
);
4384 /// Write the state of 'pragma float_control' at the end of the module.
4385 void ASTWriter::WriteFloatControlPragmaOptions(Sema
&SemaRef
) {
4386 // Don't serialize pragma float_control state for modules,
4387 // since it should only take effect on a per-submodule basis.
4392 Record
.push_back(SemaRef
.FpPragmaStack
.CurrentValue
.getAsOpaqueInt());
4393 AddSourceLocation(SemaRef
.FpPragmaStack
.CurrentPragmaLocation
, Record
);
4394 Record
.push_back(SemaRef
.FpPragmaStack
.Stack
.size());
4395 for (const auto &StackEntry
: SemaRef
.FpPragmaStack
.Stack
) {
4396 Record
.push_back(StackEntry
.Value
.getAsOpaqueInt());
4397 AddSourceLocation(StackEntry
.PragmaLocation
, Record
);
4398 AddSourceLocation(StackEntry
.PragmaPushLocation
, Record
);
4399 AddString(StackEntry
.StackSlotLabel
, Record
);
4401 Stream
.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS
, Record
);
4404 void ASTWriter::WriteModuleFileExtension(Sema
&SemaRef
,
4405 ModuleFileExtensionWriter
&Writer
) {
4406 // Enter the extension block.
4407 Stream
.EnterSubblock(EXTENSION_BLOCK_ID
, 4);
4409 // Emit the metadata record abbreviation.
4410 auto Abv
= std::make_shared
<llvm::BitCodeAbbrev
>();
4411 Abv
->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA
));
4412 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR
, 6));
4413 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR
, 6));
4414 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR
, 6));
4415 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR
, 6));
4416 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob
));
4417 unsigned Abbrev
= Stream
.EmitAbbrev(std::move(Abv
));
4419 // Emit the metadata record.
4421 auto Metadata
= Writer
.getExtension()->getExtensionMetadata();
4422 Record
.push_back(EXTENSION_METADATA
);
4423 Record
.push_back(Metadata
.MajorVersion
);
4424 Record
.push_back(Metadata
.MinorVersion
);
4425 Record
.push_back(Metadata
.BlockName
.size());
4426 Record
.push_back(Metadata
.UserInfo
.size());
4427 SmallString
<64> Buffer
;
4428 Buffer
+= Metadata
.BlockName
;
4429 Buffer
+= Metadata
.UserInfo
;
4430 Stream
.EmitRecordWithBlob(Abbrev
, Record
, Buffer
);
4432 // Emit the contents of the extension block.
4433 Writer
.writeExtensionContents(SemaRef
, Stream
);
4435 // Exit the extension block.
4439 //===----------------------------------------------------------------------===//
4440 // General Serialization Routines
4441 //===----------------------------------------------------------------------===//
4443 void ASTRecordWriter::AddAttr(const Attr
*A
) {
4444 auto &Record
= *this;
4445 // FIXME: Clang can't handle the serialization/deserialization of
4446 // preferred_name properly now. See
4447 // https://github.com/llvm/llvm-project/issues/56490 for example.
4448 if (!A
|| (isa
<PreferredNameAttr
>(A
) &&
4449 Writer
->isWritingStdCXXNamedModules()))
4450 return Record
.push_back(0);
4452 Record
.push_back(A
->getKind() + 1); // FIXME: stable encoding, target attrs
4454 Record
.AddIdentifierRef(A
->getAttrName());
4455 Record
.AddIdentifierRef(A
->getScopeName());
4456 Record
.AddSourceRange(A
->getRange());
4457 Record
.AddSourceLocation(A
->getScopeLoc());
4458 Record
.push_back(A
->getParsedKind());
4459 Record
.push_back(A
->getSyntax());
4460 Record
.push_back(A
->getAttributeSpellingListIndexRaw());
4461 Record
.push_back(A
->isRegularKeywordAttribute());
4463 #include "clang/Serialization/AttrPCHWrite.inc"
4466 /// Emit the list of attributes to the specified record.
4467 void ASTRecordWriter::AddAttributes(ArrayRef
<const Attr
*> Attrs
) {
4468 push_back(Attrs
.size());
4469 for (const auto *A
: Attrs
)
4473 void ASTWriter::AddToken(const Token
&Tok
, RecordDataImpl
&Record
) {
4474 AddSourceLocation(Tok
.getLocation(), Record
);
4475 // FIXME: Should translate token kind to a stable encoding.
4476 Record
.push_back(Tok
.getKind());
4477 // FIXME: Should translate token flags to a stable encoding.
4478 Record
.push_back(Tok
.getFlags());
4480 if (Tok
.isAnnotation()) {
4481 AddSourceLocation(Tok
.getAnnotationEndLoc(), Record
);
4482 switch (Tok
.getKind()) {
4483 case tok::annot_pragma_loop_hint
: {
4484 auto *Info
= static_cast<PragmaLoopHintInfo
*>(Tok
.getAnnotationValue());
4485 AddToken(Info
->PragmaName
, Record
);
4486 AddToken(Info
->Option
, Record
);
4487 Record
.push_back(Info
->Toks
.size());
4488 for (const auto &T
: Info
->Toks
)
4489 AddToken(T
, Record
);
4492 case tok::annot_pragma_pack
: {
4494 static_cast<Sema::PragmaPackInfo
*>(Tok
.getAnnotationValue());
4495 Record
.push_back(static_cast<unsigned>(Info
->Action
));
4496 AddString(Info
->SlotLabel
, Record
);
4497 AddToken(Info
->Alignment
, Record
);
4500 // Some annotation tokens do not use the PtrData field.
4501 case tok::annot_pragma_openmp
:
4502 case tok::annot_pragma_openmp_end
:
4503 case tok::annot_pragma_unused
:
4504 case tok::annot_pragma_openacc
:
4505 case tok::annot_pragma_openacc_end
:
4508 llvm_unreachable("missing serialization code for annotation token");
4511 Record
.push_back(Tok
.getLength());
4512 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4514 AddIdentifierRef(Tok
.getIdentifierInfo(), Record
);
4518 void ASTWriter::AddString(StringRef Str
, RecordDataImpl
&Record
) {
4519 Record
.push_back(Str
.size());
4520 Record
.insert(Record
.end(), Str
.begin(), Str
.end());
4523 bool ASTWriter::PreparePathForOutput(SmallVectorImpl
<char> &Path
) {
4524 assert(Context
&& "should have context when outputting path");
4526 // Leave special file names as they are.
4527 StringRef
PathStr(Path
.data(), Path
.size());
4528 if (PathStr
== "<built-in>" || PathStr
== "<command line>")
4532 cleanPathForOutput(Context
->getSourceManager().getFileManager(), Path
);
4534 // Remove a prefix to make the path relative, if relevant.
4535 const char *PathBegin
= Path
.data();
4536 const char *PathPtr
=
4537 adjustFilenameForRelocatableAST(PathBegin
, BaseDirectory
);
4538 if (PathPtr
!= PathBegin
) {
4539 Path
.erase(Path
.begin(), Path
.begin() + (PathPtr
- PathBegin
));
4546 void ASTWriter::AddPath(StringRef Path
, RecordDataImpl
&Record
) {
4547 SmallString
<128> FilePath(Path
);
4548 PreparePathForOutput(FilePath
);
4549 AddString(FilePath
, Record
);
4552 void ASTWriter::EmitRecordWithPath(unsigned Abbrev
, RecordDataRef Record
,
4554 SmallString
<128> FilePath(Path
);
4555 PreparePathForOutput(FilePath
);
4556 Stream
.EmitRecordWithBlob(Abbrev
, Record
, FilePath
);
4559 void ASTWriter::AddVersionTuple(const VersionTuple
&Version
,
4560 RecordDataImpl
&Record
) {
4561 Record
.push_back(Version
.getMajor());
4562 if (std::optional
<unsigned> Minor
= Version
.getMinor())
4563 Record
.push_back(*Minor
+ 1);
4565 Record
.push_back(0);
4566 if (std::optional
<unsigned> Subminor
= Version
.getSubminor())
4567 Record
.push_back(*Subminor
+ 1);
4569 Record
.push_back(0);
4572 /// Note that the identifier II occurs at the given offset
4573 /// within the identifier table.
4574 void ASTWriter::SetIdentifierOffset(const IdentifierInfo
*II
, uint32_t Offset
) {
4575 IdentID ID
= IdentifierIDs
[II
];
4576 // Only store offsets new to this AST file. Other identifier names are looked
4577 // up earlier in the chain and thus don't need an offset.
4578 if (ID
>= FirstIdentID
)
4579 IdentifierOffsets
[ID
- FirstIdentID
] = Offset
;
4582 /// Note that the selector Sel occurs at the given offset
4583 /// within the method pool/selector table.
4584 void ASTWriter::SetSelectorOffset(Selector Sel
, uint32_t Offset
) {
4585 unsigned ID
= SelectorIDs
[Sel
];
4586 assert(ID
&& "Unknown selector");
4587 // Don't record offsets for selectors that are also available in a different
4589 if (ID
< FirstSelectorID
)
4591 SelectorOffsets
[ID
- FirstSelectorID
] = Offset
;
4594 ASTWriter::ASTWriter(llvm::BitstreamWriter
&Stream
,
4595 SmallVectorImpl
<char> &Buffer
,
4596 InMemoryModuleCache
&ModuleCache
,
4597 ArrayRef
<std::shared_ptr
<ModuleFileExtension
>> Extensions
,
4598 bool IncludeTimestamps
, bool BuildingImplicitModule
)
4599 : Stream(Stream
), Buffer(Buffer
), ModuleCache(ModuleCache
),
4600 IncludeTimestamps(IncludeTimestamps
),
4601 BuildingImplicitModule(BuildingImplicitModule
) {
4602 for (const auto &Ext
: Extensions
) {
4603 if (auto Writer
= Ext
->createExtensionWriter(*this))
4604 ModuleFileExtensionWriters
.push_back(std::move(Writer
));
4608 ASTWriter::~ASTWriter() = default;
4610 const LangOptions
&ASTWriter::getLangOpts() const {
4611 assert(WritingAST
&& "can't determine lang opts when not writing AST");
4612 return Context
->getLangOpts();
4615 time_t ASTWriter::getTimestampForOutput(const FileEntry
*E
) const {
4616 return IncludeTimestamps
? E
->getModificationTime() : 0;
4619 ASTFileSignature
ASTWriter::WriteAST(Sema
&SemaRef
, StringRef OutputFile
,
4620 Module
*WritingModule
, StringRef isysroot
,
4621 bool ShouldCacheASTInMemory
) {
4622 llvm::TimeTraceScope
scope("WriteAST", OutputFile
);
4625 ASTHasCompilerErrors
=
4626 SemaRef
.PP
.getDiagnostics().hasUncompilableErrorOccurred();
4628 // Emit the file header.
4629 Stream
.Emit((unsigned)'C', 8);
4630 Stream
.Emit((unsigned)'P', 8);
4631 Stream
.Emit((unsigned)'C', 8);
4632 Stream
.Emit((unsigned)'H', 8);
4634 WriteBlockInfoBlock();
4636 Context
= &SemaRef
.Context
;
4638 this->WritingModule
= WritingModule
;
4639 ASTFileSignature Signature
= WriteASTCore(SemaRef
, isysroot
, WritingModule
);
4642 this->WritingModule
= nullptr;
4643 this->BaseDirectory
.clear();
4646 if (ShouldCacheASTInMemory
) {
4647 // Construct MemoryBuffer and update buffer manager.
4648 ModuleCache
.addBuiltPCM(OutputFile
,
4649 llvm::MemoryBuffer::getMemBufferCopy(
4650 StringRef(Buffer
.begin(), Buffer
.size())));
4655 template<typename Vector
>
4656 static void AddLazyVectorDecls(ASTWriter
&Writer
, Vector
&Vec
,
4657 ASTWriter::RecordData
&Record
) {
4658 for (typename
Vector::iterator I
= Vec
.begin(nullptr, true), E
= Vec
.end();
4660 Writer
.AddDeclRef(*I
, Record
);
4664 void ASTWriter::collectNonAffectingInputFiles() {
4665 SourceManager
&SrcMgr
= PP
->getSourceManager();
4666 unsigned N
= SrcMgr
.local_sloc_entry_size();
4668 IsSLocAffecting
.resize(N
, true);
4673 auto AffectingModuleMaps
= GetAffectingModuleMaps(*PP
, WritingModule
);
4675 unsigned FileIDAdjustment
= 0;
4676 unsigned OffsetAdjustment
= 0;
4678 NonAffectingFileIDAdjustments
.reserve(N
);
4679 NonAffectingOffsetAdjustments
.reserve(N
);
4681 NonAffectingFileIDAdjustments
.push_back(FileIDAdjustment
);
4682 NonAffectingOffsetAdjustments
.push_back(OffsetAdjustment
);
4684 for (unsigned I
= 1; I
!= N
; ++I
) {
4685 const SrcMgr::SLocEntry
*SLoc
= &SrcMgr
.getLocalSLocEntry(I
);
4686 FileID FID
= FileID::get(I
);
4687 assert(&SrcMgr
.getSLocEntry(FID
) == SLoc
);
4689 if (!SLoc
->isFile())
4691 const SrcMgr::FileInfo
&File
= SLoc
->getFile();
4692 const SrcMgr::ContentCache
*Cache
= &File
.getContentCache();
4693 if (!Cache
->OrigEntry
)
4696 if (!isModuleMap(File
.getFileCharacteristic()) ||
4697 AffectingModuleMaps
.empty() ||
4698 llvm::is_contained(AffectingModuleMaps
, *Cache
->OrigEntry
))
4701 IsSLocAffecting
[I
] = false;
4703 FileIDAdjustment
+= 1;
4704 // Even empty files take up one element in the offset table.
4705 OffsetAdjustment
+= SrcMgr
.getFileIDSize(FID
) + 1;
4707 // If the previous file was non-affecting as well, just extend its entry
4708 // with our information.
4709 if (!NonAffectingFileIDs
.empty() &&
4710 NonAffectingFileIDs
.back().ID
== FID
.ID
- 1) {
4711 NonAffectingFileIDs
.back() = FID
;
4712 NonAffectingRanges
.back().setEnd(SrcMgr
.getLocForEndOfFile(FID
));
4713 NonAffectingFileIDAdjustments
.back() = FileIDAdjustment
;
4714 NonAffectingOffsetAdjustments
.back() = OffsetAdjustment
;
4718 NonAffectingFileIDs
.push_back(FID
);
4719 NonAffectingRanges
.emplace_back(SrcMgr
.getLocForStartOfFile(FID
),
4720 SrcMgr
.getLocForEndOfFile(FID
));
4721 NonAffectingFileIDAdjustments
.push_back(FileIDAdjustment
);
4722 NonAffectingOffsetAdjustments
.push_back(OffsetAdjustment
);
4726 ASTFileSignature
ASTWriter::WriteASTCore(Sema
&SemaRef
, StringRef isysroot
,
4727 Module
*WritingModule
) {
4728 using namespace llvm
;
4730 bool isModule
= WritingModule
!= nullptr;
4732 // Make sure that the AST reader knows to finalize itself.
4734 Chain
->finalizeForWriting();
4736 ASTContext
&Context
= SemaRef
.Context
;
4737 Preprocessor
&PP
= SemaRef
.PP
;
4739 // This needs to be done very early, since everything that writes
4740 // SourceLocations or FileIDs depends on it.
4741 collectNonAffectingInputFiles();
4743 writeUnhashedControlBlock(PP
, Context
);
4745 // Set up predefined declaration IDs.
4746 auto RegisterPredefDecl
= [&] (Decl
*D
, PredefinedDeclIDs ID
) {
4748 assert(D
->isCanonicalDecl() && "predefined decl is not canonical");
4752 RegisterPredefDecl(Context
.getTranslationUnitDecl(),
4753 PREDEF_DECL_TRANSLATION_UNIT_ID
);
4754 RegisterPredefDecl(Context
.ObjCIdDecl
, PREDEF_DECL_OBJC_ID_ID
);
4755 RegisterPredefDecl(Context
.ObjCSelDecl
, PREDEF_DECL_OBJC_SEL_ID
);
4756 RegisterPredefDecl(Context
.ObjCClassDecl
, PREDEF_DECL_OBJC_CLASS_ID
);
4757 RegisterPredefDecl(Context
.ObjCProtocolClassDecl
,
4758 PREDEF_DECL_OBJC_PROTOCOL_ID
);
4759 RegisterPredefDecl(Context
.Int128Decl
, PREDEF_DECL_INT_128_ID
);
4760 RegisterPredefDecl(Context
.UInt128Decl
, PREDEF_DECL_UNSIGNED_INT_128_ID
);
4761 RegisterPredefDecl(Context
.ObjCInstanceTypeDecl
,
4762 PREDEF_DECL_OBJC_INSTANCETYPE_ID
);
4763 RegisterPredefDecl(Context
.BuiltinVaListDecl
, PREDEF_DECL_BUILTIN_VA_LIST_ID
);
4764 RegisterPredefDecl(Context
.VaListTagDecl
, PREDEF_DECL_VA_LIST_TAG
);
4765 RegisterPredefDecl(Context
.BuiltinMSVaListDecl
,
4766 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID
);
4767 RegisterPredefDecl(Context
.MSGuidTagDecl
,
4768 PREDEF_DECL_BUILTIN_MS_GUID_ID
);
4769 RegisterPredefDecl(Context
.ExternCContext
, PREDEF_DECL_EXTERN_C_CONTEXT_ID
);
4770 RegisterPredefDecl(Context
.MakeIntegerSeqDecl
,
4771 PREDEF_DECL_MAKE_INTEGER_SEQ_ID
);
4772 RegisterPredefDecl(Context
.CFConstantStringTypeDecl
,
4773 PREDEF_DECL_CF_CONSTANT_STRING_ID
);
4774 RegisterPredefDecl(Context
.CFConstantStringTagDecl
,
4775 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID
);
4776 RegisterPredefDecl(Context
.TypePackElementDecl
,
4777 PREDEF_DECL_TYPE_PACK_ELEMENT_ID
);
4779 // Build a record containing all of the tentative definitions in this file, in
4780 // TentativeDefinitions order. Generally, this record will be empty for
4782 RecordData TentativeDefinitions
;
4783 AddLazyVectorDecls(*this, SemaRef
.TentativeDefinitions
, TentativeDefinitions
);
4785 // Build a record containing all of the file scoped decls in this file.
4786 RecordData UnusedFileScopedDecls
;
4788 AddLazyVectorDecls(*this, SemaRef
.UnusedFileScopedDecls
,
4789 UnusedFileScopedDecls
);
4791 // Build a record containing all of the delegating constructors we still need
4793 RecordData DelegatingCtorDecls
;
4795 AddLazyVectorDecls(*this, SemaRef
.DelegatingCtorDecls
, DelegatingCtorDecls
);
4797 // Write the set of weak, undeclared identifiers. We always write the
4798 // entire table, since later PCH files in a PCH chain are only interested in
4799 // the results at the end of the chain.
4800 RecordData WeakUndeclaredIdentifiers
;
4801 for (const auto &WeakUndeclaredIdentifierList
:
4802 SemaRef
.WeakUndeclaredIdentifiers
) {
4803 const IdentifierInfo
*const II
= WeakUndeclaredIdentifierList
.first
;
4804 for (const auto &WI
: WeakUndeclaredIdentifierList
.second
) {
4805 AddIdentifierRef(II
, WeakUndeclaredIdentifiers
);
4806 AddIdentifierRef(WI
.getAlias(), WeakUndeclaredIdentifiers
);
4807 AddSourceLocation(WI
.getLocation(), WeakUndeclaredIdentifiers
);
4811 // Build a record containing all of the ext_vector declarations.
4812 RecordData ExtVectorDecls
;
4813 AddLazyVectorDecls(*this, SemaRef
.ExtVectorDecls
, ExtVectorDecls
);
4815 // Build a record containing all of the VTable uses information.
4816 RecordData VTableUses
;
4817 if (!SemaRef
.VTableUses
.empty()) {
4818 for (unsigned I
= 0, N
= SemaRef
.VTableUses
.size(); I
!= N
; ++I
) {
4819 AddDeclRef(SemaRef
.VTableUses
[I
].first
, VTableUses
);
4820 AddSourceLocation(SemaRef
.VTableUses
[I
].second
, VTableUses
);
4821 VTableUses
.push_back(SemaRef
.VTablesUsed
[SemaRef
.VTableUses
[I
].first
]);
4825 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4826 RecordData UnusedLocalTypedefNameCandidates
;
4827 for (const TypedefNameDecl
*TD
: SemaRef
.UnusedLocalTypedefNameCandidates
)
4828 AddDeclRef(TD
, UnusedLocalTypedefNameCandidates
);
4830 // Build a record containing all of pending implicit instantiations.
4831 RecordData PendingInstantiations
;
4832 for (const auto &I
: SemaRef
.PendingInstantiations
) {
4833 AddDeclRef(I
.first
, PendingInstantiations
);
4834 AddSourceLocation(I
.second
, PendingInstantiations
);
4836 assert(SemaRef
.PendingLocalImplicitInstantiations
.empty() &&
4837 "There are local ones at end of translation unit!");
4839 // Build a record containing some declaration references.
4840 RecordData SemaDeclRefs
;
4841 if (SemaRef
.StdNamespace
|| SemaRef
.StdBadAlloc
|| SemaRef
.StdAlignValT
) {
4842 AddDeclRef(SemaRef
.getStdNamespace(), SemaDeclRefs
);
4843 AddDeclRef(SemaRef
.getStdBadAlloc(), SemaDeclRefs
);
4844 AddDeclRef(SemaRef
.getStdAlignValT(), SemaDeclRefs
);
4847 RecordData CUDASpecialDeclRefs
;
4848 if (Context
.getcudaConfigureCallDecl()) {
4849 AddDeclRef(Context
.getcudaConfigureCallDecl(), CUDASpecialDeclRefs
);
4852 // Build a record containing all of the known namespaces.
4853 RecordData KnownNamespaces
;
4854 for (const auto &I
: SemaRef
.KnownNamespaces
) {
4856 AddDeclRef(I
.first
, KnownNamespaces
);
4859 // Build a record of all used, undefined objects that require definitions.
4860 RecordData UndefinedButUsed
;
4862 SmallVector
<std::pair
<NamedDecl
*, SourceLocation
>, 16> Undefined
;
4863 SemaRef
.getUndefinedButUsed(Undefined
);
4864 for (const auto &I
: Undefined
) {
4865 AddDeclRef(I
.first
, UndefinedButUsed
);
4866 AddSourceLocation(I
.second
, UndefinedButUsed
);
4869 // Build a record containing all delete-expressions that we would like to
4870 // analyze later in AST.
4871 RecordData DeleteExprsToAnalyze
;
4874 for (const auto &DeleteExprsInfo
:
4875 SemaRef
.getMismatchingDeleteExpressions()) {
4876 AddDeclRef(DeleteExprsInfo
.first
, DeleteExprsToAnalyze
);
4877 DeleteExprsToAnalyze
.push_back(DeleteExprsInfo
.second
.size());
4878 for (const auto &DeleteLoc
: DeleteExprsInfo
.second
) {
4879 AddSourceLocation(DeleteLoc
.first
, DeleteExprsToAnalyze
);
4880 DeleteExprsToAnalyze
.push_back(DeleteLoc
.second
);
4885 // Write the control block
4886 WriteControlBlock(PP
, Context
, isysroot
);
4888 // Write the remaining AST contents.
4889 Stream
.FlushToWord();
4890 ASTBlockRange
.first
= Stream
.GetCurrentBitNo() >> 3;
4891 Stream
.EnterSubblock(AST_BLOCK_ID
, 5);
4892 ASTBlockStartOffset
= Stream
.GetCurrentBitNo();
4894 // This is so that older clang versions, before the introduction
4895 // of the control block, can read and reject the newer PCH format.
4897 RecordData Record
= {VERSION_MAJOR
};
4898 Stream
.EmitRecord(METADATA_OLD_FORMAT
, Record
);
4901 // Create a lexical update block containing all of the declarations in the
4902 // translation unit that do not come from other AST files.
4903 const TranslationUnitDecl
*TU
= Context
.getTranslationUnitDecl();
4904 SmallVector
<uint32_t, 128> NewGlobalKindDeclPairs
;
4905 for (const auto *D
: TU
->noload_decls()) {
4906 if (!D
->isFromASTFile()) {
4907 NewGlobalKindDeclPairs
.push_back(D
->getKind());
4908 NewGlobalKindDeclPairs
.push_back(GetDeclRef(D
));
4912 auto Abv
= std::make_shared
<BitCodeAbbrev
>();
4913 Abv
->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL
));
4914 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob
));
4915 unsigned TuUpdateLexicalAbbrev
= Stream
.EmitAbbrev(std::move(Abv
));
4917 RecordData::value_type Record
[] = {TU_UPDATE_LEXICAL
};
4918 Stream
.EmitRecordWithBlob(TuUpdateLexicalAbbrev
, Record
,
4919 bytes(NewGlobalKindDeclPairs
));
4922 // And a visible updates block for the translation unit.
4923 Abv
= std::make_shared
<BitCodeAbbrev
>();
4924 Abv
->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE
));
4925 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR
, 6));
4926 Abv
->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob
));
4927 UpdateVisibleAbbrev
= Stream
.EmitAbbrev(std::move(Abv
));
4928 WriteDeclContextVisibleUpdate(TU
);
4930 // If we have any extern "C" names, write out a visible update for them.
4931 if (Context
.ExternCContext
)
4932 WriteDeclContextVisibleUpdate(Context
.ExternCContext
);
4934 // If the translation unit has an anonymous namespace, and we don't already
4935 // have an update block for it, write it as an update block.
4936 // FIXME: Why do we not do this if there's already an update block?
4937 if (NamespaceDecl
*NS
= TU
->getAnonymousNamespace()) {
4938 ASTWriter::UpdateRecord
&Record
= DeclUpdates
[TU
];
4940 Record
.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE
, NS
));
4943 // Add update records for all mangling numbers and static local numbers.
4944 // These aren't really update records, but this is a convenient way of
4945 // tagging this rare extra data onto the declarations.
4946 for (const auto &Number
: Context
.MangleNumbers
)
4947 if (!Number
.first
->isFromASTFile())
4948 DeclUpdates
[Number
.first
].push_back(DeclUpdate(UPD_MANGLING_NUMBER
,
4950 for (const auto &Number
: Context
.StaticLocalNumbers
)
4951 if (!Number
.first
->isFromASTFile())
4952 DeclUpdates
[Number
.first
].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER
,
4955 // Make sure visible decls, added to DeclContexts previously loaded from
4956 // an AST file, are registered for serialization. Likewise for template
4957 // specializations added to imported templates.
4958 for (const auto *I
: DeclsToEmitEvenIfUnreferenced
) {
4962 // Make sure all decls associated with an identifier are registered for
4963 // serialization, if we're storing decls with identifiers.
4964 if (!WritingModule
|| !getLangOpts().CPlusPlus
) {
4965 llvm::SmallVector
<const IdentifierInfo
*, 256> IIs
;
4966 for (const auto &ID
: PP
.getIdentifierTable()) {
4967 const IdentifierInfo
*II
= ID
.second
;
4968 if (!Chain
|| !II
->isFromAST() || II
->hasChangedSinceDeserialization())
4971 // Sort the identifiers to visit based on their name.
4972 llvm::sort(IIs
, llvm::deref
<std::less
<>>());
4973 for (const IdentifierInfo
*II
: IIs
)
4974 for (const Decl
*D
: SemaRef
.IdResolver
.decls(II
))
4978 // For method pool in the module, if it contains an entry for a selector,
4979 // the entry should be complete, containing everything introduced by that
4980 // module and all modules it imports. It's possible that the entry is out of
4981 // date, so we need to pull in the new content here.
4983 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4984 // safe, we copy all selectors out.
4985 llvm::SmallVector
<Selector
, 256> AllSelectors
;
4986 for (auto &SelectorAndID
: SelectorIDs
)
4987 AllSelectors
.push_back(SelectorAndID
.first
);
4988 for (auto &Selector
: AllSelectors
)
4989 SemaRef
.updateOutOfDateSelector(Selector
);
4991 // Form the record of special types.
4992 RecordData SpecialTypes
;
4993 AddTypeRef(Context
.getRawCFConstantStringType(), SpecialTypes
);
4994 AddTypeRef(Context
.getFILEType(), SpecialTypes
);
4995 AddTypeRef(Context
.getjmp_bufType(), SpecialTypes
);
4996 AddTypeRef(Context
.getsigjmp_bufType(), SpecialTypes
);
4997 AddTypeRef(Context
.ObjCIdRedefinitionType
, SpecialTypes
);
4998 AddTypeRef(Context
.ObjCClassRedefinitionType
, SpecialTypes
);
4999 AddTypeRef(Context
.ObjCSelRedefinitionType
, SpecialTypes
);
5000 AddTypeRef(Context
.getucontext_tType(), SpecialTypes
);
5003 // Write the mapping information describing our module dependencies and how
5004 // each of those modules were mapped into our own offset/ID space, so that
5005 // the reader can build the appropriate mapping to its own offset/ID space.
5006 // The map consists solely of a blob with the following format:
5008 // module-name-len:i16 module-name:len*i8
5009 // source-location-offset:i32
5010 // identifier-id:i32
5011 // preprocessed-entity-id:i32
5012 // macro-definition-id:i32
5015 // declaration-id:i32
5016 // c++-base-specifiers-id:i32
5019 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5020 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5021 // module name. Otherwise, it is the module file name.
5022 auto Abbrev
= std::make_shared
<BitCodeAbbrev
>();
5023 Abbrev
->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP
));
5024 Abbrev
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
5025 unsigned ModuleOffsetMapAbbrev
= Stream
.EmitAbbrev(std::move(Abbrev
));
5026 SmallString
<2048> Buffer
;
5028 llvm::raw_svector_ostream
Out(Buffer
);
5029 for (ModuleFile
&M
: Chain
->ModuleMgr
) {
5030 using namespace llvm::support
;
5032 endian::Writer
LE(Out
, llvm::endianness::little
);
5033 LE
.write
<uint8_t>(static_cast<uint8_t>(M
.Kind
));
5034 StringRef Name
= M
.isModule() ? M
.ModuleName
: M
.FileName
;
5035 LE
.write
<uint16_t>(Name
.size());
5036 Out
.write(Name
.data(), Name
.size());
5038 // Note: if a base ID was uint max, it would not be possible to load
5039 // another module after it or have more than one entity inside it.
5040 uint32_t None
= std::numeric_limits
<uint32_t>::max();
5042 auto writeBaseIDOrNone
= [&](auto BaseID
, bool ShouldWrite
) {
5043 assert(BaseID
< std::numeric_limits
<uint32_t>::max() && "base id too high");
5045 LE
.write
<uint32_t>(BaseID
);
5047 LE
.write
<uint32_t>(None
);
5050 // These values should be unique within a chain, since they will be read
5051 // as keys into ContinuousRangeMaps.
5052 writeBaseIDOrNone(M
.SLocEntryBaseOffset
, M
.LocalNumSLocEntries
);
5053 writeBaseIDOrNone(M
.BaseIdentifierID
, M
.LocalNumIdentifiers
);
5054 writeBaseIDOrNone(M
.BaseMacroID
, M
.LocalNumMacros
);
5055 writeBaseIDOrNone(M
.BasePreprocessedEntityID
,
5056 M
.NumPreprocessedEntities
);
5057 writeBaseIDOrNone(M
.BaseSubmoduleID
, M
.LocalNumSubmodules
);
5058 writeBaseIDOrNone(M
.BaseSelectorID
, M
.LocalNumSelectors
);
5059 writeBaseIDOrNone(M
.BaseDeclID
, M
.LocalNumDecls
);
5060 writeBaseIDOrNone(M
.BaseTypeIndex
, M
.LocalNumTypes
);
5063 RecordData::value_type Record
[] = {MODULE_OFFSET_MAP
};
5064 Stream
.EmitRecordWithBlob(ModuleOffsetMapAbbrev
, Record
,
5065 Buffer
.data(), Buffer
.size());
5068 // Build a record containing all of the DeclsToCheckForDeferredDiags.
5069 SmallVector
<serialization::DeclID
, 64> DeclsToCheckForDeferredDiags
;
5070 for (auto *D
: SemaRef
.DeclsToCheckForDeferredDiags
)
5071 DeclsToCheckForDeferredDiags
.push_back(GetDeclRef(D
));
5073 RecordData DeclUpdatesOffsetsRecord
;
5075 // Keep writing types, declarations, and declaration update records
5076 // until we've emitted all of them.
5077 Stream
.EnterSubblock(DECLTYPES_BLOCK_ID
, /*bits for abbreviations*/5);
5078 DeclTypesBlockStartOffset
= Stream
.GetCurrentBitNo();
5082 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord
);
5083 while (!DeclTypesToEmit
.empty()) {
5084 DeclOrType DOT
= DeclTypesToEmit
.front();
5085 DeclTypesToEmit
.pop();
5087 WriteType(DOT
.getType());
5089 WriteDecl(Context
, DOT
.getDecl());
5091 } while (!DeclUpdates
.empty());
5094 DoneWritingDeclsAndTypes
= true;
5096 // These things can only be done once we've written out decls and types.
5097 WriteTypeDeclOffsets();
5098 if (!DeclUpdatesOffsetsRecord
.empty())
5099 Stream
.EmitRecord(DECL_UPDATE_OFFSETS
, DeclUpdatesOffsetsRecord
);
5100 WriteFileDeclIDsMap();
5101 WriteSourceManagerBlock(Context
.getSourceManager(), PP
);
5103 WritePreprocessor(PP
, isModule
);
5104 WriteHeaderSearch(PP
.getHeaderSearchInfo());
5105 WriteSelectors(SemaRef
);
5106 WriteReferencedSelectorsPool(SemaRef
);
5107 WriteLateParsedTemplates(SemaRef
);
5108 WriteIdentifierTable(PP
, SemaRef
.IdResolver
, isModule
);
5109 WriteFPPragmaOptions(SemaRef
.CurFPFeatureOverrides());
5110 WriteOpenCLExtensions(SemaRef
);
5111 WriteCUDAPragmas(SemaRef
);
5113 // If we're emitting a module, write out the submodule information.
5115 WriteSubmodules(WritingModule
);
5117 Stream
.EmitRecord(SPECIAL_TYPES
, SpecialTypes
);
5119 // Write the record containing external, unnamed definitions.
5120 if (!EagerlyDeserializedDecls
.empty())
5121 Stream
.EmitRecord(EAGERLY_DESERIALIZED_DECLS
, EagerlyDeserializedDecls
);
5123 if (!ModularCodegenDecls
.empty())
5124 Stream
.EmitRecord(MODULAR_CODEGEN_DECLS
, ModularCodegenDecls
);
5126 // Write the record containing tentative definitions.
5127 if (!TentativeDefinitions
.empty())
5128 Stream
.EmitRecord(TENTATIVE_DEFINITIONS
, TentativeDefinitions
);
5130 // Write the record containing unused file scoped decls.
5131 if (!UnusedFileScopedDecls
.empty())
5132 Stream
.EmitRecord(UNUSED_FILESCOPED_DECLS
, UnusedFileScopedDecls
);
5134 // Write the record containing weak undeclared identifiers.
5135 if (!WeakUndeclaredIdentifiers
.empty())
5136 Stream
.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS
,
5137 WeakUndeclaredIdentifiers
);
5139 // Write the record containing ext_vector type names.
5140 if (!ExtVectorDecls
.empty())
5141 Stream
.EmitRecord(EXT_VECTOR_DECLS
, ExtVectorDecls
);
5143 // Write the record containing VTable uses information.
5144 if (!VTableUses
.empty())
5145 Stream
.EmitRecord(VTABLE_USES
, VTableUses
);
5147 // Write the record containing potentially unused local typedefs.
5148 if (!UnusedLocalTypedefNameCandidates
.empty())
5149 Stream
.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES
,
5150 UnusedLocalTypedefNameCandidates
);
5152 // Write the record containing pending implicit instantiations.
5153 if (!PendingInstantiations
.empty())
5154 Stream
.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS
, PendingInstantiations
);
5156 // Write the record containing declaration references of Sema.
5157 if (!SemaDeclRefs
.empty())
5158 Stream
.EmitRecord(SEMA_DECL_REFS
, SemaDeclRefs
);
5160 // Write the record containing decls to be checked for deferred diags.
5161 if (!DeclsToCheckForDeferredDiags
.empty())
5162 Stream
.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS
,
5163 DeclsToCheckForDeferredDiags
);
5165 // Write the record containing CUDA-specific declaration references.
5166 if (!CUDASpecialDeclRefs
.empty())
5167 Stream
.EmitRecord(CUDA_SPECIAL_DECL_REFS
, CUDASpecialDeclRefs
);
5169 // Write the delegating constructors.
5170 if (!DelegatingCtorDecls
.empty())
5171 Stream
.EmitRecord(DELEGATING_CTORS
, DelegatingCtorDecls
);
5173 // Write the known namespaces.
5174 if (!KnownNamespaces
.empty())
5175 Stream
.EmitRecord(KNOWN_NAMESPACES
, KnownNamespaces
);
5177 // Write the undefined internal functions and variables, and inline functions.
5178 if (!UndefinedButUsed
.empty())
5179 Stream
.EmitRecord(UNDEFINED_BUT_USED
, UndefinedButUsed
);
5181 if (!DeleteExprsToAnalyze
.empty())
5182 Stream
.EmitRecord(DELETE_EXPRS_TO_ANALYZE
, DeleteExprsToAnalyze
);
5184 // Write the visible updates to DeclContexts.
5185 for (auto *DC
: UpdatedDeclContexts
)
5186 WriteDeclContextVisibleUpdate(DC
);
5188 if (!WritingModule
) {
5189 // Write the submodules that were imported, if any.
5193 ModuleInfo(uint64_t ID
, Module
*M
) : ID(ID
), M(M
) {}
5195 llvm::SmallVector
<ModuleInfo
, 64> Imports
;
5196 for (const auto *I
: Context
.local_imports()) {
5197 assert(SubmoduleIDs
.contains(I
->getImportedModule()));
5198 Imports
.push_back(ModuleInfo(SubmoduleIDs
[I
->getImportedModule()],
5199 I
->getImportedModule()));
5202 if (!Imports
.empty()) {
5203 auto Cmp
= [](const ModuleInfo
&A
, const ModuleInfo
&B
) {
5206 auto Eq
= [](const ModuleInfo
&A
, const ModuleInfo
&B
) {
5207 return A
.ID
== B
.ID
;
5210 // Sort and deduplicate module IDs.
5211 llvm::sort(Imports
, Cmp
);
5212 Imports
.erase(std::unique(Imports
.begin(), Imports
.end(), Eq
),
5215 RecordData ImportedModules
;
5216 for (const auto &Import
: Imports
) {
5217 ImportedModules
.push_back(Import
.ID
);
5218 // FIXME: If the module has macros imported then later has declarations
5219 // imported, this location won't be the right one as a location for the
5220 // declaration imports.
5221 AddSourceLocation(PP
.getModuleImportLoc(Import
.M
), ImportedModules
);
5224 Stream
.EmitRecord(IMPORTED_MODULES
, ImportedModules
);
5228 WriteObjCCategories();
5229 if(!WritingModule
) {
5230 WriteOptimizePragmaOptions(SemaRef
);
5231 WriteMSStructPragmaOptions(SemaRef
);
5232 WriteMSPointersToMembersPragmaOptions(SemaRef
);
5234 WritePackPragmaOptions(SemaRef
);
5235 WriteFloatControlPragmaOptions(SemaRef
);
5237 // Some simple statistics
5238 RecordData::value_type Record
[] = {
5239 NumStatements
, NumMacros
, NumLexicalDeclContexts
, NumVisibleDeclContexts
};
5240 Stream
.EmitRecord(STATISTICS
, Record
);
5242 Stream
.FlushToWord();
5243 ASTBlockRange
.second
= Stream
.GetCurrentBitNo() >> 3;
5245 // Write the module file extension blocks.
5246 for (const auto &ExtWriter
: ModuleFileExtensionWriters
)
5247 WriteModuleFileExtension(SemaRef
, *ExtWriter
);
5249 return backpatchSignature();
5252 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl
&OffsetsRecord
) {
5253 if (DeclUpdates
.empty())
5256 DeclUpdateMap LocalUpdates
;
5257 LocalUpdates
.swap(DeclUpdates
);
5259 for (auto &DeclUpdate
: LocalUpdates
) {
5260 const Decl
*D
= DeclUpdate
.first
;
5262 bool HasUpdatedBody
= false;
5263 bool HasAddedVarDefinition
= false;
5264 RecordData RecordData
;
5265 ASTRecordWriter
Record(*this, RecordData
);
5266 for (auto &Update
: DeclUpdate
.second
) {
5267 DeclUpdateKind Kind
= (DeclUpdateKind
)Update
.getKind();
5269 // An updated body is emitted last, so that the reader doesn't need
5270 // to skip over the lazy body to reach statements for other records.
5271 if (Kind
== UPD_CXX_ADDED_FUNCTION_DEFINITION
)
5272 HasUpdatedBody
= true;
5273 else if (Kind
== UPD_CXX_ADDED_VAR_DEFINITION
)
5274 HasAddedVarDefinition
= true;
5276 Record
.push_back(Kind
);
5279 case UPD_CXX_ADDED_IMPLICIT_MEMBER
:
5280 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION
:
5281 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE
:
5282 assert(Update
.getDecl() && "no decl to add?");
5283 Record
.push_back(GetDeclRef(Update
.getDecl()));
5286 case UPD_CXX_ADDED_FUNCTION_DEFINITION
:
5287 case UPD_CXX_ADDED_VAR_DEFINITION
:
5290 case UPD_CXX_POINT_OF_INSTANTIATION
:
5291 // FIXME: Do we need to also save the template specialization kind here?
5292 Record
.AddSourceLocation(Update
.getLoc());
5295 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT
:
5296 Record
.AddStmt(const_cast<Expr
*>(
5297 cast
<ParmVarDecl
>(Update
.getDecl())->getDefaultArg()));
5300 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER
:
5302 cast
<FieldDecl
>(Update
.getDecl())->getInClassInitializer());
5305 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION
: {
5306 auto *RD
= cast
<CXXRecordDecl
>(D
);
5307 UpdatedDeclContexts
.insert(RD
->getPrimaryContext());
5308 Record
.push_back(RD
->isParamDestroyedInCallee());
5309 Record
.push_back(llvm::to_underlying(RD
->getArgPassingRestrictions()));
5310 Record
.AddCXXDefinitionData(RD
);
5311 Record
.AddOffset(WriteDeclContextLexicalBlock(
5312 *Context
, const_cast<CXXRecordDecl
*>(RD
)));
5314 // This state is sometimes updated by template instantiation, when we
5315 // switch from the specialization referring to the template declaration
5316 // to it referring to the template definition.
5317 if (auto *MSInfo
= RD
->getMemberSpecializationInfo()) {
5318 Record
.push_back(MSInfo
->getTemplateSpecializationKind());
5319 Record
.AddSourceLocation(MSInfo
->getPointOfInstantiation());
5321 auto *Spec
= cast
<ClassTemplateSpecializationDecl
>(RD
);
5322 Record
.push_back(Spec
->getTemplateSpecializationKind());
5323 Record
.AddSourceLocation(Spec
->getPointOfInstantiation());
5325 // The instantiation might have been resolved to a partial
5326 // specialization. If so, record which one.
5327 auto From
= Spec
->getInstantiatedFrom();
5328 if (auto PartialSpec
=
5329 From
.dyn_cast
<ClassTemplatePartialSpecializationDecl
*>()) {
5330 Record
.push_back(true);
5331 Record
.AddDeclRef(PartialSpec
);
5332 Record
.AddTemplateArgumentList(
5333 &Spec
->getTemplateInstantiationArgs());
5335 Record
.push_back(false);
5338 Record
.push_back(llvm::to_underlying(RD
->getTagKind()));
5339 Record
.AddSourceLocation(RD
->getLocation());
5340 Record
.AddSourceLocation(RD
->getBeginLoc());
5341 Record
.AddSourceRange(RD
->getBraceRange());
5343 // Instantiation may change attributes; write them all out afresh.
5344 Record
.push_back(D
->hasAttrs());
5346 Record
.AddAttributes(D
->getAttrs());
5348 // FIXME: Ensure we don't get here for explicit instantiations.
5352 case UPD_CXX_RESOLVED_DTOR_DELETE
:
5353 Record
.AddDeclRef(Update
.getDecl());
5354 Record
.AddStmt(cast
<CXXDestructorDecl
>(D
)->getOperatorDeleteThisArg());
5357 case UPD_CXX_RESOLVED_EXCEPTION_SPEC
: {
5359 cast
<FunctionDecl
>(D
)->getType()->castAs
<FunctionProtoType
>();
5360 Record
.writeExceptionSpecInfo(prototype
->getExceptionSpecInfo());
5364 case UPD_CXX_DEDUCED_RETURN_TYPE
:
5365 Record
.push_back(GetOrCreateTypeID(Update
.getType()));
5368 case UPD_DECL_MARKED_USED
:
5371 case UPD_MANGLING_NUMBER
:
5372 case UPD_STATIC_LOCAL_NUMBER
:
5373 Record
.push_back(Update
.getNumber());
5376 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE
:
5377 Record
.AddSourceRange(
5378 D
->getAttr
<OMPThreadPrivateDeclAttr
>()->getRange());
5381 case UPD_DECL_MARKED_OPENMP_ALLOCATE
: {
5382 auto *A
= D
->getAttr
<OMPAllocateDeclAttr
>();
5383 Record
.push_back(A
->getAllocatorType());
5384 Record
.AddStmt(A
->getAllocator());
5385 Record
.AddStmt(A
->getAlignment());
5386 Record
.AddSourceRange(A
->getRange());
5390 case UPD_DECL_MARKED_OPENMP_DECLARETARGET
:
5391 Record
.push_back(D
->getAttr
<OMPDeclareTargetDeclAttr
>()->getMapType());
5392 Record
.AddSourceRange(
5393 D
->getAttr
<OMPDeclareTargetDeclAttr
>()->getRange());
5396 case UPD_DECL_EXPORTED
:
5397 Record
.push_back(getSubmoduleID(Update
.getModule()));
5400 case UPD_ADDED_ATTR_TO_RECORD
:
5401 Record
.AddAttributes(llvm::ArrayRef(Update
.getAttr()));
5406 // Add a trailing update record, if any. These must go last because we
5407 // lazily load their attached statement.
5408 if (HasUpdatedBody
) {
5409 const auto *Def
= cast
<FunctionDecl
>(D
);
5410 Record
.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION
);
5411 Record
.push_back(Def
->isInlined());
5412 Record
.AddSourceLocation(Def
->getInnerLocStart());
5413 Record
.AddFunctionDefinition(Def
);
5414 } else if (HasAddedVarDefinition
) {
5415 const auto *VD
= cast
<VarDecl
>(D
);
5416 Record
.push_back(UPD_CXX_ADDED_VAR_DEFINITION
);
5417 Record
.push_back(VD
->isInline());
5418 Record
.push_back(VD
->isInlineSpecified());
5419 Record
.AddVarDeclInit(VD
);
5422 OffsetsRecord
.push_back(GetDeclRef(D
));
5423 OffsetsRecord
.push_back(Record
.Emit(DECL_UPDATES
));
5427 void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo
&Info
,
5428 RecordDataImpl
&Record
) {
5429 uint32_t Raw
= Sema::AlignPackInfo::getRawEncoding(Info
);
5430 Record
.push_back(Raw
);
5433 FileID
ASTWriter::getAdjustedFileID(FileID FID
) const {
5434 if (FID
.isInvalid() || PP
->getSourceManager().isLoadedFileID(FID
) ||
5435 NonAffectingFileIDs
.empty())
5437 auto It
= llvm::lower_bound(NonAffectingFileIDs
, FID
);
5438 unsigned Idx
= std::distance(NonAffectingFileIDs
.begin(), It
);
5439 unsigned Offset
= NonAffectingFileIDAdjustments
[Idx
];
5440 return FileID::get(FID
.getOpaqueValue() - Offset
);
5443 unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID
) const {
5444 unsigned NumCreatedFIDs
= PP
->getSourceManager()
5445 .getLocalSLocEntry(FID
.ID
)
5449 unsigned AdjustedNumCreatedFIDs
= 0;
5450 for (unsigned I
= FID
.ID
, N
= I
+ NumCreatedFIDs
; I
!= N
; ++I
)
5451 if (IsSLocAffecting
[I
])
5452 ++AdjustedNumCreatedFIDs
;
5453 return AdjustedNumCreatedFIDs
;
5456 SourceLocation
ASTWriter::getAdjustedLocation(SourceLocation Loc
) const {
5457 if (Loc
.isInvalid())
5459 return Loc
.getLocWithOffset(-getAdjustment(Loc
.getOffset()));
5462 SourceRange
ASTWriter::getAdjustedRange(SourceRange Range
) const {
5463 return SourceRange(getAdjustedLocation(Range
.getBegin()),
5464 getAdjustedLocation(Range
.getEnd()));
5467 SourceLocation::UIntTy
5468 ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset
) const {
5469 return Offset
- getAdjustment(Offset
);
5472 SourceLocation::UIntTy
5473 ASTWriter::getAdjustment(SourceLocation::UIntTy Offset
) const {
5474 if (NonAffectingRanges
.empty())
5477 if (PP
->getSourceManager().isLoadedOffset(Offset
))
5480 if (Offset
> NonAffectingRanges
.back().getEnd().getOffset())
5481 return NonAffectingOffsetAdjustments
.back();
5483 if (Offset
< NonAffectingRanges
.front().getBegin().getOffset())
5486 auto Contains
= [](const SourceRange
&Range
, SourceLocation::UIntTy Offset
) {
5487 return Range
.getEnd().getOffset() < Offset
;
5490 auto It
= llvm::lower_bound(NonAffectingRanges
, Offset
, Contains
);
5491 unsigned Idx
= std::distance(NonAffectingRanges
.begin(), It
);
5492 return NonAffectingOffsetAdjustments
[Idx
];
5495 void ASTWriter::AddFileID(FileID FID
, RecordDataImpl
&Record
) {
5496 Record
.push_back(getAdjustedFileID(FID
).getOpaqueValue());
5499 void ASTWriter::AddSourceLocation(SourceLocation Loc
, RecordDataImpl
&Record
,
5500 SourceLocationSequence
*Seq
) {
5501 Loc
= getAdjustedLocation(Loc
);
5502 Record
.push_back(SourceLocationEncoding::encode(Loc
, Seq
));
5505 void ASTWriter::AddSourceRange(SourceRange Range
, RecordDataImpl
&Record
,
5506 SourceLocationSequence
*Seq
) {
5507 AddSourceLocation(Range
.getBegin(), Record
, Seq
);
5508 AddSourceLocation(Range
.getEnd(), Record
, Seq
);
5511 void ASTRecordWriter::AddAPFloat(const llvm::APFloat
&Value
) {
5512 AddAPInt(Value
.bitcastToAPInt());
5515 void ASTWriter::AddIdentifierRef(const IdentifierInfo
*II
, RecordDataImpl
&Record
) {
5516 Record
.push_back(getIdentifierRef(II
));
5519 IdentID
ASTWriter::getIdentifierRef(const IdentifierInfo
*II
) {
5523 IdentID
&ID
= IdentifierIDs
[II
];
5529 MacroID
ASTWriter::getMacroRef(MacroInfo
*MI
, const IdentifierInfo
*Name
) {
5530 // Don't emit builtin macros like __LINE__ to the AST file unless they
5531 // have been redefined by the header (in which case they are not
5533 if (!MI
|| MI
->isBuiltinMacro())
5536 MacroID
&ID
= MacroIDs
[MI
];
5539 MacroInfoToEmitData Info
= { Name
, MI
, ID
};
5540 MacroInfosToEmit
.push_back(Info
);
5545 MacroID
ASTWriter::getMacroID(MacroInfo
*MI
) {
5546 if (!MI
|| MI
->isBuiltinMacro())
5549 assert(MacroIDs
.contains(MI
) && "Macro not emitted!");
5550 return MacroIDs
[MI
];
5553 uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo
*Name
) {
5554 return IdentMacroDirectivesOffsetMap
.lookup(Name
);
5557 void ASTRecordWriter::AddSelectorRef(const Selector SelRef
) {
5558 Record
->push_back(Writer
->getSelectorRef(SelRef
));
5561 SelectorID
ASTWriter::getSelectorRef(Selector Sel
) {
5562 if (Sel
.getAsOpaquePtr() == nullptr) {
5566 SelectorID SID
= SelectorIDs
[Sel
];
5567 if (SID
== 0 && Chain
) {
5568 // This might trigger a ReadSelector callback, which will set the ID for
5570 Chain
->LoadSelector(Sel
);
5571 SID
= SelectorIDs
[Sel
];
5574 SID
= NextSelectorID
++;
5575 SelectorIDs
[Sel
] = SID
;
5580 void ASTRecordWriter::AddCXXTemporary(const CXXTemporary
*Temp
) {
5581 AddDeclRef(Temp
->getDestructor());
5584 void ASTRecordWriter::AddTemplateArgumentLocInfo(
5585 TemplateArgument::ArgKind Kind
, const TemplateArgumentLocInfo
&Arg
) {
5587 case TemplateArgument::Expression
:
5588 AddStmt(Arg
.getAsExpr());
5590 case TemplateArgument::Type
:
5591 AddTypeSourceInfo(Arg
.getAsTypeSourceInfo());
5593 case TemplateArgument::Template
:
5594 AddNestedNameSpecifierLoc(Arg
.getTemplateQualifierLoc());
5595 AddSourceLocation(Arg
.getTemplateNameLoc());
5597 case TemplateArgument::TemplateExpansion
:
5598 AddNestedNameSpecifierLoc(Arg
.getTemplateQualifierLoc());
5599 AddSourceLocation(Arg
.getTemplateNameLoc());
5600 AddSourceLocation(Arg
.getTemplateEllipsisLoc());
5602 case TemplateArgument::Null
:
5603 case TemplateArgument::Integral
:
5604 case TemplateArgument::Declaration
:
5605 case TemplateArgument::NullPtr
:
5606 case TemplateArgument::Pack
:
5607 // FIXME: Is this right?
5612 void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc
&Arg
) {
5613 AddTemplateArgument(Arg
.getArgument());
5615 if (Arg
.getArgument().getKind() == TemplateArgument::Expression
) {
5616 bool InfoHasSameExpr
5617 = Arg
.getArgument().getAsExpr() == Arg
.getLocInfo().getAsExpr();
5618 Record
->push_back(InfoHasSameExpr
);
5619 if (InfoHasSameExpr
)
5620 return; // Avoid storing the same expr twice.
5622 AddTemplateArgumentLocInfo(Arg
.getArgument().getKind(), Arg
.getLocInfo());
5625 void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo
*TInfo
) {
5627 AddTypeRef(QualType());
5631 AddTypeRef(TInfo
->getType());
5632 AddTypeLoc(TInfo
->getTypeLoc());
5635 void ASTRecordWriter::AddTypeLoc(TypeLoc TL
, LocSeq
*OuterSeq
) {
5636 LocSeq::State
Seq(OuterSeq
);
5637 TypeLocWriter
TLW(*this, Seq
);
5638 for (; !TL
.isNull(); TL
= TL
.getNextTypeLoc())
5642 void ASTWriter::AddTypeRef(QualType T
, RecordDataImpl
&Record
) {
5643 Record
.push_back(GetOrCreateTypeID(T
));
5646 TypeID
ASTWriter::GetOrCreateTypeID(QualType T
) {
5648 return MakeTypeID(*Context
, T
, [&](QualType T
) -> TypeIdx
{
5651 assert(!T
.getLocalFastQualifiers());
5653 TypeIdx
&Idx
= TypeIdxs
[T
];
5654 if (Idx
.getIndex() == 0) {
5655 if (DoneWritingDeclsAndTypes
) {
5656 assert(0 && "New type seen after serializing all the types to emit!");
5660 // We haven't seen this type before. Assign it a new ID and put it
5661 // into the queue of types to emit.
5662 Idx
= TypeIdx(NextTypeID
++);
5663 DeclTypesToEmit
.push(T
);
5669 TypeID
ASTWriter::getTypeID(QualType T
) const {
5671 return MakeTypeID(*Context
, T
, [&](QualType T
) -> TypeIdx
{
5674 assert(!T
.getLocalFastQualifiers());
5676 TypeIdxMap::const_iterator I
= TypeIdxs
.find(T
);
5677 assert(I
!= TypeIdxs
.end() && "Type not emitted!");
5682 void ASTWriter::AddDeclRef(const Decl
*D
, RecordDataImpl
&Record
) {
5683 Record
.push_back(GetDeclRef(D
));
5686 DeclID
ASTWriter::GetDeclRef(const Decl
*D
) {
5687 assert(WritingAST
&& "Cannot request a declaration ID before AST writing");
5693 // If D comes from an AST file, its declaration ID is already known and
5695 if (D
->isFromASTFile())
5696 return D
->getGlobalID();
5698 assert(!(reinterpret_cast<uintptr_t>(D
) & 0x01) && "Invalid decl pointer");
5699 DeclID
&ID
= DeclIDs
[D
];
5701 if (DoneWritingDeclsAndTypes
) {
5702 assert(0 && "New decl seen after serializing all the decls to emit!");
5706 // We haven't seen this declaration before. Give it a new ID and
5707 // enqueue it in the list of declarations to emit.
5709 DeclTypesToEmit
.push(const_cast<Decl
*>(D
));
5715 DeclID
ASTWriter::getDeclID(const Decl
*D
) {
5719 // If D comes from an AST file, its declaration ID is already known and
5721 if (D
->isFromASTFile())
5722 return D
->getGlobalID();
5724 assert(DeclIDs
.contains(D
) && "Declaration not emitted!");
5728 void ASTWriter::associateDeclWithFile(const Decl
*D
, DeclID ID
) {
5732 SourceLocation Loc
= D
->getLocation();
5733 if (Loc
.isInvalid())
5736 // We only keep track of the file-level declarations of each file.
5737 if (!D
->getLexicalDeclContext()->isFileContext())
5739 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5740 // a function/objc method, should not have TU as lexical context.
5741 // TemplateTemplateParmDecls that are part of an alias template, should not
5742 // have TU as lexical context.
5743 if (isa
<ParmVarDecl
, TemplateTemplateParmDecl
>(D
))
5746 SourceManager
&SM
= Context
->getSourceManager();
5747 SourceLocation FileLoc
= SM
.getFileLoc(Loc
);
5748 assert(SM
.isLocalSourceLocation(FileLoc
));
5751 std::tie(FID
, Offset
) = SM
.getDecomposedLoc(FileLoc
);
5752 if (FID
.isInvalid())
5754 assert(SM
.getSLocEntry(FID
).isFile());
5755 assert(IsSLocAffecting
[FID
.ID
]);
5757 std::unique_ptr
<DeclIDInFileInfo
> &Info
= FileDeclIDs
[FID
];
5759 Info
= std::make_unique
<DeclIDInFileInfo
>();
5761 std::pair
<unsigned, serialization::DeclID
> LocDecl(Offset
, ID
);
5762 LocDeclIDsTy
&Decls
= Info
->DeclIDs
;
5763 Decls
.push_back(LocDecl
);
5766 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl
*D
) {
5767 assert(needsAnonymousDeclarationNumber(D
) &&
5768 "expected an anonymous declaration");
5770 // Number the anonymous declarations within this context, if we've not
5772 auto It
= AnonymousDeclarationNumbers
.find(D
);
5773 if (It
== AnonymousDeclarationNumbers
.end()) {
5774 auto *DC
= D
->getLexicalDeclContext();
5775 numberAnonymousDeclsWithin(DC
, [&](const NamedDecl
*ND
, unsigned Number
) {
5776 AnonymousDeclarationNumbers
[ND
] = Number
;
5779 It
= AnonymousDeclarationNumbers
.find(D
);
5780 assert(It
!= AnonymousDeclarationNumbers
.end() &&
5781 "declaration not found within its lexical context");
5787 void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc
&DNLoc
,
5788 DeclarationName Name
) {
5789 switch (Name
.getNameKind()) {
5790 case DeclarationName::CXXConstructorName
:
5791 case DeclarationName::CXXDestructorName
:
5792 case DeclarationName::CXXConversionFunctionName
:
5793 AddTypeSourceInfo(DNLoc
.getNamedTypeInfo());
5796 case DeclarationName::CXXOperatorName
:
5797 AddSourceRange(DNLoc
.getCXXOperatorNameRange());
5800 case DeclarationName::CXXLiteralOperatorName
:
5801 AddSourceLocation(DNLoc
.getCXXLiteralOperatorNameLoc());
5804 case DeclarationName::Identifier
:
5805 case DeclarationName::ObjCZeroArgSelector
:
5806 case DeclarationName::ObjCOneArgSelector
:
5807 case DeclarationName::ObjCMultiArgSelector
:
5808 case DeclarationName::CXXUsingDirective
:
5809 case DeclarationName::CXXDeductionGuideName
:
5814 void ASTRecordWriter::AddDeclarationNameInfo(
5815 const DeclarationNameInfo
&NameInfo
) {
5816 AddDeclarationName(NameInfo
.getName());
5817 AddSourceLocation(NameInfo
.getLoc());
5818 AddDeclarationNameLoc(NameInfo
.getInfo(), NameInfo
.getName());
5821 void ASTRecordWriter::AddQualifierInfo(const QualifierInfo
&Info
) {
5822 AddNestedNameSpecifierLoc(Info
.QualifierLoc
);
5823 Record
->push_back(Info
.NumTemplParamLists
);
5824 for (unsigned i
= 0, e
= Info
.NumTemplParamLists
; i
!= e
; ++i
)
5825 AddTemplateParameterList(Info
.TemplParamLists
[i
]);
5828 void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS
) {
5829 // Nested name specifiers usually aren't too long. I think that 8 would
5830 // typically accommodate the vast majority.
5831 SmallVector
<NestedNameSpecifierLoc
, 8> NestedNames
;
5833 // Push each of the nested-name-specifiers's onto a stack for
5834 // serialization in reverse order.
5836 NestedNames
.push_back(NNS
);
5837 NNS
= NNS
.getPrefix();
5840 Record
->push_back(NestedNames
.size());
5841 while(!NestedNames
.empty()) {
5842 NNS
= NestedNames
.pop_back_val();
5843 NestedNameSpecifier::SpecifierKind Kind
5844 = NNS
.getNestedNameSpecifier()->getKind();
5845 Record
->push_back(Kind
);
5847 case NestedNameSpecifier::Identifier
:
5848 AddIdentifierRef(NNS
.getNestedNameSpecifier()->getAsIdentifier());
5849 AddSourceRange(NNS
.getLocalSourceRange());
5852 case NestedNameSpecifier::Namespace
:
5853 AddDeclRef(NNS
.getNestedNameSpecifier()->getAsNamespace());
5854 AddSourceRange(NNS
.getLocalSourceRange());
5857 case NestedNameSpecifier::NamespaceAlias
:
5858 AddDeclRef(NNS
.getNestedNameSpecifier()->getAsNamespaceAlias());
5859 AddSourceRange(NNS
.getLocalSourceRange());
5862 case NestedNameSpecifier::TypeSpec
:
5863 case NestedNameSpecifier::TypeSpecWithTemplate
:
5864 Record
->push_back(Kind
== NestedNameSpecifier::TypeSpecWithTemplate
);
5865 AddTypeRef(NNS
.getTypeLoc().getType());
5866 AddTypeLoc(NNS
.getTypeLoc());
5867 AddSourceLocation(NNS
.getLocalSourceRange().getEnd());
5870 case NestedNameSpecifier::Global
:
5871 AddSourceLocation(NNS
.getLocalSourceRange().getEnd());
5874 case NestedNameSpecifier::Super
:
5875 AddDeclRef(NNS
.getNestedNameSpecifier()->getAsRecordDecl());
5876 AddSourceRange(NNS
.getLocalSourceRange());
5882 void ASTRecordWriter::AddTemplateParameterList(
5883 const TemplateParameterList
*TemplateParams
) {
5884 assert(TemplateParams
&& "No TemplateParams!");
5885 AddSourceLocation(TemplateParams
->getTemplateLoc());
5886 AddSourceLocation(TemplateParams
->getLAngleLoc());
5887 AddSourceLocation(TemplateParams
->getRAngleLoc());
5889 Record
->push_back(TemplateParams
->size());
5890 for (const auto &P
: *TemplateParams
)
5892 if (const Expr
*RequiresClause
= TemplateParams
->getRequiresClause()) {
5893 Record
->push_back(true);
5894 AddStmt(const_cast<Expr
*>(RequiresClause
));
5896 Record
->push_back(false);
5900 /// Emit a template argument list.
5901 void ASTRecordWriter::AddTemplateArgumentList(
5902 const TemplateArgumentList
*TemplateArgs
) {
5903 assert(TemplateArgs
&& "No TemplateArgs!");
5904 Record
->push_back(TemplateArgs
->size());
5905 for (int i
= 0, e
= TemplateArgs
->size(); i
!= e
; ++i
)
5906 AddTemplateArgument(TemplateArgs
->get(i
));
5909 void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5910 const ASTTemplateArgumentListInfo
*ASTTemplArgList
) {
5911 assert(ASTTemplArgList
&& "No ASTTemplArgList!");
5912 AddSourceLocation(ASTTemplArgList
->LAngleLoc
);
5913 AddSourceLocation(ASTTemplArgList
->RAngleLoc
);
5914 Record
->push_back(ASTTemplArgList
->NumTemplateArgs
);
5915 const TemplateArgumentLoc
*TemplArgs
= ASTTemplArgList
->getTemplateArgs();
5916 for (int i
= 0, e
= ASTTemplArgList
->NumTemplateArgs
; i
!= e
; ++i
)
5917 AddTemplateArgumentLoc(TemplArgs
[i
]);
5920 void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet
&Set
) {
5921 Record
->push_back(Set
.size());
5922 for (ASTUnresolvedSet::const_iterator
5923 I
= Set
.begin(), E
= Set
.end(); I
!= E
; ++I
) {
5924 AddDeclRef(I
.getDecl());
5925 Record
->push_back(I
.getAccess());
5929 // FIXME: Move this out of the main ASTRecordWriter interface.
5930 void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier
&Base
) {
5931 Record
->push_back(Base
.isVirtual());
5932 Record
->push_back(Base
.isBaseOfClass());
5933 Record
->push_back(Base
.getAccessSpecifierAsWritten());
5934 Record
->push_back(Base
.getInheritConstructors());
5935 AddTypeSourceInfo(Base
.getTypeSourceInfo());
5936 AddSourceRange(Base
.getSourceRange());
5937 AddSourceLocation(Base
.isPackExpansion()? Base
.getEllipsisLoc()
5938 : SourceLocation());
5941 static uint64_t EmitCXXBaseSpecifiers(ASTWriter
&W
,
5942 ArrayRef
<CXXBaseSpecifier
> Bases
) {
5943 ASTWriter::RecordData Record
;
5944 ASTRecordWriter
Writer(W
, Record
);
5945 Writer
.push_back(Bases
.size());
5947 for (auto &Base
: Bases
)
5948 Writer
.AddCXXBaseSpecifier(Base
);
5950 return Writer
.Emit(serialization::DECL_CXX_BASE_SPECIFIERS
);
5953 // FIXME: Move this out of the main ASTRecordWriter interface.
5954 void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef
<CXXBaseSpecifier
> Bases
) {
5955 AddOffset(EmitCXXBaseSpecifiers(*Writer
, Bases
));
5959 EmitCXXCtorInitializers(ASTWriter
&W
,
5960 ArrayRef
<CXXCtorInitializer
*> CtorInits
) {
5961 ASTWriter::RecordData Record
;
5962 ASTRecordWriter
Writer(W
, Record
);
5963 Writer
.push_back(CtorInits
.size());
5965 for (auto *Init
: CtorInits
) {
5966 if (Init
->isBaseInitializer()) {
5967 Writer
.push_back(CTOR_INITIALIZER_BASE
);
5968 Writer
.AddTypeSourceInfo(Init
->getTypeSourceInfo());
5969 Writer
.push_back(Init
->isBaseVirtual());
5970 } else if (Init
->isDelegatingInitializer()) {
5971 Writer
.push_back(CTOR_INITIALIZER_DELEGATING
);
5972 Writer
.AddTypeSourceInfo(Init
->getTypeSourceInfo());
5973 } else if (Init
->isMemberInitializer()){
5974 Writer
.push_back(CTOR_INITIALIZER_MEMBER
);
5975 Writer
.AddDeclRef(Init
->getMember());
5977 Writer
.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER
);
5978 Writer
.AddDeclRef(Init
->getIndirectMember());
5981 Writer
.AddSourceLocation(Init
->getMemberLocation());
5982 Writer
.AddStmt(Init
->getInit());
5983 Writer
.AddSourceLocation(Init
->getLParenLoc());
5984 Writer
.AddSourceLocation(Init
->getRParenLoc());
5985 Writer
.push_back(Init
->isWritten());
5986 if (Init
->isWritten())
5987 Writer
.push_back(Init
->getSourceOrder());
5990 return Writer
.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS
);
5993 // FIXME: Move this out of the main ASTRecordWriter interface.
5994 void ASTRecordWriter::AddCXXCtorInitializers(
5995 ArrayRef
<CXXCtorInitializer
*> CtorInits
) {
5996 AddOffset(EmitCXXCtorInitializers(*Writer
, CtorInits
));
5999 void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl
*D
) {
6000 auto &Data
= D
->data();
6002 Record
->push_back(Data
.IsLambda
);
6004 BitsPacker DefinitionBits
;
6006 #define FIELD(Name, Width, Merge) \
6007 if (!DefinitionBits.canWriteNextNBits(Width)) { \
6008 Record->push_back(DefinitionBits); \
6009 DefinitionBits.reset(0); \
6011 DefinitionBits.addBits(Data.Name, Width);
6013 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
6016 Record
->push_back(DefinitionBits
);
6018 // getODRHash will compute the ODRHash if it has not been previously computed.
6019 Record
->push_back(D
->getODRHash());
6021 bool ModulesDebugInfo
=
6022 Writer
->Context
->getLangOpts().ModulesDebugInfo
&& !D
->isDependentType();
6023 Record
->push_back(ModulesDebugInfo
);
6024 if (ModulesDebugInfo
)
6025 Writer
->ModularCodegenDecls
.push_back(Writer
->GetDeclRef(D
));
6027 // IsLambda bit is already saved.
6029 AddUnresolvedSet(Data
.Conversions
.get(*Writer
->Context
));
6030 Record
->push_back(Data
.ComputedVisibleConversions
);
6031 if (Data
.ComputedVisibleConversions
)
6032 AddUnresolvedSet(Data
.VisibleConversions
.get(*Writer
->Context
));
6033 // Data.Definition is the owning decl, no need to write it.
6035 if (!Data
.IsLambda
) {
6036 Record
->push_back(Data
.NumBases
);
6037 if (Data
.NumBases
> 0)
6038 AddCXXBaseSpecifiers(Data
.bases());
6040 // FIXME: Make VBases lazily computed when needed to avoid storing them.
6041 Record
->push_back(Data
.NumVBases
);
6042 if (Data
.NumVBases
> 0)
6043 AddCXXBaseSpecifiers(Data
.vbases());
6045 AddDeclRef(D
->getFirstFriend());
6047 auto &Lambda
= D
->getLambdaData();
6049 BitsPacker LambdaBits
;
6050 LambdaBits
.addBits(Lambda
.DependencyKind
, /*Width=*/2);
6051 LambdaBits
.addBit(Lambda
.IsGenericLambda
);
6052 LambdaBits
.addBits(Lambda
.CaptureDefault
, /*Width=*/2);
6053 LambdaBits
.addBits(Lambda
.NumCaptures
, /*Width=*/15);
6054 LambdaBits
.addBit(Lambda
.HasKnownInternalLinkage
);
6055 Record
->push_back(LambdaBits
);
6057 Record
->push_back(Lambda
.NumExplicitCaptures
);
6058 Record
->push_back(Lambda
.ManglingNumber
);
6059 Record
->push_back(D
->getDeviceLambdaManglingNumber());
6060 // The lambda context declaration and index within the context are provided
6061 // separately, so that they can be used for merging.
6062 AddTypeSourceInfo(Lambda
.MethodTyInfo
);
6063 for (unsigned I
= 0, N
= Lambda
.NumCaptures
; I
!= N
; ++I
) {
6064 const LambdaCapture
&Capture
= Lambda
.Captures
.front()[I
];
6065 AddSourceLocation(Capture
.getLocation());
6067 BitsPacker CaptureBits
;
6068 CaptureBits
.addBit(Capture
.isImplicit());
6069 CaptureBits
.addBits(Capture
.getCaptureKind(), /*Width=*/3);
6070 Record
->push_back(CaptureBits
);
6072 switch (Capture
.getCaptureKind()) {
6080 Capture
.capturesVariable() ? Capture
.getCapturedVar() : nullptr;
6082 AddSourceLocation(Capture
.isPackExpansion() ? Capture
.getEllipsisLoc()
6083 : SourceLocation());
6090 void ASTRecordWriter::AddVarDeclInit(const VarDecl
*VD
) {
6091 const Expr
*Init
= VD
->getInit();
6098 if (EvaluatedStmt
*ES
= VD
->getEvaluatedStmt()) {
6099 Val
|= (ES
->HasConstantInitialization
? 2 : 0);
6100 Val
|= (ES
->HasConstantDestruction
? 4 : 0);
6101 APValue
*Evaluated
= VD
->getEvaluatedValue();
6102 // If the evaluated result is constant, emit it.
6103 if (Evaluated
&& (Evaluated
->isInt() || Evaluated
->isFloat()))
6108 AddAPValue(*VD
->getEvaluatedValue());
6114 void ASTWriter::ReaderInitialized(ASTReader
*Reader
) {
6115 assert(Reader
&& "Cannot remove chain");
6116 assert((!Chain
|| Chain
== Reader
) && "Cannot replace chain");
6117 assert(FirstDeclID
== NextDeclID
&&
6118 FirstTypeID
== NextTypeID
&&
6119 FirstIdentID
== NextIdentID
&&
6120 FirstMacroID
== NextMacroID
&&
6121 FirstSubmoduleID
== NextSubmoduleID
&&
6122 FirstSelectorID
== NextSelectorID
&&
6123 "Setting chain after writing has started.");
6127 // Note, this will get called multiple times, once one the reader starts up
6128 // and again each time it's done reading a PCH or module.
6129 FirstDeclID
= NUM_PREDEF_DECL_IDS
+ Chain
->getTotalNumDecls();
6130 FirstTypeID
= NUM_PREDEF_TYPE_IDS
+ Chain
->getTotalNumTypes();
6131 FirstIdentID
= NUM_PREDEF_IDENT_IDS
+ Chain
->getTotalNumIdentifiers();
6132 FirstMacroID
= NUM_PREDEF_MACRO_IDS
+ Chain
->getTotalNumMacros();
6133 FirstSubmoduleID
= NUM_PREDEF_SUBMODULE_IDS
+ Chain
->getTotalNumSubmodules();
6134 FirstSelectorID
= NUM_PREDEF_SELECTOR_IDS
+ Chain
->getTotalNumSelectors();
6135 NextDeclID
= FirstDeclID
;
6136 NextTypeID
= FirstTypeID
;
6137 NextIdentID
= FirstIdentID
;
6138 NextMacroID
= FirstMacroID
;
6139 NextSelectorID
= FirstSelectorID
;
6140 NextSubmoduleID
= FirstSubmoduleID
;
6143 void ASTWriter::IdentifierRead(IdentID ID
, IdentifierInfo
*II
) {
6144 // Always keep the highest ID. See \p TypeRead() for more information.
6145 IdentID
&StoredID
= IdentifierIDs
[II
];
6150 void ASTWriter::MacroRead(serialization::MacroID ID
, MacroInfo
*MI
) {
6151 // Always keep the highest ID. See \p TypeRead() for more information.
6152 MacroID
&StoredID
= MacroIDs
[MI
];
6157 void ASTWriter::TypeRead(TypeIdx Idx
, QualType T
) {
6158 // Always take the highest-numbered type index. This copes with an interesting
6159 // case for chained AST writing where we schedule writing the type and then,
6160 // later, deserialize the type from another AST. In this case, we want to
6161 // keep the higher-numbered entry so that we can properly write it out to
6163 TypeIdx
&StoredIdx
= TypeIdxs
[T
];
6164 if (Idx
.getIndex() >= StoredIdx
.getIndex())
6168 void ASTWriter::SelectorRead(SelectorID ID
, Selector S
) {
6169 // Always keep the highest ID. See \p TypeRead() for more information.
6170 SelectorID
&StoredID
= SelectorIDs
[S
];
6175 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID
,
6176 MacroDefinitionRecord
*MD
) {
6177 assert(!MacroDefinitions
.contains(MD
));
6178 MacroDefinitions
[MD
] = ID
;
6181 void ASTWriter::ModuleRead(serialization::SubmoduleID ID
, Module
*Mod
) {
6182 assert(!SubmoduleIDs
.contains(Mod
));
6183 SubmoduleIDs
[Mod
] = ID
;
6186 void ASTWriter::CompletedTagDefinition(const TagDecl
*D
) {
6187 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6188 assert(D
->isCompleteDefinition());
6189 assert(!WritingAST
&& "Already writing the AST!");
6190 if (auto *RD
= dyn_cast
<CXXRecordDecl
>(D
)) {
6191 // We are interested when a PCH decl is modified.
6192 if (RD
->isFromASTFile()) {
6193 // A forward reference was mutated into a definition. Rewrite it.
6194 // FIXME: This happens during template instantiation, should we
6195 // have created a new definition decl instead ?
6196 assert(isTemplateInstantiation(RD
->getTemplateSpecializationKind()) &&
6197 "completed a tag from another module but not by instantiation?");
6198 DeclUpdates
[RD
].push_back(
6199 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION
));
6204 static bool isImportedDeclContext(ASTReader
*Chain
, const Decl
*D
) {
6205 if (D
->isFromASTFile())
6208 // The predefined __va_list_tag struct is imported if we imported any decls.
6209 // FIXME: This is a gross hack.
6210 return D
== D
->getASTContext().getVaListTagDecl();
6213 void ASTWriter::AddedVisibleDecl(const DeclContext
*DC
, const Decl
*D
) {
6214 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6215 assert(DC
->isLookupContext() &&
6216 "Should not add lookup results to non-lookup contexts!");
6218 // TU is handled elsewhere.
6219 if (isa
<TranslationUnitDecl
>(DC
))
6222 // Namespaces are handled elsewhere, except for template instantiations of
6223 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6224 // local instantiations are added to an imported context. Only happens when
6225 // adding ADL lookup candidates, for example templated friends.
6226 if (isa
<NamespaceDecl
>(DC
) && D
->getFriendObjectKind() == Decl::FOK_None
&&
6227 !isa
<FunctionTemplateDecl
>(D
))
6230 // We're only interested in cases where a local declaration is added to an
6231 // imported context.
6232 if (D
->isFromASTFile() || !isImportedDeclContext(Chain
, cast
<Decl
>(DC
)))
6235 assert(DC
== DC
->getPrimaryContext() && "added to non-primary context");
6236 assert(!getDefinitiveDeclContext(DC
) && "DeclContext not definitive!");
6237 assert(!WritingAST
&& "Already writing the AST!");
6238 if (UpdatedDeclContexts
.insert(DC
) && !cast
<Decl
>(DC
)->isFromASTFile()) {
6239 // We're adding a visible declaration to a predefined decl context. Ensure
6240 // that we write out all of its lookup results so we don't get a nasty
6241 // surprise when we try to emit its lookup table.
6242 llvm::append_range(DeclsToEmitEvenIfUnreferenced
, DC
->decls());
6244 DeclsToEmitEvenIfUnreferenced
.push_back(D
);
6247 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl
*RD
, const Decl
*D
) {
6248 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6249 assert(D
->isImplicit());
6251 // We're only interested in cases where a local declaration is added to an
6252 // imported context.
6253 if (D
->isFromASTFile() || !isImportedDeclContext(Chain
, RD
))
6256 if (!isa
<CXXMethodDecl
>(D
))
6259 // A decl coming from PCH was modified.
6260 assert(RD
->isCompleteDefinition());
6261 assert(!WritingAST
&& "Already writing the AST!");
6262 DeclUpdates
[RD
].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER
, D
));
6265 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl
*FD
) {
6266 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6267 assert(!DoneWritingDeclsAndTypes
&& "Already done writing updates!");
6269 Chain
->forEachImportedKeyDecl(FD
, [&](const Decl
*D
) {
6270 // If we don't already know the exception specification for this redecl
6271 // chain, add an update record for it.
6272 if (isUnresolvedExceptionSpec(cast
<FunctionDecl
>(D
)
6274 ->castAs
<FunctionProtoType
>()
6275 ->getExceptionSpecType()))
6276 DeclUpdates
[D
].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC
);
6280 void ASTWriter::DeducedReturnType(const FunctionDecl
*FD
, QualType ReturnType
) {
6281 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6282 assert(!WritingAST
&& "Already writing the AST!");
6284 Chain
->forEachImportedKeyDecl(FD
, [&](const Decl
*D
) {
6285 DeclUpdates
[D
].push_back(
6286 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE
, ReturnType
));
6290 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl
*DD
,
6291 const FunctionDecl
*Delete
,
6293 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6294 assert(!WritingAST
&& "Already writing the AST!");
6295 assert(Delete
&& "Not given an operator delete");
6297 Chain
->forEachImportedKeyDecl(DD
, [&](const Decl
*D
) {
6298 DeclUpdates
[D
].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE
, Delete
));
6302 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl
*D
) {
6303 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6304 assert(!WritingAST
&& "Already writing the AST!");
6305 if (!D
->isFromASTFile())
6306 return; // Declaration not imported from PCH.
6308 // Implicit function decl from a PCH was defined.
6309 DeclUpdates
[D
].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION
));
6312 void ASTWriter::VariableDefinitionInstantiated(const VarDecl
*D
) {
6313 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6314 assert(!WritingAST
&& "Already writing the AST!");
6315 if (!D
->isFromASTFile())
6318 DeclUpdates
[D
].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION
));
6321 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl
*D
) {
6322 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6323 assert(!WritingAST
&& "Already writing the AST!");
6324 if (!D
->isFromASTFile())
6327 DeclUpdates
[D
].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION
));
6330 void ASTWriter::InstantiationRequested(const ValueDecl
*D
) {
6331 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6332 assert(!WritingAST
&& "Already writing the AST!");
6333 if (!D
->isFromASTFile())
6336 // Since the actual instantiation is delayed, this really means that we need
6337 // to update the instantiation location.
6339 if (auto *VD
= dyn_cast
<VarDecl
>(D
))
6340 POI
= VD
->getPointOfInstantiation();
6342 POI
= cast
<FunctionDecl
>(D
)->getPointOfInstantiation();
6343 DeclUpdates
[D
].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION
, POI
));
6346 void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl
*D
) {
6347 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6348 assert(!WritingAST
&& "Already writing the AST!");
6349 if (!D
->isFromASTFile())
6352 DeclUpdates
[D
].push_back(
6353 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT
, D
));
6356 void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl
*D
) {
6357 assert(!WritingAST
&& "Already writing the AST!");
6358 if (!D
->isFromASTFile())
6361 DeclUpdates
[D
].push_back(
6362 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER
, D
));
6365 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl
*CatD
,
6366 const ObjCInterfaceDecl
*IFD
) {
6367 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6368 assert(!WritingAST
&& "Already writing the AST!");
6369 if (!IFD
->isFromASTFile())
6370 return; // Declaration not imported from PCH.
6372 assert(IFD
->getDefinition() && "Category on a class without a definition?");
6373 ObjCClassesWithCategories
.insert(
6374 const_cast<ObjCInterfaceDecl
*>(IFD
->getDefinition()));
6377 void ASTWriter::DeclarationMarkedUsed(const Decl
*D
) {
6378 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6379 assert(!WritingAST
&& "Already writing the AST!");
6381 // If there is *any* declaration of the entity that's not from an AST file,
6382 // we can skip writing the update record. We make sure that isUsed() triggers
6383 // completion of the redeclaration chain of the entity.
6384 for (auto Prev
= D
->getMostRecentDecl(); Prev
; Prev
= Prev
->getPreviousDecl())
6385 if (IsLocalDecl(Prev
))
6388 DeclUpdates
[D
].push_back(DeclUpdate(UPD_DECL_MARKED_USED
));
6391 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl
*D
) {
6392 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6393 assert(!WritingAST
&& "Already writing the AST!");
6394 if (!D
->isFromASTFile())
6397 DeclUpdates
[D
].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE
));
6400 void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl
*D
, const Attr
*A
) {
6401 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6402 assert(!WritingAST
&& "Already writing the AST!");
6403 if (!D
->isFromASTFile())
6406 DeclUpdates
[D
].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE
, A
));
6409 void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl
*D
,
6411 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6412 assert(!WritingAST
&& "Already writing the AST!");
6413 if (!D
->isFromASTFile())
6416 DeclUpdates
[D
].push_back(
6417 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET
, Attr
));
6420 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl
*D
, Module
*M
) {
6421 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6422 assert(!WritingAST
&& "Already writing the AST!");
6423 assert(!D
->isUnconditionallyVisible() && "expected a hidden declaration");
6424 DeclUpdates
[D
].push_back(DeclUpdate(UPD_DECL_EXPORTED
, M
));
6427 void ASTWriter::AddedAttributeToRecord(const Attr
*Attr
,
6428 const RecordDecl
*Record
) {
6429 if (Chain
&& Chain
->isProcessingUpdateRecords()) return;
6430 assert(!WritingAST
&& "Already writing the AST!");
6431 if (!Record
->isFromASTFile())
6433 DeclUpdates
[Record
].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD
, Attr
));
6436 void ASTWriter::AddedCXXTemplateSpecialization(
6437 const ClassTemplateDecl
*TD
, const ClassTemplateSpecializationDecl
*D
) {
6438 assert(!WritingAST
&& "Already writing the AST!");
6440 if (!TD
->getFirstDecl()->isFromASTFile())
6442 if (Chain
&& Chain
->isProcessingUpdateRecords())
6445 DeclsToEmitEvenIfUnreferenced
.push_back(D
);
6448 void ASTWriter::AddedCXXTemplateSpecialization(
6449 const VarTemplateDecl
*TD
, const VarTemplateSpecializationDecl
*D
) {
6450 assert(!WritingAST
&& "Already writing the AST!");
6452 if (!TD
->getFirstDecl()->isFromASTFile())
6454 if (Chain
&& Chain
->isProcessingUpdateRecords())
6457 DeclsToEmitEvenIfUnreferenced
.push_back(D
);
6460 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl
*TD
,
6461 const FunctionDecl
*D
) {
6462 assert(!WritingAST
&& "Already writing the AST!");
6464 if (!TD
->getFirstDecl()->isFromASTFile())
6466 if (Chain
&& Chain
->isProcessingUpdateRecords())
6469 DeclsToEmitEvenIfUnreferenced
.push_back(D
);
6472 //===----------------------------------------------------------------------===//
6473 //// OMPClause Serialization
6474 ////===----------------------------------------------------------------------===//
6478 class OMPClauseWriter
: public OMPClauseVisitor
<OMPClauseWriter
> {
6479 ASTRecordWriter
&Record
;
6482 OMPClauseWriter(ASTRecordWriter
&Record
) : Record(Record
) {}
6483 #define GEN_CLANG_CLAUSE_CLASS
6484 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
6485 #include "llvm/Frontend/OpenMP/OMP.inc"
6486 void writeClause(OMPClause
*C
);
6487 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit
*C
);
6488 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate
*C
);
6493 void ASTRecordWriter::writeOMPClause(OMPClause
*C
) {
6494 OMPClauseWriter(*this).writeClause(C
);
6497 void OMPClauseWriter::writeClause(OMPClause
*C
) {
6498 Record
.push_back(unsigned(C
->getClauseKind()));
6500 Record
.AddSourceLocation(C
->getBeginLoc());
6501 Record
.AddSourceLocation(C
->getEndLoc());
6504 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit
*C
) {
6505 Record
.push_back(uint64_t(C
->getCaptureRegion()));
6506 Record
.AddStmt(C
->getPreInitStmt());
6509 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate
*C
) {
6510 VisitOMPClauseWithPreInit(C
);
6511 Record
.AddStmt(C
->getPostUpdateExpr());
6514 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause
*C
) {
6515 VisitOMPClauseWithPreInit(C
);
6516 Record
.push_back(uint64_t(C
->getNameModifier()));
6517 Record
.AddSourceLocation(C
->getNameModifierLoc());
6518 Record
.AddSourceLocation(C
->getColonLoc());
6519 Record
.AddStmt(C
->getCondition());
6520 Record
.AddSourceLocation(C
->getLParenLoc());
6523 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause
*C
) {
6524 VisitOMPClauseWithPreInit(C
);
6525 Record
.AddStmt(C
->getCondition());
6526 Record
.AddSourceLocation(C
->getLParenLoc());
6529 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause
*C
) {
6530 VisitOMPClauseWithPreInit(C
);
6531 Record
.AddStmt(C
->getNumThreads());
6532 Record
.AddSourceLocation(C
->getLParenLoc());
6535 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause
*C
) {
6536 Record
.AddStmt(C
->getSafelen());
6537 Record
.AddSourceLocation(C
->getLParenLoc());
6540 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause
*C
) {
6541 Record
.AddStmt(C
->getSimdlen());
6542 Record
.AddSourceLocation(C
->getLParenLoc());
6545 void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause
*C
) {
6546 Record
.push_back(C
->getNumSizes());
6547 for (Expr
*Size
: C
->getSizesRefs())
6548 Record
.AddStmt(Size
);
6549 Record
.AddSourceLocation(C
->getLParenLoc());
6552 void OMPClauseWriter::VisitOMPFullClause(OMPFullClause
*C
) {}
6554 void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause
*C
) {
6555 Record
.AddStmt(C
->getFactor());
6556 Record
.AddSourceLocation(C
->getLParenLoc());
6559 void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause
*C
) {
6560 Record
.AddStmt(C
->getAllocator());
6561 Record
.AddSourceLocation(C
->getLParenLoc());
6564 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause
*C
) {
6565 Record
.AddStmt(C
->getNumForLoops());
6566 Record
.AddSourceLocation(C
->getLParenLoc());
6569 void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause
*C
) {
6570 Record
.AddStmt(C
->getEventHandler());
6571 Record
.AddSourceLocation(C
->getLParenLoc());
6574 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause
*C
) {
6575 Record
.push_back(unsigned(C
->getDefaultKind()));
6576 Record
.AddSourceLocation(C
->getLParenLoc());
6577 Record
.AddSourceLocation(C
->getDefaultKindKwLoc());
6580 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause
*C
) {
6581 Record
.push_back(unsigned(C
->getProcBindKind()));
6582 Record
.AddSourceLocation(C
->getLParenLoc());
6583 Record
.AddSourceLocation(C
->getProcBindKindKwLoc());
6586 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause
*C
) {
6587 VisitOMPClauseWithPreInit(C
);
6588 Record
.push_back(C
->getScheduleKind());
6589 Record
.push_back(C
->getFirstScheduleModifier());
6590 Record
.push_back(C
->getSecondScheduleModifier());
6591 Record
.AddStmt(C
->getChunkSize());
6592 Record
.AddSourceLocation(C
->getLParenLoc());
6593 Record
.AddSourceLocation(C
->getFirstScheduleModifierLoc());
6594 Record
.AddSourceLocation(C
->getSecondScheduleModifierLoc());
6595 Record
.AddSourceLocation(C
->getScheduleKindLoc());
6596 Record
.AddSourceLocation(C
->getCommaLoc());
6599 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause
*C
) {
6600 Record
.push_back(C
->getLoopNumIterations().size());
6601 Record
.AddStmt(C
->getNumForLoops());
6602 for (Expr
*NumIter
: C
->getLoopNumIterations())
6603 Record
.AddStmt(NumIter
);
6604 for (unsigned I
= 0, E
= C
->getLoopNumIterations().size(); I
<E
; ++I
)
6605 Record
.AddStmt(C
->getLoopCounter(I
));
6606 Record
.AddSourceLocation(C
->getLParenLoc());
6609 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause
*) {}
6611 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause
*) {}
6613 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause
*) {}
6615 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause
*) {}
6617 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause
*) {}
6619 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause
*C
) {
6620 Record
.push_back(C
->isExtended() ? 1 : 0);
6621 if (C
->isExtended()) {
6622 Record
.AddSourceLocation(C
->getLParenLoc());
6623 Record
.AddSourceLocation(C
->getArgumentLoc());
6624 Record
.writeEnum(C
->getDependencyKind());
6628 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause
*) {}
6630 void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause
*) {}
6632 // Save the parameter of fail clause.
6633 void OMPClauseWriter::VisitOMPFailClause(OMPFailClause
*C
) {
6634 Record
.AddSourceLocation(C
->getLParenLoc());
6635 Record
.AddSourceLocation(C
->getFailParameterLoc());
6636 Record
.writeEnum(C
->getFailParameter());
6639 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause
*) {}
6641 void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause
*) {}
6643 void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause
*) {}
6645 void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause
*) {}
6647 void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause
*) {}
6649 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause
*) {}
6651 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause
*) {}
6653 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause
*) {}
6655 void OMPClauseWriter::VisitOMPInitClause(OMPInitClause
*C
) {
6656 Record
.push_back(C
->varlist_size());
6657 for (Expr
*VE
: C
->varlists())
6659 Record
.writeBool(C
->getIsTarget());
6660 Record
.writeBool(C
->getIsTargetSync());
6661 Record
.AddSourceLocation(C
->getLParenLoc());
6662 Record
.AddSourceLocation(C
->getVarLoc());
6665 void OMPClauseWriter::VisitOMPUseClause(OMPUseClause
*C
) {
6666 Record
.AddStmt(C
->getInteropVar());
6667 Record
.AddSourceLocation(C
->getLParenLoc());
6668 Record
.AddSourceLocation(C
->getVarLoc());
6671 void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause
*C
) {
6672 Record
.AddStmt(C
->getInteropVar());
6673 Record
.AddSourceLocation(C
->getLParenLoc());
6674 Record
.AddSourceLocation(C
->getVarLoc());
6677 void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause
*C
) {
6678 VisitOMPClauseWithPreInit(C
);
6679 Record
.AddStmt(C
->getCondition());
6680 Record
.AddSourceLocation(C
->getLParenLoc());
6683 void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause
*C
) {
6684 VisitOMPClauseWithPreInit(C
);
6685 Record
.AddStmt(C
->getCondition());
6686 Record
.AddSourceLocation(C
->getLParenLoc());
6689 void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause
*C
) {
6690 VisitOMPClauseWithPreInit(C
);
6691 Record
.AddStmt(C
->getThreadID());
6692 Record
.AddSourceLocation(C
->getLParenLoc());
6695 void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause
*C
) {
6696 Record
.AddStmt(C
->getAlignment());
6697 Record
.AddSourceLocation(C
->getLParenLoc());
6700 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause
*C
) {
6701 Record
.push_back(C
->varlist_size());
6702 Record
.AddSourceLocation(C
->getLParenLoc());
6703 for (auto *VE
: C
->varlists()) {
6706 for (auto *VE
: C
->private_copies()) {
6711 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause
*C
) {
6712 Record
.push_back(C
->varlist_size());
6713 VisitOMPClauseWithPreInit(C
);
6714 Record
.AddSourceLocation(C
->getLParenLoc());
6715 for (auto *VE
: C
->varlists()) {
6718 for (auto *VE
: C
->private_copies()) {
6721 for (auto *VE
: C
->inits()) {
6726 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause
*C
) {
6727 Record
.push_back(C
->varlist_size());
6728 VisitOMPClauseWithPostUpdate(C
);
6729 Record
.AddSourceLocation(C
->getLParenLoc());
6730 Record
.writeEnum(C
->getKind());
6731 Record
.AddSourceLocation(C
->getKindLoc());
6732 Record
.AddSourceLocation(C
->getColonLoc());
6733 for (auto *VE
: C
->varlists())
6735 for (auto *E
: C
->private_copies())
6737 for (auto *E
: C
->source_exprs())
6739 for (auto *E
: C
->destination_exprs())
6741 for (auto *E
: C
->assignment_ops())
6745 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause
*C
) {
6746 Record
.push_back(C
->varlist_size());
6747 Record
.AddSourceLocation(C
->getLParenLoc());
6748 for (auto *VE
: C
->varlists())
6752 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause
*C
) {
6753 Record
.push_back(C
->varlist_size());
6754 Record
.writeEnum(C
->getModifier());
6755 VisitOMPClauseWithPostUpdate(C
);
6756 Record
.AddSourceLocation(C
->getLParenLoc());
6757 Record
.AddSourceLocation(C
->getModifierLoc());
6758 Record
.AddSourceLocation(C
->getColonLoc());
6759 Record
.AddNestedNameSpecifierLoc(C
->getQualifierLoc());
6760 Record
.AddDeclarationNameInfo(C
->getNameInfo());
6761 for (auto *VE
: C
->varlists())
6763 for (auto *VE
: C
->privates())
6765 for (auto *E
: C
->lhs_exprs())
6767 for (auto *E
: C
->rhs_exprs())
6769 for (auto *E
: C
->reduction_ops())
6771 if (C
->getModifier() == clang::OMPC_REDUCTION_inscan
) {
6772 for (auto *E
: C
->copy_ops())
6774 for (auto *E
: C
->copy_array_temps())
6776 for (auto *E
: C
->copy_array_elems())
6781 void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause
*C
) {
6782 Record
.push_back(C
->varlist_size());
6783 VisitOMPClauseWithPostUpdate(C
);
6784 Record
.AddSourceLocation(C
->getLParenLoc());
6785 Record
.AddSourceLocation(C
->getColonLoc());
6786 Record
.AddNestedNameSpecifierLoc(C
->getQualifierLoc());
6787 Record
.AddDeclarationNameInfo(C
->getNameInfo());
6788 for (auto *VE
: C
->varlists())
6790 for (auto *VE
: C
->privates())
6792 for (auto *E
: C
->lhs_exprs())
6794 for (auto *E
: C
->rhs_exprs())
6796 for (auto *E
: C
->reduction_ops())
6800 void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause
*C
) {
6801 Record
.push_back(C
->varlist_size());
6802 VisitOMPClauseWithPostUpdate(C
);
6803 Record
.AddSourceLocation(C
->getLParenLoc());
6804 Record
.AddSourceLocation(C
->getColonLoc());
6805 Record
.AddNestedNameSpecifierLoc(C
->getQualifierLoc());
6806 Record
.AddDeclarationNameInfo(C
->getNameInfo());
6807 for (auto *VE
: C
->varlists())
6809 for (auto *VE
: C
->privates())
6811 for (auto *E
: C
->lhs_exprs())
6813 for (auto *E
: C
->rhs_exprs())
6815 for (auto *E
: C
->reduction_ops())
6817 for (auto *E
: C
->taskgroup_descriptors())
6821 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause
*C
) {
6822 Record
.push_back(C
->varlist_size());
6823 VisitOMPClauseWithPostUpdate(C
);
6824 Record
.AddSourceLocation(C
->getLParenLoc());
6825 Record
.AddSourceLocation(C
->getColonLoc());
6826 Record
.push_back(C
->getModifier());
6827 Record
.AddSourceLocation(C
->getModifierLoc());
6828 for (auto *VE
: C
->varlists()) {
6831 for (auto *VE
: C
->privates()) {
6834 for (auto *VE
: C
->inits()) {
6837 for (auto *VE
: C
->updates()) {
6840 for (auto *VE
: C
->finals()) {
6843 Record
.AddStmt(C
->getStep());
6844 Record
.AddStmt(C
->getCalcStep());
6845 for (auto *VE
: C
->used_expressions())
6849 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause
*C
) {
6850 Record
.push_back(C
->varlist_size());
6851 Record
.AddSourceLocation(C
->getLParenLoc());
6852 Record
.AddSourceLocation(C
->getColonLoc());
6853 for (auto *VE
: C
->varlists())
6855 Record
.AddStmt(C
->getAlignment());
6858 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause
*C
) {
6859 Record
.push_back(C
->varlist_size());
6860 Record
.AddSourceLocation(C
->getLParenLoc());
6861 for (auto *VE
: C
->varlists())
6863 for (auto *E
: C
->source_exprs())
6865 for (auto *E
: C
->destination_exprs())
6867 for (auto *E
: C
->assignment_ops())
6871 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause
*C
) {
6872 Record
.push_back(C
->varlist_size());
6873 Record
.AddSourceLocation(C
->getLParenLoc());
6874 for (auto *VE
: C
->varlists())
6876 for (auto *E
: C
->source_exprs())
6878 for (auto *E
: C
->destination_exprs())
6880 for (auto *E
: C
->assignment_ops())
6884 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause
*C
) {
6885 Record
.push_back(C
->varlist_size());
6886 Record
.AddSourceLocation(C
->getLParenLoc());
6887 for (auto *VE
: C
->varlists())
6891 void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause
*C
) {
6892 Record
.AddStmt(C
->getDepobj());
6893 Record
.AddSourceLocation(C
->getLParenLoc());
6896 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause
*C
) {
6897 Record
.push_back(C
->varlist_size());
6898 Record
.push_back(C
->getNumLoops());
6899 Record
.AddSourceLocation(C
->getLParenLoc());
6900 Record
.AddStmt(C
->getModifier());
6901 Record
.push_back(C
->getDependencyKind());
6902 Record
.AddSourceLocation(C
->getDependencyLoc());
6903 Record
.AddSourceLocation(C
->getColonLoc());
6904 Record
.AddSourceLocation(C
->getOmpAllMemoryLoc());
6905 for (auto *VE
: C
->varlists())
6907 for (unsigned I
= 0, E
= C
->getNumLoops(); I
< E
; ++I
)
6908 Record
.AddStmt(C
->getLoopData(I
));
6911 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause
*C
) {
6912 VisitOMPClauseWithPreInit(C
);
6913 Record
.writeEnum(C
->getModifier());
6914 Record
.AddStmt(C
->getDevice());
6915 Record
.AddSourceLocation(C
->getModifierLoc());
6916 Record
.AddSourceLocation(C
->getLParenLoc());
6919 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause
*C
) {
6920 Record
.push_back(C
->varlist_size());
6921 Record
.push_back(C
->getUniqueDeclarationsNum());
6922 Record
.push_back(C
->getTotalComponentListNum());
6923 Record
.push_back(C
->getTotalComponentsNum());
6924 Record
.AddSourceLocation(C
->getLParenLoc());
6925 bool HasIteratorModifier
= false;
6926 for (unsigned I
= 0; I
< NumberOfOMPMapClauseModifiers
; ++I
) {
6927 Record
.push_back(C
->getMapTypeModifier(I
));
6928 Record
.AddSourceLocation(C
->getMapTypeModifierLoc(I
));
6929 if (C
->getMapTypeModifier(I
) == OMPC_MAP_MODIFIER_iterator
)
6930 HasIteratorModifier
= true;
6932 Record
.AddNestedNameSpecifierLoc(C
->getMapperQualifierLoc());
6933 Record
.AddDeclarationNameInfo(C
->getMapperIdInfo());
6934 Record
.push_back(C
->getMapType());
6935 Record
.AddSourceLocation(C
->getMapLoc());
6936 Record
.AddSourceLocation(C
->getColonLoc());
6937 for (auto *E
: C
->varlists())
6939 for (auto *E
: C
->mapperlists())
6941 if (HasIteratorModifier
)
6942 Record
.AddStmt(C
->getIteratorModifier());
6943 for (auto *D
: C
->all_decls())
6944 Record
.AddDeclRef(D
);
6945 for (auto N
: C
->all_num_lists())
6946 Record
.push_back(N
);
6947 for (auto N
: C
->all_lists_sizes())
6948 Record
.push_back(N
);
6949 for (auto &M
: C
->all_components()) {
6950 Record
.AddStmt(M
.getAssociatedExpression());
6951 Record
.AddDeclRef(M
.getAssociatedDeclaration());
6955 void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause
*C
) {
6956 Record
.push_back(C
->varlist_size());
6957 Record
.AddSourceLocation(C
->getLParenLoc());
6958 Record
.AddSourceLocation(C
->getColonLoc());
6959 Record
.AddStmt(C
->getAllocator());
6960 for (auto *VE
: C
->varlists())
6964 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause
*C
) {
6965 VisitOMPClauseWithPreInit(C
);
6966 Record
.AddStmt(C
->getNumTeams());
6967 Record
.AddSourceLocation(C
->getLParenLoc());
6970 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause
*C
) {
6971 VisitOMPClauseWithPreInit(C
);
6972 Record
.AddStmt(C
->getThreadLimit());
6973 Record
.AddSourceLocation(C
->getLParenLoc());
6976 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause
*C
) {
6977 VisitOMPClauseWithPreInit(C
);
6978 Record
.AddStmt(C
->getPriority());
6979 Record
.AddSourceLocation(C
->getLParenLoc());
6982 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause
*C
) {
6983 VisitOMPClauseWithPreInit(C
);
6984 Record
.writeEnum(C
->getModifier());
6985 Record
.AddStmt(C
->getGrainsize());
6986 Record
.AddSourceLocation(C
->getModifierLoc());
6987 Record
.AddSourceLocation(C
->getLParenLoc());
6990 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause
*C
) {
6991 VisitOMPClauseWithPreInit(C
);
6992 Record
.writeEnum(C
->getModifier());
6993 Record
.AddStmt(C
->getNumTasks());
6994 Record
.AddSourceLocation(C
->getModifierLoc());
6995 Record
.AddSourceLocation(C
->getLParenLoc());
6998 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause
*C
) {
6999 Record
.AddStmt(C
->getHint());
7000 Record
.AddSourceLocation(C
->getLParenLoc());
7003 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause
*C
) {
7004 VisitOMPClauseWithPreInit(C
);
7005 Record
.push_back(C
->getDistScheduleKind());
7006 Record
.AddStmt(C
->getChunkSize());
7007 Record
.AddSourceLocation(C
->getLParenLoc());
7008 Record
.AddSourceLocation(C
->getDistScheduleKindLoc());
7009 Record
.AddSourceLocation(C
->getCommaLoc());
7012 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause
*C
) {
7013 Record
.push_back(C
->getDefaultmapKind());
7014 Record
.push_back(C
->getDefaultmapModifier());
7015 Record
.AddSourceLocation(C
->getLParenLoc());
7016 Record
.AddSourceLocation(C
->getDefaultmapModifierLoc());
7017 Record
.AddSourceLocation(C
->getDefaultmapKindLoc());
7020 void OMPClauseWriter::VisitOMPToClause(OMPToClause
*C
) {
7021 Record
.push_back(C
->varlist_size());
7022 Record
.push_back(C
->getUniqueDeclarationsNum());
7023 Record
.push_back(C
->getTotalComponentListNum());
7024 Record
.push_back(C
->getTotalComponentsNum());
7025 Record
.AddSourceLocation(C
->getLParenLoc());
7026 for (unsigned I
= 0; I
< NumberOfOMPMotionModifiers
; ++I
) {
7027 Record
.push_back(C
->getMotionModifier(I
));
7028 Record
.AddSourceLocation(C
->getMotionModifierLoc(I
));
7030 Record
.AddNestedNameSpecifierLoc(C
->getMapperQualifierLoc());
7031 Record
.AddDeclarationNameInfo(C
->getMapperIdInfo());
7032 Record
.AddSourceLocation(C
->getColonLoc());
7033 for (auto *E
: C
->varlists())
7035 for (auto *E
: C
->mapperlists())
7037 for (auto *D
: C
->all_decls())
7038 Record
.AddDeclRef(D
);
7039 for (auto N
: C
->all_num_lists())
7040 Record
.push_back(N
);
7041 for (auto N
: C
->all_lists_sizes())
7042 Record
.push_back(N
);
7043 for (auto &M
: C
->all_components()) {
7044 Record
.AddStmt(M
.getAssociatedExpression());
7045 Record
.writeBool(M
.isNonContiguous());
7046 Record
.AddDeclRef(M
.getAssociatedDeclaration());
7050 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause
*C
) {
7051 Record
.push_back(C
->varlist_size());
7052 Record
.push_back(C
->getUniqueDeclarationsNum());
7053 Record
.push_back(C
->getTotalComponentListNum());
7054 Record
.push_back(C
->getTotalComponentsNum());
7055 Record
.AddSourceLocation(C
->getLParenLoc());
7056 for (unsigned I
= 0; I
< NumberOfOMPMotionModifiers
; ++I
) {
7057 Record
.push_back(C
->getMotionModifier(I
));
7058 Record
.AddSourceLocation(C
->getMotionModifierLoc(I
));
7060 Record
.AddNestedNameSpecifierLoc(C
->getMapperQualifierLoc());
7061 Record
.AddDeclarationNameInfo(C
->getMapperIdInfo());
7062 Record
.AddSourceLocation(C
->getColonLoc());
7063 for (auto *E
: C
->varlists())
7065 for (auto *E
: C
->mapperlists())
7067 for (auto *D
: C
->all_decls())
7068 Record
.AddDeclRef(D
);
7069 for (auto N
: C
->all_num_lists())
7070 Record
.push_back(N
);
7071 for (auto N
: C
->all_lists_sizes())
7072 Record
.push_back(N
);
7073 for (auto &M
: C
->all_components()) {
7074 Record
.AddStmt(M
.getAssociatedExpression());
7075 Record
.writeBool(M
.isNonContiguous());
7076 Record
.AddDeclRef(M
.getAssociatedDeclaration());
7080 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause
*C
) {
7081 Record
.push_back(C
->varlist_size());
7082 Record
.push_back(C
->getUniqueDeclarationsNum());
7083 Record
.push_back(C
->getTotalComponentListNum());
7084 Record
.push_back(C
->getTotalComponentsNum());
7085 Record
.AddSourceLocation(C
->getLParenLoc());
7086 for (auto *E
: C
->varlists())
7088 for (auto *VE
: C
->private_copies())
7090 for (auto *VE
: C
->inits())
7092 for (auto *D
: C
->all_decls())
7093 Record
.AddDeclRef(D
);
7094 for (auto N
: C
->all_num_lists())
7095 Record
.push_back(N
);
7096 for (auto N
: C
->all_lists_sizes())
7097 Record
.push_back(N
);
7098 for (auto &M
: C
->all_components()) {
7099 Record
.AddStmt(M
.getAssociatedExpression());
7100 Record
.AddDeclRef(M
.getAssociatedDeclaration());
7104 void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause
*C
) {
7105 Record
.push_back(C
->varlist_size());
7106 Record
.push_back(C
->getUniqueDeclarationsNum());
7107 Record
.push_back(C
->getTotalComponentListNum());
7108 Record
.push_back(C
->getTotalComponentsNum());
7109 Record
.AddSourceLocation(C
->getLParenLoc());
7110 for (auto *E
: C
->varlists())
7112 for (auto *D
: C
->all_decls())
7113 Record
.AddDeclRef(D
);
7114 for (auto N
: C
->all_num_lists())
7115 Record
.push_back(N
);
7116 for (auto N
: C
->all_lists_sizes())
7117 Record
.push_back(N
);
7118 for (auto &M
: C
->all_components()) {
7119 Record
.AddStmt(M
.getAssociatedExpression());
7120 Record
.AddDeclRef(M
.getAssociatedDeclaration());
7124 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause
*C
) {
7125 Record
.push_back(C
->varlist_size());
7126 Record
.push_back(C
->getUniqueDeclarationsNum());
7127 Record
.push_back(C
->getTotalComponentListNum());
7128 Record
.push_back(C
->getTotalComponentsNum());
7129 Record
.AddSourceLocation(C
->getLParenLoc());
7130 for (auto *E
: C
->varlists())
7132 for (auto *D
: C
->all_decls())
7133 Record
.AddDeclRef(D
);
7134 for (auto N
: C
->all_num_lists())
7135 Record
.push_back(N
);
7136 for (auto N
: C
->all_lists_sizes())
7137 Record
.push_back(N
);
7138 for (auto &M
: C
->all_components()) {
7139 Record
.AddStmt(M
.getAssociatedExpression());
7140 Record
.AddDeclRef(M
.getAssociatedDeclaration());
7144 void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause
*C
) {
7145 Record
.push_back(C
->varlist_size());
7146 Record
.push_back(C
->getUniqueDeclarationsNum());
7147 Record
.push_back(C
->getTotalComponentListNum());
7148 Record
.push_back(C
->getTotalComponentsNum());
7149 Record
.AddSourceLocation(C
->getLParenLoc());
7150 for (auto *E
: C
->varlists())
7152 for (auto *D
: C
->all_decls())
7153 Record
.AddDeclRef(D
);
7154 for (auto N
: C
->all_num_lists())
7155 Record
.push_back(N
);
7156 for (auto N
: C
->all_lists_sizes())
7157 Record
.push_back(N
);
7158 for (auto &M
: C
->all_components()) {
7159 Record
.AddStmt(M
.getAssociatedExpression());
7160 Record
.AddDeclRef(M
.getAssociatedDeclaration());
7164 void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause
*) {}
7166 void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7167 OMPUnifiedSharedMemoryClause
*) {}
7169 void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause
*) {}
7172 OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause
*) {
7175 void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7176 OMPAtomicDefaultMemOrderClause
*C
) {
7177 Record
.push_back(C
->getAtomicDefaultMemOrderKind());
7178 Record
.AddSourceLocation(C
->getLParenLoc());
7179 Record
.AddSourceLocation(C
->getAtomicDefaultMemOrderKindKwLoc());
7182 void OMPClauseWriter::VisitOMPAtClause(OMPAtClause
*C
) {
7183 Record
.push_back(C
->getAtKind());
7184 Record
.AddSourceLocation(C
->getLParenLoc());
7185 Record
.AddSourceLocation(C
->getAtKindKwLoc());
7188 void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause
*C
) {
7189 Record
.push_back(C
->getSeverityKind());
7190 Record
.AddSourceLocation(C
->getLParenLoc());
7191 Record
.AddSourceLocation(C
->getSeverityKindKwLoc());
7194 void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause
*C
) {
7195 Record
.AddStmt(C
->getMessageString());
7196 Record
.AddSourceLocation(C
->getLParenLoc());
7199 void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause
*C
) {
7200 Record
.push_back(C
->varlist_size());
7201 Record
.AddSourceLocation(C
->getLParenLoc());
7202 for (auto *VE
: C
->varlists())
7204 for (auto *E
: C
->private_refs())
7208 void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause
*C
) {
7209 Record
.push_back(C
->varlist_size());
7210 Record
.AddSourceLocation(C
->getLParenLoc());
7211 for (auto *VE
: C
->varlists())
7215 void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause
*C
) {
7216 Record
.push_back(C
->varlist_size());
7217 Record
.AddSourceLocation(C
->getLParenLoc());
7218 for (auto *VE
: C
->varlists())
7222 void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause
*C
) {
7223 Record
.writeEnum(C
->getKind());
7224 Record
.writeEnum(C
->getModifier());
7225 Record
.AddSourceLocation(C
->getLParenLoc());
7226 Record
.AddSourceLocation(C
->getKindKwLoc());
7227 Record
.AddSourceLocation(C
->getModifierKwLoc());
7230 void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause
*C
) {
7231 Record
.push_back(C
->getNumberOfAllocators());
7232 Record
.AddSourceLocation(C
->getLParenLoc());
7233 for (unsigned I
= 0, E
= C
->getNumberOfAllocators(); I
< E
; ++I
) {
7234 OMPUsesAllocatorsClause::Data Data
= C
->getAllocatorData(I
);
7235 Record
.AddStmt(Data
.Allocator
);
7236 Record
.AddStmt(Data
.AllocatorTraits
);
7237 Record
.AddSourceLocation(Data
.LParenLoc
);
7238 Record
.AddSourceLocation(Data
.RParenLoc
);
7242 void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause
*C
) {
7243 Record
.push_back(C
->varlist_size());
7244 Record
.AddSourceLocation(C
->getLParenLoc());
7245 Record
.AddStmt(C
->getModifier());
7246 Record
.AddSourceLocation(C
->getColonLoc());
7247 for (Expr
*E
: C
->varlists())
7251 void OMPClauseWriter::VisitOMPBindClause(OMPBindClause
*C
) {
7252 Record
.writeEnum(C
->getBindKind());
7253 Record
.AddSourceLocation(C
->getLParenLoc());
7254 Record
.AddSourceLocation(C
->getBindKindLoc());
7257 void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause
*C
) {
7258 VisitOMPClauseWithPreInit(C
);
7259 Record
.AddStmt(C
->getSize());
7260 Record
.AddSourceLocation(C
->getLParenLoc());
7263 void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause
*C
) {
7264 Record
.push_back(C
->varlist_size());
7265 Record
.push_back(C
->getNumLoops());
7266 Record
.AddSourceLocation(C
->getLParenLoc());
7267 Record
.push_back(C
->getDependenceType());
7268 Record
.AddSourceLocation(C
->getDependenceLoc());
7269 Record
.AddSourceLocation(C
->getColonLoc());
7270 for (auto *VE
: C
->varlists())
7272 for (unsigned I
= 0, E
= C
->getNumLoops(); I
< E
; ++I
)
7273 Record
.AddStmt(C
->getLoopData(I
));
7276 void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause
*C
) {
7277 Record
.AddAttributes(C
->getAttrs());
7278 Record
.AddSourceLocation(C
->getBeginLoc());
7279 Record
.AddSourceLocation(C
->getLParenLoc());
7280 Record
.AddSourceLocation(C
->getEndLoc());
7283 void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause
*C
) {}
7285 void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo
*TI
) {
7286 writeUInt32(TI
->Sets
.size());
7287 for (const auto &Set
: TI
->Sets
) {
7288 writeEnum(Set
.Kind
);
7289 writeUInt32(Set
.Selectors
.size());
7290 for (const auto &Selector
: Set
.Selectors
) {
7291 writeEnum(Selector
.Kind
);
7292 writeBool(Selector
.ScoreOrCondition
);
7293 if (Selector
.ScoreOrCondition
)
7294 writeExprRef(Selector
.ScoreOrCondition
);
7295 writeUInt32(Selector
.Properties
.size());
7296 for (const auto &Property
: Selector
.Properties
)
7297 writeEnum(Property
.Kind
);
7302 void ASTRecordWriter::writeOMPChildren(OMPChildren
*Data
) {
7305 writeUInt32(Data
->getNumClauses());
7306 writeUInt32(Data
->getNumChildren());
7307 writeBool(Data
->hasAssociatedStmt());
7308 for (unsigned I
= 0, E
= Data
->getNumClauses(); I
< E
; ++I
)
7309 writeOMPClause(Data
->getClauses()[I
]);
7310 if (Data
->hasAssociatedStmt())
7311 AddStmt(Data
->getAssociatedStmt());
7312 for (unsigned I
= 0, E
= Data
->getNumChildren(); I
< E
; ++I
)
7313 AddStmt(Data
->getChildren()[I
]);