1 //===-- ASTReader.cpp - AST File Reader ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTReader class, which reads AST files.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ASTReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/Frontend/PCHContainerOperations.h"
23 #include "clang/AST/ASTMutationListener.h"
24 #include "clang/AST/NestedNameSpecifier.h"
25 #include "clang/AST/Type.h"
26 #include "clang/AST/TypeLocVisitor.h"
27 #include "clang/Basic/DiagnosticOptions.h"
28 #include "clang/Basic/FileManager.h"
29 #include "clang/Basic/SourceManager.h"
30 #include "clang/Basic/SourceManagerInternals.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/Basic/TargetOptions.h"
33 #include "clang/Basic/Version.h"
34 #include "clang/Basic/VersionTuple.h"
35 #include "clang/Frontend/Utils.h"
36 #include "clang/Lex/HeaderSearch.h"
37 #include "clang/Lex/HeaderSearchOptions.h"
38 #include "clang/Lex/MacroInfo.h"
39 #include "clang/Lex/PreprocessingRecord.h"
40 #include "clang/Lex/Preprocessor.h"
41 #include "clang/Lex/PreprocessorOptions.h"
42 #include "clang/Sema/Scope.h"
43 #include "clang/Sema/Sema.h"
44 #include "clang/Serialization/ASTDeserializationListener.h"
45 #include "clang/Serialization/GlobalModuleIndex.h"
46 #include "clang/Serialization/ModuleManager.h"
47 #include "clang/Serialization/SerializationDiagnostic.h"
48 #include "llvm/ADT/Hashing.h"
49 #include "llvm/ADT/StringExtras.h"
50 #include "llvm/Bitcode/BitstreamReader.h"
51 #include "llvm/Support/Compression.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/FileSystem.h"
54 #include "llvm/Support/MemoryBuffer.h"
55 #include "llvm/Support/Path.h"
56 #include "llvm/Support/SaveAndRestore.h"
57 #include "llvm/Support/raw_ostream.h"
61 #include <system_error>
63 using namespace clang;
64 using namespace clang::serialization;
65 using namespace clang::serialization::reader;
66 using llvm::BitstreamCursor;
69 //===----------------------------------------------------------------------===//
70 // ChainedASTReaderListener implementation
71 //===----------------------------------------------------------------------===//
74 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
75 return First->ReadFullVersionInformation(FullVersion) ||
76 Second->ReadFullVersionInformation(FullVersion);
78 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
79 First->ReadModuleName(ModuleName);
80 Second->ReadModuleName(ModuleName);
82 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
83 First->ReadModuleMapFile(ModuleMapPath);
84 Second->ReadModuleMapFile(ModuleMapPath);
87 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
89 bool AllowCompatibleDifferences) {
90 return First->ReadLanguageOptions(LangOpts, Complain,
91 AllowCompatibleDifferences) ||
92 Second->ReadLanguageOptions(LangOpts, Complain,
93 AllowCompatibleDifferences);
95 bool ChainedASTReaderListener::ReadTargetOptions(
96 const TargetOptions &TargetOpts, bool Complain,
97 bool AllowCompatibleDifferences) {
98 return First->ReadTargetOptions(TargetOpts, Complain,
99 AllowCompatibleDifferences) ||
100 Second->ReadTargetOptions(TargetOpts, Complain,
101 AllowCompatibleDifferences);
103 bool ChainedASTReaderListener::ReadDiagnosticOptions(
104 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
105 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
106 Second->ReadDiagnosticOptions(DiagOpts, Complain);
109 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
111 return First->ReadFileSystemOptions(FSOpts, Complain) ||
112 Second->ReadFileSystemOptions(FSOpts, Complain);
115 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
116 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
118 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
120 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
123 bool ChainedASTReaderListener::ReadPreprocessorOptions(
124 const PreprocessorOptions &PPOpts, bool Complain,
125 std::string &SuggestedPredefines) {
126 return First->ReadPreprocessorOptions(PPOpts, Complain,
127 SuggestedPredefines) ||
128 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
130 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
132 First->ReadCounter(M, Value);
133 Second->ReadCounter(M, Value);
135 bool ChainedASTReaderListener::needsInputFileVisitation() {
136 return First->needsInputFileVisitation() ||
137 Second->needsInputFileVisitation();
139 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
140 return First->needsSystemInputFileVisitation() ||
141 Second->needsSystemInputFileVisitation();
143 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
145 First->visitModuleFile(Filename, Kind);
146 Second->visitModuleFile(Filename, Kind);
148 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
151 bool isExplicitModule) {
152 bool Continue = false;
153 if (First->needsInputFileVisitation() &&
154 (!isSystem || First->needsSystemInputFileVisitation()))
155 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
157 if (Second->needsInputFileVisitation() &&
158 (!isSystem || Second->needsSystemInputFileVisitation()))
159 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
164 void ChainedASTReaderListener::readModuleFileExtension(
165 const ModuleFileExtensionMetadata &Metadata) {
166 First->readModuleFileExtension(Metadata);
167 Second->readModuleFileExtension(Metadata);
170 //===----------------------------------------------------------------------===//
171 // PCH validator implementation
172 //===----------------------------------------------------------------------===//
174 ASTReaderListener::~ASTReaderListener() {}
176 /// \brief Compare the given set of language options against an existing set of
177 /// language options.
179 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
180 /// \param AllowCompatibleDifferences If true, differences between compatible
181 /// language options will be permitted.
183 /// \returns true if the languagae options mis-match, false otherwise.
184 static bool checkLanguageOptions(const LangOptions &LangOpts,
185 const LangOptions &ExistingLangOpts,
186 DiagnosticsEngine *Diags,
187 bool AllowCompatibleDifferences = true) {
188 #define LANGOPT(Name, Bits, Default, Description) \
189 if (ExistingLangOpts.Name != LangOpts.Name) { \
191 Diags->Report(diag::err_pch_langopt_mismatch) \
192 << Description << LangOpts.Name << ExistingLangOpts.Name; \
196 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
197 if (ExistingLangOpts.Name != LangOpts.Name) { \
199 Diags->Report(diag::err_pch_langopt_value_mismatch) \
204 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
205 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
207 Diags->Report(diag::err_pch_langopt_value_mismatch) \
212 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
213 if (!AllowCompatibleDifferences) \
214 LANGOPT(Name, Bits, Default, Description)
216 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
217 if (!AllowCompatibleDifferences) \
218 ENUM_LANGOPT(Name, Bits, Default, Description)
220 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
221 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
222 #include "clang/Basic/LangOptions.def"
224 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
226 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
230 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
232 Diags->Report(diag::err_pch_langopt_value_mismatch)
233 << "target Objective-C runtime";
237 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
238 LangOpts.CommentOpts.BlockCommandNames) {
240 Diags->Report(diag::err_pch_langopt_value_mismatch)
241 << "block command names";
248 /// \brief Compare the given set of target options against an existing set of
251 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
253 /// \returns true if the target options mis-match, false otherwise.
254 static bool checkTargetOptions(const TargetOptions &TargetOpts,
255 const TargetOptions &ExistingTargetOpts,
256 DiagnosticsEngine *Diags,
257 bool AllowCompatibleDifferences = true) {
258 #define CHECK_TARGET_OPT(Field, Name) \
259 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
261 Diags->Report(diag::err_pch_targetopt_mismatch) \
262 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
266 // The triple and ABI must match exactly.
267 CHECK_TARGET_OPT(Triple, "target");
268 CHECK_TARGET_OPT(ABI, "target ABI");
270 // We can tolerate different CPUs in many cases, notably when one CPU
271 // supports a strict superset of another. When allowing compatible
272 // differences skip this check.
273 if (!AllowCompatibleDifferences)
274 CHECK_TARGET_OPT(CPU, "target CPU");
276 #undef CHECK_TARGET_OPT
278 // Compare feature sets.
279 SmallVector<StringRef, 4> ExistingFeatures(
280 ExistingTargetOpts.FeaturesAsWritten.begin(),
281 ExistingTargetOpts.FeaturesAsWritten.end());
282 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
283 TargetOpts.FeaturesAsWritten.end());
284 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
285 std::sort(ReadFeatures.begin(), ReadFeatures.end());
287 // We compute the set difference in both directions explicitly so that we can
288 // diagnose the differences differently.
289 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
291 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
292 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
293 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
294 ExistingFeatures.begin(), ExistingFeatures.end(),
295 std::back_inserter(UnmatchedReadFeatures));
297 // If we are allowing compatible differences and the read feature set is
298 // a strict subset of the existing feature set, there is nothing to diagnose.
299 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
303 for (StringRef Feature : UnmatchedReadFeatures)
304 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
305 << /* is-existing-feature */ false << Feature;
306 for (StringRef Feature : UnmatchedExistingFeatures)
307 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
308 << /* is-existing-feature */ true << Feature;
311 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
315 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
317 bool AllowCompatibleDifferences) {
318 const LangOptions &ExistingLangOpts = PP.getLangOpts();
319 return checkLanguageOptions(LangOpts, ExistingLangOpts,
320 Complain ? &Reader.Diags : nullptr,
321 AllowCompatibleDifferences);
324 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
326 bool AllowCompatibleDifferences) {
327 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
328 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
329 Complain ? &Reader.Diags : nullptr,
330 AllowCompatibleDifferences);
334 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
336 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
340 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
341 DiagnosticsEngine &Diags,
343 typedef DiagnosticsEngine::Level Level;
345 // Check current mappings for new -Werror mappings, and the stored mappings
346 // for cases that were explicitly mapped to *not* be errors that are now
347 // errors because of options like -Werror.
348 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
350 for (DiagnosticsEngine *MappingSource : MappingSources) {
351 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
352 diag::kind DiagID = DiagIDMappingPair.first;
353 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
354 if (CurLevel < DiagnosticsEngine::Error)
355 continue; // not significant
357 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
358 if (StoredLevel < DiagnosticsEngine::Error) {
360 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
361 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
370 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
371 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
372 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
374 return Ext >= diag::Severity::Error;
377 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
378 DiagnosticsEngine &Diags,
379 bool IsSystem, bool Complain) {
382 if (Diags.getSuppressSystemWarnings())
384 // If -Wsystem-headers was not enabled before, be conservative
385 if (StoredDiags.getSuppressSystemWarnings()) {
387 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
392 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
394 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
398 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
399 !StoredDiags.getEnableAllWarnings()) {
401 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
405 if (isExtHandlingFromDiagsError(Diags) &&
406 !isExtHandlingFromDiagsError(StoredDiags)) {
408 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
412 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
415 bool PCHValidator::ReadDiagnosticOptions(
416 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
417 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
418 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
419 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
420 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
421 // This should never fail, because we would have processed these options
422 // before writing them to an ASTFile.
423 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
425 ModuleManager &ModuleMgr = Reader.getModuleManager();
426 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
428 // If the original import came from a file explicitly generated by the user,
429 // don't check the diagnostic mappings.
430 // FIXME: currently this is approximated by checking whether this is not a
431 // module import of an implicitly-loaded module file.
432 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
433 // the transitive closure of its imports, since unrelated modules cannot be
434 // imported until after this module finishes validation.
435 ModuleFile *TopImport = *ModuleMgr.rbegin();
436 while (!TopImport->ImportedBy.empty())
437 TopImport = TopImport->ImportedBy[0];
438 if (TopImport->Kind != MK_ImplicitModule)
441 StringRef ModuleName = TopImport->ModuleName;
442 assert(!ModuleName.empty() && "diagnostic options read before module name");
444 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
445 assert(M && "missing module");
447 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
448 // contains the union of their flags.
449 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
452 /// \brief Collect the macro definitions provided by the given preprocessor
455 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
456 MacroDefinitionsMap &Macros,
457 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
458 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
459 StringRef Macro = PPOpts.Macros[I].first;
460 bool IsUndef = PPOpts.Macros[I].second;
462 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
463 StringRef MacroName = MacroPair.first;
464 StringRef MacroBody = MacroPair.second;
466 // For an #undef'd macro, we only care about the name.
468 if (MacroNames && !Macros.count(MacroName))
469 MacroNames->push_back(MacroName);
471 Macros[MacroName] = std::make_pair("", true);
475 // For a #define'd macro, figure out the actual definition.
476 if (MacroName.size() == Macro.size())
479 // Note: GCC drops anything following an end-of-line character.
480 StringRef::size_type End = MacroBody.find_first_of("\n\r");
481 MacroBody = MacroBody.substr(0, End);
484 if (MacroNames && !Macros.count(MacroName))
485 MacroNames->push_back(MacroName);
486 Macros[MacroName] = std::make_pair(MacroBody, false);
490 /// \brief Check the preprocessor options deserialized from the control block
491 /// against the preprocessor options in an existing preprocessor.
493 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
494 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
495 const PreprocessorOptions &ExistingPPOpts,
496 DiagnosticsEngine *Diags,
497 FileManager &FileMgr,
498 std::string &SuggestedPredefines,
499 const LangOptions &LangOpts) {
500 // Check macro definitions.
501 MacroDefinitionsMap ASTFileMacros;
502 collectMacroDefinitions(PPOpts, ASTFileMacros);
503 MacroDefinitionsMap ExistingMacros;
504 SmallVector<StringRef, 4> ExistingMacroNames;
505 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
507 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
508 // Dig out the macro definition in the existing preprocessor options.
509 StringRef MacroName = ExistingMacroNames[I];
510 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
512 // Check whether we know anything about this macro name or not.
513 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
514 = ASTFileMacros.find(MacroName);
515 if (Known == ASTFileMacros.end()) {
516 // FIXME: Check whether this identifier was referenced anywhere in the
517 // AST file. If so, we should reject the AST file. Unfortunately, this
518 // information isn't in the control block. What shall we do about it?
520 if (Existing.second) {
521 SuggestedPredefines += "#undef ";
522 SuggestedPredefines += MacroName.str();
523 SuggestedPredefines += '\n';
525 SuggestedPredefines += "#define ";
526 SuggestedPredefines += MacroName.str();
527 SuggestedPredefines += ' ';
528 SuggestedPredefines += Existing.first.str();
529 SuggestedPredefines += '\n';
534 // If the macro was defined in one but undef'd in the other, we have a
536 if (Existing.second != Known->second.second) {
538 Diags->Report(diag::err_pch_macro_def_undef)
539 << MacroName << Known->second.second;
544 // If the macro was #undef'd in both, or if the macro bodies are identical,
546 if (Existing.second || Existing.first == Known->second.first)
549 // The macro bodies differ; complain.
551 Diags->Report(diag::err_pch_macro_def_conflict)
552 << MacroName << Known->second.first << Existing.first;
557 // Check whether we're using predefines.
558 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
560 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
565 // Detailed record is important since it is used for the module cache hash.
566 if (LangOpts.Modules &&
567 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
569 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
574 // Compute the #include and #include_macros lines we need.
575 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
576 StringRef File = ExistingPPOpts.Includes[I];
577 if (File == ExistingPPOpts.ImplicitPCHInclude)
580 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
581 != PPOpts.Includes.end())
584 SuggestedPredefines += "#include \"";
585 SuggestedPredefines += File;
586 SuggestedPredefines += "\"\n";
589 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
590 StringRef File = ExistingPPOpts.MacroIncludes[I];
591 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
593 != PPOpts.MacroIncludes.end())
596 SuggestedPredefines += "#__include_macros \"";
597 SuggestedPredefines += File;
598 SuggestedPredefines += "\"\n##\n";
604 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
606 std::string &SuggestedPredefines) {
607 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
609 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
610 Complain? &Reader.Diags : nullptr,
616 /// Check the header search options deserialized from the control block
617 /// against the header search options in an existing preprocessor.
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
621 StringRef SpecificModuleCachePath,
622 StringRef ExistingModuleCachePath,
623 DiagnosticsEngine *Diags,
624 const LangOptions &LangOpts) {
625 if (LangOpts.Modules) {
626 if (SpecificModuleCachePath != ExistingModuleCachePath) {
628 Diags->Report(diag::err_pch_modulecache_mismatch)
629 << SpecificModuleCachePath << ExistingModuleCachePath;
637 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
638 StringRef SpecificModuleCachePath,
640 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
641 PP.getHeaderSearchInfo().getModuleCachePath(),
642 Complain ? &Reader.Diags : nullptr,
646 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
647 PP.setCounterValue(Value);
650 //===----------------------------------------------------------------------===//
651 // AST reader implementation
652 //===----------------------------------------------------------------------===//
654 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
655 bool TakeOwnership) {
656 DeserializationListener = Listener;
657 OwnsDeserializationListener = TakeOwnership;
662 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
663 return serialization::ComputeHash(Sel);
667 std::pair<unsigned, unsigned>
668 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
669 using namespace llvm::support;
670 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
671 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
672 return std::make_pair(KeyLen, DataLen);
675 ASTSelectorLookupTrait::internal_key_type
676 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
677 using namespace llvm::support;
678 SelectorTable &SelTable = Reader.getContext().Selectors;
679 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
680 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
681 F, endian::readNext<uint32_t, little, unaligned>(d));
683 return SelTable.getNullarySelector(FirstII);
685 return SelTable.getUnarySelector(FirstII);
687 SmallVector<IdentifierInfo *, 16> Args;
688 Args.push_back(FirstII);
689 for (unsigned I = 1; I != N; ++I)
690 Args.push_back(Reader.getLocalIdentifier(
691 F, endian::readNext<uint32_t, little, unaligned>(d)));
693 return SelTable.getSelector(N, Args.data());
696 ASTSelectorLookupTrait::data_type
697 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
699 using namespace llvm::support;
703 Result.ID = Reader.getGlobalSelectorID(
704 F, endian::readNext<uint32_t, little, unaligned>(d));
705 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
706 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
707 Result.InstanceBits = FullInstanceBits & 0x3;
708 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
709 Result.FactoryBits = FullFactoryBits & 0x3;
710 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
711 unsigned NumInstanceMethods = FullInstanceBits >> 3;
712 unsigned NumFactoryMethods = FullFactoryBits >> 3;
714 // Load instance methods
715 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
716 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
717 F, endian::readNext<uint32_t, little, unaligned>(d)))
718 Result.Instance.push_back(Method);
721 // Load factory methods
722 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
723 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
724 F, endian::readNext<uint32_t, little, unaligned>(d)))
725 Result.Factory.push_back(Method);
731 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
732 return llvm::HashString(a);
735 std::pair<unsigned, unsigned>
736 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
737 using namespace llvm::support;
738 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
739 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
740 return std::make_pair(KeyLen, DataLen);
743 ASTIdentifierLookupTraitBase::internal_key_type
744 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
745 assert(n >= 2 && d[n-1] == '\0');
746 return StringRef((const char*) d, n-1);
749 /// \brief Whether the given identifier is "interesting".
750 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
752 return II.hadMacroDefinition() ||
754 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
755 II.hasRevertedTokenIDToIdentifier() ||
756 (!(IsModule && Reader.getContext().getLangOpts().CPlusPlus) &&
757 II.getFETokenInfo<void>());
760 static bool readBit(unsigned &Bits) {
761 bool Value = Bits & 0x1;
766 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
767 using namespace llvm::support;
768 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
769 return Reader.getGlobalIdentifierID(F, RawID >> 1);
772 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
773 if (!II.isFromAST()) {
775 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
776 if (isInterestingIdentifier(Reader, II, IsModule))
777 II.setChangedSinceDeserialization();
781 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
782 const unsigned char* d,
784 using namespace llvm::support;
785 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
786 bool IsInteresting = RawID & 0x01;
788 // Wipe out the "is interesting" bit.
791 // Build the IdentifierInfo and link the identifier ID with it.
792 IdentifierInfo *II = KnownII;
794 II = &Reader.getIdentifierTable().getOwn(k);
797 markIdentifierFromAST(Reader, *II);
798 Reader.markIdentifierUpToDate(II);
800 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
801 if (!IsInteresting) {
802 // For uninteresting identifiers, there's nothing else to do. Just notify
803 // the reader that we've finished loading this identifier.
804 Reader.SetIdentifierInfo(ID, II);
808 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
809 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
810 bool CPlusPlusOperatorKeyword = readBit(Bits);
811 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
812 bool HasRevertedBuiltin = readBit(Bits);
813 bool Poisoned = readBit(Bits);
814 bool ExtensionToken = readBit(Bits);
815 bool HadMacroDefinition = readBit(Bits);
817 assert(Bits == 0 && "Extra bits in the identifier?");
820 // Set or check the various bits in the IdentifierInfo structure.
821 // Token IDs are read-only.
822 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
823 II->revertTokenIDToIdentifier();
825 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
826 else if (HasRevertedBuiltin && II->getBuiltinID()) {
828 assert((II->hasRevertedBuiltin() ||
829 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
830 "Incorrect ObjC keyword or builtin ID");
832 assert(II->isExtensionToken() == ExtensionToken &&
833 "Incorrect extension token flag");
834 (void)ExtensionToken;
836 II->setIsPoisoned(true);
837 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
838 "Incorrect C++ operator keyword flag");
839 (void)CPlusPlusOperatorKeyword;
841 // If this identifier is a macro, deserialize the macro
843 if (HadMacroDefinition) {
844 uint32_t MacroDirectivesOffset =
845 endian::readNext<uint32_t, little, unaligned>(d);
848 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
851 Reader.SetIdentifierInfo(ID, II);
853 // Read all of the declarations visible at global scope with this
856 SmallVector<uint32_t, 4> DeclIDs;
857 for (; DataLen > 0; DataLen -= 4)
858 DeclIDs.push_back(Reader.getGlobalDeclID(
859 F, endian::readNext<uint32_t, little, unaligned>(d)));
860 Reader.SetGloballyVisibleDecls(II, DeclIDs);
866 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
867 : Kind(Name.getNameKind()) {
869 case DeclarationName::Identifier:
870 Data = (uint64_t)Name.getAsIdentifierInfo();
872 case DeclarationName::ObjCZeroArgSelector:
873 case DeclarationName::ObjCOneArgSelector:
874 case DeclarationName::ObjCMultiArgSelector:
875 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
877 case DeclarationName::CXXOperatorName:
878 Data = Name.getCXXOverloadedOperator();
880 case DeclarationName::CXXLiteralOperatorName:
881 Data = (uint64_t)Name.getCXXLiteralIdentifier();
883 case DeclarationName::CXXConstructorName:
884 case DeclarationName::CXXDestructorName:
885 case DeclarationName::CXXConversionFunctionName:
886 case DeclarationName::CXXUsingDirective:
892 unsigned DeclarationNameKey::getHash() const {
893 llvm::FoldingSetNodeID ID;
897 case DeclarationName::Identifier:
898 case DeclarationName::CXXLiteralOperatorName:
899 ID.AddString(((IdentifierInfo*)Data)->getName());
901 case DeclarationName::ObjCZeroArgSelector:
902 case DeclarationName::ObjCOneArgSelector:
903 case DeclarationName::ObjCMultiArgSelector:
904 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
906 case DeclarationName::CXXOperatorName:
907 ID.AddInteger((OverloadedOperatorKind)Data);
909 case DeclarationName::CXXConstructorName:
910 case DeclarationName::CXXDestructorName:
911 case DeclarationName::CXXConversionFunctionName:
912 case DeclarationName::CXXUsingDirective:
916 return ID.ComputeHash();
920 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
921 using namespace llvm::support;
922 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
923 return Reader.getLocalModuleFile(F, ModuleFileID);
926 std::pair<unsigned, unsigned>
927 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
928 using namespace llvm::support;
929 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
930 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
931 return std::make_pair(KeyLen, DataLen);
934 ASTDeclContextNameLookupTrait::internal_key_type
935 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
936 using namespace llvm::support;
938 auto Kind = (DeclarationName::NameKind)*d++;
941 case DeclarationName::Identifier:
942 Data = (uint64_t)Reader.getLocalIdentifier(
943 F, endian::readNext<uint32_t, little, unaligned>(d));
945 case DeclarationName::ObjCZeroArgSelector:
946 case DeclarationName::ObjCOneArgSelector:
947 case DeclarationName::ObjCMultiArgSelector:
949 (uint64_t)Reader.getLocalSelector(
950 F, endian::readNext<uint32_t, little, unaligned>(
951 d)).getAsOpaquePtr();
953 case DeclarationName::CXXOperatorName:
954 Data = *d++; // OverloadedOperatorKind
956 case DeclarationName::CXXLiteralOperatorName:
957 Data = (uint64_t)Reader.getLocalIdentifier(
958 F, endian::readNext<uint32_t, little, unaligned>(d));
960 case DeclarationName::CXXConstructorName:
961 case DeclarationName::CXXDestructorName:
962 case DeclarationName::CXXConversionFunctionName:
963 case DeclarationName::CXXUsingDirective:
968 return DeclarationNameKey(Kind, Data);
971 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
972 const unsigned char *d,
974 data_type_builder &Val) {
975 using namespace llvm::support;
976 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
977 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
978 Val.insert(Reader.getGlobalDeclID(F, LocalID));
982 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
983 BitstreamCursor &Cursor,
988 SavedStreamPosition SavedPosition(Cursor);
989 Cursor.JumpToBit(Offset);
993 unsigned Code = Cursor.ReadCode();
994 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
995 if (RecCode != DECL_CONTEXT_LEXICAL) {
996 Error("Expected lexical block");
1000 assert(!isa<TranslationUnitDecl>(DC) &&
1001 "expected a TU_UPDATE_LEXICAL record for TU");
1002 // If we are handling a C++ class template instantiation, we can see multiple
1003 // lexical updates for the same record. It's important that we select only one
1004 // of them, so that field numbering works properly. Just pick the first one we
1006 auto &Lex = LexicalDecls[DC];
1008 Lex = std::make_pair(
1009 &M, llvm::makeArrayRef(
1010 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1014 DC->setHasExternalLexicalStorage(true);
1018 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1019 BitstreamCursor &Cursor,
1022 assert(Offset != 0);
1024 SavedStreamPosition SavedPosition(Cursor);
1025 Cursor.JumpToBit(Offset);
1029 unsigned Code = Cursor.ReadCode();
1030 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1031 if (RecCode != DECL_CONTEXT_VISIBLE) {
1032 Error("Expected visible lookup table block");
1036 // We can't safely determine the primary context yet, so delay attaching the
1037 // lookup table until we're done with recursive deserialization.
1038 auto *Data = (const unsigned char*)Blob.data();
1039 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1043 void ASTReader::Error(StringRef Msg) {
1044 Error(diag::err_fe_pch_malformed, Msg);
1045 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1046 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1047 Diag(diag::note_module_cache_path)
1048 << PP.getHeaderSearchInfo().getModuleCachePath();
1052 void ASTReader::Error(unsigned DiagID,
1053 StringRef Arg1, StringRef Arg2) {
1054 if (Diags.isDiagnosticInFlight())
1055 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1057 Diag(DiagID) << Arg1 << Arg2;
1060 //===----------------------------------------------------------------------===//
1061 // Source Manager Deserialization
1062 //===----------------------------------------------------------------------===//
1064 /// \brief Read the line table in the source manager block.
1065 /// \returns true if there was an error.
1066 bool ASTReader::ParseLineTable(ModuleFile &F,
1067 const RecordData &Record) {
1069 LineTableInfo &LineTable = SourceMgr.getLineTable();
1071 // Parse the file names
1072 std::map<int, int> FileIDs;
1073 for (unsigned I = 0; Record[Idx]; ++I) {
1074 // Extract the file name
1075 auto Filename = ReadPath(F, Record, Idx);
1076 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1080 // Parse the line entries
1081 std::vector<LineEntry> Entries;
1082 while (Idx < Record.size()) {
1083 int FID = Record[Idx++];
1084 assert(FID >= 0 && "Serialized line entries for non-local file.");
1085 // Remap FileID from 1-based old view.
1086 FID += F.SLocEntryBaseID - 1;
1088 // Extract the line entries
1089 unsigned NumEntries = Record[Idx++];
1090 assert(NumEntries && "no line entries for file ID");
1092 Entries.reserve(NumEntries);
1093 for (unsigned I = 0; I != NumEntries; ++I) {
1094 unsigned FileOffset = Record[Idx++];
1095 unsigned LineNo = Record[Idx++];
1096 int FilenameID = FileIDs[Record[Idx++]];
1097 SrcMgr::CharacteristicKind FileKind
1098 = (SrcMgr::CharacteristicKind)Record[Idx++];
1099 unsigned IncludeOffset = Record[Idx++];
1100 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1101 FileKind, IncludeOffset));
1103 LineTable.AddEntry(FileID::get(FID), Entries);
1109 /// \brief Read a source manager block
1110 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1111 using namespace SrcMgr;
1113 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1115 // Set the source-location entry cursor to the current position in
1116 // the stream. This cursor will be used to read the contents of the
1117 // source manager block initially, and then lazily read
1118 // source-location entries as needed.
1119 SLocEntryCursor = F.Stream;
1121 // The stream itself is going to skip over the source manager block.
1122 if (F.Stream.SkipBlock()) {
1123 Error("malformed block record in AST file");
1127 // Enter the source manager block.
1128 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1129 Error("malformed source manager block record in AST file");
1135 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1138 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1139 case llvm::BitstreamEntry::Error:
1140 Error("malformed block record in AST file");
1142 case llvm::BitstreamEntry::EndBlock:
1144 case llvm::BitstreamEntry::Record:
1145 // The interesting case.
1152 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1153 default: // Default behavior: ignore.
1156 case SM_SLOC_FILE_ENTRY:
1157 case SM_SLOC_BUFFER_ENTRY:
1158 case SM_SLOC_EXPANSION_ENTRY:
1159 // Once we hit one of the source location entries, we're done.
1165 /// \brief If a header file is not found at the path that we expect it to be
1166 /// and the PCH file was moved from its original location, try to resolve the
1167 /// file by assuming that header+PCH were moved together and the header is in
1168 /// the same place relative to the PCH.
1170 resolveFileRelativeToOriginalDir(const std::string &Filename,
1171 const std::string &OriginalDir,
1172 const std::string &CurrDir) {
1173 assert(OriginalDir != CurrDir &&
1174 "No point trying to resolve the file if the PCH dir didn't change");
1175 using namespace llvm::sys;
1176 SmallString<128> filePath(Filename);
1177 fs::make_absolute(filePath);
1178 assert(path::is_absolute(OriginalDir));
1179 SmallString<128> currPCHPath(CurrDir);
1181 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1182 fileDirE = path::end(path::parent_path(filePath));
1183 path::const_iterator origDirI = path::begin(OriginalDir),
1184 origDirE = path::end(OriginalDir);
1185 // Skip the common path components from filePath and OriginalDir.
1186 while (fileDirI != fileDirE && origDirI != origDirE &&
1187 *fileDirI == *origDirI) {
1191 for (; origDirI != origDirE; ++origDirI)
1192 path::append(currPCHPath, "..");
1193 path::append(currPCHPath, fileDirI, fileDirE);
1194 path::append(currPCHPath, path::filename(Filename));
1195 return currPCHPath.str();
1198 bool ASTReader::ReadSLocEntry(int ID) {
1202 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1203 Error("source location entry ID out-of-range for AST file");
1207 // Local helper to read the (possibly-compressed) buffer data following the
1209 auto ReadBuffer = [this](
1210 BitstreamCursor &SLocEntryCursor,
1211 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1214 unsigned Code = SLocEntryCursor.ReadCode();
1215 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1217 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1218 SmallString<0> Uncompressed;
1219 if (llvm::zlib::uncompress(Blob, Uncompressed, Record[0]) !=
1220 llvm::zlib::StatusOK) {
1221 Error("could not decompress embedded file contents");
1224 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1225 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1226 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1228 Error("AST record has invalid code");
1233 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1234 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1235 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1236 unsigned BaseOffset = F->SLocEntryBaseOffset;
1238 ++NumSLocEntriesRead;
1239 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1240 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1241 Error("incorrectly-formatted source location entry in AST file");
1247 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1249 Error("incorrectly-formatted source location entry in AST file");
1252 case SM_SLOC_FILE_ENTRY: {
1253 // We will detect whether a file changed and return 'Failure' for it, but
1254 // we will also try to fail gracefully by setting up the SLocEntry.
1255 unsigned InputID = Record[4];
1256 InputFile IF = getInputFile(*F, InputID);
1257 const FileEntry *File = IF.getFile();
1258 bool OverriddenBuffer = IF.isOverridden();
1260 // Note that we only check if a File was returned. If it was out-of-date
1261 // we have complained but we will continue creating a FileID to recover
1266 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1267 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1268 // This is the module's main file.
1269 IncludeLoc = getImportLocation(F);
1271 SrcMgr::CharacteristicKind
1272 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1273 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1274 ID, BaseOffset + Record[0]);
1275 SrcMgr::FileInfo &FileInfo =
1276 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1277 FileInfo.NumCreatedFIDs = Record[5];
1279 FileInfo.setHasLineDirectives();
1281 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1282 unsigned NumFileDecls = Record[7];
1284 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1285 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1289 const SrcMgr::ContentCache *ContentCache
1290 = SourceMgr.getOrCreateContentCache(File,
1291 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1292 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1293 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1294 !ContentCache->getRawBuffer()) {
1295 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1298 SourceMgr.overrideFileContents(File, std::move(Buffer));
1304 case SM_SLOC_BUFFER_ENTRY: {
1305 const char *Name = Blob.data();
1306 unsigned Offset = Record[0];
1307 SrcMgr::CharacteristicKind
1308 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1309 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1310 if (IncludeLoc.isInvalid() &&
1311 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
1312 IncludeLoc = getImportLocation(F);
1315 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1318 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1319 BaseOffset + Offset, IncludeLoc);
1323 case SM_SLOC_EXPANSION_ENTRY: {
1324 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1325 SourceMgr.createExpansionLoc(SpellingLoc,
1326 ReadSourceLocation(*F, Record[2]),
1327 ReadSourceLocation(*F, Record[3]),
1330 BaseOffset + Record[0]);
1338 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1340 return std::make_pair(SourceLocation(), "");
1342 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1343 Error("source location entry ID out-of-range for AST file");
1344 return std::make_pair(SourceLocation(), "");
1347 // Find which module file this entry lands in.
1348 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1349 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
1350 return std::make_pair(SourceLocation(), "");
1352 // FIXME: Can we map this down to a particular submodule? That would be
1354 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1357 /// \brief Find the location where the module F is imported.
1358 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1359 if (F->ImportLoc.isValid())
1360 return F->ImportLoc;
1362 // Otherwise we have a PCH. It's considered to be "imported" at the first
1363 // location of its includer.
1364 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1365 // Main file is the importer.
1366 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1367 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1369 return F->ImportedBy[0]->FirstLoc;
1372 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1373 /// specified cursor. Read the abbreviations that are at the top of the block
1374 /// and then leave the cursor pointing into the block.
1375 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1376 if (Cursor.EnterSubBlock(BlockID))
1380 uint64_t Offset = Cursor.GetCurrentBitNo();
1381 unsigned Code = Cursor.ReadCode();
1383 // We expect all abbrevs to be at the start of the block.
1384 if (Code != llvm::bitc::DEFINE_ABBREV) {
1385 Cursor.JumpToBit(Offset);
1388 Cursor.ReadAbbrevRecord();
1392 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1396 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1397 Tok.setLength(Record[Idx++]);
1398 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1399 Tok.setIdentifierInfo(II);
1400 Tok.setKind((tok::TokenKind)Record[Idx++]);
1401 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1405 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1406 BitstreamCursor &Stream = F.MacroCursor;
1408 // Keep track of where we are in the stream, then jump back there
1409 // after reading this macro.
1410 SavedStreamPosition SavedPosition(Stream);
1412 Stream.JumpToBit(Offset);
1414 SmallVector<IdentifierInfo*, 16> MacroArgs;
1415 MacroInfo *Macro = nullptr;
1418 // Advance to the next record, but if we get to the end of the block, don't
1419 // pop it (removing all the abbreviations from the cursor) since we want to
1420 // be able to reseek within the block and read entries.
1421 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1422 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1424 switch (Entry.Kind) {
1425 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1426 case llvm::BitstreamEntry::Error:
1427 Error("malformed block record in AST file");
1429 case llvm::BitstreamEntry::EndBlock:
1431 case llvm::BitstreamEntry::Record:
1432 // The interesting case.
1438 PreprocessorRecordTypes RecType =
1439 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1441 case PP_MODULE_MACRO:
1442 case PP_MACRO_DIRECTIVE_HISTORY:
1445 case PP_MACRO_OBJECT_LIKE:
1446 case PP_MACRO_FUNCTION_LIKE: {
1447 // If we already have a macro, that means that we've hit the end
1448 // of the definition of the macro we were looking for. We're
1453 unsigned NextIndex = 1; // Skip identifier ID.
1454 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1455 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1456 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1457 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1458 MI->setIsUsed(Record[NextIndex++]);
1459 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1461 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1462 // Decode function-like macro info.
1463 bool isC99VarArgs = Record[NextIndex++];
1464 bool isGNUVarArgs = Record[NextIndex++];
1465 bool hasCommaPasting = Record[NextIndex++];
1467 unsigned NumArgs = Record[NextIndex++];
1468 for (unsigned i = 0; i != NumArgs; ++i)
1469 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1471 // Install function-like macro info.
1472 MI->setIsFunctionLike();
1473 if (isC99VarArgs) MI->setIsC99Varargs();
1474 if (isGNUVarArgs) MI->setIsGNUVarargs();
1475 if (hasCommaPasting) MI->setHasCommaPasting();
1476 MI->setArgumentList(MacroArgs, PP.getPreprocessorAllocator());
1479 // Remember that we saw this macro last so that we add the tokens that
1480 // form its body to it.
1483 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1484 Record[NextIndex]) {
1485 // We have a macro definition. Register the association
1486 PreprocessedEntityID
1487 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1488 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1489 PreprocessingRecord::PPEntityID PPID =
1490 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1491 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1492 PPRec.getPreprocessedEntity(PPID));
1494 PPRec.RegisterMacroDefinition(Macro, PPDef);
1502 // If we see a TOKEN before a PP_MACRO_*, then the file is
1503 // erroneous, just pretend we didn't see this.
1507 Token Tok = ReadToken(F, Record, Idx);
1508 Macro->AddTokenToBody(Tok);
1515 PreprocessedEntityID
1516 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1517 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1518 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1519 assert(I != M.PreprocessedEntityRemap.end()
1520 && "Invalid index into preprocessed entity index remap");
1522 return LocalID + I->second;
1525 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1526 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1529 HeaderFileInfoTrait::internal_key_type
1530 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1531 internal_key_type ikey = {FE->getSize(),
1532 M.HasTimestamps ? FE->getModificationTime() : 0,
1533 FE->getName(), /*Imported*/ false};
1537 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1538 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1541 if (llvm::sys::path::is_absolute(a.Filename) &&
1542 strcmp(a.Filename, b.Filename) == 0)
1545 // Determine whether the actual files are equivalent.
1546 FileManager &FileMgr = Reader.getFileManager();
1547 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1549 return FileMgr.getFile(Key.Filename);
1551 std::string Resolved = Key.Filename;
1552 Reader.ResolveImportedPath(M, Resolved);
1553 return FileMgr.getFile(Resolved);
1556 const FileEntry *FEA = GetFile(a);
1557 const FileEntry *FEB = GetFile(b);
1558 return FEA && FEA == FEB;
1561 std::pair<unsigned, unsigned>
1562 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1563 using namespace llvm::support;
1564 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1565 unsigned DataLen = (unsigned) *d++;
1566 return std::make_pair(KeyLen, DataLen);
1569 HeaderFileInfoTrait::internal_key_type
1570 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1571 using namespace llvm::support;
1572 internal_key_type ikey;
1573 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1574 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1575 ikey.Filename = (const char *)d;
1576 ikey.Imported = true;
1580 HeaderFileInfoTrait::data_type
1581 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1583 const unsigned char *End = d + DataLen;
1584 using namespace llvm::support;
1586 unsigned Flags = *d++;
1587 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1588 HFI.isImport |= (Flags >> 4) & 0x01;
1589 HFI.isPragmaOnce |= (Flags >> 3) & 0x01;
1590 HFI.DirInfo = (Flags >> 1) & 0x03;
1591 HFI.IndexHeaderMapHeader = Flags & 0x01;
1592 // FIXME: Find a better way to handle this. Maybe just store a
1593 // "has been included" flag?
1594 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1596 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1597 M, endian::readNext<uint32_t, little, unaligned>(d));
1598 if (unsigned FrameworkOffset =
1599 endian::readNext<uint32_t, little, unaligned>(d)) {
1600 // The framework offset is 1 greater than the actual offset,
1601 // since 0 is used as an indicator for "no framework name".
1602 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1603 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1606 assert((End - d) % 4 == 0 &&
1607 "Wrong data length in HeaderFileInfo deserialization");
1609 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1610 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1613 // This header is part of a module. Associate it with the module to enable
1614 // implicit module import.
1615 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1616 Module *Mod = Reader.getSubmodule(GlobalSMID);
1617 FileManager &FileMgr = Reader.getFileManager();
1619 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1621 std::string Filename = key.Filename;
1623 Reader.ResolveImportedPath(M, Filename);
1624 // FIXME: This is not always the right filename-as-written, but we're not
1625 // going to use this information to rebuild the module, so it doesn't make
1626 // a lot of difference.
1627 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1628 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1629 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1632 // This HeaderFileInfo was externally loaded.
1633 HFI.External = true;
1638 void ASTReader::addPendingMacro(IdentifierInfo *II,
1640 uint64_t MacroDirectivesOffset) {
1641 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1642 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1645 void ASTReader::ReadDefinedMacros() {
1646 // Note that we are loading defined macros.
1647 Deserializing Macros(this);
1649 for (auto &I : llvm::reverse(ModuleMgr)) {
1650 BitstreamCursor &MacroCursor = I->MacroCursor;
1652 // If there was no preprocessor block, skip this file.
1653 if (!MacroCursor.getBitStreamReader())
1656 BitstreamCursor Cursor = MacroCursor;
1657 Cursor.JumpToBit(I->MacroStartOffset);
1661 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1664 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1665 case llvm::BitstreamEntry::Error:
1666 Error("malformed block record in AST file");
1668 case llvm::BitstreamEntry::EndBlock:
1671 case llvm::BitstreamEntry::Record:
1673 switch (Cursor.readRecord(E.ID, Record)) {
1674 default: // Default behavior: ignore.
1677 case PP_MACRO_OBJECT_LIKE:
1678 case PP_MACRO_FUNCTION_LIKE:
1679 getLocalIdentifier(*I, Record[0]);
1694 /// \brief Visitor class used to look up identifirs in an AST file.
1695 class IdentifierLookupVisitor {
1698 unsigned PriorGeneration;
1699 unsigned &NumIdentifierLookups;
1700 unsigned &NumIdentifierLookupHits;
1701 IdentifierInfo *Found;
1704 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1705 unsigned &NumIdentifierLookups,
1706 unsigned &NumIdentifierLookupHits)
1707 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1708 PriorGeneration(PriorGeneration),
1709 NumIdentifierLookups(NumIdentifierLookups),
1710 NumIdentifierLookupHits(NumIdentifierLookupHits),
1715 bool operator()(ModuleFile &M) {
1716 // If we've already searched this module file, skip it now.
1717 if (M.Generation <= PriorGeneration)
1720 ASTIdentifierLookupTable *IdTable
1721 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1725 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
1727 ++NumIdentifierLookups;
1728 ASTIdentifierLookupTable::iterator Pos =
1729 IdTable->find_hashed(Name, NameHash, &Trait);
1730 if (Pos == IdTable->end())
1733 // Dereferencing the iterator has the effect of building the
1734 // IdentifierInfo node and populating it with the various
1735 // declarations it needs.
1736 ++NumIdentifierLookupHits;
1741 // \brief Retrieve the identifier info found within the module
1743 IdentifierInfo *getIdentifierInfo() const { return Found; }
1747 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1748 // Note that we are loading an identifier.
1749 Deserializing AnIdentifier(this);
1751 unsigned PriorGeneration = 0;
1752 if (getContext().getLangOpts().Modules)
1753 PriorGeneration = IdentifierGeneration[&II];
1755 // If there is a global index, look there first to determine which modules
1756 // provably do not have any results for this identifier.
1757 GlobalModuleIndex::HitSet Hits;
1758 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1759 if (!loadGlobalIndex()) {
1760 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1765 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1766 NumIdentifierLookups,
1767 NumIdentifierLookupHits);
1768 ModuleMgr.visit(Visitor, HitsPtr);
1769 markIdentifierUpToDate(&II);
1772 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1776 II->setOutOfDate(false);
1778 // Update the generation for this identifier.
1779 if (getContext().getLangOpts().Modules)
1780 IdentifierGeneration[II] = getGeneration();
1783 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1784 const PendingMacroInfo &PMInfo) {
1785 ModuleFile &M = *PMInfo.M;
1787 BitstreamCursor &Cursor = M.MacroCursor;
1788 SavedStreamPosition SavedPosition(Cursor);
1789 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1791 struct ModuleMacroRecord {
1792 SubmoduleID SubModID;
1794 SmallVector<SubmoduleID, 8> Overrides;
1796 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
1798 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1799 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1803 llvm::BitstreamEntry Entry =
1804 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1805 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1806 Error("malformed block record in AST file");
1811 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1812 case PP_MACRO_DIRECTIVE_HISTORY:
1815 case PP_MODULE_MACRO: {
1816 ModuleMacros.push_back(ModuleMacroRecord());
1817 auto &Info = ModuleMacros.back();
1818 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1819 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1820 for (int I = 2, N = Record.size(); I != N; ++I)
1821 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1826 Error("malformed block record in AST file");
1830 // We found the macro directive history; that's the last record
1835 // Module macros are listed in reverse dependency order.
1837 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
1838 llvm::SmallVector<ModuleMacro*, 8> Overrides;
1839 for (auto &MMR : ModuleMacros) {
1841 for (unsigned ModID : MMR.Overrides) {
1842 Module *Mod = getSubmodule(ModID);
1843 auto *Macro = PP.getModuleMacro(Mod, II);
1844 assert(Macro && "missing definition for overridden macro");
1845 Overrides.push_back(Macro);
1848 bool Inserted = false;
1849 Module *Owner = getSubmodule(MMR.SubModID);
1850 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
1854 // Don't read the directive history for a module; we don't have anywhere
1856 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1859 // Deserialize the macro directives history in reverse source-order.
1860 MacroDirective *Latest = nullptr, *Earliest = nullptr;
1861 unsigned Idx = 0, N = Record.size();
1863 MacroDirective *MD = nullptr;
1864 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1865 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1867 case MacroDirective::MD_Define: {
1868 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
1869 MD = PP.AllocateDefMacroDirective(MI, Loc);
1872 case MacroDirective::MD_Undefine: {
1873 MD = PP.AllocateUndefMacroDirective(Loc);
1876 case MacroDirective::MD_Visibility:
1877 bool isPublic = Record[Idx++];
1878 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1885 Earliest->setPrevious(MD);
1890 PP.setLoadedMacroDirective(II, Latest);
1893 ASTReader::InputFileInfo
1894 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
1895 // Go find this input file.
1896 BitstreamCursor &Cursor = F.InputFilesCursor;
1897 SavedStreamPosition SavedPosition(Cursor);
1898 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1900 unsigned Code = Cursor.ReadCode();
1904 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1905 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1906 "invalid record type for input file");
1909 assert(Record[0] == ID && "Bogus stored ID or offset");
1911 R.StoredSize = static_cast<off_t>(Record[1]);
1912 R.StoredTime = static_cast<time_t>(Record[2]);
1913 R.Overridden = static_cast<bool>(Record[3]);
1914 R.Transient = static_cast<bool>(Record[4]);
1916 ResolveImportedPath(F, R.Filename);
1920 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1921 // If this ID is bogus, just return an empty input file.
1922 if (ID == 0 || ID > F.InputFilesLoaded.size())
1925 // If we've already loaded this input file, return it.
1926 if (F.InputFilesLoaded[ID-1].getFile())
1927 return F.InputFilesLoaded[ID-1];
1929 if (F.InputFilesLoaded[ID-1].isNotFound())
1932 // Go find this input file.
1933 BitstreamCursor &Cursor = F.InputFilesCursor;
1934 SavedStreamPosition SavedPosition(Cursor);
1935 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1937 InputFileInfo FI = readInputFileInfo(F, ID);
1938 off_t StoredSize = FI.StoredSize;
1939 time_t StoredTime = FI.StoredTime;
1940 bool Overridden = FI.Overridden;
1941 bool Transient = FI.Transient;
1942 StringRef Filename = FI.Filename;
1944 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
1946 // If we didn't find the file, resolve it relative to the
1947 // original directory from which this AST file was created.
1948 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1949 F.OriginalDir != CurrentDir) {
1950 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1953 if (!Resolved.empty())
1954 File = FileMgr.getFile(Resolved);
1957 // For an overridden file, create a virtual file with the stored
1959 if ((Overridden || Transient) && File == nullptr)
1960 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1962 if (File == nullptr) {
1964 std::string ErrorStr = "could not find file '";
1965 ErrorStr += Filename;
1966 ErrorStr += "' referenced by AST file '";
1967 ErrorStr += F.FileName;
1969 Error(ErrorStr.c_str());
1971 // Record that we didn't find the file.
1972 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1976 // Check if there was a request to override the contents of the file
1977 // that was part of the precompiled header. Overridding such a file
1978 // can lead to problems when lexing using the source locations from the
1980 SourceManager &SM = getSourceManager();
1981 // FIXME: Reject if the overrides are different.
1982 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
1984 Error(diag::err_fe_pch_file_overridden, Filename);
1985 // After emitting the diagnostic, recover by disabling the override so
1986 // that the original file will be used.
1988 // FIXME: This recovery is just as broken as the original state; there may
1989 // be another precompiled module that's using the overridden contents, or
1990 // we might be half way through parsing it. Instead, we should treat the
1991 // overridden contents as belonging to a separate FileEntry.
1992 SM.disableFileContentsOverride(File);
1993 // The FileEntry is a virtual file entry with the size of the contents
1994 // that would override the original contents. Set it to the original's
1996 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1997 StoredSize, StoredTime);
2000 bool IsOutOfDate = false;
2002 // For an overridden file, there is nothing to validate.
2003 if (!Overridden && //
2004 (StoredSize != File->getSize() ||
2005 #if defined(LLVM_ON_WIN32)
2008 // In our regression testing, the Windows file system seems to
2009 // have inconsistent modification times that sometimes
2010 // erroneously trigger this error-handling path.
2012 // FIXME: This probably also breaks HeaderFileInfo lookups on Windows.
2013 (StoredTime && StoredTime != File->getModificationTime() &&
2018 // Build a list of the PCH imports that got us here (in reverse).
2019 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2020 while (ImportStack.back()->ImportedBy.size() > 0)
2021 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2023 // The top-level PCH is stale.
2024 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2025 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2027 // Print the import stack.
2028 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2029 Diag(diag::note_pch_required_by)
2030 << Filename << ImportStack[0]->FileName;
2031 for (unsigned I = 1; I < ImportStack.size(); ++I)
2032 Diag(diag::note_pch_required_by)
2033 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2036 if (!Diags.isDiagnosticInFlight())
2037 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2042 // FIXME: If the file is overridden and we've already opened it,
2043 // issue an error (or split it into a separate FileEntry).
2045 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2047 // Note that we've loaded this input file.
2048 F.InputFilesLoaded[ID-1] = IF;
2052 /// \brief If we are loading a relocatable PCH or module file, and the filename
2053 /// is not an absolute path, add the system or module root to the beginning of
2055 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2056 // Resolve relative to the base directory, if we have one.
2057 if (!M.BaseDirectory.empty())
2058 return ResolveImportedPath(Filename, M.BaseDirectory);
2061 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2062 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2065 SmallString<128> Buffer;
2066 llvm::sys::path::append(Buffer, Prefix, Filename);
2067 Filename.assign(Buffer.begin(), Buffer.end());
2070 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2072 case ASTReader::Failure: return true;
2073 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2074 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2075 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2076 case ASTReader::ConfigurationMismatch:
2077 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2078 case ASTReader::HadErrors: return true;
2079 case ASTReader::Success: return false;
2082 llvm_unreachable("unknown ASTReadResult");
2085 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2086 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2087 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2088 std::string &SuggestedPredefines) {
2089 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2092 // Read all of the records in the options block.
2094 ASTReadResult Result = Success;
2096 llvm::BitstreamEntry Entry = Stream.advance();
2098 switch (Entry.Kind) {
2099 case llvm::BitstreamEntry::Error:
2100 case llvm::BitstreamEntry::SubBlock:
2103 case llvm::BitstreamEntry::EndBlock:
2106 case llvm::BitstreamEntry::Record:
2107 // The interesting case.
2111 // Read and process a record.
2113 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2114 case LANGUAGE_OPTIONS: {
2115 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2116 if (ParseLanguageOptions(Record, Complain, Listener,
2117 AllowCompatibleConfigurationMismatch))
2118 Result = ConfigurationMismatch;
2122 case TARGET_OPTIONS: {
2123 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2124 if (ParseTargetOptions(Record, Complain, Listener,
2125 AllowCompatibleConfigurationMismatch))
2126 Result = ConfigurationMismatch;
2130 case DIAGNOSTIC_OPTIONS: {
2131 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2132 if (!AllowCompatibleConfigurationMismatch &&
2133 ParseDiagnosticOptions(Record, Complain, Listener))
2138 case FILE_SYSTEM_OPTIONS: {
2139 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2140 if (!AllowCompatibleConfigurationMismatch &&
2141 ParseFileSystemOptions(Record, Complain, Listener))
2142 Result = ConfigurationMismatch;
2146 case HEADER_SEARCH_OPTIONS: {
2147 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2148 if (!AllowCompatibleConfigurationMismatch &&
2149 ParseHeaderSearchOptions(Record, Complain, Listener))
2150 Result = ConfigurationMismatch;
2154 case PREPROCESSOR_OPTIONS:
2155 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2156 if (!AllowCompatibleConfigurationMismatch &&
2157 ParsePreprocessorOptions(Record, Complain, Listener,
2158 SuggestedPredefines))
2159 Result = ConfigurationMismatch;
2165 ASTReader::ASTReadResult
2166 ASTReader::ReadControlBlock(ModuleFile &F,
2167 SmallVectorImpl<ImportedModule> &Loaded,
2168 const ModuleFile *ImportedBy,
2169 unsigned ClientLoadCapabilities) {
2170 BitstreamCursor &Stream = F.Stream;
2171 ASTReadResult Result = Success;
2173 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2174 Error("malformed block record in AST file");
2178 // Read all of the records and blocks in the control block.
2180 unsigned NumInputs = 0;
2181 unsigned NumUserInputs = 0;
2183 llvm::BitstreamEntry Entry = Stream.advance();
2185 switch (Entry.Kind) {
2186 case llvm::BitstreamEntry::Error:
2187 Error("malformed block record in AST file");
2189 case llvm::BitstreamEntry::EndBlock: {
2190 // Validate input files.
2191 const HeaderSearchOptions &HSOpts =
2192 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2194 // All user input files reside at the index range [0, NumUserInputs), and
2195 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2196 // loaded module files, ignore missing inputs.
2197 if (!DisableValidation && F.Kind != MK_ExplicitModule) {
2198 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2200 // If we are reading a module, we will create a verification timestamp,
2201 // so we verify all input files. Otherwise, verify only user input
2204 unsigned N = NumUserInputs;
2205 if (ValidateSystemInputs ||
2206 (HSOpts.ModulesValidateOncePerBuildSession &&
2207 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2208 F.Kind == MK_ImplicitModule))
2211 for (unsigned I = 0; I < N; ++I) {
2212 InputFile IF = getInputFile(F, I+1, Complain);
2213 if (!IF.getFile() || IF.isOutOfDate())
2219 Listener->visitModuleFile(F.FileName, F.Kind);
2221 if (Listener && Listener->needsInputFileVisitation()) {
2222 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2224 for (unsigned I = 0; I < N; ++I) {
2225 bool IsSystem = I >= NumUserInputs;
2226 InputFileInfo FI = readInputFileInfo(F, I+1);
2227 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2228 F.Kind == MK_ExplicitModule);
2235 case llvm::BitstreamEntry::SubBlock:
2237 case INPUT_FILES_BLOCK_ID:
2238 F.InputFilesCursor = Stream;
2239 if (Stream.SkipBlock() || // Skip with the main cursor
2240 // Read the abbreviations
2241 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2242 Error("malformed block record in AST file");
2247 case OPTIONS_BLOCK_ID:
2248 // If we're reading the first module for this group, check its options
2249 // are compatible with ours. For modules it imports, no further checking
2250 // is required, because we checked them when we built it.
2251 if (Listener && !ImportedBy) {
2252 // Should we allow the configuration of the module file to differ from
2253 // the configuration of the current translation unit in a compatible
2256 // FIXME: Allow this for files explicitly specified with -include-pch.
2257 bool AllowCompatibleConfigurationMismatch =
2258 F.Kind == MK_ExplicitModule;
2260 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2261 AllowCompatibleConfigurationMismatch,
2262 *Listener, SuggestedPredefines);
2263 if (Result == Failure) {
2264 Error("malformed block record in AST file");
2268 if (DisableValidation ||
2269 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2272 // If we can't load the module, exit early since we likely
2273 // will rebuild the module anyway. The stream may be in the
2274 // middle of a block.
2275 if (Result != Success)
2277 } else if (Stream.SkipBlock()) {
2278 Error("malformed block record in AST file");
2284 if (Stream.SkipBlock()) {
2285 Error("malformed block record in AST file");
2291 case llvm::BitstreamEntry::Record:
2292 // The interesting case.
2296 // Read and process a record.
2299 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2301 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2302 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2303 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2304 : diag::err_pch_version_too_new);
2305 return VersionMismatch;
2308 bool hasErrors = Record[6];
2309 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2310 Diag(diag::err_pch_with_compiler_errors);
2314 F.RelocatablePCH = Record[4];
2315 // Relative paths in a relocatable PCH are relative to our sysroot.
2316 if (F.RelocatablePCH)
2317 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2319 F.HasTimestamps = Record[5];
2321 const std::string &CurBranch = getClangFullRepositoryVersion();
2322 StringRef ASTBranch = Blob;
2323 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2324 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2325 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2326 return VersionMismatch;
2332 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2333 F.Signature = Record[0];
2337 // Load each of the imported PCH files.
2338 unsigned Idx = 0, N = Record.size();
2340 // Read information about the AST file.
2341 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2342 // The import location will be the local one for now; we will adjust
2343 // all import locations of module imports after the global source
2344 // location info are setup.
2345 SourceLocation ImportLoc =
2346 SourceLocation::getFromRawEncoding(Record[Idx++]);
2347 off_t StoredSize = (off_t)Record[Idx++];
2348 time_t StoredModTime = (time_t)Record[Idx++];
2349 ASTFileSignature StoredSignature = Record[Idx++];
2350 auto ImportedFile = ReadPath(F, Record, Idx);
2352 // If our client can't cope with us being out of date, we can't cope with
2353 // our dependency being missing.
2354 unsigned Capabilities = ClientLoadCapabilities;
2355 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2356 Capabilities &= ~ARR_Missing;
2358 // Load the AST file.
2359 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2360 Loaded, StoredSize, StoredModTime,
2361 StoredSignature, Capabilities);
2363 // If we diagnosed a problem, produce a backtrace.
2364 if (isDiagnosedResult(Result, Capabilities))
2365 Diag(diag::note_module_file_imported_by)
2366 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2369 case Failure: return Failure;
2370 // If we have to ignore the dependency, we'll have to ignore this too.
2372 case OutOfDate: return OutOfDate;
2373 case VersionMismatch: return VersionMismatch;
2374 case ConfigurationMismatch: return ConfigurationMismatch;
2375 case HadErrors: return HadErrors;
2376 case Success: break;
2383 F.OriginalSourceFileID = FileID::get(Record[0]);
2384 F.ActualOriginalSourceFileName = Blob;
2385 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2386 ResolveImportedPath(F, F.OriginalSourceFileName);
2389 case ORIGINAL_FILE_ID:
2390 F.OriginalSourceFileID = FileID::get(Record[0]);
2393 case ORIGINAL_PCH_DIR:
2394 F.OriginalDir = Blob;
2398 F.ModuleName = Blob;
2400 Listener->ReadModuleName(F.ModuleName);
2403 case MODULE_DIRECTORY: {
2404 assert(!F.ModuleName.empty() &&
2405 "MODULE_DIRECTORY found before MODULE_NAME");
2406 // If we've already loaded a module map file covering this module, we may
2407 // have a better path for it (relative to the current build).
2408 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2409 if (M && M->Directory) {
2410 // If we're implicitly loading a module, the base directory can't
2411 // change between the build and use.
2412 if (F.Kind != MK_ExplicitModule) {
2413 const DirectoryEntry *BuildDir =
2414 PP.getFileManager().getDirectory(Blob);
2415 if (!BuildDir || BuildDir != M->Directory) {
2416 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2417 Diag(diag::err_imported_module_relocated)
2418 << F.ModuleName << Blob << M->Directory->getName();
2422 F.BaseDirectory = M->Directory->getName();
2424 F.BaseDirectory = Blob;
2429 case MODULE_MAP_FILE:
2430 if (ASTReadResult Result =
2431 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2435 case INPUT_FILE_OFFSETS:
2436 NumInputs = Record[0];
2437 NumUserInputs = Record[1];
2438 F.InputFileOffsets =
2439 (const llvm::support::unaligned_uint64_t *)Blob.data();
2440 F.InputFilesLoaded.resize(NumInputs);
2446 ASTReader::ASTReadResult
2447 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2448 BitstreamCursor &Stream = F.Stream;
2450 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2451 Error("malformed block record in AST file");
2455 // Read all of the records and blocks for the AST file.
2458 llvm::BitstreamEntry Entry = Stream.advance();
2460 switch (Entry.Kind) {
2461 case llvm::BitstreamEntry::Error:
2462 Error("error at end of module block in AST file");
2464 case llvm::BitstreamEntry::EndBlock: {
2465 // Outside of C++, we do not store a lookup map for the translation unit.
2466 // Instead, mark it as needing a lookup map to be built if this module
2467 // contains any declarations lexically within it (which it always does!).
2468 // This usually has no cost, since we very rarely need the lookup map for
2469 // the translation unit outside C++.
2470 DeclContext *DC = Context.getTranslationUnitDecl();
2471 if (DC->hasExternalLexicalStorage() &&
2472 !getContext().getLangOpts().CPlusPlus)
2473 DC->setMustBuildLookupTable();
2477 case llvm::BitstreamEntry::SubBlock:
2479 case DECLTYPES_BLOCK_ID:
2480 // We lazily load the decls block, but we want to set up the
2481 // DeclsCursor cursor to point into it. Clone our current bitcode
2482 // cursor to it, enter the block and read the abbrevs in that block.
2483 // With the main cursor, we just skip over it.
2484 F.DeclsCursor = Stream;
2485 if (Stream.SkipBlock() || // Skip with the main cursor.
2486 // Read the abbrevs.
2487 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2488 Error("malformed block record in AST file");
2493 case PREPROCESSOR_BLOCK_ID:
2494 F.MacroCursor = Stream;
2495 if (!PP.getExternalSource())
2496 PP.setExternalSource(this);
2498 if (Stream.SkipBlock() ||
2499 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2500 Error("malformed block record in AST file");
2503 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2506 case PREPROCESSOR_DETAIL_BLOCK_ID:
2507 F.PreprocessorDetailCursor = Stream;
2508 if (Stream.SkipBlock() ||
2509 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2510 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2511 Error("malformed preprocessor detail record in AST file");
2514 F.PreprocessorDetailStartOffset
2515 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2517 if (!PP.getPreprocessingRecord())
2518 PP.createPreprocessingRecord();
2519 if (!PP.getPreprocessingRecord()->getExternalSource())
2520 PP.getPreprocessingRecord()->SetExternalSource(*this);
2523 case SOURCE_MANAGER_BLOCK_ID:
2524 if (ReadSourceManagerBlock(F))
2528 case SUBMODULE_BLOCK_ID:
2529 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2533 case COMMENTS_BLOCK_ID: {
2534 BitstreamCursor C = Stream;
2535 if (Stream.SkipBlock() ||
2536 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2537 Error("malformed comments block in AST file");
2540 CommentsCursors.push_back(std::make_pair(C, &F));
2545 if (Stream.SkipBlock()) {
2546 Error("malformed block record in AST file");
2553 case llvm::BitstreamEntry::Record:
2554 // The interesting case.
2558 // Read and process a record.
2561 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2562 default: // Default behavior: ignore.
2566 if (F.LocalNumTypes != 0) {
2567 Error("duplicate TYPE_OFFSET record in AST file");
2570 F.TypeOffsets = (const uint32_t *)Blob.data();
2571 F.LocalNumTypes = Record[0];
2572 unsigned LocalBaseTypeIndex = Record[1];
2573 F.BaseTypeIndex = getTotalNumTypes();
2575 if (F.LocalNumTypes > 0) {
2576 // Introduce the global -> local mapping for types within this module.
2577 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2579 // Introduce the local -> global mapping for types within this module.
2580 F.TypeRemap.insertOrReplace(
2581 std::make_pair(LocalBaseTypeIndex,
2582 F.BaseTypeIndex - LocalBaseTypeIndex));
2584 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2590 if (F.LocalNumDecls != 0) {
2591 Error("duplicate DECL_OFFSET record in AST file");
2594 F.DeclOffsets = (const DeclOffset *)Blob.data();
2595 F.LocalNumDecls = Record[0];
2596 unsigned LocalBaseDeclID = Record[1];
2597 F.BaseDeclID = getTotalNumDecls();
2599 if (F.LocalNumDecls > 0) {
2600 // Introduce the global -> local mapping for declarations within this
2602 GlobalDeclMap.insert(
2603 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2605 // Introduce the local -> global mapping for declarations within this
2607 F.DeclRemap.insertOrReplace(
2608 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2610 // Introduce the global -> local mapping for declarations within this
2612 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2614 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2619 case TU_UPDATE_LEXICAL: {
2620 DeclContext *TU = Context.getTranslationUnitDecl();
2621 LexicalContents Contents(
2622 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2624 static_cast<unsigned int>(Blob.size() / 4));
2625 TULexicalDecls.push_back(std::make_pair(&F, Contents));
2626 TU->setHasExternalLexicalStorage(true);
2630 case UPDATE_VISIBLE: {
2632 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2633 auto *Data = (const unsigned char*)Blob.data();
2634 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
2635 // If we've already loaded the decl, perform the updates when we finish
2636 // loading this block.
2637 if (Decl *D = GetExistingDecl(ID))
2638 PendingUpdateRecords.push_back(std::make_pair(ID, D));
2642 case IDENTIFIER_TABLE:
2643 F.IdentifierTableData = Blob.data();
2645 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2646 (const unsigned char *)F.IdentifierTableData + Record[0],
2647 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2648 (const unsigned char *)F.IdentifierTableData,
2649 ASTIdentifierLookupTrait(*this, F));
2651 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2655 case IDENTIFIER_OFFSET: {
2656 if (F.LocalNumIdentifiers != 0) {
2657 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2660 F.IdentifierOffsets = (const uint32_t *)Blob.data();
2661 F.LocalNumIdentifiers = Record[0];
2662 unsigned LocalBaseIdentifierID = Record[1];
2663 F.BaseIdentifierID = getTotalNumIdentifiers();
2665 if (F.LocalNumIdentifiers > 0) {
2666 // Introduce the global -> local mapping for identifiers within this
2668 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2671 // Introduce the local -> global mapping for identifiers within this
2673 F.IdentifierRemap.insertOrReplace(
2674 std::make_pair(LocalBaseIdentifierID,
2675 F.BaseIdentifierID - LocalBaseIdentifierID));
2677 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2678 + F.LocalNumIdentifiers);
2683 case INTERESTING_IDENTIFIERS:
2684 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2687 case EAGERLY_DESERIALIZED_DECLS:
2688 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2689 // about "interesting" decls (for instance, if we're building a module).
2690 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2691 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2695 if (SpecialTypes.empty()) {
2696 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2697 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2701 if (SpecialTypes.size() != Record.size()) {
2702 Error("invalid special-types record");
2706 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2707 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2708 if (!SpecialTypes[I])
2709 SpecialTypes[I] = ID;
2710 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2716 TotalNumStatements += Record[0];
2717 TotalNumMacros += Record[1];
2718 TotalLexicalDeclContexts += Record[2];
2719 TotalVisibleDeclContexts += Record[3];
2722 case UNUSED_FILESCOPED_DECLS:
2723 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2724 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2727 case DELEGATING_CTORS:
2728 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2729 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2732 case WEAK_UNDECLARED_IDENTIFIERS:
2733 if (Record.size() % 4 != 0) {
2734 Error("invalid weak identifiers record");
2738 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2739 // files. This isn't the way to do it :)
2740 WeakUndeclaredIdentifiers.clear();
2742 // Translate the weak, undeclared identifiers into global IDs.
2743 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2744 WeakUndeclaredIdentifiers.push_back(
2745 getGlobalIdentifierID(F, Record[I++]));
2746 WeakUndeclaredIdentifiers.push_back(
2747 getGlobalIdentifierID(F, Record[I++]));
2748 WeakUndeclaredIdentifiers.push_back(
2749 ReadSourceLocation(F, Record, I).getRawEncoding());
2750 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2754 case SELECTOR_OFFSETS: {
2755 F.SelectorOffsets = (const uint32_t *)Blob.data();
2756 F.LocalNumSelectors = Record[0];
2757 unsigned LocalBaseSelectorID = Record[1];
2758 F.BaseSelectorID = getTotalNumSelectors();
2760 if (F.LocalNumSelectors > 0) {
2761 // Introduce the global -> local mapping for selectors within this
2763 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2765 // Introduce the local -> global mapping for selectors within this
2767 F.SelectorRemap.insertOrReplace(
2768 std::make_pair(LocalBaseSelectorID,
2769 F.BaseSelectorID - LocalBaseSelectorID));
2771 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2777 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2779 F.SelectorLookupTable
2780 = ASTSelectorLookupTable::Create(
2781 F.SelectorLookupTableData + Record[0],
2782 F.SelectorLookupTableData,
2783 ASTSelectorLookupTrait(*this, F));
2784 TotalNumMethodPoolEntries += Record[1];
2787 case REFERENCED_SELECTOR_POOL:
2788 if (!Record.empty()) {
2789 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2790 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2792 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2798 case PP_COUNTER_VALUE:
2799 if (!Record.empty() && Listener)
2800 Listener->ReadCounter(F, Record[0]);
2803 case FILE_SORTED_DECLS:
2804 F.FileSortedDecls = (const DeclID *)Blob.data();
2805 F.NumFileSortedDecls = Record[0];
2808 case SOURCE_LOCATION_OFFSETS: {
2809 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2810 F.LocalNumSLocEntries = Record[0];
2811 unsigned SLocSpaceSize = Record[1];
2812 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2813 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2815 if (!F.SLocEntryBaseID) {
2816 Error("ran out of source locations");
2819 // Make our entry in the range map. BaseID is negative and growing, so
2820 // we invert it. Because we invert it, though, we need the other end of
2822 unsigned RangeStart =
2823 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2824 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2825 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2827 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2828 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2829 GlobalSLocOffsetMap.insert(
2830 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2831 - SLocSpaceSize,&F));
2833 // Initialize the remapping table.
2834 // Invalid stays invalid.
2835 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
2836 // This module. Base was 2 when being compiled.
2837 F.SLocRemap.insertOrReplace(std::make_pair(2U,
2838 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2840 TotalNumSLocEntries += F.LocalNumSLocEntries;
2844 case MODULE_OFFSET_MAP: {
2845 // Additional remapping information.
2846 const unsigned char *Data = (const unsigned char*)Blob.data();
2847 const unsigned char *DataEnd = Data + Blob.size();
2849 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2850 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2851 F.SLocRemap.insert(std::make_pair(0U, 0));
2852 F.SLocRemap.insert(std::make_pair(2U, 1));
2855 // Continuous range maps we may be updating in our module.
2856 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2858 RemapBuilder SLocRemap(F.SLocRemap);
2859 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2860 RemapBuilder MacroRemap(F.MacroRemap);
2861 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2862 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2863 RemapBuilder SelectorRemap(F.SelectorRemap);
2864 RemapBuilder DeclRemap(F.DeclRemap);
2865 RemapBuilder TypeRemap(F.TypeRemap);
2867 while (Data < DataEnd) {
2868 using namespace llvm::support;
2869 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
2870 StringRef Name = StringRef((const char*)Data, Len);
2872 ModuleFile *OM = ModuleMgr.lookup(Name);
2874 Error("SourceLocation remap refers to unknown module");
2878 uint32_t SLocOffset =
2879 endian::readNext<uint32_t, little, unaligned>(Data);
2880 uint32_t IdentifierIDOffset =
2881 endian::readNext<uint32_t, little, unaligned>(Data);
2882 uint32_t MacroIDOffset =
2883 endian::readNext<uint32_t, little, unaligned>(Data);
2884 uint32_t PreprocessedEntityIDOffset =
2885 endian::readNext<uint32_t, little, unaligned>(Data);
2886 uint32_t SubmoduleIDOffset =
2887 endian::readNext<uint32_t, little, unaligned>(Data);
2888 uint32_t SelectorIDOffset =
2889 endian::readNext<uint32_t, little, unaligned>(Data);
2890 uint32_t DeclIDOffset =
2891 endian::readNext<uint32_t, little, unaligned>(Data);
2892 uint32_t TypeIndexOffset =
2893 endian::readNext<uint32_t, little, unaligned>(Data);
2895 uint32_t None = std::numeric_limits<uint32_t>::max();
2897 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2898 RemapBuilder &Remap) {
2900 Remap.insert(std::make_pair(Offset,
2901 static_cast<int>(BaseOffset - Offset)));
2903 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2904 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2905 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2906 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2907 PreprocessedEntityRemap);
2908 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2909 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2910 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2911 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
2913 // Global -> local mappings.
2914 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2919 case SOURCE_MANAGER_LINE_TABLE:
2920 if (ParseLineTable(F, Record))
2924 case SOURCE_LOCATION_PRELOADS: {
2925 // Need to transform from the local view (1-based IDs) to the global view,
2926 // which is based off F.SLocEntryBaseID.
2927 if (!F.PreloadSLocEntries.empty()) {
2928 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2932 F.PreloadSLocEntries.swap(Record);
2936 case EXT_VECTOR_DECLS:
2937 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2938 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2942 if (Record.size() % 3 != 0) {
2943 Error("Invalid VTABLE_USES record");
2947 // Later tables overwrite earlier ones.
2948 // FIXME: Modules will have some trouble with this. This is clearly not
2949 // the right way to do this.
2952 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2953 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2954 VTableUses.push_back(
2955 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2956 VTableUses.push_back(Record[Idx++]);
2960 case PENDING_IMPLICIT_INSTANTIATIONS:
2961 if (PendingInstantiations.size() % 2 != 0) {
2962 Error("Invalid existing PendingInstantiations");
2966 if (Record.size() % 2 != 0) {
2967 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2971 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2972 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2973 PendingInstantiations.push_back(
2974 ReadSourceLocation(F, Record, I).getRawEncoding());
2978 case SEMA_DECL_REFS:
2979 if (Record.size() != 2) {
2980 Error("Invalid SEMA_DECL_REFS block");
2983 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2984 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2987 case PPD_ENTITIES_OFFSETS: {
2988 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2989 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2990 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
2992 unsigned LocalBasePreprocessedEntityID = Record[0];
2994 unsigned StartingID;
2995 if (!PP.getPreprocessingRecord())
2996 PP.createPreprocessingRecord();
2997 if (!PP.getPreprocessingRecord()->getExternalSource())
2998 PP.getPreprocessingRecord()->SetExternalSource(*this);
3000 = PP.getPreprocessingRecord()
3001 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3002 F.BasePreprocessedEntityID = StartingID;
3004 if (F.NumPreprocessedEntities > 0) {
3005 // Introduce the global -> local mapping for preprocessed entities in
3007 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3009 // Introduce the local -> global mapping for preprocessed entities in
3011 F.PreprocessedEntityRemap.insertOrReplace(
3012 std::make_pair(LocalBasePreprocessedEntityID,
3013 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3019 case DECL_UPDATE_OFFSETS: {
3020 if (Record.size() % 2 != 0) {
3021 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3024 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3025 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3026 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3028 // If we've already loaded the decl, perform the updates when we finish
3029 // loading this block.
3030 if (Decl *D = GetExistingDecl(ID))
3031 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3036 case OBJC_CATEGORIES_MAP: {
3037 if (F.LocalNumObjCCategoriesInMap != 0) {
3038 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3042 F.LocalNumObjCCategoriesInMap = Record[0];
3043 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3047 case OBJC_CATEGORIES:
3048 F.ObjCCategories.swap(Record);
3051 case CXX_BASE_SPECIFIER_OFFSETS: {
3052 if (F.LocalNumCXXBaseSpecifiers != 0) {
3053 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
3057 F.LocalNumCXXBaseSpecifiers = Record[0];
3058 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
3062 case CXX_CTOR_INITIALIZERS_OFFSETS: {
3063 if (F.LocalNumCXXCtorInitializers != 0) {
3064 Error("duplicate CXX_CTOR_INITIALIZERS_OFFSETS record in AST file");
3068 F.LocalNumCXXCtorInitializers = Record[0];
3069 F.CXXCtorInitializersOffsets = (const uint32_t *)Blob.data();
3073 case DIAG_PRAGMA_MAPPINGS:
3074 if (F.PragmaDiagMappings.empty())
3075 F.PragmaDiagMappings.swap(Record);
3077 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3078 Record.begin(), Record.end());
3081 case CUDA_SPECIAL_DECL_REFS:
3082 // Later tables overwrite earlier ones.
3083 // FIXME: Modules will have trouble with this.
3084 CUDASpecialDeclRefs.clear();
3085 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3086 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3089 case HEADER_SEARCH_TABLE: {
3090 F.HeaderFileInfoTableData = Blob.data();
3091 F.LocalNumHeaderFileInfos = Record[1];
3093 F.HeaderFileInfoTable
3094 = HeaderFileInfoLookupTable::Create(
3095 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3096 (const unsigned char *)F.HeaderFileInfoTableData,
3097 HeaderFileInfoTrait(*this, F,
3098 &PP.getHeaderSearchInfo(),
3099 Blob.data() + Record[2]));
3101 PP.getHeaderSearchInfo().SetExternalSource(this);
3102 if (!PP.getHeaderSearchInfo().getExternalLookup())
3103 PP.getHeaderSearchInfo().SetExternalLookup(this);
3108 case FP_PRAGMA_OPTIONS:
3109 // Later tables overwrite earlier ones.
3110 FPPragmaOptions.swap(Record);
3113 case OPENCL_EXTENSIONS:
3114 // Later tables overwrite earlier ones.
3115 OpenCLExtensions.swap(Record);
3118 case TENTATIVE_DEFINITIONS:
3119 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3120 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3123 case KNOWN_NAMESPACES:
3124 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3125 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3128 case UNDEFINED_BUT_USED:
3129 if (UndefinedButUsed.size() % 2 != 0) {
3130 Error("Invalid existing UndefinedButUsed");
3134 if (Record.size() % 2 != 0) {
3135 Error("invalid undefined-but-used record");
3138 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3139 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3140 UndefinedButUsed.push_back(
3141 ReadSourceLocation(F, Record, I).getRawEncoding());
3144 case DELETE_EXPRS_TO_ANALYZE:
3145 for (unsigned I = 0, N = Record.size(); I != N;) {
3146 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3147 const uint64_t Count = Record[I++];
3148 DelayedDeleteExprs.push_back(Count);
3149 for (uint64_t C = 0; C < Count; ++C) {
3150 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3151 bool IsArrayForm = Record[I++] == 1;
3152 DelayedDeleteExprs.push_back(IsArrayForm);
3157 case IMPORTED_MODULES: {
3158 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
3159 // If we aren't loading a module (which has its own exports), make
3160 // all of the imported modules visible.
3161 // FIXME: Deal with macros-only imports.
3162 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3163 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3164 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3166 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3172 case MACRO_OFFSET: {
3173 if (F.LocalNumMacros != 0) {
3174 Error("duplicate MACRO_OFFSET record in AST file");
3177 F.MacroOffsets = (const uint32_t *)Blob.data();
3178 F.LocalNumMacros = Record[0];
3179 unsigned LocalBaseMacroID = Record[1];
3180 F.BaseMacroID = getTotalNumMacros();
3182 if (F.LocalNumMacros > 0) {
3183 // Introduce the global -> local mapping for macros within this module.
3184 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3186 // Introduce the local -> global mapping for macros within this module.
3187 F.MacroRemap.insertOrReplace(
3188 std::make_pair(LocalBaseMacroID,
3189 F.BaseMacroID - LocalBaseMacroID));
3191 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3196 case LATE_PARSED_TEMPLATE: {
3197 LateParsedTemplates.append(Record.begin(), Record.end());
3201 case OPTIMIZE_PRAGMA_OPTIONS:
3202 if (Record.size() != 1) {
3203 Error("invalid pragma optimize record");
3206 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3209 case MSSTRUCT_PRAGMA_OPTIONS:
3210 if (Record.size() != 1) {
3211 Error("invalid pragma ms_struct record");
3214 PragmaMSStructState = Record[0];
3217 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3218 if (Record.size() != 2) {
3219 Error("invalid pragma ms_struct record");
3222 PragmaMSPointersToMembersState = Record[0];
3223 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3226 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3227 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3228 UnusedLocalTypedefNameCandidates.push_back(
3229 getGlobalDeclID(F, Record[I]));
3235 ASTReader::ASTReadResult
3236 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3237 const ModuleFile *ImportedBy,
3238 unsigned ClientLoadCapabilities) {
3240 F.ModuleMapPath = ReadPath(F, Record, Idx);
3242 if (F.Kind == MK_ExplicitModule) {
3243 // For an explicitly-loaded module, we don't care whether the original
3244 // module map file exists or matches.
3248 // Try to resolve ModuleName in the current header search context and
3249 // verify that it is found in the same module map file as we saved. If the
3250 // top-level AST file is a main file, skip this check because there is no
3251 // usable header search context.
3252 assert(!F.ModuleName.empty() &&
3253 "MODULE_NAME should come before MODULE_MAP_FILE");
3254 if (F.Kind == MK_ImplicitModule &&
3255 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3256 // An implicitly-loaded module file should have its module listed in some
3257 // module map file that we've already loaded.
3258 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3259 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3260 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3262 assert(ImportedBy && "top-level import should be verified");
3263 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3264 if (auto *ASTFE = M ? M->getASTFile() : nullptr)
3265 // This module was defined by an imported (explicit) module.
3266 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3267 << ASTFE->getName();
3269 // This module was built with a different module map.
3270 Diag(diag::err_imported_module_not_found)
3271 << F.ModuleName << F.FileName << ImportedBy->FileName
3277 assert(M->Name == F.ModuleName && "found module with different name");
3279 // Check the primary module map file.
3280 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3281 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3282 assert(ModMap && "found module is missing module map file");
3283 assert(ImportedBy && "top-level import should be verified");
3284 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3285 Diag(diag::err_imported_module_modmap_changed)
3286 << F.ModuleName << ImportedBy->FileName
3287 << ModMap->getName() << F.ModuleMapPath;
3291 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3292 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3293 // FIXME: we should use input files rather than storing names.
3294 std::string Filename = ReadPath(F, Record, Idx);
3295 const FileEntry *F =
3296 FileMgr.getFile(Filename, false, false);
3298 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3299 Error("could not find file '" + Filename +"' referenced by AST file");
3302 AdditionalStoredMaps.insert(F);
3305 // Check any additional module map files (e.g. module.private.modulemap)
3306 // that are not in the pcm.
3307 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3308 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3309 // Remove files that match
3310 // Note: SmallPtrSet::erase is really remove
3311 if (!AdditionalStoredMaps.erase(ModMap)) {
3312 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3313 Diag(diag::err_module_different_modmap)
3314 << F.ModuleName << /*new*/0 << ModMap->getName();
3320 // Check any additional module map files that are in the pcm, but not
3321 // found in header search. Cases that match are already removed.
3322 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3323 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3324 Diag(diag::err_module_different_modmap)
3325 << F.ModuleName << /*not new*/1 << ModMap->getName();
3331 Listener->ReadModuleMapFile(F.ModuleMapPath);
3336 /// \brief Move the given method to the back of the global list of methods.
3337 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3338 // Find the entry for this selector in the method pool.
3339 Sema::GlobalMethodPool::iterator Known
3340 = S.MethodPool.find(Method->getSelector());
3341 if (Known == S.MethodPool.end())
3344 // Retrieve the appropriate method list.
3345 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3346 : Known->second.second;
3348 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3350 if (List->getMethod() == Method) {
3358 if (List->getNext())
3359 List->setMethod(List->getNext()->getMethod());
3361 List->setMethod(Method);
3365 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3366 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3367 for (Decl *D : Names) {
3368 bool wasHidden = D->Hidden;
3371 if (wasHidden && SemaObj) {
3372 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3373 moveMethodToBackOfGlobalList(*SemaObj, Method);
3379 void ASTReader::makeModuleVisible(Module *Mod,
3380 Module::NameVisibilityKind NameVisibility,
3381 SourceLocation ImportLoc) {
3382 llvm::SmallPtrSet<Module *, 4> Visited;
3383 SmallVector<Module *, 4> Stack;
3384 Stack.push_back(Mod);
3385 while (!Stack.empty()) {
3386 Mod = Stack.pop_back_val();
3388 if (NameVisibility <= Mod->NameVisibility) {
3389 // This module already has this level of visibility (or greater), so
3390 // there is nothing more to do.
3394 if (!Mod->isAvailable()) {
3395 // Modules that aren't available cannot be made visible.
3399 // Update the module's name visibility.
3400 Mod->NameVisibility = NameVisibility;
3402 // If we've already deserialized any names from this module,
3403 // mark them as visible.
3404 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3405 if (Hidden != HiddenNamesMap.end()) {
3406 auto HiddenNames = std::move(*Hidden);
3407 HiddenNamesMap.erase(Hidden);
3408 makeNamesVisible(HiddenNames.second, HiddenNames.first);
3409 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3410 "making names visible added hidden names");
3413 // Push any exported modules onto the stack to be marked as visible.
3414 SmallVector<Module *, 16> Exports;
3415 Mod->getExportedModules(Exports);
3416 for (SmallVectorImpl<Module *>::iterator
3417 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3418 Module *Exported = *I;
3419 if (Visited.insert(Exported).second)
3420 Stack.push_back(Exported);
3425 bool ASTReader::loadGlobalIndex() {
3429 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3430 !Context.getLangOpts().Modules)
3433 // Try to load the global index.
3434 TriedLoadingGlobalIndex = true;
3435 StringRef ModuleCachePath
3436 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3437 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3438 = GlobalModuleIndex::readIndex(ModuleCachePath);
3442 GlobalIndex.reset(Result.first);
3443 ModuleMgr.setGlobalIndex(GlobalIndex.get());
3447 bool ASTReader::isGlobalIndexUnavailable() const {
3448 return Context.getLangOpts().Modules && UseGlobalIndex &&
3449 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3452 static void updateModuleTimestamp(ModuleFile &MF) {
3453 // Overwrite the timestamp file contents so that file's mtime changes.
3454 std::string TimestampFilename = MF.getTimestampFilename();
3456 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3459 OS << "Timestamp file\n";
3462 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3463 /// cursor into the start of the given block ID, returning false on success and
3464 /// true on failure.
3465 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3467 llvm::BitstreamEntry Entry = Cursor.advance();
3468 switch (Entry.Kind) {
3469 case llvm::BitstreamEntry::Error:
3470 case llvm::BitstreamEntry::EndBlock:
3473 case llvm::BitstreamEntry::Record:
3474 // Ignore top-level records.
3475 Cursor.skipRecord(Entry.ID);
3478 case llvm::BitstreamEntry::SubBlock:
3479 if (Entry.ID == BlockID) {
3480 if (Cursor.EnterSubBlock(BlockID))
3486 if (Cursor.SkipBlock())
3492 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
3494 SourceLocation ImportLoc,
3495 unsigned ClientLoadCapabilities) {
3496 llvm::SaveAndRestore<SourceLocation>
3497 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3499 // Defer any pending actions until we get to the end of reading the AST file.
3500 Deserializing AnASTFile(this);
3502 // Bump the generation number.
3503 unsigned PreviousGeneration = incrementGeneration(Context);
3505 unsigned NumModules = ModuleMgr.size();
3506 SmallVector<ImportedModule, 4> Loaded;
3507 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3508 /*ImportedBy=*/nullptr, Loaded,
3510 ClientLoadCapabilities)) {
3514 case VersionMismatch:
3515 case ConfigurationMismatch:
3517 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3518 for (const ImportedModule &IM : Loaded)
3519 LoadedSet.insert(IM.Mod);
3521 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3523 Context.getLangOpts().Modules
3524 ? &PP.getHeaderSearchInfo().getModuleMap()
3527 // If we find that any modules are unusable, the global index is going
3528 // to be out-of-date. Just remove it.
3529 GlobalIndex.reset();
3530 ModuleMgr.setGlobalIndex(nullptr);
3537 // Here comes stuff that we only do once the entire chain is loaded.
3539 // Load the AST blocks of all of the modules that we loaded.
3540 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3541 MEnd = Loaded.end();
3543 ModuleFile &F = *M->Mod;
3545 // Read the AST block.
3546 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3549 // Read the extension blocks.
3550 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3551 if (ASTReadResult Result = ReadExtensionBlock(F))
3555 // Once read, set the ModuleFile bit base offset and update the size in
3556 // bits of all files we've seen.
3557 F.GlobalBitOffset = TotalModulesSizeInBits;
3558 TotalModulesSizeInBits += F.SizeInBits;
3559 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3561 // Preload SLocEntries.
3562 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3563 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3564 // Load it through the SourceManager and don't call ReadSLocEntry()
3565 // directly because the entry may have already been loaded in which case
3566 // calling ReadSLocEntry() directly would trigger an assertion in
3568 SourceMgr.getLoadedSLocEntryByID(Index);
3571 // Preload all the pending interesting identifiers by marking them out of
3573 for (auto Offset : F.PreloadIdentifierOffsets) {
3574 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3575 F.IdentifierTableData + Offset);
3577 ASTIdentifierLookupTrait Trait(*this, F);
3578 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3579 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
3580 auto &II = PP.getIdentifierTable().getOwn(Key);
3581 II.setOutOfDate(true);
3583 // Mark this identifier as being from an AST file so that we can track
3584 // whether we need to serialize it.
3585 markIdentifierFromAST(*this, II);
3587 // Associate the ID with the identifier so that the writer can reuse it.
3588 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3589 SetIdentifierInfo(ID, &II);
3593 // Setup the import locations and notify the module manager that we've
3594 // committed to these module files.
3595 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3596 MEnd = Loaded.end();
3598 ModuleFile &F = *M->Mod;
3600 ModuleMgr.moduleFileAccepted(&F);
3602 // Set the import location.
3603 F.DirectImportLoc = ImportLoc;
3605 F.ImportLoc = M->ImportLoc;
3607 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3608 M->ImportLoc.getRawEncoding());
3611 if (!Context.getLangOpts().CPlusPlus ||
3612 (Type != MK_ImplicitModule && Type != MK_ExplicitModule)) {
3613 // Mark all of the identifiers in the identifier table as being out of date,
3614 // so that various accessors know to check the loaded modules when the
3615 // identifier is used.
3617 // For C++ modules, we don't need information on many identifiers (just
3618 // those that provide macros or are poisoned), so we mark all of
3619 // the interesting ones via PreloadIdentifierOffsets.
3620 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3621 IdEnd = PP.getIdentifierTable().end();
3623 Id->second->setOutOfDate(true);
3626 // Resolve any unresolved module exports.
3627 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3628 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3629 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3630 Module *ResolvedMod = getSubmodule(GlobalID);
3632 switch (Unresolved.Kind) {
3633 case UnresolvedModuleRef::Conflict:
3635 Module::Conflict Conflict;
3636 Conflict.Other = ResolvedMod;
3637 Conflict.Message = Unresolved.String.str();
3638 Unresolved.Mod->Conflicts.push_back(Conflict);
3642 case UnresolvedModuleRef::Import:
3644 Unresolved.Mod->Imports.insert(ResolvedMod);
3647 case UnresolvedModuleRef::Export:
3648 if (ResolvedMod || Unresolved.IsWildcard)
3649 Unresolved.Mod->Exports.push_back(
3650 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3654 UnresolvedModuleRefs.clear();
3656 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3657 // Might be unnecessary as use declarations are only used to build the
3660 InitializeContext();
3665 if (DeserializationListener)
3666 DeserializationListener->ReaderInitialized(this);
3668 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3669 if (PrimaryModule.OriginalSourceFileID.isValid()) {
3670 PrimaryModule.OriginalSourceFileID
3671 = FileID::get(PrimaryModule.SLocEntryBaseID
3672 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3674 // If this AST file is a precompiled preamble, then set the
3675 // preamble file ID of the source manager to the file source file
3676 // from which the preamble was built.
3677 if (Type == MK_Preamble) {
3678 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3679 } else if (Type == MK_MainFile) {
3680 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3684 // For any Objective-C class definitions we have already loaded, make sure
3685 // that we load any additional categories.
3686 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3687 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3688 ObjCClassesLoaded[I],
3689 PreviousGeneration);
3692 if (PP.getHeaderSearchInfo()
3693 .getHeaderSearchOpts()
3694 .ModulesValidateOncePerBuildSession) {
3695 // Now we are certain that the module and all modules it depends on are
3696 // up to date. Create or update timestamp files for modules that are
3697 // located in the module cache (not for PCH files that could be anywhere
3698 // in the filesystem).
3699 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3700 ImportedModule &M = Loaded[I];
3701 if (M.Mod->Kind == MK_ImplicitModule) {
3702 updateModuleTimestamp(*M.Mod);
3710 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3712 /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3713 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3714 return Stream.Read(8) == 'C' &&
3715 Stream.Read(8) == 'P' &&
3716 Stream.Read(8) == 'C' &&
3717 Stream.Read(8) == 'H';
3720 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
3724 case MK_ImplicitModule:
3725 case MK_ExplicitModule:
3729 return 2; // main source file
3731 llvm_unreachable("unknown module kind");
3734 ASTReader::ASTReadResult
3735 ASTReader::ReadASTCore(StringRef FileName,
3737 SourceLocation ImportLoc,
3738 ModuleFile *ImportedBy,
3739 SmallVectorImpl<ImportedModule> &Loaded,
3740 off_t ExpectedSize, time_t ExpectedModTime,
3741 ASTFileSignature ExpectedSignature,
3742 unsigned ClientLoadCapabilities) {
3744 std::string ErrorStr;
3745 ModuleManager::AddModuleResult AddResult
3746 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3747 getGeneration(), ExpectedSize, ExpectedModTime,
3748 ExpectedSignature, readASTFileSignature,
3751 switch (AddResult) {
3752 case ModuleManager::AlreadyLoaded:
3755 case ModuleManager::NewlyLoaded:
3756 // Load module file below.
3759 case ModuleManager::Missing:
3760 // The module file was missing; if the client can handle that, return
3762 if (ClientLoadCapabilities & ARR_Missing)
3765 // Otherwise, return an error.
3766 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
3767 << FileName << ErrorStr.empty()
3771 case ModuleManager::OutOfDate:
3772 // We couldn't load the module file because it is out-of-date. If the
3773 // client can handle out-of-date, return it.
3774 if (ClientLoadCapabilities & ARR_OutOfDate)
3777 // Otherwise, return an error.
3778 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
3779 << FileName << ErrorStr.empty()
3784 assert(M && "Missing module file");
3786 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3788 if (FileName != "-") {
3789 CurrentDir = llvm::sys::path::parent_path(FileName);
3790 if (CurrentDir.empty()) CurrentDir = ".";
3794 BitstreamCursor &Stream = F.Stream;
3795 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
3796 Stream.init(&F.StreamFile);
3797 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3799 // Sniff for the signature.
3800 if (!startsWithASTFileMagic(Stream)) {
3801 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
3806 // This is used for compatibility with older PCH formats.
3807 bool HaveReadControlBlock = false;
3809 llvm::BitstreamEntry Entry = Stream.advance();
3811 switch (Entry.Kind) {
3812 case llvm::BitstreamEntry::Error:
3813 case llvm::BitstreamEntry::Record:
3814 case llvm::BitstreamEntry::EndBlock:
3815 Error("invalid record at top-level of AST file");
3818 case llvm::BitstreamEntry::SubBlock:
3823 case CONTROL_BLOCK_ID:
3824 HaveReadControlBlock = true;
3825 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
3827 // Check that we didn't try to load a non-module AST file as a module.
3829 // FIXME: Should we also perform the converse check? Loading a module as
3830 // a PCH file sort of works, but it's a bit wonky.
3831 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule) &&
3832 F.ModuleName.empty()) {
3833 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
3834 if (Result != OutOfDate ||
3835 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
3836 Diag(diag::err_module_file_not_module) << FileName;
3841 case Failure: return Failure;
3842 case Missing: return Missing;
3843 case OutOfDate: return OutOfDate;
3844 case VersionMismatch: return VersionMismatch;
3845 case ConfigurationMismatch: return ConfigurationMismatch;
3846 case HadErrors: return HadErrors;
3851 if (!HaveReadControlBlock) {
3852 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3853 Diag(diag::err_pch_version_too_old);
3854 return VersionMismatch;
3857 // Record that we've loaded this module.
3858 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3862 if (Stream.SkipBlock()) {
3863 Error("malformed block record in AST file");
3873 /// Parse a record and blob containing module file extension metadata.
3874 static bool parseModuleFileExtensionMetadata(
3875 const SmallVectorImpl<uint64_t> &Record,
3877 ModuleFileExtensionMetadata &Metadata) {
3878 if (Record.size() < 4) return true;
3880 Metadata.MajorVersion = Record[0];
3881 Metadata.MinorVersion = Record[1];
3883 unsigned BlockNameLen = Record[2];
3884 unsigned UserInfoLen = Record[3];
3886 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
3888 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
3889 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
3890 Blob.data() + BlockNameLen + UserInfoLen);
3894 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
3895 BitstreamCursor &Stream = F.Stream;
3899 llvm::BitstreamEntry Entry = Stream.advance();
3900 switch (Entry.Kind) {
3901 case llvm::BitstreamEntry::SubBlock:
3902 if (Stream.SkipBlock())
3907 case llvm::BitstreamEntry::EndBlock:
3910 case llvm::BitstreamEntry::Error:
3913 case llvm::BitstreamEntry::Record:
3919 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3921 case EXTENSION_METADATA: {
3922 ModuleFileExtensionMetadata Metadata;
3923 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
3926 // Find a module file extension with this block name.
3927 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
3928 if (Known == ModuleFileExtensions.end()) break;
3931 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
3933 F.ExtensionReaders.push_back(std::move(Reader));
3944 void ASTReader::InitializeContext() {
3945 // If there's a listener, notify them that we "read" the translation unit.
3946 if (DeserializationListener)
3947 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3948 Context.getTranslationUnitDecl());
3950 // FIXME: Find a better way to deal with collisions between these
3951 // built-in types. Right now, we just ignore the problem.
3953 // Load the special types.
3954 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3955 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3956 if (!Context.CFConstantStringTypeDecl)
3957 Context.setCFConstantStringType(GetType(String));
3960 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3961 QualType FileType = GetType(File);
3962 if (FileType.isNull()) {
3963 Error("FILE type is NULL");
3967 if (!Context.FILEDecl) {
3968 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3969 Context.setFILEDecl(Typedef->getDecl());
3971 const TagType *Tag = FileType->getAs<TagType>();
3973 Error("Invalid FILE type in AST file");
3976 Context.setFILEDecl(Tag->getDecl());
3981 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3982 QualType Jmp_bufType = GetType(Jmp_buf);
3983 if (Jmp_bufType.isNull()) {
3984 Error("jmp_buf type is NULL");
3988 if (!Context.jmp_bufDecl) {
3989 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3990 Context.setjmp_bufDecl(Typedef->getDecl());
3992 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3994 Error("Invalid jmp_buf type in AST file");
3997 Context.setjmp_bufDecl(Tag->getDecl());
4002 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4003 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4004 if (Sigjmp_bufType.isNull()) {
4005 Error("sigjmp_buf type is NULL");
4009 if (!Context.sigjmp_bufDecl) {
4010 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4011 Context.setsigjmp_bufDecl(Typedef->getDecl());
4013 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4014 assert(Tag && "Invalid sigjmp_buf type in AST file");
4015 Context.setsigjmp_bufDecl(Tag->getDecl());
4020 if (unsigned ObjCIdRedef
4021 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4022 if (Context.ObjCIdRedefinitionType.isNull())
4023 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4026 if (unsigned ObjCClassRedef
4027 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4028 if (Context.ObjCClassRedefinitionType.isNull())
4029 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4032 if (unsigned ObjCSelRedef
4033 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4034 if (Context.ObjCSelRedefinitionType.isNull())
4035 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4038 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4039 QualType Ucontext_tType = GetType(Ucontext_t);
4040 if (Ucontext_tType.isNull()) {
4041 Error("ucontext_t type is NULL");
4045 if (!Context.ucontext_tDecl) {
4046 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4047 Context.setucontext_tDecl(Typedef->getDecl());
4049 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4050 assert(Tag && "Invalid ucontext_t type in AST file");
4051 Context.setucontext_tDecl(Tag->getDecl());
4057 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4059 // If there were any CUDA special declarations, deserialize them.
4060 if (!CUDASpecialDeclRefs.empty()) {
4061 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4062 Context.setcudaConfigureCallDecl(
4063 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4066 // Re-export any modules that were imported by a non-module AST file.
4067 // FIXME: This does not make macro-only imports visible again.
4068 for (auto &Import : ImportedModules) {
4069 if (Module *Imported = getSubmodule(Import.ID)) {
4070 makeModuleVisible(Imported, Module::AllVisible,
4071 /*ImportLoc=*/Import.ImportLoc);
4072 if (Import.ImportLoc.isValid())
4073 PP.makeModuleVisible(Imported, Import.ImportLoc);
4074 // FIXME: should we tell Sema to make the module visible too?
4077 ImportedModules.clear();
4080 void ASTReader::finalizeForWriting() {
4081 // Nothing to do for now.
4084 /// \brief Reads and return the signature record from \p StreamFile's control
4085 /// block, or else returns 0.
4086 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4087 BitstreamCursor Stream(StreamFile);
4088 if (!startsWithASTFileMagic(Stream))
4091 // Scan for the CONTROL_BLOCK_ID block.
4092 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4095 // Scan for SIGNATURE inside the control block.
4096 ASTReader::RecordData Record;
4098 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4099 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4100 Entry.Kind != llvm::BitstreamEntry::Record)
4105 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4110 /// \brief Retrieve the name of the original source file name
4111 /// directly from the AST file, without actually loading the AST
4113 std::string ASTReader::getOriginalSourceFile(
4114 const std::string &ASTFileName, FileManager &FileMgr,
4115 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4116 // Open the AST file.
4117 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4119 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4120 << ASTFileName << Buffer.getError().message();
4121 return std::string();
4124 // Initialize the stream
4125 llvm::BitstreamReader StreamFile;
4126 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
4127 BitstreamCursor Stream(StreamFile);
4129 // Sniff for the signature.
4130 if (!startsWithASTFileMagic(Stream)) {
4131 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4132 return std::string();
4135 // Scan for the CONTROL_BLOCK_ID block.
4136 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4137 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4138 return std::string();
4141 // Scan for ORIGINAL_FILE inside the control block.
4144 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4145 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4146 return std::string();
4148 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4149 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4150 return std::string();
4155 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4161 class SimplePCHValidator : public ASTReaderListener {
4162 const LangOptions &ExistingLangOpts;
4163 const TargetOptions &ExistingTargetOpts;
4164 const PreprocessorOptions &ExistingPPOpts;
4165 std::string ExistingModuleCachePath;
4166 FileManager &FileMgr;
4169 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4170 const TargetOptions &ExistingTargetOpts,
4171 const PreprocessorOptions &ExistingPPOpts,
4172 StringRef ExistingModuleCachePath,
4173 FileManager &FileMgr)
4174 : ExistingLangOpts(ExistingLangOpts),
4175 ExistingTargetOpts(ExistingTargetOpts),
4176 ExistingPPOpts(ExistingPPOpts),
4177 ExistingModuleCachePath(ExistingModuleCachePath),
4182 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4183 bool AllowCompatibleDifferences) override {
4184 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4185 AllowCompatibleDifferences);
4187 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4188 bool AllowCompatibleDifferences) override {
4189 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4190 AllowCompatibleDifferences);
4192 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4193 StringRef SpecificModuleCachePath,
4194 bool Complain) override {
4195 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4196 ExistingModuleCachePath,
4197 nullptr, ExistingLangOpts);
4199 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4201 std::string &SuggestedPredefines) override {
4202 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4203 SuggestedPredefines, ExistingLangOpts);
4208 bool ASTReader::readASTFileControlBlock(
4209 StringRef Filename, FileManager &FileMgr,
4210 const PCHContainerReader &PCHContainerRdr,
4211 bool FindModuleFileExtensions,
4212 ASTReaderListener &Listener) {
4213 // Open the AST file.
4214 // FIXME: This allows use of the VFS; we do not allow use of the
4215 // VFS when actually loading a module.
4216 auto Buffer = FileMgr.getBufferForFile(Filename);
4221 // Initialize the stream
4222 llvm::BitstreamReader StreamFile;
4223 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
4224 BitstreamCursor Stream(StreamFile);
4226 // Sniff for the signature.
4227 if (!startsWithASTFileMagic(Stream))
4230 // Scan for the CONTROL_BLOCK_ID block.
4231 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4234 bool NeedsInputFiles = Listener.needsInputFileVisitation();
4235 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4236 bool NeedsImports = Listener.needsImportVisitation();
4237 BitstreamCursor InputFilesCursor;
4240 std::string ModuleDir;
4241 bool DoneWithControlBlock = false;
4242 while (!DoneWithControlBlock) {
4243 llvm::BitstreamEntry Entry = Stream.advance();
4245 switch (Entry.Kind) {
4246 case llvm::BitstreamEntry::SubBlock: {
4248 case OPTIONS_BLOCK_ID: {
4249 std::string IgnoredSuggestedPredefines;
4250 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4251 /*AllowCompatibleConfigurationMismatch*/ false,
4252 Listener, IgnoredSuggestedPredefines) != Success)
4257 case INPUT_FILES_BLOCK_ID:
4258 InputFilesCursor = Stream;
4259 if (Stream.SkipBlock() ||
4261 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4266 if (Stream.SkipBlock())
4274 case llvm::BitstreamEntry::EndBlock:
4275 DoneWithControlBlock = true;
4278 case llvm::BitstreamEntry::Error:
4281 case llvm::BitstreamEntry::Record:
4285 if (DoneWithControlBlock) break;
4289 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4290 switch ((ControlRecordTypes)RecCode) {
4292 if (Record[0] != VERSION_MAJOR)
4295 if (Listener.ReadFullVersionInformation(Blob))
4301 Listener.ReadModuleName(Blob);
4303 case MODULE_DIRECTORY:
4306 case MODULE_MAP_FILE: {
4308 auto Path = ReadString(Record, Idx);
4309 ResolveImportedPath(Path, ModuleDir);
4310 Listener.ReadModuleMapFile(Path);
4313 case INPUT_FILE_OFFSETS: {
4314 if (!NeedsInputFiles)
4317 unsigned NumInputFiles = Record[0];
4318 unsigned NumUserFiles = Record[1];
4319 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
4320 for (unsigned I = 0; I != NumInputFiles; ++I) {
4321 // Go find this input file.
4322 bool isSystemFile = I >= NumUserFiles;
4324 if (isSystemFile && !NeedsSystemInputFiles)
4325 break; // the rest are system input files
4327 BitstreamCursor &Cursor = InputFilesCursor;
4328 SavedStreamPosition SavedPosition(Cursor);
4329 Cursor.JumpToBit(InputFileOffs[I]);
4331 unsigned Code = Cursor.ReadCode();
4334 bool shouldContinue = false;
4335 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4337 bool Overridden = static_cast<bool>(Record[3]);
4338 std::string Filename = Blob;
4339 ResolveImportedPath(Filename, ModuleDir);
4340 shouldContinue = Listener.visitInputFile(
4341 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
4344 if (!shouldContinue)
4354 unsigned Idx = 0, N = Record.size();
4356 // Read information about the AST file.
4357 Idx += 5; // ImportLoc, Size, ModTime, Signature
4358 std::string Filename = ReadString(Record, Idx);
4359 ResolveImportedPath(Filename, ModuleDir);
4360 Listener.visitImport(Filename);
4366 // No other validation to perform.
4371 // Look for module file extension blocks, if requested.
4372 if (FindModuleFileExtensions) {
4373 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4374 bool DoneWithExtensionBlock = false;
4375 while (!DoneWithExtensionBlock) {
4376 llvm::BitstreamEntry Entry = Stream.advance();
4378 switch (Entry.Kind) {
4379 case llvm::BitstreamEntry::SubBlock:
4380 if (Stream.SkipBlock())
4385 case llvm::BitstreamEntry::EndBlock:
4386 DoneWithExtensionBlock = true;
4389 case llvm::BitstreamEntry::Error:
4392 case llvm::BitstreamEntry::Record:
4398 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4400 case EXTENSION_METADATA: {
4401 ModuleFileExtensionMetadata Metadata;
4402 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4405 Listener.readModuleFileExtension(Metadata);
4416 bool ASTReader::isAcceptableASTFile(
4417 StringRef Filename, FileManager &FileMgr,
4418 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
4419 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4420 std::string ExistingModuleCachePath) {
4421 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4422 ExistingModuleCachePath, FileMgr);
4423 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
4424 /*FindModuleFileExtensions=*/false,
4428 ASTReader::ASTReadResult
4429 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4430 // Enter the submodule block.
4431 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4432 Error("malformed submodule block record in AST file");
4436 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4438 Module *CurrentModule = nullptr;
4441 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4443 switch (Entry.Kind) {
4444 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4445 case llvm::BitstreamEntry::Error:
4446 Error("malformed block record in AST file");
4448 case llvm::BitstreamEntry::EndBlock:
4450 case llvm::BitstreamEntry::Record:
4451 // The interesting case.
4458 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4460 if ((Kind == SUBMODULE_METADATA) != First) {
4461 Error("submodule metadata record should be at beginning of block");
4466 // Submodule information is only valid if we have a current module.
4467 // FIXME: Should we error on these cases?
4468 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4469 Kind != SUBMODULE_DEFINITION)
4473 default: // Default behavior: ignore.
4476 case SUBMODULE_DEFINITION: {
4477 if (Record.size() < 8) {
4478 Error("malformed module definition");
4482 StringRef Name = Blob;
4484 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4485 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4486 bool IsFramework = Record[Idx++];
4487 bool IsExplicit = Record[Idx++];
4488 bool IsSystem = Record[Idx++];
4489 bool IsExternC = Record[Idx++];
4490 bool InferSubmodules = Record[Idx++];
4491 bool InferExplicitSubmodules = Record[Idx++];
4492 bool InferExportWildcard = Record[Idx++];
4493 bool ConfigMacrosExhaustive = Record[Idx++];
4495 Module *ParentModule = nullptr;
4497 ParentModule = getSubmodule(Parent);
4499 // Retrieve this (sub)module from the module map, creating it if
4501 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
4504 // FIXME: set the definition loc for CurrentModule, or call
4505 // ModMap.setInferredModuleAllowedBy()
4507 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4508 if (GlobalIndex >= SubmodulesLoaded.size() ||
4509 SubmodulesLoaded[GlobalIndex]) {
4510 Error("too many submodules");
4514 if (!ParentModule) {
4515 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4516 if (CurFile != F.File) {
4517 if (!Diags.isDiagnosticInFlight()) {
4518 Diag(diag::err_module_file_conflict)
4519 << CurrentModule->getTopLevelModuleName()
4520 << CurFile->getName()
4521 << F.File->getName();
4527 CurrentModule->setASTFile(F.File);
4530 CurrentModule->Signature = F.Signature;
4531 CurrentModule->IsFromModuleFile = true;
4532 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
4533 CurrentModule->IsExternC = IsExternC;
4534 CurrentModule->InferSubmodules = InferSubmodules;
4535 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4536 CurrentModule->InferExportWildcard = InferExportWildcard;
4537 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
4538 if (DeserializationListener)
4539 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4541 SubmodulesLoaded[GlobalIndex] = CurrentModule;
4543 // Clear out data that will be replaced by what is the module file.
4544 CurrentModule->LinkLibraries.clear();
4545 CurrentModule->ConfigMacros.clear();
4546 CurrentModule->UnresolvedConflicts.clear();
4547 CurrentModule->Conflicts.clear();
4551 case SUBMODULE_UMBRELLA_HEADER: {
4552 std::string Filename = Blob;
4553 ResolveImportedPath(F, Filename);
4554 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
4555 if (!CurrentModule->getUmbrellaHeader())
4556 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4557 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
4558 // This can be a spurious difference caused by changing the VFS to
4559 // point to a different copy of the file, and it is too late to
4560 // to rebuild safely.
4561 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4562 // after input file validation only real problems would remain and we
4563 // could just error. For now, assume it's okay.
4570 case SUBMODULE_HEADER:
4571 case SUBMODULE_EXCLUDED_HEADER:
4572 case SUBMODULE_PRIVATE_HEADER:
4573 // We lazily associate headers with their modules via the HeaderInfo table.
4574 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4575 // of complete filenames or remove it entirely.
4578 case SUBMODULE_TEXTUAL_HEADER:
4579 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4580 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4584 case SUBMODULE_TOPHEADER: {
4585 CurrentModule->addTopHeaderFilename(Blob);
4589 case SUBMODULE_UMBRELLA_DIR: {
4590 std::string Dirname = Blob;
4591 ResolveImportedPath(F, Dirname);
4592 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
4593 if (!CurrentModule->getUmbrellaDir())
4594 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4595 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
4596 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4597 Error("mismatched umbrella directories in submodule");
4604 case SUBMODULE_METADATA: {
4605 F.BaseSubmoduleID = getTotalNumSubmodules();
4606 F.LocalNumSubmodules = Record[0];
4607 unsigned LocalBaseSubmoduleID = Record[1];
4608 if (F.LocalNumSubmodules > 0) {
4609 // Introduce the global -> local mapping for submodules within this
4611 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4613 // Introduce the local -> global mapping for submodules within this
4615 F.SubmoduleRemap.insertOrReplace(
4616 std::make_pair(LocalBaseSubmoduleID,
4617 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4619 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4624 case SUBMODULE_IMPORTS: {
4625 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
4626 UnresolvedModuleRef Unresolved;
4627 Unresolved.File = &F;
4628 Unresolved.Mod = CurrentModule;
4629 Unresolved.ID = Record[Idx];
4630 Unresolved.Kind = UnresolvedModuleRef::Import;
4631 Unresolved.IsWildcard = false;
4632 UnresolvedModuleRefs.push_back(Unresolved);
4637 case SUBMODULE_EXPORTS: {
4638 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
4639 UnresolvedModuleRef Unresolved;
4640 Unresolved.File = &F;
4641 Unresolved.Mod = CurrentModule;
4642 Unresolved.ID = Record[Idx];
4643 Unresolved.Kind = UnresolvedModuleRef::Export;
4644 Unresolved.IsWildcard = Record[Idx + 1];
4645 UnresolvedModuleRefs.push_back(Unresolved);
4648 // Once we've loaded the set of exports, there's no reason to keep
4649 // the parsed, unresolved exports around.
4650 CurrentModule->UnresolvedExports.clear();
4653 case SUBMODULE_REQUIRES: {
4654 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
4655 Context.getTargetInfo());
4659 case SUBMODULE_LINK_LIBRARY:
4660 CurrentModule->LinkLibraries.push_back(
4661 Module::LinkLibrary(Blob, Record[0]));
4664 case SUBMODULE_CONFIG_MACRO:
4665 CurrentModule->ConfigMacros.push_back(Blob.str());
4668 case SUBMODULE_CONFLICT: {
4669 UnresolvedModuleRef Unresolved;
4670 Unresolved.File = &F;
4671 Unresolved.Mod = CurrentModule;
4672 Unresolved.ID = Record[0];
4673 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4674 Unresolved.IsWildcard = false;
4675 Unresolved.String = Blob;
4676 UnresolvedModuleRefs.push_back(Unresolved);
4683 /// \brief Parse the record that corresponds to a LangOptions data
4686 /// This routine parses the language options from the AST file and then gives
4687 /// them to the AST listener if one is set.
4689 /// \returns true if the listener deems the file unacceptable, false otherwise.
4690 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4692 ASTReaderListener &Listener,
4693 bool AllowCompatibleDifferences) {
4694 LangOptions LangOpts;
4696 #define LANGOPT(Name, Bits, Default, Description) \
4697 LangOpts.Name = Record[Idx++];
4698 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4699 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4700 #include "clang/Basic/LangOptions.def"
4701 #define SANITIZER(NAME, ID) \
4702 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
4703 #include "clang/Basic/Sanitizers.def"
4705 for (unsigned N = Record[Idx++]; N; --N)
4706 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
4708 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4709 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4710 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4712 LangOpts.CurrentModule = ReadString(Record, Idx);
4715 for (unsigned N = Record[Idx++]; N; --N) {
4716 LangOpts.CommentOpts.BlockCommandNames.push_back(
4717 ReadString(Record, Idx));
4719 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4721 // OpenMP offloading options.
4722 for (unsigned N = Record[Idx++]; N; --N) {
4723 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
4726 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
4728 return Listener.ReadLanguageOptions(LangOpts, Complain,
4729 AllowCompatibleDifferences);
4732 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4733 ASTReaderListener &Listener,
4734 bool AllowCompatibleDifferences) {
4736 TargetOptions TargetOpts;
4737 TargetOpts.Triple = ReadString(Record, Idx);
4738 TargetOpts.CPU = ReadString(Record, Idx);
4739 TargetOpts.ABI = ReadString(Record, Idx);
4740 for (unsigned N = Record[Idx++]; N; --N) {
4741 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4743 for (unsigned N = Record[Idx++]; N; --N) {
4744 TargetOpts.Features.push_back(ReadString(Record, Idx));
4747 return Listener.ReadTargetOptions(TargetOpts, Complain,
4748 AllowCompatibleDifferences);
4751 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4752 ASTReaderListener &Listener) {
4753 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
4755 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
4756 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4757 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
4758 #include "clang/Basic/DiagnosticOptions.def"
4760 for (unsigned N = Record[Idx++]; N; --N)
4761 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
4762 for (unsigned N = Record[Idx++]; N; --N)
4763 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
4765 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4768 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4769 ASTReaderListener &Listener) {
4770 FileSystemOptions FSOpts;
4772 FSOpts.WorkingDir = ReadString(Record, Idx);
4773 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4776 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4778 ASTReaderListener &Listener) {
4779 HeaderSearchOptions HSOpts;
4781 HSOpts.Sysroot = ReadString(Record, Idx);
4784 for (unsigned N = Record[Idx++]; N; --N) {
4785 std::string Path = ReadString(Record, Idx);
4786 frontend::IncludeDirGroup Group
4787 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4788 bool IsFramework = Record[Idx++];
4789 bool IgnoreSysRoot = Record[Idx++];
4790 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4794 // System header prefixes.
4795 for (unsigned N = Record[Idx++]; N; --N) {
4796 std::string Prefix = ReadString(Record, Idx);
4797 bool IsSystemHeader = Record[Idx++];
4798 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
4801 HSOpts.ResourceDir = ReadString(Record, Idx);
4802 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4803 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
4804 HSOpts.DisableModuleHash = Record[Idx++];
4805 HSOpts.UseBuiltinIncludes = Record[Idx++];
4806 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4807 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4808 HSOpts.UseLibcxx = Record[Idx++];
4809 std::string SpecificModuleCachePath = ReadString(Record, Idx);
4811 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4815 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4817 ASTReaderListener &Listener,
4818 std::string &SuggestedPredefines) {
4819 PreprocessorOptions PPOpts;
4822 // Macro definitions/undefs
4823 for (unsigned N = Record[Idx++]; N; --N) {
4824 std::string Macro = ReadString(Record, Idx);
4825 bool IsUndef = Record[Idx++];
4826 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4830 for (unsigned N = Record[Idx++]; N; --N) {
4831 PPOpts.Includes.push_back(ReadString(Record, Idx));
4835 for (unsigned N = Record[Idx++]; N; --N) {
4836 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4839 PPOpts.UsePredefines = Record[Idx++];
4840 PPOpts.DetailedRecord = Record[Idx++];
4841 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4842 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4843 PPOpts.ObjCXXARCStandardLibrary =
4844 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4845 SuggestedPredefines.clear();
4846 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4847 SuggestedPredefines);
4850 std::pair<ModuleFile *, unsigned>
4851 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4852 GlobalPreprocessedEntityMapType::iterator
4853 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4854 assert(I != GlobalPreprocessedEntityMap.end() &&
4855 "Corrupted global preprocessed entity map");
4856 ModuleFile *M = I->second;
4857 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4858 return std::make_pair(M, LocalIndex);
4861 llvm::iterator_range<PreprocessingRecord::iterator>
4862 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4863 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4864 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4865 Mod.NumPreprocessedEntities);
4867 return llvm::make_range(PreprocessingRecord::iterator(),
4868 PreprocessingRecord::iterator());
4871 llvm::iterator_range<ASTReader::ModuleDeclIterator>
4872 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4873 return llvm::make_range(
4874 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4875 ModuleDeclIterator(this, &Mod,
4876 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4879 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4880 PreprocessedEntityID PPID = Index+1;
4881 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4882 ModuleFile &M = *PPInfo.first;
4883 unsigned LocalIndex = PPInfo.second;
4884 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4886 if (!PP.getPreprocessingRecord()) {
4887 Error("no preprocessing record");
4891 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4892 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4894 llvm::BitstreamEntry Entry =
4895 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4896 if (Entry.Kind != llvm::BitstreamEntry::Record)
4900 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
4901 TranslateSourceLocation(M, PPOffs.getEnd()));
4902 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4905 PreprocessorDetailRecordTypes RecType =
4906 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4907 Entry.ID, Record, &Blob);
4909 case PPD_MACRO_EXPANSION: {
4910 bool isBuiltin = Record[0];
4911 IdentifierInfo *Name = nullptr;
4912 MacroDefinitionRecord *Def = nullptr;
4914 Name = getLocalIdentifier(M, Record[1]);
4916 PreprocessedEntityID GlobalID =
4917 getGlobalPreprocessedEntityID(M, Record[1]);
4918 Def = cast<MacroDefinitionRecord>(
4919 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
4924 ME = new (PPRec) MacroExpansion(Name, Range);
4926 ME = new (PPRec) MacroExpansion(Def, Range);
4931 case PPD_MACRO_DEFINITION: {
4932 // Decode the identifier info and then check again; if the macro is
4933 // still defined and associated with the identifier,
4934 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4935 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
4937 if (DeserializationListener)
4938 DeserializationListener->MacroDefinitionRead(PPID, MD);
4943 case PPD_INCLUSION_DIRECTIVE: {
4944 const char *FullFileNameStart = Blob.data() + Record[0];
4945 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4946 const FileEntry *File = nullptr;
4947 if (!FullFileName.empty())
4948 File = PP.getFileManager().getFile(FullFileName);
4950 // FIXME: Stable encoding
4951 InclusionDirective::InclusionKind Kind
4952 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4953 InclusionDirective *ID
4954 = new (PPRec) InclusionDirective(PPRec, Kind,
4955 StringRef(Blob.data(), Record[0]),
4956 Record[1], Record[3],
4963 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4966 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4967 /// preprocessed entities or the entities it contains are not the ones we are
4968 /// looking for. Find the next module that contains entities and return the ID
4969 /// of the first entry.
4970 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4971 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4973 for (GlobalSLocOffsetMapType::const_iterator
4974 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4975 ModuleFile &M = *SLocMapI->second;
4976 if (M.NumPreprocessedEntities)
4977 return M.BasePreprocessedEntityID;
4980 return getTotalNumPreprocessedEntities();
4985 template <unsigned PPEntityOffset::*PPLoc>
4986 struct PPEntityComp {
4987 const ASTReader &Reader;
4990 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4992 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4993 SourceLocation LHS = getLoc(L);
4994 SourceLocation RHS = getLoc(R);
4995 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4998 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4999 SourceLocation LHS = getLoc(L);
5000 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5003 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5004 SourceLocation RHS = getLoc(R);
5005 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5008 SourceLocation getLoc(const PPEntityOffset &PPE) const {
5009 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
5015 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5016 bool EndsAfter) const {
5017 if (SourceMgr.isLocalSourceLocation(Loc))
5018 return getTotalNumPreprocessedEntities();
5020 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5021 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5022 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5023 "Corrupted global sloc offset map");
5025 if (SLocMapI->second->NumPreprocessedEntities == 0)
5026 return findNextPreprocessedEntity(SLocMapI);
5028 ModuleFile &M = *SLocMapI->second;
5029 typedef const PPEntityOffset *pp_iterator;
5030 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5031 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5033 size_t Count = M.NumPreprocessedEntities;
5035 pp_iterator First = pp_begin;
5039 PPI = std::upper_bound(pp_begin, pp_end, Loc,
5040 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
5042 // Do a binary search manually instead of using std::lower_bound because
5043 // The end locations of entities may be unordered (when a macro expansion
5044 // is inside another macro argument), but for this case it is not important
5045 // whether we get the first macro expansion or its containing macro.
5049 std::advance(PPI, Half);
5050 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
5054 Count = Count - Half - 1;
5061 return findNextPreprocessedEntity(SLocMapI);
5063 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5066 /// \brief Returns a pair of [Begin, End) indices of preallocated
5067 /// preprocessed entities that \arg Range encompasses.
5068 std::pair<unsigned, unsigned>
5069 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5070 if (Range.isInvalid())
5071 return std::make_pair(0,0);
5072 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5074 PreprocessedEntityID BeginID =
5075 findPreprocessedEntity(Range.getBegin(), false);
5076 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5077 return std::make_pair(BeginID, EndID);
5080 /// \brief Optionally returns true or false if the preallocated preprocessed
5081 /// entity with index \arg Index came from file \arg FID.
5082 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
5084 if (FID.isInvalid())
5087 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5088 ModuleFile &M = *PPInfo.first;
5089 unsigned LocalIndex = PPInfo.second;
5090 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5092 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
5093 if (Loc.isInvalid())
5096 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5103 /// \brief Visitor used to search for information about a header file.
5104 class HeaderFileInfoVisitor {
5105 const FileEntry *FE;
5107 Optional<HeaderFileInfo> HFI;
5110 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5113 bool operator()(ModuleFile &M) {
5114 HeaderFileInfoLookupTable *Table
5115 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5119 // Look in the on-disk hash table for an entry for this file name.
5120 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
5121 if (Pos == Table->end())
5128 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5132 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
5133 HeaderFileInfoVisitor Visitor(FE);
5134 ModuleMgr.visit(Visitor);
5135 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5138 return HeaderFileInfo();
5141 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5142 // FIXME: Make it work properly with modules.
5143 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
5144 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5145 ModuleFile &F = *(*I);
5148 assert(!Diag.DiagStates.empty());
5149 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5150 while (Idx < F.PragmaDiagMappings.size()) {
5151 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5152 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5153 if (DiagStateID != 0) {
5154 Diag.DiagStatePoints.push_back(
5155 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5156 FullSourceLoc(Loc, SourceMgr)));
5160 assert(DiagStateID == 0);
5161 // A new DiagState was created here.
5162 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5163 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5164 DiagStates.push_back(NewState);
5165 Diag.DiagStatePoints.push_back(
5166 DiagnosticsEngine::DiagStatePoint(NewState,
5167 FullSourceLoc(Loc, SourceMgr)));
5169 assert(Idx < F.PragmaDiagMappings.size() &&
5170 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5171 if (Idx >= F.PragmaDiagMappings.size()) {
5172 break; // Something is messed up but at least avoid infinite loop in
5175 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5176 if (DiagID == (unsigned)-1) {
5177 break; // no more diag/map pairs for this location.
5179 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5180 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5181 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
5187 /// \brief Get the correct cursor and offset for loading a type.
5188 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5189 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5190 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5191 ModuleFile *M = I->second;
5192 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5195 /// \brief Read and return the type with the given index..
5197 /// The index is the type ID, shifted and minus the number of predefs. This
5198 /// routine actually reads the record corresponding to the type at the given
5199 /// location. It is a helper routine for GetType, which deals with reading type
5201 QualType ASTReader::readTypeRecord(unsigned Index) {
5202 RecordLocation Loc = TypeCursorForIndex(Index);
5203 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5205 // Keep track of where we are in the stream, then jump back there
5206 // after reading this type.
5207 SavedStreamPosition SavedPosition(DeclsCursor);
5209 ReadingKindTracker ReadingKind(Read_Type, *this);
5211 // Note that we are loading a type record.
5212 Deserializing AType(this);
5215 DeclsCursor.JumpToBit(Loc.Offset);
5217 unsigned Code = DeclsCursor.ReadCode();
5218 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5219 case TYPE_EXT_QUAL: {
5220 if (Record.size() != 2) {
5221 Error("Incorrect encoding of extended qualifier type");
5224 QualType Base = readType(*Loc.F, Record, Idx);
5225 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5226 return Context.getQualifiedType(Base, Quals);
5229 case TYPE_COMPLEX: {
5230 if (Record.size() != 1) {
5231 Error("Incorrect encoding of complex type");
5234 QualType ElemType = readType(*Loc.F, Record, Idx);
5235 return Context.getComplexType(ElemType);
5238 case TYPE_POINTER: {
5239 if (Record.size() != 1) {
5240 Error("Incorrect encoding of pointer type");
5243 QualType PointeeType = readType(*Loc.F, Record, Idx);
5244 return Context.getPointerType(PointeeType);
5247 case TYPE_DECAYED: {
5248 if (Record.size() != 1) {
5249 Error("Incorrect encoding of decayed type");
5252 QualType OriginalType = readType(*Loc.F, Record, Idx);
5253 QualType DT = Context.getAdjustedParameterType(OriginalType);
5254 if (!isa<DecayedType>(DT))
5255 Error("Decayed type does not decay");
5259 case TYPE_ADJUSTED: {
5260 if (Record.size() != 2) {
5261 Error("Incorrect encoding of adjusted type");
5264 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5265 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5266 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5269 case TYPE_BLOCK_POINTER: {
5270 if (Record.size() != 1) {
5271 Error("Incorrect encoding of block pointer type");
5274 QualType PointeeType = readType(*Loc.F, Record, Idx);
5275 return Context.getBlockPointerType(PointeeType);
5278 case TYPE_LVALUE_REFERENCE: {
5279 if (Record.size() != 2) {
5280 Error("Incorrect encoding of lvalue reference type");
5283 QualType PointeeType = readType(*Loc.F, Record, Idx);
5284 return Context.getLValueReferenceType(PointeeType, Record[1]);
5287 case TYPE_RVALUE_REFERENCE: {
5288 if (Record.size() != 1) {
5289 Error("Incorrect encoding of rvalue reference type");
5292 QualType PointeeType = readType(*Loc.F, Record, Idx);
5293 return Context.getRValueReferenceType(PointeeType);
5296 case TYPE_MEMBER_POINTER: {
5297 if (Record.size() != 2) {
5298 Error("Incorrect encoding of member pointer type");
5301 QualType PointeeType = readType(*Loc.F, Record, Idx);
5302 QualType ClassType = readType(*Loc.F, Record, Idx);
5303 if (PointeeType.isNull() || ClassType.isNull())
5306 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5309 case TYPE_CONSTANT_ARRAY: {
5310 QualType ElementType = readType(*Loc.F, Record, Idx);
5311 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5312 unsigned IndexTypeQuals = Record[2];
5314 llvm::APInt Size = ReadAPInt(Record, Idx);
5315 return Context.getConstantArrayType(ElementType, Size,
5316 ASM, IndexTypeQuals);
5319 case TYPE_INCOMPLETE_ARRAY: {
5320 QualType ElementType = readType(*Loc.F, Record, Idx);
5321 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5322 unsigned IndexTypeQuals = Record[2];
5323 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5326 case TYPE_VARIABLE_ARRAY: {
5327 QualType ElementType = readType(*Loc.F, Record, Idx);
5328 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5329 unsigned IndexTypeQuals = Record[2];
5330 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5331 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5332 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5333 ASM, IndexTypeQuals,
5334 SourceRange(LBLoc, RBLoc));
5338 if (Record.size() != 3) {
5339 Error("incorrect encoding of vector type in AST file");
5343 QualType ElementType = readType(*Loc.F, Record, Idx);
5344 unsigned NumElements = Record[1];
5345 unsigned VecKind = Record[2];
5346 return Context.getVectorType(ElementType, NumElements,
5347 (VectorType::VectorKind)VecKind);
5350 case TYPE_EXT_VECTOR: {
5351 if (Record.size() != 3) {
5352 Error("incorrect encoding of extended vector type in AST file");
5356 QualType ElementType = readType(*Loc.F, Record, Idx);
5357 unsigned NumElements = Record[1];
5358 return Context.getExtVectorType(ElementType, NumElements);
5361 case TYPE_FUNCTION_NO_PROTO: {
5362 if (Record.size() != 6) {
5363 Error("incorrect encoding of no-proto function type");
5366 QualType ResultType = readType(*Loc.F, Record, Idx);
5367 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5368 (CallingConv)Record[4], Record[5]);
5369 return Context.getFunctionNoProtoType(ResultType, Info);
5372 case TYPE_FUNCTION_PROTO: {
5373 QualType ResultType = readType(*Loc.F, Record, Idx);
5375 FunctionProtoType::ExtProtoInfo EPI;
5376 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5377 /*hasregparm*/ Record[2],
5378 /*regparm*/ Record[3],
5379 static_cast<CallingConv>(Record[4]),
5380 /*produces*/ Record[5]);
5384 EPI.Variadic = Record[Idx++];
5385 EPI.HasTrailingReturn = Record[Idx++];
5386 EPI.TypeQuals = Record[Idx++];
5387 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5388 SmallVector<QualType, 8> ExceptionStorage;
5389 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
5391 unsigned NumParams = Record[Idx++];
5392 SmallVector<QualType, 16> ParamTypes;
5393 for (unsigned I = 0; I != NumParams; ++I)
5394 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5396 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
5397 if (Idx != Record.size()) {
5398 for (unsigned I = 0; I != NumParams; ++I)
5399 ExtParameterInfos.push_back(
5400 FunctionProtoType::ExtParameterInfo
5401 ::getFromOpaqueValue(Record[Idx++]));
5402 EPI.ExtParameterInfos = ExtParameterInfos.data();
5405 assert(Idx == Record.size());
5407 return Context.getFunctionType(ResultType, ParamTypes, EPI);
5410 case TYPE_UNRESOLVED_USING: {
5412 return Context.getTypeDeclType(
5413 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5416 case TYPE_TYPEDEF: {
5417 if (Record.size() != 2) {
5418 Error("incorrect encoding of typedef type");
5422 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5423 QualType Canonical = readType(*Loc.F, Record, Idx);
5424 if (!Canonical.isNull())
5425 Canonical = Context.getCanonicalType(Canonical);
5426 return Context.getTypedefType(Decl, Canonical);
5429 case TYPE_TYPEOF_EXPR:
5430 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5433 if (Record.size() != 1) {
5434 Error("incorrect encoding of typeof(type) in AST file");
5437 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5438 return Context.getTypeOfType(UnderlyingType);
5441 case TYPE_DECLTYPE: {
5442 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5443 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5446 case TYPE_UNARY_TRANSFORM: {
5447 QualType BaseType = readType(*Loc.F, Record, Idx);
5448 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5449 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5450 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5454 QualType Deduced = readType(*Loc.F, Record, Idx);
5455 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
5456 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
5457 return Context.getAutoType(Deduced, Keyword, IsDependent);
5461 if (Record.size() != 2) {
5462 Error("incorrect encoding of record type");
5466 bool IsDependent = Record[Idx++];
5467 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5468 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5469 QualType T = Context.getRecordType(RD);
5470 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5475 if (Record.size() != 2) {
5476 Error("incorrect encoding of enum type");
5480 bool IsDependent = Record[Idx++];
5482 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5483 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5487 case TYPE_ATTRIBUTED: {
5488 if (Record.size() != 3) {
5489 Error("incorrect encoding of attributed type");
5492 QualType modifiedType = readType(*Loc.F, Record, Idx);
5493 QualType equivalentType = readType(*Loc.F, Record, Idx);
5494 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5495 return Context.getAttributedType(kind, modifiedType, equivalentType);
5499 if (Record.size() != 1) {
5500 Error("incorrect encoding of paren type");
5503 QualType InnerType = readType(*Loc.F, Record, Idx);
5504 return Context.getParenType(InnerType);
5507 case TYPE_PACK_EXPANSION: {
5508 if (Record.size() != 2) {
5509 Error("incorrect encoding of pack expansion type");
5512 QualType Pattern = readType(*Loc.F, Record, Idx);
5513 if (Pattern.isNull())
5515 Optional<unsigned> NumExpansions;
5517 NumExpansions = Record[1] - 1;
5518 return Context.getPackExpansionType(Pattern, NumExpansions);
5521 case TYPE_ELABORATED: {
5523 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5524 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5525 QualType NamedType = readType(*Loc.F, Record, Idx);
5526 return Context.getElaboratedType(Keyword, NNS, NamedType);
5529 case TYPE_OBJC_INTERFACE: {
5531 ObjCInterfaceDecl *ItfD
5532 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5533 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5536 case TYPE_OBJC_OBJECT: {
5538 QualType Base = readType(*Loc.F, Record, Idx);
5539 unsigned NumTypeArgs = Record[Idx++];
5540 SmallVector<QualType, 4> TypeArgs;
5541 for (unsigned I = 0; I != NumTypeArgs; ++I)
5542 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
5543 unsigned NumProtos = Record[Idx++];
5544 SmallVector<ObjCProtocolDecl*, 4> Protos;
5545 for (unsigned I = 0; I != NumProtos; ++I)
5546 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5547 bool IsKindOf = Record[Idx++];
5548 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
5551 case TYPE_OBJC_OBJECT_POINTER: {
5553 QualType Pointee = readType(*Loc.F, Record, Idx);
5554 return Context.getObjCObjectPointerType(Pointee);
5557 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5559 QualType Parm = readType(*Loc.F, Record, Idx);
5560 QualType Replacement = readType(*Loc.F, Record, Idx);
5561 return Context.getSubstTemplateTypeParmType(
5562 cast<TemplateTypeParmType>(Parm),
5563 Context.getCanonicalType(Replacement));
5566 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5568 QualType Parm = readType(*Loc.F, Record, Idx);
5569 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5570 return Context.getSubstTemplateTypeParmPackType(
5571 cast<TemplateTypeParmType>(Parm),
5575 case TYPE_INJECTED_CLASS_NAME: {
5576 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5577 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5578 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5579 // for AST reading, too much interdependencies.
5580 const Type *T = nullptr;
5581 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5582 if (const Type *Existing = DI->getTypeForDecl()) {
5588 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5589 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5590 DI->setTypeForDecl(T);
5592 return QualType(T, 0);
5595 case TYPE_TEMPLATE_TYPE_PARM: {
5597 unsigned Depth = Record[Idx++];
5598 unsigned Index = Record[Idx++];
5599 bool Pack = Record[Idx++];
5600 TemplateTypeParmDecl *D
5601 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5602 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5605 case TYPE_DEPENDENT_NAME: {
5607 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5608 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5609 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
5610 QualType Canon = readType(*Loc.F, Record, Idx);
5611 if (!Canon.isNull())
5612 Canon = Context.getCanonicalType(Canon);
5613 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5616 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5618 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5619 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5620 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
5621 unsigned NumArgs = Record[Idx++];
5622 SmallVector<TemplateArgument, 8> Args;
5623 Args.reserve(NumArgs);
5625 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5626 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5627 Args.size(), Args.data());
5630 case TYPE_DEPENDENT_SIZED_ARRAY: {
5634 QualType ElementType = readType(*Loc.F, Record, Idx);
5635 ArrayType::ArraySizeModifier ASM
5636 = (ArrayType::ArraySizeModifier)Record[Idx++];
5637 unsigned IndexTypeQuals = Record[Idx++];
5639 // DependentSizedArrayType
5640 Expr *NumElts = ReadExpr(*Loc.F);
5641 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5643 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5644 IndexTypeQuals, Brackets);
5647 case TYPE_TEMPLATE_SPECIALIZATION: {
5649 bool IsDependent = Record[Idx++];
5650 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5651 SmallVector<TemplateArgument, 8> Args;
5652 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5653 QualType Underlying = readType(*Loc.F, Record, Idx);
5655 if (Underlying.isNull())
5656 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5659 T = Context.getTemplateSpecializationType(Name, Args.data(),
5660 Args.size(), Underlying);
5661 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5666 if (Record.size() != 1) {
5667 Error("Incorrect encoding of atomic type");
5670 QualType ValueType = readType(*Loc.F, Record, Idx);
5671 return Context.getAtomicType(ValueType);
5675 if (Record.size() != 1) {
5676 Error("Incorrect encoding of pipe type");
5680 // Reading the pipe element type.
5681 QualType ElementType = readType(*Loc.F, Record, Idx);
5682 return Context.getPipeType(ElementType);
5685 llvm_unreachable("Invalid TypeCode!");
5688 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5689 SmallVectorImpl<QualType> &Exceptions,
5690 FunctionProtoType::ExceptionSpecInfo &ESI,
5691 const RecordData &Record, unsigned &Idx) {
5692 ExceptionSpecificationType EST =
5693 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5695 if (EST == EST_Dynamic) {
5696 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
5697 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5698 ESI.Exceptions = Exceptions;
5699 } else if (EST == EST_ComputedNoexcept) {
5700 ESI.NoexceptExpr = ReadExpr(ModuleFile);
5701 } else if (EST == EST_Uninstantiated) {
5702 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5703 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5704 } else if (EST == EST_Unevaluated) {
5705 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5709 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5712 const ASTReader::RecordData &Record;
5715 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5717 return Reader.ReadSourceLocation(F, R, I);
5720 template<typename T>
5721 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5722 return Reader.ReadDeclAs<T>(F, Record, Idx);
5726 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5727 const ASTReader::RecordData &Record, unsigned &Idx)
5728 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5731 // We want compile-time assurance that we've enumerated all of
5732 // these, so unfortunately we have to declare them first, then
5733 // define them out-of-line.
5734 #define ABSTRACT_TYPELOC(CLASS, PARENT)
5735 #define TYPELOC(CLASS, PARENT) \
5736 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5737 #include "clang/AST/TypeLocNodes.def"
5739 void VisitFunctionTypeLoc(FunctionTypeLoc);
5740 void VisitArrayTypeLoc(ArrayTypeLoc);
5743 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5746 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5747 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5748 if (TL.needsExtraLocalData()) {
5749 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5750 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5751 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5752 TL.setModeAttr(Record[Idx++]);
5755 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5756 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5758 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5759 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5761 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5764 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5767 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5768 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5770 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5771 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5773 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5774 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5776 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5777 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5778 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5780 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5781 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5782 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5784 TL.setSizeExpr(Reader.ReadExpr(F));
5786 TL.setSizeExpr(nullptr);
5788 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5789 VisitArrayTypeLoc(TL);
5791 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5792 VisitArrayTypeLoc(TL);
5794 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5795 VisitArrayTypeLoc(TL);
5797 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5798 DependentSizedArrayTypeLoc TL) {
5799 VisitArrayTypeLoc(TL);
5801 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5802 DependentSizedExtVectorTypeLoc TL) {
5803 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5805 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5806 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5808 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5809 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5811 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5812 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5813 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5814 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5815 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5816 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5817 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5820 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5821 VisitFunctionTypeLoc(TL);
5823 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5824 VisitFunctionTypeLoc(TL);
5826 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5827 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5829 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5830 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5832 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5833 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5834 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5835 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5837 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5838 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5839 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5840 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5841 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5843 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5844 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5846 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5847 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5848 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5849 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5850 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5852 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5853 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5855 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5856 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5858 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5859 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5861 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5862 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5863 if (TL.hasAttrOperand()) {
5865 range.setBegin(ReadSourceLocation(Record, Idx));
5866 range.setEnd(ReadSourceLocation(Record, Idx));
5867 TL.setAttrOperandParensRange(range);
5869 if (TL.hasAttrExprOperand()) {
5871 TL.setAttrExprOperand(Reader.ReadExpr(F));
5873 TL.setAttrExprOperand(nullptr);
5874 } else if (TL.hasAttrEnumOperand())
5875 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5877 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5878 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5880 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5881 SubstTemplateTypeParmTypeLoc TL) {
5882 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5884 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5885 SubstTemplateTypeParmPackTypeLoc TL) {
5886 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5888 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5889 TemplateSpecializationTypeLoc TL) {
5890 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5891 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5892 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5893 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5894 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5896 Reader.GetTemplateArgumentLocInfo(F,
5897 TL.getTypePtr()->getArg(i).getKind(),
5900 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5901 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5902 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5904 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5905 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5906 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5908 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5909 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5911 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5912 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5913 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5914 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5916 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5917 DependentTemplateSpecializationTypeLoc TL) {
5918 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5919 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5920 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5921 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5922 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5923 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5924 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5926 Reader.GetTemplateArgumentLocInfo(F,
5927 TL.getTypePtr()->getArg(I).getKind(),
5930 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5931 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5933 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5934 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5936 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5937 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5938 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx));
5939 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx));
5940 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
5941 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx));
5942 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx));
5943 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx));
5944 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5945 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5947 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5948 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5950 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5951 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5952 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5953 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5955 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
5956 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5959 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5960 const RecordData &Record,
5962 QualType InfoTy = readType(F, Record, Idx);
5963 if (InfoTy.isNull())
5966 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5967 TypeLocReader TLR(*this, F, Record, Idx);
5968 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5973 QualType ASTReader::GetType(TypeID ID) {
5974 unsigned FastQuals = ID & Qualifiers::FastMask;
5975 unsigned Index = ID >> Qualifiers::FastWidth;
5977 if (Index < NUM_PREDEF_TYPE_IDS) {
5979 switch ((PredefinedTypeIDs)Index) {
5980 case PREDEF_TYPE_NULL_ID:
5982 case PREDEF_TYPE_VOID_ID:
5985 case PREDEF_TYPE_BOOL_ID:
5989 case PREDEF_TYPE_CHAR_U_ID:
5990 case PREDEF_TYPE_CHAR_S_ID:
5991 // FIXME: Check that the signedness of CharTy is correct!
5995 case PREDEF_TYPE_UCHAR_ID:
5996 T = Context.UnsignedCharTy;
5998 case PREDEF_TYPE_USHORT_ID:
5999 T = Context.UnsignedShortTy;
6001 case PREDEF_TYPE_UINT_ID:
6002 T = Context.UnsignedIntTy;
6004 case PREDEF_TYPE_ULONG_ID:
6005 T = Context.UnsignedLongTy;
6007 case PREDEF_TYPE_ULONGLONG_ID:
6008 T = Context.UnsignedLongLongTy;
6010 case PREDEF_TYPE_UINT128_ID:
6011 T = Context.UnsignedInt128Ty;
6013 case PREDEF_TYPE_SCHAR_ID:
6014 T = Context.SignedCharTy;
6016 case PREDEF_TYPE_WCHAR_ID:
6017 T = Context.WCharTy;
6019 case PREDEF_TYPE_SHORT_ID:
6020 T = Context.ShortTy;
6022 case PREDEF_TYPE_INT_ID:
6025 case PREDEF_TYPE_LONG_ID:
6028 case PREDEF_TYPE_LONGLONG_ID:
6029 T = Context.LongLongTy;
6031 case PREDEF_TYPE_INT128_ID:
6032 T = Context.Int128Ty;
6034 case PREDEF_TYPE_HALF_ID:
6037 case PREDEF_TYPE_FLOAT_ID:
6038 T = Context.FloatTy;
6040 case PREDEF_TYPE_DOUBLE_ID:
6041 T = Context.DoubleTy;
6043 case PREDEF_TYPE_LONGDOUBLE_ID:
6044 T = Context.LongDoubleTy;
6046 case PREDEF_TYPE_OVERLOAD_ID:
6047 T = Context.OverloadTy;
6049 case PREDEF_TYPE_BOUND_MEMBER:
6050 T = Context.BoundMemberTy;
6052 case PREDEF_TYPE_PSEUDO_OBJECT:
6053 T = Context.PseudoObjectTy;
6055 case PREDEF_TYPE_DEPENDENT_ID:
6056 T = Context.DependentTy;
6058 case PREDEF_TYPE_UNKNOWN_ANY:
6059 T = Context.UnknownAnyTy;
6061 case PREDEF_TYPE_NULLPTR_ID:
6062 T = Context.NullPtrTy;
6064 case PREDEF_TYPE_CHAR16_ID:
6065 T = Context.Char16Ty;
6067 case PREDEF_TYPE_CHAR32_ID:
6068 T = Context.Char32Ty;
6070 case PREDEF_TYPE_OBJC_ID:
6071 T = Context.ObjCBuiltinIdTy;
6073 case PREDEF_TYPE_OBJC_CLASS:
6074 T = Context.ObjCBuiltinClassTy;
6076 case PREDEF_TYPE_OBJC_SEL:
6077 T = Context.ObjCBuiltinSelTy;
6079 case PREDEF_TYPE_IMAGE1D_ID:
6080 T = Context.OCLImage1dTy;
6082 case PREDEF_TYPE_IMAGE1D_ARR_ID:
6083 T = Context.OCLImage1dArrayTy;
6085 case PREDEF_TYPE_IMAGE1D_BUFF_ID:
6086 T = Context.OCLImage1dBufferTy;
6088 case PREDEF_TYPE_IMAGE2D_ID:
6089 T = Context.OCLImage2dTy;
6091 case PREDEF_TYPE_IMAGE2D_ARR_ID:
6092 T = Context.OCLImage2dArrayTy;
6094 case PREDEF_TYPE_IMAGE2D_DEP_ID:
6095 T = Context.OCLImage2dDepthTy;
6097 case PREDEF_TYPE_IMAGE2D_ARR_DEP_ID:
6098 T = Context.OCLImage2dArrayDepthTy;
6100 case PREDEF_TYPE_IMAGE2D_MSAA_ID:
6101 T = Context.OCLImage2dMSAATy;
6103 case PREDEF_TYPE_IMAGE2D_ARR_MSAA_ID:
6104 T = Context.OCLImage2dArrayMSAATy;
6106 case PREDEF_TYPE_IMAGE2D_MSAA_DEP_ID:
6107 T = Context.OCLImage2dMSAADepthTy;
6109 case PREDEF_TYPE_IMAGE2D_ARR_MSAA_DEPTH_ID:
6110 T = Context.OCLImage2dArrayMSAADepthTy;
6112 case PREDEF_TYPE_IMAGE3D_ID:
6113 T = Context.OCLImage3dTy;
6115 case PREDEF_TYPE_SAMPLER_ID:
6116 T = Context.OCLSamplerTy;
6118 case PREDEF_TYPE_EVENT_ID:
6119 T = Context.OCLEventTy;
6121 case PREDEF_TYPE_CLK_EVENT_ID:
6122 T = Context.OCLClkEventTy;
6124 case PREDEF_TYPE_QUEUE_ID:
6125 T = Context.OCLQueueTy;
6127 case PREDEF_TYPE_NDRANGE_ID:
6128 T = Context.OCLNDRangeTy;
6130 case PREDEF_TYPE_RESERVE_ID_ID:
6131 T = Context.OCLReserveIDTy;
6133 case PREDEF_TYPE_AUTO_DEDUCT:
6134 T = Context.getAutoDeductType();
6137 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6138 T = Context.getAutoRRefDeductType();
6141 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6142 T = Context.ARCUnbridgedCastTy;
6145 case PREDEF_TYPE_BUILTIN_FN:
6146 T = Context.BuiltinFnTy;
6149 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6150 T = Context.OMPArraySectionTy;
6154 assert(!T.isNull() && "Unknown predefined type");
6155 return T.withFastQualifiers(FastQuals);
6158 Index -= NUM_PREDEF_TYPE_IDS;
6159 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6160 if (TypesLoaded[Index].isNull()) {
6161 TypesLoaded[Index] = readTypeRecord(Index);
6162 if (TypesLoaded[Index].isNull())
6165 TypesLoaded[Index]->setFromAST();
6166 if (DeserializationListener)
6167 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6168 TypesLoaded[Index]);
6171 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6174 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6175 return GetType(getGlobalTypeID(F, LocalID));
6178 serialization::TypeID
6179 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6180 unsigned FastQuals = LocalID & Qualifiers::FastMask;
6181 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6183 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6186 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6187 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6188 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6190 unsigned GlobalIndex = LocalIndex + I->second;
6191 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6194 TemplateArgumentLocInfo
6195 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6196 TemplateArgument::ArgKind Kind,
6197 const RecordData &Record,
6200 case TemplateArgument::Expression:
6202 case TemplateArgument::Type:
6203 return GetTypeSourceInfo(F, Record, Index);
6204 case TemplateArgument::Template: {
6205 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6207 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6208 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6211 case TemplateArgument::TemplateExpansion: {
6212 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6214 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6215 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6216 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6219 case TemplateArgument::Null:
6220 case TemplateArgument::Integral:
6221 case TemplateArgument::Declaration:
6222 case TemplateArgument::NullPtr:
6223 case TemplateArgument::Pack:
6224 // FIXME: Is this right?
6225 return TemplateArgumentLocInfo();
6227 llvm_unreachable("unexpected template argument loc");
6231 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6232 const RecordData &Record, unsigned &Index) {
6233 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6235 if (Arg.getKind() == TemplateArgument::Expression) {
6236 if (Record[Index++]) // bool InfoHasSameExpr.
6237 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6239 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6243 const ASTTemplateArgumentListInfo*
6244 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6245 const RecordData &Record,
6247 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6248 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6249 unsigned NumArgsAsWritten = Record[Index++];
6250 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6251 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6252 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6253 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6256 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6260 template<typename TemplateSpecializationDecl>
6261 static void completeRedeclChainForTemplateSpecialization(Decl *D) {
6262 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
6263 TSD->getSpecializedTemplate()->LoadLazySpecializations();
6266 void ASTReader::CompleteRedeclChain(const Decl *D) {
6267 if (NumCurrentElementsDeserializing) {
6268 // We arrange to not care about the complete redeclaration chain while we're
6269 // deserializing. Just remember that the AST has marked this one as complete
6270 // but that it's not actually complete yet, so we know we still need to
6271 // complete it later.
6272 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6276 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6278 // If this is a named declaration, complete it by looking it up
6279 // within its context.
6281 // FIXME: Merging a function definition should merge
6282 // all mergeable entities within it.
6283 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6284 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6285 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
6286 if (!getContext().getLangOpts().CPlusPlus &&
6287 isa<TranslationUnitDecl>(DC)) {
6288 // Outside of C++, we don't have a lookup table for the TU, so update
6289 // the identifier instead. (For C++ modules, we don't store decls
6290 // in the serialized identifier table, so we do the lookup in the TU.)
6291 auto *II = Name.getAsIdentifierInfo();
6292 assert(II && "non-identifier name in C?");
6293 if (II->isOutOfDate())
6294 updateOutOfDateIdentifier(*II);
6297 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
6298 // Find all declarations of this kind from the relevant context.
6299 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
6300 auto *DC = cast<DeclContext>(DCDecl);
6301 SmallVector<Decl*, 8> Decls;
6302 FindExternalLexicalDecls(
6303 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
6308 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
6309 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
6310 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
6311 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
6312 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6313 if (auto *Template = FD->getPrimaryTemplate())
6314 Template->LoadLazySpecializations();
6318 uint64_t ASTReader::ReadCXXCtorInitializersRef(ModuleFile &M,
6319 const RecordData &Record,
6321 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXCtorInitializers) {
6322 Error("malformed AST file: missing C++ ctor initializers");
6326 unsigned LocalID = Record[Idx++];
6327 return getGlobalBitOffset(M, M.CXXCtorInitializersOffsets[LocalID - 1]);
6330 CXXCtorInitializer **
6331 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
6332 RecordLocation Loc = getLocalBitOffset(Offset);
6333 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6334 SavedStreamPosition SavedPosition(Cursor);
6335 Cursor.JumpToBit(Loc.Offset);
6336 ReadingKindTracker ReadingKind(Read_Decl, *this);
6339 unsigned Code = Cursor.ReadCode();
6340 unsigned RecCode = Cursor.readRecord(Code, Record);
6341 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
6342 Error("malformed AST file: missing C++ ctor initializers");
6347 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
6350 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6351 const RecordData &Record,
6353 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6354 Error("malformed AST file: missing C++ base specifier");
6358 unsigned LocalID = Record[Idx++];
6359 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6362 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6363 RecordLocation Loc = getLocalBitOffset(Offset);
6364 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6365 SavedStreamPosition SavedPosition(Cursor);
6366 Cursor.JumpToBit(Loc.Offset);
6367 ReadingKindTracker ReadingKind(Read_Decl, *this);
6369 unsigned Code = Cursor.ReadCode();
6370 unsigned RecCode = Cursor.readRecord(Code, Record);
6371 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
6372 Error("malformed AST file: missing C++ base specifiers");
6377 unsigned NumBases = Record[Idx++];
6378 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6379 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6380 for (unsigned I = 0; I != NumBases; ++I)
6381 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6385 serialization::DeclID
6386 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6387 if (LocalID < NUM_PREDEF_DECL_IDS)
6390 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6391 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6392 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6394 return LocalID + I->second;
6397 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6398 ModuleFile &M) const {
6399 // Predefined decls aren't from any module.
6400 if (ID < NUM_PREDEF_DECL_IDS)
6403 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
6404 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
6407 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
6408 if (!D->isFromASTFile())
6410 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6411 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6415 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6416 if (ID < NUM_PREDEF_DECL_IDS)
6417 return SourceLocation();
6419 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6421 if (Index > DeclsLoaded.size()) {
6422 Error("declaration ID out-of-range for AST file");
6423 return SourceLocation();
6426 if (Decl *D = DeclsLoaded[Index])
6427 return D->getLocation();
6430 DeclCursorForID(ID, Loc);
6434 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6436 case PREDEF_DECL_NULL_ID:
6439 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6440 return Context.getTranslationUnitDecl();
6442 case PREDEF_DECL_OBJC_ID_ID:
6443 return Context.getObjCIdDecl();
6445 case PREDEF_DECL_OBJC_SEL_ID:
6446 return Context.getObjCSelDecl();
6448 case PREDEF_DECL_OBJC_CLASS_ID:
6449 return Context.getObjCClassDecl();
6451 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6452 return Context.getObjCProtocolDecl();
6454 case PREDEF_DECL_INT_128_ID:
6455 return Context.getInt128Decl();
6457 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6458 return Context.getUInt128Decl();
6460 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6461 return Context.getObjCInstanceTypeDecl();
6463 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6464 return Context.getBuiltinVaListDecl();
6466 case PREDEF_DECL_VA_LIST_TAG:
6467 return Context.getVaListTagDecl();
6469 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
6470 return Context.getBuiltinMSVaListDecl();
6472 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6473 return Context.getExternCContextDecl();
6475 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
6476 return Context.getMakeIntegerSeqDecl();
6478 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
6479 return Context.getCFConstantStringDecl();
6481 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
6482 return Context.getCFConstantStringTagDecl();
6484 llvm_unreachable("PredefinedDeclIDs unknown enum value");
6487 Decl *ASTReader::GetExistingDecl(DeclID ID) {
6488 if (ID < NUM_PREDEF_DECL_IDS) {
6489 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6491 // Track that we have merged the declaration with ID \p ID into the
6492 // pre-existing predefined declaration \p D.
6493 auto &Merged = KeyDecls[D->getCanonicalDecl()];
6495 Merged.push_back(ID);
6500 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6502 if (Index >= DeclsLoaded.size()) {
6503 assert(0 && "declaration ID out-of-range for AST file");
6504 Error("declaration ID out-of-range for AST file");
6508 return DeclsLoaded[Index];
6511 Decl *ASTReader::GetDecl(DeclID ID) {
6512 if (ID < NUM_PREDEF_DECL_IDS)
6513 return GetExistingDecl(ID);
6515 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6517 if (Index >= DeclsLoaded.size()) {
6518 assert(0 && "declaration ID out-of-range for AST file");
6519 Error("declaration ID out-of-range for AST file");
6523 if (!DeclsLoaded[Index]) {
6525 if (DeserializationListener)
6526 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6529 return DeclsLoaded[Index];
6532 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6534 if (GlobalID < NUM_PREDEF_DECL_IDS)
6537 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6538 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6539 ModuleFile *Owner = I->second;
6541 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6542 = M.GlobalToLocalDeclIDs.find(Owner);
6543 if (Pos == M.GlobalToLocalDeclIDs.end())
6546 return GlobalID - Owner->BaseDeclID + Pos->second;
6549 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6550 const RecordData &Record,
6552 if (Idx >= Record.size()) {
6553 Error("Corrupted AST file");
6557 return getGlobalDeclID(F, Record[Idx++]);
6560 /// \brief Resolve the offset of a statement into a statement.
6562 /// This operation will read a new statement from the external
6563 /// source each time it is called, and is meant to be used via a
6564 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6565 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6566 // Switch case IDs are per Decl.
6567 ClearSwitchCaseIDs();
6569 // Offset here is a global offset across the entire chain.
6570 RecordLocation Loc = getLocalBitOffset(Offset);
6571 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6572 return ReadStmtFromStream(*Loc.F);
6575 void ASTReader::FindExternalLexicalDecls(
6576 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
6577 SmallVectorImpl<Decl *> &Decls) {
6578 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
6580 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
6581 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
6582 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
6583 auto K = (Decl::Kind)+LexicalDecls[I];
6584 if (!IsKindWeWant(K))
6587 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
6589 // Don't add predefined declarations to the lexical context more
6591 if (ID < NUM_PREDEF_DECL_IDS) {
6592 if (PredefsVisited[ID])
6595 PredefsVisited[ID] = true;
6598 if (Decl *D = GetLocalDecl(*M, ID)) {
6599 assert(D->getKind() == K && "wrong kind for lexical decl");
6600 if (!DC->isDeclInLexicalTraversal(D))
6606 if (isa<TranslationUnitDecl>(DC)) {
6607 for (auto Lexical : TULexicalDecls)
6608 Visit(Lexical.first, Lexical.second);
6610 auto I = LexicalDecls.find(DC);
6611 if (I != LexicalDecls.end())
6612 Visit(I->second.first, I->second.second);
6615 ++NumLexicalDeclContextsRead;
6625 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6627 bool operator()(LocalDeclID L, LocalDeclID R) const {
6628 SourceLocation LHS = getLocation(L);
6629 SourceLocation RHS = getLocation(R);
6630 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6633 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6634 SourceLocation RHS = getLocation(R);
6635 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6638 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6639 SourceLocation LHS = getLocation(L);
6640 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6643 SourceLocation getLocation(LocalDeclID ID) const {
6644 return Reader.getSourceManager().getFileLoc(
6645 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6651 void ASTReader::FindFileRegionDecls(FileID File,
6652 unsigned Offset, unsigned Length,
6653 SmallVectorImpl<Decl *> &Decls) {
6654 SourceManager &SM = getSourceManager();
6656 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6657 if (I == FileDeclIDs.end())
6660 FileDeclsInfo &DInfo = I->second;
6661 if (DInfo.Decls.empty())
6665 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6666 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6668 DeclIDComp DIDComp(*this, *DInfo.Mod);
6669 ArrayRef<serialization::LocalDeclID>::iterator
6670 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6672 if (BeginIt != DInfo.Decls.begin())
6675 // If we are pointing at a top-level decl inside an objc container, we need
6676 // to backtrack until we find it otherwise we will fail to report that the
6677 // region overlaps with an objc container.
6678 while (BeginIt != DInfo.Decls.begin() &&
6679 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6680 ->isTopLevelDeclInObjCContainer())
6683 ArrayRef<serialization::LocalDeclID>::iterator
6684 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6686 if (EndIt != DInfo.Decls.end())
6689 for (ArrayRef<serialization::LocalDeclID>::iterator
6690 DIt = BeginIt; DIt != EndIt; ++DIt)
6691 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6695 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6696 DeclarationName Name) {
6697 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
6698 "DeclContext has no visible decls in storage");
6702 auto It = Lookups.find(DC);
6703 if (It == Lookups.end())
6706 Deserializing LookupResults(this);
6708 // Load the list of declarations.
6709 SmallVector<NamedDecl *, 64> Decls;
6710 for (DeclID ID : It->second.Table.find(Name)) {
6711 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6712 if (ND->getDeclName() == Name)
6713 Decls.push_back(ND);
6716 ++NumVisibleDeclContextsRead;
6717 SetExternalVisibleDeclsForName(DC, Name, Decls);
6718 return !Decls.empty();
6721 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6722 if (!DC->hasExternalVisibleStorage())
6725 auto It = Lookups.find(DC);
6726 assert(It != Lookups.end() &&
6727 "have external visible storage but no lookup tables");
6731 for (DeclID ID : It->second.Table.findAll()) {
6732 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6733 Decls[ND->getDeclName()].push_back(ND);
6736 ++NumVisibleDeclContextsRead;
6738 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
6739 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6741 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6744 const serialization::reader::DeclContextLookupTable *
6745 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
6746 auto I = Lookups.find(Primary);
6747 return I == Lookups.end() ? nullptr : &I->second;
6750 /// \brief Under non-PCH compilation the consumer receives the objc methods
6751 /// before receiving the implementation, and codegen depends on this.
6752 /// We simulate this by deserializing and passing to consumer the methods of the
6753 /// implementation before passing the deserialized implementation decl.
6754 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6755 ASTConsumer *Consumer) {
6756 assert(ImplD && Consumer);
6758 for (auto *I : ImplD->methods())
6759 Consumer->HandleInterestingDecl(DeclGroupRef(I));
6761 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6764 void ASTReader::PassInterestingDeclsToConsumer() {
6767 if (PassingDeclsToConsumer)
6770 // Guard variable to avoid recursively redoing the process of passing
6771 // decls to consumer.
6772 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6775 // Ensure that we've loaded all potentially-interesting declarations
6776 // that need to be eagerly loaded.
6777 for (auto ID : EagerlyDeserializedDecls)
6779 EagerlyDeserializedDecls.clear();
6781 while (!InterestingDecls.empty()) {
6782 Decl *D = InterestingDecls.front();
6783 InterestingDecls.pop_front();
6785 PassInterestingDeclToConsumer(D);
6789 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6790 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6791 PassObjCImplDeclToConsumer(ImplD, Consumer);
6793 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6796 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6797 this->Consumer = Consumer;
6800 PassInterestingDeclsToConsumer();
6802 if (DeserializationListener)
6803 DeserializationListener->ReaderInitialized(this);
6806 void ASTReader::PrintStats() {
6807 std::fprintf(stderr, "*** AST File Statistics:\n");
6809 unsigned NumTypesLoaded
6810 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6812 unsigned NumDeclsLoaded
6813 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6815 unsigned NumIdentifiersLoaded
6816 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6817 IdentifiersLoaded.end(),
6818 (IdentifierInfo *)nullptr);
6819 unsigned NumMacrosLoaded
6820 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6822 (MacroInfo *)nullptr);
6823 unsigned NumSelectorsLoaded
6824 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6825 SelectorsLoaded.end(),
6828 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6829 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6830 NumSLocEntriesRead, TotalNumSLocEntries,
6831 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6832 if (!TypesLoaded.empty())
6833 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6834 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6835 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6836 if (!DeclsLoaded.empty())
6837 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6838 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6839 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6840 if (!IdentifiersLoaded.empty())
6841 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6842 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6843 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6844 if (!MacrosLoaded.empty())
6845 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6846 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6847 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6848 if (!SelectorsLoaded.empty())
6849 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6850 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6851 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6852 if (TotalNumStatements)
6853 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6854 NumStatementsRead, TotalNumStatements,
6855 ((float)NumStatementsRead/TotalNumStatements * 100));
6857 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6858 NumMacrosRead, TotalNumMacros,
6859 ((float)NumMacrosRead/TotalNumMacros * 100));
6860 if (TotalLexicalDeclContexts)
6861 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6862 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6863 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6865 if (TotalVisibleDeclContexts)
6866 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6867 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6868 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6870 if (TotalNumMethodPoolEntries) {
6871 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6872 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6873 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6876 if (NumMethodPoolLookups) {
6877 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6878 NumMethodPoolHits, NumMethodPoolLookups,
6879 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6881 if (NumMethodPoolTableLookups) {
6882 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6883 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6884 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6888 if (NumIdentifierLookupHits) {
6889 std::fprintf(stderr,
6890 " %u / %u identifier table lookups succeeded (%f%%)\n",
6891 NumIdentifierLookupHits, NumIdentifierLookups,
6892 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6896 std::fprintf(stderr, "\n");
6897 GlobalIndex->printStats();
6900 std::fprintf(stderr, "\n");
6902 std::fprintf(stderr, "\n");
6905 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6907 dumpModuleIDMap(StringRef Name,
6908 const ContinuousRangeMap<Key, ModuleFile *,
6909 InitialCapacity> &Map) {
6910 if (Map.begin() == Map.end())
6913 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6914 llvm::errs() << Name << ":\n";
6915 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6917 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6922 LLVM_DUMP_METHOD void ASTReader::dump() {
6923 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6924 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6925 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6926 dumpModuleIDMap("Global type map", GlobalTypeMap);
6927 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6928 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6929 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6930 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6931 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6932 dumpModuleIDMap("Global preprocessed entity map",
6933 GlobalPreprocessedEntityMap);
6935 llvm::errs() << "\n*** PCH/Modules Loaded:";
6936 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6937 MEnd = ModuleMgr.end();
6942 /// Return the amount of memory used by memory buffers, breaking down
6943 /// by heap-backed versus mmap'ed memory.
6944 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6945 for (ModuleConstIterator I = ModuleMgr.begin(),
6946 E = ModuleMgr.end(); I != E; ++I) {
6947 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6948 size_t bytes = buf->getBufferSize();
6949 switch (buf->getBufferKind()) {
6950 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6951 sizes.malloc_bytes += bytes;
6953 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6954 sizes.mmap_bytes += bytes;
6961 void ASTReader::InitializeSema(Sema &S) {
6963 S.addExternalSource(this);
6965 // Makes sure any declarations that were deserialized "too early"
6966 // still get added to the identifier's declaration chains.
6967 for (uint64_t ID : PreloadedDeclIDs) {
6968 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6969 pushExternalDeclIntoScope(D, D->getDeclName());
6971 PreloadedDeclIDs.clear();
6973 // FIXME: What happens if these are changed by a module import?
6974 if (!FPPragmaOptions.empty()) {
6975 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6976 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6979 // FIXME: What happens if these are changed by a module import?
6980 if (!OpenCLExtensions.empty()) {
6982 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6983 #include "clang/Basic/OpenCLExtensions.def"
6985 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6991 void ASTReader::UpdateSema() {
6992 assert(SemaObj && "no Sema to update");
6994 // Load the offsets of the declarations that Sema references.
6995 // They will be lazily deserialized when needed.
6996 if (!SemaDeclRefs.empty()) {
6997 assert(SemaDeclRefs.size() % 2 == 0);
6998 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6999 if (!SemaObj->StdNamespace)
7000 SemaObj->StdNamespace = SemaDeclRefs[I];
7001 if (!SemaObj->StdBadAlloc)
7002 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7004 SemaDeclRefs.clear();
7007 // Update the state of pragmas. Use the same API as if we had encountered the
7008 // pragma in the source.
7009 if(OptimizeOffPragmaLocation.isValid())
7010 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
7011 if (PragmaMSStructState != -1)
7012 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7013 if (PointersToMembersPragmaLocation.isValid()) {
7014 SemaObj->ActOnPragmaMSPointersToMembers(
7015 (LangOptions::PragmaMSPointersToMembersKind)
7016 PragmaMSPointersToMembersState,
7017 PointersToMembersPragmaLocation);
7021 IdentifierInfo *ASTReader::get(StringRef Name) {
7022 // Note that we are loading an identifier.
7023 Deserializing AnIdentifier(this);
7025 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7026 NumIdentifierLookups,
7027 NumIdentifierLookupHits);
7029 // We don't need to do identifier table lookups in C++ modules (we preload
7030 // all interesting declarations, and don't need to use the scope for name
7031 // lookups). Perform the lookup in PCH files, though, since we don't build
7032 // a complete initial identifier table if we're carrying on from a PCH.
7033 if (Context.getLangOpts().CPlusPlus) {
7034 for (auto F : ModuleMgr.pch_modules())
7038 // If there is a global index, look there first to determine which modules
7039 // provably do not have any results for this identifier.
7040 GlobalModuleIndex::HitSet Hits;
7041 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7042 if (!loadGlobalIndex()) {
7043 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7048 ModuleMgr.visit(Visitor, HitsPtr);
7051 IdentifierInfo *II = Visitor.getIdentifierInfo();
7052 markIdentifierUpToDate(II);
7057 /// \brief An identifier-lookup iterator that enumerates all of the
7058 /// identifiers stored within a set of AST files.
7059 class ASTIdentifierIterator : public IdentifierIterator {
7060 /// \brief The AST reader whose identifiers are being enumerated.
7061 const ASTReader &Reader;
7063 /// \brief The current index into the chain of AST files stored in
7067 /// \brief The current position within the identifier lookup table
7068 /// of the current AST file.
7069 ASTIdentifierLookupTable::key_iterator Current;
7071 /// \brief The end position within the identifier lookup table of
7072 /// the current AST file.
7073 ASTIdentifierLookupTable::key_iterator End;
7076 explicit ASTIdentifierIterator(const ASTReader &Reader);
7078 StringRef Next() override;
7082 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
7083 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
7084 ASTIdentifierLookupTable *IdTable
7085 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
7086 Current = IdTable->key_begin();
7087 End = IdTable->key_end();
7090 StringRef ASTIdentifierIterator::Next() {
7091 while (Current == End) {
7092 // If we have exhausted all of our AST files, we're done.
7097 ASTIdentifierLookupTable *IdTable
7098 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
7099 IdentifierLookupTable;
7100 Current = IdTable->key_begin();
7101 End = IdTable->key_end();
7104 // We have any identifiers remaining in the current AST file; return
7106 StringRef Result = *Current;
7111 IdentifierIterator *ASTReader::getIdentifiers() {
7112 if (!loadGlobalIndex())
7113 return GlobalIndex->createIdentifierIterator();
7115 return new ASTIdentifierIterator(*this);
7118 namespace clang { namespace serialization {
7119 class ReadMethodPoolVisitor {
7122 unsigned PriorGeneration;
7123 unsigned InstanceBits;
7124 unsigned FactoryBits;
7125 bool InstanceHasMoreThanOneDecl;
7126 bool FactoryHasMoreThanOneDecl;
7127 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7128 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7131 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7132 unsigned PriorGeneration)
7133 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
7134 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7135 FactoryHasMoreThanOneDecl(false) {}
7137 bool operator()(ModuleFile &M) {
7138 if (!M.SelectorLookupTable)
7141 // If we've already searched this module file, skip it now.
7142 if (M.Generation <= PriorGeneration)
7145 ++Reader.NumMethodPoolTableLookups;
7146 ASTSelectorLookupTable *PoolTable
7147 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7148 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
7149 if (Pos == PoolTable->end())
7152 ++Reader.NumMethodPoolTableHits;
7153 ++Reader.NumSelectorsRead;
7154 // FIXME: Not quite happy with the statistics here. We probably should
7155 // disable this tracking when called via LoadSelector.
7156 // Also, should entries without methods count as misses?
7157 ++Reader.NumMethodPoolEntriesRead;
7158 ASTSelectorLookupTrait::data_type Data = *Pos;
7159 if (Reader.DeserializationListener)
7160 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
7162 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7163 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7164 InstanceBits = Data.InstanceBits;
7165 FactoryBits = Data.FactoryBits;
7166 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7167 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
7171 /// \brief Retrieve the instance methods found by this visitor.
7172 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7173 return InstanceMethods;
7176 /// \brief Retrieve the instance methods found by this visitor.
7177 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7178 return FactoryMethods;
7181 unsigned getInstanceBits() const { return InstanceBits; }
7182 unsigned getFactoryBits() const { return FactoryBits; }
7183 bool instanceHasMoreThanOneDecl() const {
7184 return InstanceHasMoreThanOneDecl;
7186 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
7188 } } // end namespace clang::serialization
7190 /// \brief Add the given set of methods to the method list.
7191 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7192 ObjCMethodList &List) {
7193 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7194 S.addMethodToGlobalList(&List, Methods[I]);
7198 void ASTReader::ReadMethodPool(Selector Sel) {
7199 // Get the selector generation and update it to the current generation.
7200 unsigned &Generation = SelectorGeneration[Sel];
7201 unsigned PriorGeneration = Generation;
7202 Generation = getGeneration();
7204 // Search for methods defined with this selector.
7205 ++NumMethodPoolLookups;
7206 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7207 ModuleMgr.visit(Visitor);
7209 if (Visitor.getInstanceMethods().empty() &&
7210 Visitor.getFactoryMethods().empty())
7213 ++NumMethodPoolHits;
7218 Sema &S = *getSema();
7219 Sema::GlobalMethodPool::iterator Pos
7220 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7222 Pos->second.first.setBits(Visitor.getInstanceBits());
7223 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
7224 Pos->second.second.setBits(Visitor.getFactoryBits());
7225 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
7227 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7228 // when building a module we keep every method individually and may need to
7229 // update hasMoreThanOneDecl as we add the methods.
7230 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7231 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
7234 void ASTReader::ReadKnownNamespaces(
7235 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7238 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7239 if (NamespaceDecl *Namespace
7240 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7241 Namespaces.push_back(Namespace);
7245 void ASTReader::ReadUndefinedButUsed(
7246 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
7247 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7248 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
7249 SourceLocation Loc =
7250 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
7251 Undefined.insert(std::make_pair(D, Loc));
7255 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7256 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7258 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7259 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7260 uint64_t Count = DelayedDeleteExprs[Idx++];
7261 for (uint64_t C = 0; C < Count; ++C) {
7262 SourceLocation DeleteLoc =
7263 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7264 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7265 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7270 void ASTReader::ReadTentativeDefinitions(
7271 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7272 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7273 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7275 TentativeDefs.push_back(Var);
7277 TentativeDefinitions.clear();
7280 void ASTReader::ReadUnusedFileScopedDecls(
7281 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7282 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7284 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7288 UnusedFileScopedDecls.clear();
7291 void ASTReader::ReadDelegatingConstructors(
7292 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7293 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7294 CXXConstructorDecl *D
7295 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7299 DelegatingCtorDecls.clear();
7302 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7303 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7305 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7309 ExtVectorDecls.clear();
7312 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7313 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7314 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7316 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7317 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7321 UnusedLocalTypedefNameCandidates.clear();
7324 void ASTReader::ReadReferencedSelectors(
7325 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7326 if (ReferencedSelectorsData.empty())
7329 // If there are @selector references added them to its pool. This is for
7330 // implementation of -Wselector.
7331 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7333 while (I < DataSize) {
7334 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7335 SourceLocation SelLoc
7336 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7337 Sels.push_back(std::make_pair(Sel, SelLoc));
7339 ReferencedSelectorsData.clear();
7342 void ASTReader::ReadWeakUndeclaredIdentifiers(
7343 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7344 if (WeakUndeclaredIdentifiers.empty())
7347 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7348 IdentifierInfo *WeakId
7349 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7350 IdentifierInfo *AliasId
7351 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7353 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7354 bool Used = WeakUndeclaredIdentifiers[I++];
7355 WeakInfo WI(AliasId, Loc);
7357 WeakIDs.push_back(std::make_pair(WeakId, WI));
7359 WeakUndeclaredIdentifiers.clear();
7362 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7363 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7364 ExternalVTableUse VT;
7365 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7366 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7367 VT.DefinitionRequired = VTableUses[Idx++];
7368 VTables.push_back(VT);
7374 void ASTReader::ReadPendingInstantiations(
7375 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7376 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7377 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7379 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7381 Pending.push_back(std::make_pair(D, Loc));
7383 PendingInstantiations.clear();
7386 void ASTReader::ReadLateParsedTemplates(
7387 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7388 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7390 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7392 LateParsedTemplate *LT = new LateParsedTemplate;
7393 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7395 ModuleFile *F = getOwningModuleFile(LT->D);
7396 assert(F && "No module");
7398 unsigned TokN = LateParsedTemplates[Idx++];
7399 LT->Toks.reserve(TokN);
7400 for (unsigned T = 0; T < TokN; ++T)
7401 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7403 LPTMap.insert(std::make_pair(FD, LT));
7406 LateParsedTemplates.clear();
7409 void ASTReader::LoadSelector(Selector Sel) {
7410 // It would be complicated to avoid reading the methods anyway. So don't.
7411 ReadMethodPool(Sel);
7414 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7415 assert(ID && "Non-zero identifier ID required");
7416 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7417 IdentifiersLoaded[ID - 1] = II;
7418 if (DeserializationListener)
7419 DeserializationListener->IdentifierRead(ID, II);
7422 /// \brief Set the globally-visible declarations associated with the given
7425 /// If the AST reader is currently in a state where the given declaration IDs
7426 /// cannot safely be resolved, they are queued until it is safe to resolve
7429 /// \param II an IdentifierInfo that refers to one or more globally-visible
7432 /// \param DeclIDs the set of declaration IDs with the name @p II that are
7433 /// visible at global scope.
7435 /// \param Decls if non-null, this vector will be populated with the set of
7436 /// deserialized declarations. These declarations will not be pushed into
7439 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7440 const SmallVectorImpl<uint32_t> &DeclIDs,
7441 SmallVectorImpl<Decl *> *Decls) {
7442 if (NumCurrentElementsDeserializing && !Decls) {
7443 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
7447 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7449 // Queue this declaration so that it will be added to the
7450 // translation unit scope and identifier's declaration chain
7451 // once a Sema object is known.
7452 PreloadedDeclIDs.push_back(DeclIDs[I]);
7456 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7458 // If we're simply supposed to record the declarations, do so now.
7460 Decls->push_back(D);
7464 // Introduce this declaration into the translation-unit scope
7465 // and add it to the declaration chain for this identifier, so
7466 // that (unqualified) name lookup will find it.
7467 pushExternalDeclIntoScope(D, II);
7471 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
7475 if (IdentifiersLoaded.empty()) {
7476 Error("no identifier table in AST file");
7481 if (!IdentifiersLoaded[ID]) {
7482 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7483 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7484 ModuleFile *M = I->second;
7485 unsigned Index = ID - M->BaseIdentifierID;
7486 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7488 // All of the strings in the AST file are preceded by a 16-bit length.
7489 // Extract that 16-bit length to avoid having to execute strlen().
7490 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7491 // unsigned integers. This is important to avoid integer overflow when
7492 // we cast them to 'unsigned'.
7493 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7494 unsigned StrLen = (((unsigned) StrLenPtr[0])
7495 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
7496 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
7497 IdentifiersLoaded[ID] = &II;
7498 markIdentifierFromAST(*this, II);
7499 if (DeserializationListener)
7500 DeserializationListener->IdentifierRead(ID + 1, &II);
7503 return IdentifiersLoaded[ID];
7506 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7507 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
7510 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7511 if (LocalID < NUM_PREDEF_IDENT_IDS)
7514 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7515 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7516 assert(I != M.IdentifierRemap.end()
7517 && "Invalid index into identifier index remap");
7519 return LocalID + I->second;
7522 MacroInfo *ASTReader::getMacro(MacroID ID) {
7526 if (MacrosLoaded.empty()) {
7527 Error("no macro table in AST file");
7531 ID -= NUM_PREDEF_MACRO_IDS;
7532 if (!MacrosLoaded[ID]) {
7533 GlobalMacroMapType::iterator I
7534 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7535 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7536 ModuleFile *M = I->second;
7537 unsigned Index = ID - M->BaseMacroID;
7538 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7540 if (DeserializationListener)
7541 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7545 return MacrosLoaded[ID];
7548 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7549 if (LocalID < NUM_PREDEF_MACRO_IDS)
7552 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7553 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7554 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7556 return LocalID + I->second;
7559 serialization::SubmoduleID
7560 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7561 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7564 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7565 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7566 assert(I != M.SubmoduleRemap.end()
7567 && "Invalid index into submodule index remap");
7569 return LocalID + I->second;
7572 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7573 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7574 assert(GlobalID == 0 && "Unhandled global submodule ID");
7578 if (GlobalID > SubmodulesLoaded.size()) {
7579 Error("submodule ID out of range in AST file");
7583 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7586 Module *ASTReader::getModule(unsigned ID) {
7587 return getSubmodule(ID);
7590 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
7592 // It's a module, look it up by submodule ID.
7593 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
7594 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
7596 // It's a prefix (preamble, PCH, ...). Look it up by index.
7597 unsigned IndexFromEnd = ID >> 1;
7598 assert(IndexFromEnd && "got reference to unknown module file");
7599 return getModuleManager().pch_modules().end()[-IndexFromEnd];
7603 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
7607 // For a file representing a module, use the submodule ID of the top-level
7608 // module as the file ID. For any other kind of file, the number of such
7609 // files loaded beforehand will be the same on reload.
7610 // FIXME: Is this true even if we have an explicit module file and a PCH?
7612 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
7614 auto PCHModules = getModuleManager().pch_modules();
7615 auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
7616 assert(I != PCHModules.end() && "emitting reference to unknown file");
7617 return (I - PCHModules.end()) << 1;
7620 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
7621 ASTReader::getSourceDescriptor(unsigned ID) {
7622 if (const Module *M = getSubmodule(ID))
7623 return ExternalASTSource::ASTSourceDescriptor(*M);
7625 // If there is only a single PCH, return it instead.
7626 // Chained PCH are not suported.
7627 if (ModuleMgr.size() == 1) {
7628 ModuleFile &MF = ModuleMgr.getPrimaryModule();
7629 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
7630 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir,
7631 MF.FileName, MF.Signature);
7636 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7637 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7640 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7644 if (ID > SelectorsLoaded.size()) {
7645 Error("selector ID out of range in AST file");
7649 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
7650 // Load this selector from the selector table.
7651 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7652 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7653 ModuleFile &M = *I->second;
7654 ASTSelectorLookupTrait Trait(*this, M);
7655 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7656 SelectorsLoaded[ID - 1] =
7657 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7658 if (DeserializationListener)
7659 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7662 return SelectorsLoaded[ID - 1];
7665 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7666 return DecodeSelector(ID);
7669 uint32_t ASTReader::GetNumExternalSelectors() {
7670 // ID 0 (the null selector) is considered an external selector.
7671 return getTotalNumSelectors() + 1;
7674 serialization::SelectorID
7675 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7676 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7679 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7680 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7681 assert(I != M.SelectorRemap.end()
7682 && "Invalid index into selector index remap");
7684 return LocalID + I->second;
7688 ASTReader::ReadDeclarationName(ModuleFile &F,
7689 const RecordData &Record, unsigned &Idx) {
7690 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7692 case DeclarationName::Identifier:
7693 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
7695 case DeclarationName::ObjCZeroArgSelector:
7696 case DeclarationName::ObjCOneArgSelector:
7697 case DeclarationName::ObjCMultiArgSelector:
7698 return DeclarationName(ReadSelector(F, Record, Idx));
7700 case DeclarationName::CXXConstructorName:
7701 return Context.DeclarationNames.getCXXConstructorName(
7702 Context.getCanonicalType(readType(F, Record, Idx)));
7704 case DeclarationName::CXXDestructorName:
7705 return Context.DeclarationNames.getCXXDestructorName(
7706 Context.getCanonicalType(readType(F, Record, Idx)));
7708 case DeclarationName::CXXConversionFunctionName:
7709 return Context.DeclarationNames.getCXXConversionFunctionName(
7710 Context.getCanonicalType(readType(F, Record, Idx)));
7712 case DeclarationName::CXXOperatorName:
7713 return Context.DeclarationNames.getCXXOperatorName(
7714 (OverloadedOperatorKind)Record[Idx++]);
7716 case DeclarationName::CXXLiteralOperatorName:
7717 return Context.DeclarationNames.getCXXLiteralOperatorName(
7718 GetIdentifierInfo(F, Record, Idx));
7720 case DeclarationName::CXXUsingDirective:
7721 return DeclarationName::getUsingDirectiveName();
7724 llvm_unreachable("Invalid NameKind!");
7727 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7728 DeclarationNameLoc &DNLoc,
7729 DeclarationName Name,
7730 const RecordData &Record, unsigned &Idx) {
7731 switch (Name.getNameKind()) {
7732 case DeclarationName::CXXConstructorName:
7733 case DeclarationName::CXXDestructorName:
7734 case DeclarationName::CXXConversionFunctionName:
7735 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7738 case DeclarationName::CXXOperatorName:
7739 DNLoc.CXXOperatorName.BeginOpNameLoc
7740 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7741 DNLoc.CXXOperatorName.EndOpNameLoc
7742 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7745 case DeclarationName::CXXLiteralOperatorName:
7746 DNLoc.CXXLiteralOperatorName.OpNameLoc
7747 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7750 case DeclarationName::Identifier:
7751 case DeclarationName::ObjCZeroArgSelector:
7752 case DeclarationName::ObjCOneArgSelector:
7753 case DeclarationName::ObjCMultiArgSelector:
7754 case DeclarationName::CXXUsingDirective:
7759 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7760 DeclarationNameInfo &NameInfo,
7761 const RecordData &Record, unsigned &Idx) {
7762 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7763 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7764 DeclarationNameLoc DNLoc;
7765 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7766 NameInfo.setInfo(DNLoc);
7769 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7770 const RecordData &Record, unsigned &Idx) {
7771 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7772 unsigned NumTPLists = Record[Idx++];
7773 Info.NumTemplParamLists = NumTPLists;
7775 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7776 for (unsigned i=0; i != NumTPLists; ++i)
7777 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7782 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7784 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7786 case TemplateName::Template:
7787 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7789 case TemplateName::OverloadedTemplate: {
7790 unsigned size = Record[Idx++];
7791 UnresolvedSet<8> Decls;
7793 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7795 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7798 case TemplateName::QualifiedTemplate: {
7799 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7800 bool hasTemplKeyword = Record[Idx++];
7801 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7802 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7805 case TemplateName::DependentTemplate: {
7806 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7807 if (Record[Idx++]) // isIdentifier
7808 return Context.getDependentTemplateName(NNS,
7809 GetIdentifierInfo(F, Record,
7811 return Context.getDependentTemplateName(NNS,
7812 (OverloadedOperatorKind)Record[Idx++]);
7815 case TemplateName::SubstTemplateTemplateParm: {
7816 TemplateTemplateParmDecl *param
7817 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7818 if (!param) return TemplateName();
7819 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7820 return Context.getSubstTemplateTemplateParm(param, replacement);
7823 case TemplateName::SubstTemplateTemplateParmPack: {
7824 TemplateTemplateParmDecl *Param
7825 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7827 return TemplateName();
7829 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7830 if (ArgPack.getKind() != TemplateArgument::Pack)
7831 return TemplateName();
7833 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7837 llvm_unreachable("Unhandled template name kind!");
7840 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
7841 const RecordData &Record,
7843 bool Canonicalize) {
7845 // The caller wants a canonical template argument. Sometimes the AST only
7846 // wants template arguments in canonical form (particularly as the template
7847 // argument lists of template specializations) so ensure we preserve that
7848 // canonical form across serialization.
7849 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
7850 return Context.getCanonicalTemplateArgument(Arg);
7853 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7855 case TemplateArgument::Null:
7856 return TemplateArgument();
7857 case TemplateArgument::Type:
7858 return TemplateArgument(readType(F, Record, Idx));
7859 case TemplateArgument::Declaration: {
7860 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7861 return TemplateArgument(D, readType(F, Record, Idx));
7863 case TemplateArgument::NullPtr:
7864 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7865 case TemplateArgument::Integral: {
7866 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7867 QualType T = readType(F, Record, Idx);
7868 return TemplateArgument(Context, Value, T);
7870 case TemplateArgument::Template:
7871 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7872 case TemplateArgument::TemplateExpansion: {
7873 TemplateName Name = ReadTemplateName(F, Record, Idx);
7874 Optional<unsigned> NumTemplateExpansions;
7875 if (unsigned NumExpansions = Record[Idx++])
7876 NumTemplateExpansions = NumExpansions - 1;
7877 return TemplateArgument(Name, NumTemplateExpansions);
7879 case TemplateArgument::Expression:
7880 return TemplateArgument(ReadExpr(F));
7881 case TemplateArgument::Pack: {
7882 unsigned NumArgs = Record[Idx++];
7883 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7884 for (unsigned I = 0; I != NumArgs; ++I)
7885 Args[I] = ReadTemplateArgument(F, Record, Idx);
7886 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
7890 llvm_unreachable("Unhandled template argument kind!");
7893 TemplateParameterList *
7894 ASTReader::ReadTemplateParameterList(ModuleFile &F,
7895 const RecordData &Record, unsigned &Idx) {
7896 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7897 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7898 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7900 unsigned NumParams = Record[Idx++];
7901 SmallVector<NamedDecl *, 16> Params;
7902 Params.reserve(NumParams);
7904 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7906 TemplateParameterList* TemplateParams =
7907 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7909 return TemplateParams;
7914 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
7915 ModuleFile &F, const RecordData &Record,
7916 unsigned &Idx, bool Canonicalize) {
7917 unsigned NumTemplateArgs = Record[Idx++];
7918 TemplArgs.reserve(NumTemplateArgs);
7919 while (NumTemplateArgs--)
7920 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
7923 /// \brief Read a UnresolvedSet structure.
7924 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
7925 const RecordData &Record, unsigned &Idx) {
7926 unsigned NumDecls = Record[Idx++];
7927 Set.reserve(Context, NumDecls);
7928 while (NumDecls--) {
7929 DeclID ID = ReadDeclID(F, Record, Idx);
7930 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
7931 Set.addLazyDecl(Context, ID, AS);
7936 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7937 const RecordData &Record, unsigned &Idx) {
7938 bool isVirtual = static_cast<bool>(Record[Idx++]);
7939 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7940 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7941 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7942 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7943 SourceRange Range = ReadSourceRange(F, Record, Idx);
7944 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7945 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7947 Result.setInheritConstructors(inheritConstructors);
7951 CXXCtorInitializer **
7952 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7954 unsigned NumInitializers = Record[Idx++];
7955 assert(NumInitializers && "wrote ctor initializers but have no inits");
7956 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7957 for (unsigned i = 0; i != NumInitializers; ++i) {
7958 TypeSourceInfo *TInfo = nullptr;
7959 bool IsBaseVirtual = false;
7960 FieldDecl *Member = nullptr;
7961 IndirectFieldDecl *IndirectMember = nullptr;
7963 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7965 case CTOR_INITIALIZER_BASE:
7966 TInfo = GetTypeSourceInfo(F, Record, Idx);
7967 IsBaseVirtual = Record[Idx++];
7970 case CTOR_INITIALIZER_DELEGATING:
7971 TInfo = GetTypeSourceInfo(F, Record, Idx);
7974 case CTOR_INITIALIZER_MEMBER:
7975 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7978 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7979 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7983 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7984 Expr *Init = ReadExpr(F);
7985 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7986 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7987 bool IsWritten = Record[Idx++];
7988 unsigned SourceOrderOrNumArrayIndices;
7989 SmallVector<VarDecl *, 8> Indices;
7991 SourceOrderOrNumArrayIndices = Record[Idx++];
7993 SourceOrderOrNumArrayIndices = Record[Idx++];
7994 Indices.reserve(SourceOrderOrNumArrayIndices);
7995 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7996 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7999 CXXCtorInitializer *BOMInit;
8000 if (Type == CTOR_INITIALIZER_BASE) {
8001 BOMInit = new (Context)
8002 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8003 RParenLoc, MemberOrEllipsisLoc);
8004 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
8005 BOMInit = new (Context)
8006 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8007 } else if (IsWritten) {
8009 BOMInit = new (Context) CXXCtorInitializer(
8010 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
8012 BOMInit = new (Context)
8013 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8014 LParenLoc, Init, RParenLoc);
8016 if (IndirectMember) {
8017 assert(Indices.empty() && "Indirect field improperly initialized");
8018 BOMInit = new (Context)
8019 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8020 LParenLoc, Init, RParenLoc);
8022 BOMInit = CXXCtorInitializer::Create(
8023 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
8024 Indices.data(), Indices.size());
8029 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
8030 CtorInitializers[i] = BOMInit;
8033 return CtorInitializers;
8036 NestedNameSpecifier *
8037 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8038 const RecordData &Record, unsigned &Idx) {
8039 unsigned N = Record[Idx++];
8040 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
8041 for (unsigned I = 0; I != N; ++I) {
8042 NestedNameSpecifier::SpecifierKind Kind
8043 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8045 case NestedNameSpecifier::Identifier: {
8046 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8047 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8051 case NestedNameSpecifier::Namespace: {
8052 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8053 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8057 case NestedNameSpecifier::NamespaceAlias: {
8058 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8059 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8063 case NestedNameSpecifier::TypeSpec:
8064 case NestedNameSpecifier::TypeSpecWithTemplate: {
8065 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8069 bool Template = Record[Idx++];
8070 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8074 case NestedNameSpecifier::Global: {
8075 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8076 // No associated value, and there can't be a prefix.
8080 case NestedNameSpecifier::Super: {
8081 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8082 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8091 NestedNameSpecifierLoc
8092 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8094 unsigned N = Record[Idx++];
8095 NestedNameSpecifierLocBuilder Builder;
8096 for (unsigned I = 0; I != N; ++I) {
8097 NestedNameSpecifier::SpecifierKind Kind
8098 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8100 case NestedNameSpecifier::Identifier: {
8101 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8102 SourceRange Range = ReadSourceRange(F, Record, Idx);
8103 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8107 case NestedNameSpecifier::Namespace: {
8108 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8109 SourceRange Range = ReadSourceRange(F, Record, Idx);
8110 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8114 case NestedNameSpecifier::NamespaceAlias: {
8115 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8116 SourceRange Range = ReadSourceRange(F, Record, Idx);
8117 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8121 case NestedNameSpecifier::TypeSpec:
8122 case NestedNameSpecifier::TypeSpecWithTemplate: {
8123 bool Template = Record[Idx++];
8124 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8126 return NestedNameSpecifierLoc();
8127 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8129 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8130 Builder.Extend(Context,
8131 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8132 T->getTypeLoc(), ColonColonLoc);
8136 case NestedNameSpecifier::Global: {
8137 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8138 Builder.MakeGlobal(Context, ColonColonLoc);
8142 case NestedNameSpecifier::Super: {
8143 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8144 SourceRange Range = ReadSourceRange(F, Record, Idx);
8145 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8151 return Builder.getWithLocInContext(Context);
8155 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8157 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8158 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8159 return SourceRange(beg, end);
8162 /// \brief Read an integral value
8163 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8164 unsigned BitWidth = Record[Idx++];
8165 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8166 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8171 /// \brief Read a signed integral value
8172 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8173 bool isUnsigned = Record[Idx++];
8174 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8177 /// \brief Read a floating-point value
8178 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8179 const llvm::fltSemantics &Sem,
8181 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
8184 // \brief Read a string
8185 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8186 unsigned Len = Record[Idx++];
8187 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8192 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8194 std::string Filename = ReadString(Record, Idx);
8195 ResolveImportedPath(F, Filename);
8199 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8201 unsigned Major = Record[Idx++];
8202 unsigned Minor = Record[Idx++];
8203 unsigned Subminor = Record[Idx++];
8205 return VersionTuple(Major);
8207 return VersionTuple(Major, Minor - 1);
8208 return VersionTuple(Major, Minor - 1, Subminor - 1);
8211 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8212 const RecordData &Record,
8214 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8215 return CXXTemporary::Create(Context, Decl);
8218 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
8219 return Diag(CurrentImportLoc, DiagID);
8222 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8223 return Diags.Report(Loc, DiagID);
8226 /// \brief Retrieve the identifier table associated with the
8228 IdentifierTable &ASTReader::getIdentifierTable() {
8229 return PP.getIdentifierTable();
8232 /// \brief Record that the given ID maps to the given switch-case
8234 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8235 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8236 "Already have a SwitchCase with this ID");
8237 (*CurrSwitchCaseStmts)[ID] = SC;
8240 /// \brief Retrieve the switch-case statement with the given ID.
8241 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8242 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8243 return (*CurrSwitchCaseStmts)[ID];
8246 void ASTReader::ClearSwitchCaseIDs() {
8247 CurrSwitchCaseStmts->clear();
8250 void ASTReader::ReadComments() {
8251 std::vector<RawComment *> Comments;
8252 for (SmallVectorImpl<std::pair<BitstreamCursor,
8253 serialization::ModuleFile *> >::iterator
8254 I = CommentsCursors.begin(),
8255 E = CommentsCursors.end();
8258 BitstreamCursor &Cursor = I->first;
8259 serialization::ModuleFile &F = *I->second;
8260 SavedStreamPosition SavedPosition(Cursor);
8264 llvm::BitstreamEntry Entry =
8265 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
8267 switch (Entry.Kind) {
8268 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8269 case llvm::BitstreamEntry::Error:
8270 Error("malformed block record in AST file");
8272 case llvm::BitstreamEntry::EndBlock:
8274 case llvm::BitstreamEntry::Record:
8275 // The interesting case.
8281 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
8282 case COMMENTS_RAW_COMMENT: {
8284 SourceRange SR = ReadSourceRange(F, Record, Idx);
8285 RawComment::CommentKind Kind =
8286 (RawComment::CommentKind) Record[Idx++];
8287 bool IsTrailingComment = Record[Idx++];
8288 bool IsAlmostTrailingComment = Record[Idx++];
8289 Comments.push_back(new (Context) RawComment(
8290 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8291 Context.getLangOpts().CommentOpts.ParseAllComments));
8297 Context.Comments.addDeserializedComments(Comments);
8301 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8302 // If we know the owning module, use it.
8303 if (Module *M = D->getImportedOwningModule())
8304 return M->getFullModuleName();
8306 // Otherwise, use the name of the top-level module the decl is within.
8307 if (ModuleFile *M = getOwningModuleFile(D))
8308 return M->ModuleName;
8310 // Not from a module.
8314 void ASTReader::finishPendingActions() {
8315 while (!PendingIdentifierInfos.empty() ||
8316 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
8317 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
8318 !PendingUpdateRecords.empty()) {
8319 // If any identifiers with corresponding top-level declarations have
8320 // been loaded, load those declarations now.
8321 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8323 TopLevelDeclsMap TopLevelDecls;
8325 while (!PendingIdentifierInfos.empty()) {
8326 IdentifierInfo *II = PendingIdentifierInfos.back().first;
8327 SmallVector<uint32_t, 4> DeclIDs =
8328 std::move(PendingIdentifierInfos.back().second);
8329 PendingIdentifierInfos.pop_back();
8331 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
8334 // For each decl chain that we wanted to complete while deserializing, mark
8335 // it as "still needs to be completed".
8336 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8337 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8339 PendingIncompleteDeclChains.clear();
8341 // Load pending declaration chains.
8342 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
8343 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second);
8344 PendingDeclChains.clear();
8346 // Make the most recent of the top-level declarations visible.
8347 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8348 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
8349 IdentifierInfo *II = TLD->first;
8350 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
8351 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
8355 // Load any pending macro definitions.
8356 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
8357 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8358 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8359 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8360 // Initialize the macro history from chained-PCHs ahead of module imports.
8361 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8363 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8364 if (Info.M->Kind != MK_ImplicitModule &&
8365 Info.M->Kind != MK_ExplicitModule)
8366 resolvePendingMacro(II, Info);
8368 // Handle module imports.
8369 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8371 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8372 if (Info.M->Kind == MK_ImplicitModule ||
8373 Info.M->Kind == MK_ExplicitModule)
8374 resolvePendingMacro(II, Info);
8377 PendingMacroIDs.clear();
8379 // Wire up the DeclContexts for Decls that we delayed setting until
8380 // recursive loading is completed.
8381 while (!PendingDeclContextInfos.empty()) {
8382 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8383 PendingDeclContextInfos.pop_front();
8384 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8385 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8386 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8389 // Perform any pending declaration updates.
8390 while (!PendingUpdateRecords.empty()) {
8391 auto Update = PendingUpdateRecords.pop_back_val();
8392 ReadingKindTracker ReadingKind(Read_Decl, *this);
8393 loadDeclUpdateRecords(Update.first, Update.second);
8397 // At this point, all update records for loaded decls are in place, so any
8398 // fake class definitions should have become real.
8399 assert(PendingFakeDefinitionData.empty() &&
8400 "faked up a class definition but never saw the real one");
8402 // If we deserialized any C++ or Objective-C class definitions, any
8403 // Objective-C protocol definitions, or any redeclarable templates, make sure
8404 // that all redeclarations point to the definitions. Note that this can only
8405 // happen now, after the redeclaration chains have been fully wired.
8406 for (Decl *D : PendingDefinitions) {
8407 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
8408 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
8409 // Make sure that the TagType points at the definition.
8410 const_cast<TagType*>(TagT)->decl = TD;
8413 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
8414 for (auto *R = getMostRecentExistingDecl(RD); R;
8415 R = R->getPreviousDecl()) {
8417 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
8418 "declaration thinks it's the definition but it isn't");
8419 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
8426 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
8427 // Make sure that the ObjCInterfaceType points at the definition.
8428 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8431 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8432 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8437 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
8438 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8439 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8444 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
8445 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8446 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
8448 PendingDefinitions.clear();
8450 // Load the bodies of any functions or methods we've encountered. We do
8451 // this now (delayed) so that we can be sure that the declaration chains
8452 // have been fully wired up (hasBody relies on this).
8453 // FIXME: We shouldn't require complete redeclaration chains here.
8454 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8455 PBEnd = PendingBodies.end();
8456 PB != PBEnd; ++PB) {
8457 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8458 // FIXME: Check for =delete/=default?
8459 // FIXME: Complain about ODR violations here?
8460 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8461 FD->setLazyBody(PB->second);
8465 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8466 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8467 MD->setLazyBody(PB->second);
8469 PendingBodies.clear();
8472 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8473 getContext().deduplicateMergedDefinitonsFor(ND);
8474 PendingMergedDefinitionsToDeduplicate.clear();
8477 void ASTReader::diagnoseOdrViolations() {
8478 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8481 // Trigger the import of the full definition of each class that had any
8482 // odr-merging problems, so we can produce better diagnostics for them.
8483 // These updates may in turn find and diagnose some ODR failures, so take
8484 // ownership of the set first.
8485 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8486 PendingOdrMergeFailures.clear();
8487 for (auto &Merge : OdrMergeFailures) {
8488 Merge.first->buildLookup();
8489 Merge.first->decls_begin();
8490 Merge.first->bases_begin();
8491 Merge.first->vbases_begin();
8492 for (auto *RD : Merge.second) {
8499 // For each declaration from a merged context, check that the canonical
8500 // definition of that context also contains a declaration of the same
8503 // Caution: this loop does things that might invalidate iterators into
8504 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8505 while (!PendingOdrMergeChecks.empty()) {
8506 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8508 // FIXME: Skip over implicit declarations for now. This matters for things
8509 // like implicitly-declared special member functions. This isn't entirely
8510 // correct; we can end up with multiple unmerged declarations of the same
8512 if (D->isImplicit())
8515 DeclContext *CanonDef = D->getDeclContext();
8518 const Decl *DCanon = D->getCanonicalDecl();
8520 for (auto RI : D->redecls()) {
8521 if (RI->getLexicalDeclContext() == CanonDef) {
8529 // Quick check failed, time to do the slow thing. Note, we can't just
8530 // look up the name of D in CanonDef here, because the member that is
8531 // in CanonDef might not be found by name lookup (it might have been
8532 // replaced by a more recent declaration in the lookup table), and we
8533 // can't necessarily find it in the redeclaration chain because it might
8534 // be merely mergeable, not redeclarable.
8535 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8536 for (auto *CanonMember : CanonDef->decls()) {
8537 if (CanonMember->getCanonicalDecl() == DCanon) {
8538 // This can happen if the declaration is merely mergeable and not
8539 // actually redeclarable (we looked for redeclarations earlier).
8541 // FIXME: We should be able to detect this more efficiently, without
8542 // pulling in all of the members of CanonDef.
8546 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
8547 if (ND->getDeclName() == D->getDeclName())
8548 Candidates.push_back(ND);
8552 // The AST doesn't like TagDecls becoming invalid after they've been
8553 // completed. We only really need to mark FieldDecls as invalid here.
8554 if (!isa<TagDecl>(D))
8555 D->setInvalidDecl();
8557 // Ensure we don't accidentally recursively enter deserialization while
8558 // we're producing our diagnostic.
8559 Deserializing RecursionGuard(this);
8561 std::string CanonDefModule =
8562 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8563 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8564 << D << getOwningModuleNameForDiagnostic(D)
8565 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8567 if (Candidates.empty())
8568 Diag(cast<Decl>(CanonDef)->getLocation(),
8569 diag::note_module_odr_violation_no_possible_decls) << D;
8571 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8572 Diag(Candidates[I]->getLocation(),
8573 diag::note_module_odr_violation_possible_decl)
8577 DiagnosedOdrMergeFailures.insert(CanonDef);
8581 if (OdrMergeFailures.empty())
8584 // Ensure we don't accidentally recursively enter deserialization while
8585 // we're producing our diagnostics.
8586 Deserializing RecursionGuard(this);
8588 // Issue any pending ODR-failure diagnostics.
8589 for (auto &Merge : OdrMergeFailures) {
8590 // If we've already pointed out a specific problem with this class, don't
8591 // bother issuing a general "something's different" diagnostic.
8592 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
8595 bool Diagnosed = false;
8596 for (auto *RD : Merge.second) {
8597 // Multiple different declarations got merged together; tell the user
8598 // where they came from.
8599 if (Merge.first != RD) {
8600 // FIXME: Walk the definition, figure out what's different,
8601 // and diagnose that.
8603 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8604 Diag(Merge.first->getLocation(),
8605 diag::err_module_odr_violation_different_definitions)
8606 << Merge.first << Module.empty() << Module;
8610 Diag(RD->getLocation(),
8611 diag::note_module_odr_violation_different_definitions)
8612 << getOwningModuleNameForDiagnostic(RD);
8617 // All definitions are updates to the same declaration. This happens if a
8618 // module instantiates the declaration of a class template specialization
8619 // and two or more other modules instantiate its definition.
8621 // FIXME: Indicate which modules had instantiations of this definition.
8622 // FIXME: How can this even happen?
8623 Diag(Merge.first->getLocation(),
8624 diag::err_module_odr_violation_different_instantiations)
8630 void ASTReader::StartedDeserializing() {
8631 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
8632 ReadTimer->startTimer();
8635 void ASTReader::FinishedDeserializing() {
8636 assert(NumCurrentElementsDeserializing &&
8637 "FinishedDeserializing not paired with StartedDeserializing");
8638 if (NumCurrentElementsDeserializing == 1) {
8639 // We decrease NumCurrentElementsDeserializing only after pending actions
8640 // are finished, to avoid recursively re-calling finishPendingActions().
8641 finishPendingActions();
8643 --NumCurrentElementsDeserializing;
8645 if (NumCurrentElementsDeserializing == 0) {
8646 // Propagate exception specification updates along redeclaration chains.
8647 while (!PendingExceptionSpecUpdates.empty()) {
8648 auto Updates = std::move(PendingExceptionSpecUpdates);
8649 PendingExceptionSpecUpdates.clear();
8650 for (auto Update : Updates) {
8651 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
8652 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
8653 if (auto *Listener = Context.getASTMutationListener())
8654 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
8655 for (auto *Redecl : Update.second->redecls())
8656 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
8661 ReadTimer->stopTimer();
8663 diagnoseOdrViolations();
8665 // We are not in recursive loading, so it's safe to pass the "interesting"
8666 // decls to the consumer.
8668 PassInterestingDeclsToConsumer();
8672 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
8673 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8674 // Remove any fake results before adding any real ones.
8675 auto It = PendingFakeLookupResults.find(II);
8676 if (It != PendingFakeLookupResults.end()) {
8677 for (auto *ND : It->second)
8678 SemaObj->IdResolver.RemoveDecl(ND);
8679 // FIXME: this works around module+PCH performance issue.
8680 // Rather than erase the result from the map, which is O(n), just clear
8681 // the vector of NamedDecls.
8686 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8687 SemaObj->TUScope->AddDecl(D);
8688 } else if (SemaObj->TUScope) {
8689 // Adding the decl to IdResolver may have failed because it was already in
8690 // (even though it was not added in scope). If it is already in, make sure
8691 // it gets in the scope as well.
8692 if (std::find(SemaObj->IdResolver.begin(Name),
8693 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8694 SemaObj->TUScope->AddDecl(D);
8698 ASTReader::ASTReader(
8699 Preprocessor &PP, ASTContext &Context,
8700 const PCHContainerReader &PCHContainerRdr,
8701 ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
8702 StringRef isysroot, bool DisableValidation,
8703 bool AllowASTWithCompilerErrors,
8704 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8705 bool UseGlobalIndex,
8706 std::unique_ptr<llvm::Timer> ReadTimer)
8707 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8708 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8709 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr),
8710 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
8711 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr),
8712 ReadTimer(std::move(ReadTimer)),
8713 PragmaMSStructState(-1),
8714 PragmaMSPointersToMembersState(-1),
8715 isysroot(isysroot), DisableValidation(DisableValidation),
8716 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8717 AllowConfigurationMismatch(AllowConfigurationMismatch),
8718 ValidateSystemInputs(ValidateSystemInputs),
8719 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8720 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8721 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8722 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8723 NumIdentifierLookupHits(0), NumSelectorsRead(0),
8724 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8725 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8726 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8727 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8728 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8729 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8730 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
8731 SourceMgr.setExternalSLocEntrySource(this);
8733 for (const auto &Ext : Extensions) {
8734 auto BlockName = Ext->getExtensionMetadata().BlockName;
8735 auto Known = ModuleFileExtensions.find(BlockName);
8736 if (Known != ModuleFileExtensions.end()) {
8737 Diags.Report(diag::warn_duplicate_module_file_extension)
8742 ModuleFileExtensions.insert({BlockName, Ext});
8746 ASTReader::~ASTReader() {
8747 if (OwnsDeserializationListener)
8748 delete DeserializationListener;