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/AST/NestedNameSpecifier.h"
23 #include "clang/AST/Type.h"
24 #include "clang/AST/TypeLocVisitor.h"
25 #include "clang/Basic/DiagnosticOptions.h"
26 #include "clang/Basic/FileManager.h"
27 #include "clang/Basic/SourceManager.h"
28 #include "clang/Basic/SourceManagerInternals.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Basic/TargetOptions.h"
31 #include "clang/Basic/Version.h"
32 #include "clang/Basic/VersionTuple.h"
33 #include "clang/Frontend/Utils.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Lex/HeaderSearchOptions.h"
36 #include "clang/Lex/MacroInfo.h"
37 #include "clang/Lex/PreprocessingRecord.h"
38 #include "clang/Lex/Preprocessor.h"
39 #include "clang/Lex/PreprocessorOptions.h"
40 #include "clang/Sema/Scope.h"
41 #include "clang/Sema/Sema.h"
42 #include "clang/Serialization/ASTDeserializationListener.h"
43 #include "clang/Serialization/GlobalModuleIndex.h"
44 #include "clang/Serialization/ModuleManager.h"
45 #include "clang/Serialization/SerializationDiagnostic.h"
46 #include "llvm/ADT/Hashing.h"
47 #include "llvm/ADT/StringExtras.h"
48 #include "llvm/Bitcode/BitstreamReader.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/FileSystem.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include "llvm/Support/Path.h"
53 #include "llvm/Support/SaveAndRestore.h"
54 #include "llvm/Support/raw_ostream.h"
58 #include <system_error>
60 using namespace clang;
61 using namespace clang::serialization;
62 using namespace clang::serialization::reader;
63 using llvm::BitstreamCursor;
66 //===----------------------------------------------------------------------===//
67 // ChainedASTReaderListener implementation
68 //===----------------------------------------------------------------------===//
71 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72 return First->ReadFullVersionInformation(FullVersion) ||
73 Second->ReadFullVersionInformation(FullVersion);
75 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
76 First->ReadModuleName(ModuleName);
77 Second->ReadModuleName(ModuleName);
79 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
80 First->ReadModuleMapFile(ModuleMapPath);
81 Second->ReadModuleMapFile(ModuleMapPath);
84 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
86 bool AllowCompatibleDifferences) {
87 return First->ReadLanguageOptions(LangOpts, Complain,
88 AllowCompatibleDifferences) ||
89 Second->ReadLanguageOptions(LangOpts, Complain,
90 AllowCompatibleDifferences);
92 bool ChainedASTReaderListener::ReadTargetOptions(
93 const TargetOptions &TargetOpts, bool Complain,
94 bool AllowCompatibleDifferences) {
95 return First->ReadTargetOptions(TargetOpts, Complain,
96 AllowCompatibleDifferences) ||
97 Second->ReadTargetOptions(TargetOpts, Complain,
98 AllowCompatibleDifferences);
100 bool ChainedASTReaderListener::ReadDiagnosticOptions(
101 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
102 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
103 Second->ReadDiagnosticOptions(DiagOpts, Complain);
106 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
108 return First->ReadFileSystemOptions(FSOpts, Complain) ||
109 Second->ReadFileSystemOptions(FSOpts, Complain);
112 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
113 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
115 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
117 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
120 bool ChainedASTReaderListener::ReadPreprocessorOptions(
121 const PreprocessorOptions &PPOpts, bool Complain,
122 std::string &SuggestedPredefines) {
123 return First->ReadPreprocessorOptions(PPOpts, Complain,
124 SuggestedPredefines) ||
125 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
127 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
129 First->ReadCounter(M, Value);
130 Second->ReadCounter(M, Value);
132 bool ChainedASTReaderListener::needsInputFileVisitation() {
133 return First->needsInputFileVisitation() ||
134 Second->needsInputFileVisitation();
136 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
137 return First->needsSystemInputFileVisitation() ||
138 Second->needsSystemInputFileVisitation();
140 void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
141 First->visitModuleFile(Filename);
142 Second->visitModuleFile(Filename);
144 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
147 bool Continue = false;
148 if (First->needsInputFileVisitation() &&
149 (!isSystem || First->needsSystemInputFileVisitation()))
150 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
151 if (Second->needsInputFileVisitation() &&
152 (!isSystem || Second->needsSystemInputFileVisitation()))
153 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
157 //===----------------------------------------------------------------------===//
158 // PCH validator implementation
159 //===----------------------------------------------------------------------===//
161 ASTReaderListener::~ASTReaderListener() {}
163 /// \brief Compare the given set of language options against an existing set of
164 /// language options.
166 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
167 /// \param AllowCompatibleDifferences If true, differences between compatible
168 /// language options will be permitted.
170 /// \returns true if the languagae options mis-match, false otherwise.
171 static bool checkLanguageOptions(const LangOptions &LangOpts,
172 const LangOptions &ExistingLangOpts,
173 DiagnosticsEngine *Diags,
174 bool AllowCompatibleDifferences = true) {
175 #define LANGOPT(Name, Bits, Default, Description) \
176 if (ExistingLangOpts.Name != LangOpts.Name) { \
178 Diags->Report(diag::err_pch_langopt_mismatch) \
179 << Description << LangOpts.Name << ExistingLangOpts.Name; \
183 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
184 if (ExistingLangOpts.Name != LangOpts.Name) { \
186 Diags->Report(diag::err_pch_langopt_value_mismatch) \
191 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
192 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
194 Diags->Report(diag::err_pch_langopt_value_mismatch) \
199 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
200 if (!AllowCompatibleDifferences) \
201 LANGOPT(Name, Bits, Default, Description)
203 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
204 if (!AllowCompatibleDifferences) \
205 ENUM_LANGOPT(Name, Bits, Default, Description)
207 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
208 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
209 #include "clang/Basic/LangOptions.def"
211 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
213 Diags->Report(diag::err_pch_langopt_value_mismatch)
214 << "target Objective-C runtime";
218 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
219 LangOpts.CommentOpts.BlockCommandNames) {
221 Diags->Report(diag::err_pch_langopt_value_mismatch)
222 << "block command names";
229 /// \brief Compare the given set of target options against an existing set of
232 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
234 /// \returns true if the target options mis-match, false otherwise.
235 static bool checkTargetOptions(const TargetOptions &TargetOpts,
236 const TargetOptions &ExistingTargetOpts,
237 DiagnosticsEngine *Diags,
238 bool AllowCompatibleDifferences = true) {
239 #define CHECK_TARGET_OPT(Field, Name) \
240 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
242 Diags->Report(diag::err_pch_targetopt_mismatch) \
243 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
247 // The triple and ABI must match exactly.
248 CHECK_TARGET_OPT(Triple, "target");
249 CHECK_TARGET_OPT(ABI, "target ABI");
251 // We can tolerate different CPUs in many cases, notably when one CPU
252 // supports a strict superset of another. When allowing compatible
253 // differences skip this check.
254 if (!AllowCompatibleDifferences)
255 CHECK_TARGET_OPT(CPU, "target CPU");
257 #undef CHECK_TARGET_OPT
259 // Compare feature sets.
260 SmallVector<StringRef, 4> ExistingFeatures(
261 ExistingTargetOpts.FeaturesAsWritten.begin(),
262 ExistingTargetOpts.FeaturesAsWritten.end());
263 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
264 TargetOpts.FeaturesAsWritten.end());
265 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
266 std::sort(ReadFeatures.begin(), ReadFeatures.end());
268 // We compute the set difference in both directions explicitly so that we can
269 // diagnose the differences differently.
270 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
272 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
273 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
274 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
275 ExistingFeatures.begin(), ExistingFeatures.end(),
276 std::back_inserter(UnmatchedReadFeatures));
278 // If we are allowing compatible differences and the read feature set is
279 // a strict subset of the existing feature set, there is nothing to diagnose.
280 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
284 for (StringRef Feature : UnmatchedReadFeatures)
285 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
286 << /* is-existing-feature */ false << Feature;
287 for (StringRef Feature : UnmatchedExistingFeatures)
288 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
289 << /* is-existing-feature */ true << Feature;
292 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
296 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
298 bool AllowCompatibleDifferences) {
299 const LangOptions &ExistingLangOpts = PP.getLangOpts();
300 return checkLanguageOptions(LangOpts, ExistingLangOpts,
301 Complain ? &Reader.Diags : nullptr,
302 AllowCompatibleDifferences);
305 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
307 bool AllowCompatibleDifferences) {
308 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
309 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
310 Complain ? &Reader.Diags : nullptr,
311 AllowCompatibleDifferences);
315 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
317 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
321 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
322 DiagnosticsEngine &Diags,
324 typedef DiagnosticsEngine::Level Level;
326 // Check current mappings for new -Werror mappings, and the stored mappings
327 // for cases that were explicitly mapped to *not* be errors that are now
328 // errors because of options like -Werror.
329 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
331 for (DiagnosticsEngine *MappingSource : MappingSources) {
332 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
333 diag::kind DiagID = DiagIDMappingPair.first;
334 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
335 if (CurLevel < DiagnosticsEngine::Error)
336 continue; // not significant
338 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
339 if (StoredLevel < DiagnosticsEngine::Error) {
341 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
342 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
351 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
352 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
353 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
355 return Ext >= diag::Severity::Error;
358 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
359 DiagnosticsEngine &Diags,
360 bool IsSystem, bool Complain) {
363 if (Diags.getSuppressSystemWarnings())
365 // If -Wsystem-headers was not enabled before, be conservative
366 if (StoredDiags.getSuppressSystemWarnings()) {
368 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
373 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
375 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
379 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
380 !StoredDiags.getEnableAllWarnings()) {
382 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
386 if (isExtHandlingFromDiagsError(Diags) &&
387 !isExtHandlingFromDiagsError(StoredDiags)) {
389 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
393 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
396 bool PCHValidator::ReadDiagnosticOptions(
397 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
398 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
399 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
400 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
401 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
402 // This should never fail, because we would have processed these options
403 // before writing them to an ASTFile.
404 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
406 ModuleManager &ModuleMgr = Reader.getModuleManager();
407 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
409 // If the original import came from a file explicitly generated by the user,
410 // don't check the diagnostic mappings.
411 // FIXME: currently this is approximated by checking whether this is not a
412 // module import of an implicitly-loaded module file.
413 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
414 // the transitive closure of its imports, since unrelated modules cannot be
415 // imported until after this module finishes validation.
416 ModuleFile *TopImport = *ModuleMgr.rbegin();
417 while (!TopImport->ImportedBy.empty())
418 TopImport = TopImport->ImportedBy[0];
419 if (TopImport->Kind != MK_ImplicitModule)
422 StringRef ModuleName = TopImport->ModuleName;
423 assert(!ModuleName.empty() && "diagnostic options read before module name");
425 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
426 assert(M && "missing module");
428 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
429 // contains the union of their flags.
430 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
433 /// \brief Collect the macro definitions provided by the given preprocessor
436 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
437 MacroDefinitionsMap &Macros,
438 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
439 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
440 StringRef Macro = PPOpts.Macros[I].first;
441 bool IsUndef = PPOpts.Macros[I].second;
443 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
444 StringRef MacroName = MacroPair.first;
445 StringRef MacroBody = MacroPair.second;
447 // For an #undef'd macro, we only care about the name.
449 if (MacroNames && !Macros.count(MacroName))
450 MacroNames->push_back(MacroName);
452 Macros[MacroName] = std::make_pair("", true);
456 // For a #define'd macro, figure out the actual definition.
457 if (MacroName.size() == Macro.size())
460 // Note: GCC drops anything following an end-of-line character.
461 StringRef::size_type End = MacroBody.find_first_of("\n\r");
462 MacroBody = MacroBody.substr(0, End);
465 if (MacroNames && !Macros.count(MacroName))
466 MacroNames->push_back(MacroName);
467 Macros[MacroName] = std::make_pair(MacroBody, false);
471 /// \brief Check the preprocessor options deserialized from the control block
472 /// against the preprocessor options in an existing preprocessor.
474 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
475 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
476 const PreprocessorOptions &ExistingPPOpts,
477 DiagnosticsEngine *Diags,
478 FileManager &FileMgr,
479 std::string &SuggestedPredefines,
480 const LangOptions &LangOpts) {
481 // Check macro definitions.
482 MacroDefinitionsMap ASTFileMacros;
483 collectMacroDefinitions(PPOpts, ASTFileMacros);
484 MacroDefinitionsMap ExistingMacros;
485 SmallVector<StringRef, 4> ExistingMacroNames;
486 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
488 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
489 // Dig out the macro definition in the existing preprocessor options.
490 StringRef MacroName = ExistingMacroNames[I];
491 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
493 // Check whether we know anything about this macro name or not.
494 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
495 = ASTFileMacros.find(MacroName);
496 if (Known == ASTFileMacros.end()) {
497 // FIXME: Check whether this identifier was referenced anywhere in the
498 // AST file. If so, we should reject the AST file. Unfortunately, this
499 // information isn't in the control block. What shall we do about it?
501 if (Existing.second) {
502 SuggestedPredefines += "#undef ";
503 SuggestedPredefines += MacroName.str();
504 SuggestedPredefines += '\n';
506 SuggestedPredefines += "#define ";
507 SuggestedPredefines += MacroName.str();
508 SuggestedPredefines += ' ';
509 SuggestedPredefines += Existing.first.str();
510 SuggestedPredefines += '\n';
515 // If the macro was defined in one but undef'd in the other, we have a
517 if (Existing.second != Known->second.second) {
519 Diags->Report(diag::err_pch_macro_def_undef)
520 << MacroName << Known->second.second;
525 // If the macro was #undef'd in both, or if the macro bodies are identical,
527 if (Existing.second || Existing.first == Known->second.first)
530 // The macro bodies differ; complain.
532 Diags->Report(diag::err_pch_macro_def_conflict)
533 << MacroName << Known->second.first << Existing.first;
538 // Check whether we're using predefines.
539 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
541 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
546 // Detailed record is important since it is used for the module cache hash.
547 if (LangOpts.Modules &&
548 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
550 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
555 // Compute the #include and #include_macros lines we need.
556 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
557 StringRef File = ExistingPPOpts.Includes[I];
558 if (File == ExistingPPOpts.ImplicitPCHInclude)
561 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
562 != PPOpts.Includes.end())
565 SuggestedPredefines += "#include \"";
566 SuggestedPredefines += File;
567 SuggestedPredefines += "\"\n";
570 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
571 StringRef File = ExistingPPOpts.MacroIncludes[I];
572 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
574 != PPOpts.MacroIncludes.end())
577 SuggestedPredefines += "#__include_macros \"";
578 SuggestedPredefines += File;
579 SuggestedPredefines += "\"\n##\n";
585 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
587 std::string &SuggestedPredefines) {
588 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
590 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
591 Complain? &Reader.Diags : nullptr,
597 /// Check the header search options deserialized from the control block
598 /// against the header search options in an existing preprocessor.
600 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
601 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
602 StringRef SpecificModuleCachePath,
603 StringRef ExistingModuleCachePath,
604 DiagnosticsEngine *Diags,
605 const LangOptions &LangOpts) {
606 if (LangOpts.Modules) {
607 if (SpecificModuleCachePath != ExistingModuleCachePath) {
609 Diags->Report(diag::err_pch_modulecache_mismatch)
610 << SpecificModuleCachePath << ExistingModuleCachePath;
618 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
619 StringRef SpecificModuleCachePath,
621 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
622 PP.getHeaderSearchInfo().getModuleCachePath(),
623 Complain ? &Reader.Diags : nullptr,
627 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
628 PP.setCounterValue(Value);
631 //===----------------------------------------------------------------------===//
632 // AST reader implementation
633 //===----------------------------------------------------------------------===//
635 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
636 bool TakeOwnership) {
637 DeserializationListener = Listener;
638 OwnsDeserializationListener = TakeOwnership;
643 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
644 return serialization::ComputeHash(Sel);
648 std::pair<unsigned, unsigned>
649 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
650 using namespace llvm::support;
651 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
652 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
653 return std::make_pair(KeyLen, DataLen);
656 ASTSelectorLookupTrait::internal_key_type
657 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
658 using namespace llvm::support;
659 SelectorTable &SelTable = Reader.getContext().Selectors;
660 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
661 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
662 F, endian::readNext<uint32_t, little, unaligned>(d));
664 return SelTable.getNullarySelector(FirstII);
666 return SelTable.getUnarySelector(FirstII);
668 SmallVector<IdentifierInfo *, 16> Args;
669 Args.push_back(FirstII);
670 for (unsigned I = 1; I != N; ++I)
671 Args.push_back(Reader.getLocalIdentifier(
672 F, endian::readNext<uint32_t, little, unaligned>(d)));
674 return SelTable.getSelector(N, Args.data());
677 ASTSelectorLookupTrait::data_type
678 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
680 using namespace llvm::support;
684 Result.ID = Reader.getGlobalSelectorID(
685 F, endian::readNext<uint32_t, little, unaligned>(d));
686 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
687 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
688 Result.InstanceBits = FullInstanceBits & 0x3;
689 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
690 Result.FactoryBits = FullFactoryBits & 0x3;
691 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
692 unsigned NumInstanceMethods = FullInstanceBits >> 3;
693 unsigned NumFactoryMethods = FullFactoryBits >> 3;
695 // Load instance methods
696 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
697 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
698 F, endian::readNext<uint32_t, little, unaligned>(d)))
699 Result.Instance.push_back(Method);
702 // Load factory methods
703 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
704 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
705 F, endian::readNext<uint32_t, little, unaligned>(d)))
706 Result.Factory.push_back(Method);
712 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
713 return llvm::HashString(a);
716 std::pair<unsigned, unsigned>
717 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
718 using namespace llvm::support;
719 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
720 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
721 return std::make_pair(KeyLen, DataLen);
724 ASTIdentifierLookupTraitBase::internal_key_type
725 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
726 assert(n >= 2 && d[n-1] == '\0');
727 return StringRef((const char*) d, n-1);
730 /// \brief Whether the given identifier is "interesting".
731 static bool isInterestingIdentifier(IdentifierInfo &II) {
732 return II.isPoisoned() ||
733 II.isExtensionToken() ||
734 II.getObjCOrBuiltinID() ||
735 II.hasRevertedTokenIDToIdentifier() ||
736 II.hadMacroDefinition() ||
737 II.getFETokenInfo<void>();
740 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
741 const unsigned char* d,
743 using namespace llvm::support;
744 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
745 bool IsInteresting = RawID & 0x01;
747 // Wipe out the "is interesting" bit.
750 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
751 if (!IsInteresting) {
752 // For uninteresting identifiers, just build the IdentifierInfo
753 // and associate it with the persistent ID.
754 IdentifierInfo *II = KnownII;
756 II = &Reader.getIdentifierTable().getOwn(k);
759 Reader.SetIdentifierInfo(ID, II);
760 if (!II->isFromAST()) {
761 bool WasInteresting = isInterestingIdentifier(*II);
764 II->setChangedSinceDeserialization();
766 Reader.markIdentifierUpToDate(II);
770 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
771 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
772 bool CPlusPlusOperatorKeyword = Bits & 0x01;
774 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
776 bool Poisoned = Bits & 0x01;
778 bool ExtensionToken = Bits & 0x01;
780 bool hadMacroDefinition = Bits & 0x01;
783 assert(Bits == 0 && "Extra bits in the identifier?");
786 // Build the IdentifierInfo itself and link the identifier ID with
787 // the new IdentifierInfo.
788 IdentifierInfo *II = KnownII;
790 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
793 Reader.markIdentifierUpToDate(II);
794 if (!II->isFromAST()) {
795 bool WasInteresting = isInterestingIdentifier(*II);
798 II->setChangedSinceDeserialization();
801 // Set or check the various bits in the IdentifierInfo structure.
802 // Token IDs are read-only.
803 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
804 II->RevertTokenIDToIdentifier();
805 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
806 assert(II->isExtensionToken() == ExtensionToken &&
807 "Incorrect extension token flag");
808 (void)ExtensionToken;
810 II->setIsPoisoned(true);
811 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
812 "Incorrect C++ operator keyword flag");
813 (void)CPlusPlusOperatorKeyword;
815 // If this identifier is a macro, deserialize the macro
817 if (hadMacroDefinition) {
818 uint32_t MacroDirectivesOffset =
819 endian::readNext<uint32_t, little, unaligned>(d);
822 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
825 Reader.SetIdentifierInfo(ID, II);
827 // Read all of the declarations visible at global scope with this
830 SmallVector<uint32_t, 4> DeclIDs;
831 for (; DataLen > 0; DataLen -= 4)
832 DeclIDs.push_back(Reader.getGlobalDeclID(
833 F, endian::readNext<uint32_t, little, unaligned>(d)));
834 Reader.SetGloballyVisibleDecls(II, DeclIDs);
841 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
842 llvm::FoldingSetNodeID ID;
843 ID.AddInteger(Key.Kind);
846 case DeclarationName::Identifier:
847 case DeclarationName::CXXLiteralOperatorName:
848 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
850 case DeclarationName::ObjCZeroArgSelector:
851 case DeclarationName::ObjCOneArgSelector:
852 case DeclarationName::ObjCMultiArgSelector:
853 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
855 case DeclarationName::CXXOperatorName:
856 ID.AddInteger((OverloadedOperatorKind)Key.Data);
858 case DeclarationName::CXXConstructorName:
859 case DeclarationName::CXXDestructorName:
860 case DeclarationName::CXXConversionFunctionName:
861 case DeclarationName::CXXUsingDirective:
865 return ID.ComputeHash();
868 ASTDeclContextNameLookupTrait::internal_key_type
869 ASTDeclContextNameLookupTrait::GetInternalKey(
870 const external_key_type& Name) const {
872 Key.Kind = Name.getNameKind();
873 switch (Name.getNameKind()) {
874 case DeclarationName::Identifier:
875 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
877 case DeclarationName::ObjCZeroArgSelector:
878 case DeclarationName::ObjCOneArgSelector:
879 case DeclarationName::ObjCMultiArgSelector:
880 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
882 case DeclarationName::CXXOperatorName:
883 Key.Data = Name.getCXXOverloadedOperator();
885 case DeclarationName::CXXLiteralOperatorName:
886 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
888 case DeclarationName::CXXConstructorName:
889 case DeclarationName::CXXDestructorName:
890 case DeclarationName::CXXConversionFunctionName:
891 case DeclarationName::CXXUsingDirective:
899 std::pair<unsigned, unsigned>
900 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
901 using namespace llvm::support;
902 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
903 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
904 return std::make_pair(KeyLen, DataLen);
907 ASTDeclContextNameLookupTrait::internal_key_type
908 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
909 using namespace llvm::support;
912 Key.Kind = (DeclarationName::NameKind)*d++;
914 case DeclarationName::Identifier:
915 Key.Data = (uint64_t)Reader.getLocalIdentifier(
916 F, endian::readNext<uint32_t, little, unaligned>(d));
918 case DeclarationName::ObjCZeroArgSelector:
919 case DeclarationName::ObjCOneArgSelector:
920 case DeclarationName::ObjCMultiArgSelector:
922 (uint64_t)Reader.getLocalSelector(
923 F, endian::readNext<uint32_t, little, unaligned>(
924 d)).getAsOpaquePtr();
926 case DeclarationName::CXXOperatorName:
927 Key.Data = *d++; // OverloadedOperatorKind
929 case DeclarationName::CXXLiteralOperatorName:
930 Key.Data = (uint64_t)Reader.getLocalIdentifier(
931 F, endian::readNext<uint32_t, little, unaligned>(d));
933 case DeclarationName::CXXConstructorName:
934 case DeclarationName::CXXDestructorName:
935 case DeclarationName::CXXConversionFunctionName:
936 case DeclarationName::CXXUsingDirective:
944 ASTDeclContextNameLookupTrait::data_type
945 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
946 const unsigned char* d,
948 using namespace llvm::support;
949 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
950 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
951 const_cast<unsigned char *>(d));
952 return std::make_pair(Start, Start + NumDecls);
955 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
956 BitstreamCursor &Cursor,
957 const std::pair<uint64_t, uint64_t> &Offsets,
958 DeclContextInfo &Info) {
959 SavedStreamPosition SavedPosition(Cursor);
960 // First the lexical decls.
961 if (Offsets.first != 0) {
962 Cursor.JumpToBit(Offsets.first);
966 unsigned Code = Cursor.ReadCode();
967 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
968 if (RecCode != DECL_CONTEXT_LEXICAL) {
969 Error("Expected lexical block");
973 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
974 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
977 // Now the lookup table.
978 if (Offsets.second != 0) {
979 Cursor.JumpToBit(Offsets.second);
983 unsigned Code = Cursor.ReadCode();
984 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
985 if (RecCode != DECL_CONTEXT_VISIBLE) {
986 Error("Expected visible lookup table block");
989 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
990 (const unsigned char *)Blob.data() + Record[0],
991 (const unsigned char *)Blob.data() + sizeof(uint32_t),
992 (const unsigned char *)Blob.data(),
993 ASTDeclContextNameLookupTrait(*this, M));
999 void ASTReader::Error(StringRef Msg) {
1000 Error(diag::err_fe_pch_malformed, Msg);
1001 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1002 Diag(diag::note_module_cache_path)
1003 << PP.getHeaderSearchInfo().getModuleCachePath();
1007 void ASTReader::Error(unsigned DiagID,
1008 StringRef Arg1, StringRef Arg2) {
1009 if (Diags.isDiagnosticInFlight())
1010 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1012 Diag(DiagID) << Arg1 << Arg2;
1015 //===----------------------------------------------------------------------===//
1016 // Source Manager Deserialization
1017 //===----------------------------------------------------------------------===//
1019 /// \brief Read the line table in the source manager block.
1020 /// \returns true if there was an error.
1021 bool ASTReader::ParseLineTable(ModuleFile &F,
1022 const RecordData &Record) {
1024 LineTableInfo &LineTable = SourceMgr.getLineTable();
1026 // Parse the file names
1027 std::map<int, int> FileIDs;
1028 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1029 // Extract the file name
1030 auto Filename = ReadPath(F, Record, Idx);
1031 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1034 // Parse the line entries
1035 std::vector<LineEntry> Entries;
1036 while (Idx < Record.size()) {
1037 int FID = Record[Idx++];
1038 assert(FID >= 0 && "Serialized line entries for non-local file.");
1039 // Remap FileID from 1-based old view.
1040 FID += F.SLocEntryBaseID - 1;
1042 // Extract the line entries
1043 unsigned NumEntries = Record[Idx++];
1044 assert(NumEntries && "Numentries is 00000");
1046 Entries.reserve(NumEntries);
1047 for (unsigned I = 0; I != NumEntries; ++I) {
1048 unsigned FileOffset = Record[Idx++];
1049 unsigned LineNo = Record[Idx++];
1050 int FilenameID = FileIDs[Record[Idx++]];
1051 SrcMgr::CharacteristicKind FileKind
1052 = (SrcMgr::CharacteristicKind)Record[Idx++];
1053 unsigned IncludeOffset = Record[Idx++];
1054 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1055 FileKind, IncludeOffset));
1057 LineTable.AddEntry(FileID::get(FID), Entries);
1063 /// \brief Read a source manager block
1064 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1065 using namespace SrcMgr;
1067 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1069 // Set the source-location entry cursor to the current position in
1070 // the stream. This cursor will be used to read the contents of the
1071 // source manager block initially, and then lazily read
1072 // source-location entries as needed.
1073 SLocEntryCursor = F.Stream;
1075 // The stream itself is going to skip over the source manager block.
1076 if (F.Stream.SkipBlock()) {
1077 Error("malformed block record in AST file");
1081 // Enter the source manager block.
1082 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1083 Error("malformed source manager block record in AST file");
1089 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1092 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1093 case llvm::BitstreamEntry::Error:
1094 Error("malformed block record in AST file");
1096 case llvm::BitstreamEntry::EndBlock:
1098 case llvm::BitstreamEntry::Record:
1099 // The interesting case.
1106 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1107 default: // Default behavior: ignore.
1110 case SM_SLOC_FILE_ENTRY:
1111 case SM_SLOC_BUFFER_ENTRY:
1112 case SM_SLOC_EXPANSION_ENTRY:
1113 // Once we hit one of the source location entries, we're done.
1119 /// \brief If a header file is not found at the path that we expect it to be
1120 /// and the PCH file was moved from its original location, try to resolve the
1121 /// file by assuming that header+PCH were moved together and the header is in
1122 /// the same place relative to the PCH.
1124 resolveFileRelativeToOriginalDir(const std::string &Filename,
1125 const std::string &OriginalDir,
1126 const std::string &CurrDir) {
1127 assert(OriginalDir != CurrDir &&
1128 "No point trying to resolve the file if the PCH dir didn't change");
1129 using namespace llvm::sys;
1130 SmallString<128> filePath(Filename);
1131 fs::make_absolute(filePath);
1132 assert(path::is_absolute(OriginalDir));
1133 SmallString<128> currPCHPath(CurrDir);
1135 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1136 fileDirE = path::end(path::parent_path(filePath));
1137 path::const_iterator origDirI = path::begin(OriginalDir),
1138 origDirE = path::end(OriginalDir);
1139 // Skip the common path components from filePath and OriginalDir.
1140 while (fileDirI != fileDirE && origDirI != origDirE &&
1141 *fileDirI == *origDirI) {
1145 for (; origDirI != origDirE; ++origDirI)
1146 path::append(currPCHPath, "..");
1147 path::append(currPCHPath, fileDirI, fileDirE);
1148 path::append(currPCHPath, path::filename(Filename));
1149 return currPCHPath.str();
1152 bool ASTReader::ReadSLocEntry(int ID) {
1156 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1157 Error("source location entry ID out-of-range for AST file");
1161 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1162 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1163 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1164 unsigned BaseOffset = F->SLocEntryBaseOffset;
1166 ++NumSLocEntriesRead;
1167 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1168 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1169 Error("incorrectly-formatted source location entry in AST file");
1175 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1177 Error("incorrectly-formatted source location entry in AST file");
1180 case SM_SLOC_FILE_ENTRY: {
1181 // We will detect whether a file changed and return 'Failure' for it, but
1182 // we will also try to fail gracefully by setting up the SLocEntry.
1183 unsigned InputID = Record[4];
1184 InputFile IF = getInputFile(*F, InputID);
1185 const FileEntry *File = IF.getFile();
1186 bool OverriddenBuffer = IF.isOverridden();
1188 // Note that we only check if a File was returned. If it was out-of-date
1189 // we have complained but we will continue creating a FileID to recover
1194 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1195 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1196 // This is the module's main file.
1197 IncludeLoc = getImportLocation(F);
1199 SrcMgr::CharacteristicKind
1200 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1201 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1202 ID, BaseOffset + Record[0]);
1203 SrcMgr::FileInfo &FileInfo =
1204 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1205 FileInfo.NumCreatedFIDs = Record[5];
1207 FileInfo.setHasLineDirectives();
1209 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1210 unsigned NumFileDecls = Record[7];
1212 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1213 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1217 const SrcMgr::ContentCache *ContentCache
1218 = SourceMgr.getOrCreateContentCache(File,
1219 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1220 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1221 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1222 unsigned Code = SLocEntryCursor.ReadCode();
1224 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1226 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1227 Error("AST record has invalid code");
1231 std::unique_ptr<llvm::MemoryBuffer> Buffer
1232 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
1233 SourceMgr.overrideFileContents(File, std::move(Buffer));
1239 case SM_SLOC_BUFFER_ENTRY: {
1240 const char *Name = Blob.data();
1241 unsigned Offset = Record[0];
1242 SrcMgr::CharacteristicKind
1243 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1244 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1245 if (IncludeLoc.isInvalid() &&
1246 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
1247 IncludeLoc = getImportLocation(F);
1249 unsigned Code = SLocEntryCursor.ReadCode();
1252 = SLocEntryCursor.readRecord(Code, Record, &Blob);
1254 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1255 Error("AST record has invalid code");
1259 std::unique_ptr<llvm::MemoryBuffer> Buffer =
1260 llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1261 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1262 BaseOffset + Offset, IncludeLoc);
1266 case SM_SLOC_EXPANSION_ENTRY: {
1267 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1268 SourceMgr.createExpansionLoc(SpellingLoc,
1269 ReadSourceLocation(*F, Record[2]),
1270 ReadSourceLocation(*F, Record[3]),
1273 BaseOffset + Record[0]);
1281 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1283 return std::make_pair(SourceLocation(), "");
1285 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1286 Error("source location entry ID out-of-range for AST file");
1287 return std::make_pair(SourceLocation(), "");
1290 // Find which module file this entry lands in.
1291 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1292 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
1293 return std::make_pair(SourceLocation(), "");
1295 // FIXME: Can we map this down to a particular submodule? That would be
1297 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1300 /// \brief Find the location where the module F is imported.
1301 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1302 if (F->ImportLoc.isValid())
1303 return F->ImportLoc;
1305 // Otherwise we have a PCH. It's considered to be "imported" at the first
1306 // location of its includer.
1307 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1308 // Main file is the importer.
1309 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1310 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1312 return F->ImportedBy[0]->FirstLoc;
1315 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1316 /// specified cursor. Read the abbreviations that are at the top of the block
1317 /// and then leave the cursor pointing into the block.
1318 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1319 if (Cursor.EnterSubBlock(BlockID)) {
1320 Error("malformed block record in AST file");
1325 uint64_t Offset = Cursor.GetCurrentBitNo();
1326 unsigned Code = Cursor.ReadCode();
1328 // We expect all abbrevs to be at the start of the block.
1329 if (Code != llvm::bitc::DEFINE_ABBREV) {
1330 Cursor.JumpToBit(Offset);
1333 Cursor.ReadAbbrevRecord();
1337 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1341 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1342 Tok.setLength(Record[Idx++]);
1343 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1344 Tok.setIdentifierInfo(II);
1345 Tok.setKind((tok::TokenKind)Record[Idx++]);
1346 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1350 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1351 BitstreamCursor &Stream = F.MacroCursor;
1353 // Keep track of where we are in the stream, then jump back there
1354 // after reading this macro.
1355 SavedStreamPosition SavedPosition(Stream);
1357 Stream.JumpToBit(Offset);
1359 SmallVector<IdentifierInfo*, 16> MacroArgs;
1360 MacroInfo *Macro = nullptr;
1363 // Advance to the next record, but if we get to the end of the block, don't
1364 // pop it (removing all the abbreviations from the cursor) since we want to
1365 // be able to reseek within the block and read entries.
1366 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1367 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1369 switch (Entry.Kind) {
1370 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1371 case llvm::BitstreamEntry::Error:
1372 Error("malformed block record in AST file");
1374 case llvm::BitstreamEntry::EndBlock:
1376 case llvm::BitstreamEntry::Record:
1377 // The interesting case.
1383 PreprocessorRecordTypes RecType =
1384 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1386 case PP_MODULE_MACRO:
1387 case PP_MACRO_DIRECTIVE_HISTORY:
1390 case PP_MACRO_OBJECT_LIKE:
1391 case PP_MACRO_FUNCTION_LIKE: {
1392 // If we already have a macro, that means that we've hit the end
1393 // of the definition of the macro we were looking for. We're
1398 unsigned NextIndex = 1; // Skip identifier ID.
1399 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1400 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1401 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1402 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1403 MI->setIsUsed(Record[NextIndex++]);
1404 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1406 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1407 // Decode function-like macro info.
1408 bool isC99VarArgs = Record[NextIndex++];
1409 bool isGNUVarArgs = Record[NextIndex++];
1410 bool hasCommaPasting = Record[NextIndex++];
1412 unsigned NumArgs = Record[NextIndex++];
1413 for (unsigned i = 0; i != NumArgs; ++i)
1414 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1416 // Install function-like macro info.
1417 MI->setIsFunctionLike();
1418 if (isC99VarArgs) MI->setIsC99Varargs();
1419 if (isGNUVarArgs) MI->setIsGNUVarargs();
1420 if (hasCommaPasting) MI->setHasCommaPasting();
1421 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1422 PP.getPreprocessorAllocator());
1425 // Remember that we saw this macro last so that we add the tokens that
1426 // form its body to it.
1429 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1430 Record[NextIndex]) {
1431 // We have a macro definition. Register the association
1432 PreprocessedEntityID
1433 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1434 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1435 PreprocessingRecord::PPEntityID PPID =
1436 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1437 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1438 PPRec.getPreprocessedEntity(PPID));
1440 PPRec.RegisterMacroDefinition(Macro, PPDef);
1448 // If we see a TOKEN before a PP_MACRO_*, then the file is
1449 // erroneous, just pretend we didn't see this.
1453 Token Tok = ReadToken(F, Record, Idx);
1454 Macro->AddTokenToBody(Tok);
1461 PreprocessedEntityID
1462 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1463 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1464 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1465 assert(I != M.PreprocessedEntityRemap.end()
1466 && "Invalid index into preprocessed entity index remap");
1468 return LocalID + I->second;
1471 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1472 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1475 HeaderFileInfoTrait::internal_key_type
1476 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1477 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1478 FE->getName(), /*Imported*/false };
1482 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1483 if (a.Size != b.Size || a.ModTime != b.ModTime)
1486 if (llvm::sys::path::is_absolute(a.Filename) &&
1487 strcmp(a.Filename, b.Filename) == 0)
1490 // Determine whether the actual files are equivalent.
1491 FileManager &FileMgr = Reader.getFileManager();
1492 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1494 return FileMgr.getFile(Key.Filename);
1496 std::string Resolved = Key.Filename;
1497 Reader.ResolveImportedPath(M, Resolved);
1498 return FileMgr.getFile(Resolved);
1501 const FileEntry *FEA = GetFile(a);
1502 const FileEntry *FEB = GetFile(b);
1503 return FEA && FEA == FEB;
1506 std::pair<unsigned, unsigned>
1507 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1508 using namespace llvm::support;
1509 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1510 unsigned DataLen = (unsigned) *d++;
1511 return std::make_pair(KeyLen, DataLen);
1514 HeaderFileInfoTrait::internal_key_type
1515 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1516 using namespace llvm::support;
1517 internal_key_type ikey;
1518 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1519 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1520 ikey.Filename = (const char *)d;
1521 ikey.Imported = true;
1525 HeaderFileInfoTrait::data_type
1526 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1528 const unsigned char *End = d + DataLen;
1529 using namespace llvm::support;
1531 unsigned Flags = *d++;
1532 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1533 ((Flags >> 6) & 0x03);
1534 HFI.isImport = (Flags >> 5) & 0x01;
1535 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1536 HFI.DirInfo = (Flags >> 2) & 0x03;
1537 HFI.Resolved = (Flags >> 1) & 0x01;
1538 HFI.IndexHeaderMapHeader = Flags & 0x01;
1539 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1540 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1541 M, endian::readNext<uint32_t, little, unaligned>(d));
1542 if (unsigned FrameworkOffset =
1543 endian::readNext<uint32_t, little, unaligned>(d)) {
1544 // The framework offset is 1 greater than the actual offset,
1545 // since 0 is used as an indicator for "no framework name".
1546 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1547 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1551 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1553 // This header is part of a module. Associate it with the module to enable
1554 // implicit module import.
1555 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1556 Module *Mod = Reader.getSubmodule(GlobalSMID);
1557 HFI.isModuleHeader = true;
1558 FileManager &FileMgr = Reader.getFileManager();
1560 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1561 // FIXME: This information should be propagated through the
1562 // SUBMODULE_HEADER etc records rather than from here.
1563 // FIXME: We don't ever mark excluded headers.
1564 std::string Filename = key.Filename;
1566 Reader.ResolveImportedPath(M, Filename);
1567 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1568 ModMap.addHeader(Mod, H, HFI.getHeaderRole());
1572 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1575 // This HeaderFileInfo was externally loaded.
1576 HFI.External = true;
1580 void ASTReader::addPendingMacro(IdentifierInfo *II,
1582 uint64_t MacroDirectivesOffset) {
1583 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1584 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1587 void ASTReader::ReadDefinedMacros() {
1588 // Note that we are loading defined macros.
1589 Deserializing Macros(this);
1591 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1592 E = ModuleMgr.rend(); I != E; ++I) {
1593 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1595 // If there was no preprocessor block, skip this file.
1596 if (!MacroCursor.getBitStreamReader())
1599 BitstreamCursor Cursor = MacroCursor;
1600 Cursor.JumpToBit((*I)->MacroStartOffset);
1604 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1607 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1608 case llvm::BitstreamEntry::Error:
1609 Error("malformed block record in AST file");
1611 case llvm::BitstreamEntry::EndBlock:
1614 case llvm::BitstreamEntry::Record:
1616 switch (Cursor.readRecord(E.ID, Record)) {
1617 default: // Default behavior: ignore.
1620 case PP_MACRO_OBJECT_LIKE:
1621 case PP_MACRO_FUNCTION_LIKE:
1622 getLocalIdentifier(**I, Record[0]);
1637 /// \brief Visitor class used to look up identifirs in an AST file.
1638 class IdentifierLookupVisitor {
1640 unsigned PriorGeneration;
1641 unsigned &NumIdentifierLookups;
1642 unsigned &NumIdentifierLookupHits;
1643 IdentifierInfo *Found;
1646 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1647 unsigned &NumIdentifierLookups,
1648 unsigned &NumIdentifierLookupHits)
1649 : Name(Name), PriorGeneration(PriorGeneration),
1650 NumIdentifierLookups(NumIdentifierLookups),
1651 NumIdentifierLookupHits(NumIdentifierLookupHits),
1656 static bool visit(ModuleFile &M, void *UserData) {
1657 IdentifierLookupVisitor *This
1658 = static_cast<IdentifierLookupVisitor *>(UserData);
1660 // If we've already searched this module file, skip it now.
1661 if (M.Generation <= This->PriorGeneration)
1664 ASTIdentifierLookupTable *IdTable
1665 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1669 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1671 ++This->NumIdentifierLookups;
1672 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1673 if (Pos == IdTable->end())
1676 // Dereferencing the iterator has the effect of building the
1677 // IdentifierInfo node and populating it with the various
1678 // declarations it needs.
1679 ++This->NumIdentifierLookupHits;
1684 // \brief Retrieve the identifier info found within the module
1686 IdentifierInfo *getIdentifierInfo() const { return Found; }
1690 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1691 // Note that we are loading an identifier.
1692 Deserializing AnIdentifier(this);
1694 unsigned PriorGeneration = 0;
1695 if (getContext().getLangOpts().Modules)
1696 PriorGeneration = IdentifierGeneration[&II];
1698 // If there is a global index, look there first to determine which modules
1699 // provably do not have any results for this identifier.
1700 GlobalModuleIndex::HitSet Hits;
1701 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1702 if (!loadGlobalIndex()) {
1703 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1708 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1709 NumIdentifierLookups,
1710 NumIdentifierLookupHits);
1711 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1712 markIdentifierUpToDate(&II);
1715 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1719 II->setOutOfDate(false);
1721 // Update the generation for this identifier.
1722 if (getContext().getLangOpts().Modules)
1723 IdentifierGeneration[II] = getGeneration();
1726 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1727 const PendingMacroInfo &PMInfo) {
1728 ModuleFile &M = *PMInfo.M;
1730 BitstreamCursor &Cursor = M.MacroCursor;
1731 SavedStreamPosition SavedPosition(Cursor);
1732 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1734 struct ModuleMacroRecord {
1735 SubmoduleID SubModID;
1737 SmallVector<SubmoduleID, 8> Overrides;
1739 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
1741 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1742 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1746 llvm::BitstreamEntry Entry =
1747 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1748 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1749 Error("malformed block record in AST file");
1754 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1755 case PP_MACRO_DIRECTIVE_HISTORY:
1758 case PP_MODULE_MACRO: {
1759 ModuleMacros.push_back(ModuleMacroRecord());
1760 auto &Info = ModuleMacros.back();
1761 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1762 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1763 for (int I = 2, N = Record.size(); I != N; ++I)
1764 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1769 Error("malformed block record in AST file");
1773 // We found the macro directive history; that's the last record
1778 // Module macros are listed in reverse dependency order.
1780 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
1781 llvm::SmallVector<ModuleMacro*, 8> Overrides;
1782 for (auto &MMR : ModuleMacros) {
1784 for (unsigned ModID : MMR.Overrides) {
1785 Module *Mod = getSubmodule(ModID);
1786 auto *Macro = PP.getModuleMacro(Mod, II);
1787 assert(Macro && "missing definition for overridden macro");
1788 Overrides.push_back(Macro);
1791 bool Inserted = false;
1792 Module *Owner = getSubmodule(MMR.SubModID);
1793 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
1797 // Don't read the directive history for a module; we don't have anywhere
1799 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1802 // Deserialize the macro directives history in reverse source-order.
1803 MacroDirective *Latest = nullptr, *Earliest = nullptr;
1804 unsigned Idx = 0, N = Record.size();
1806 MacroDirective *MD = nullptr;
1807 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1808 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1810 case MacroDirective::MD_Define: {
1811 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
1812 MD = PP.AllocateDefMacroDirective(MI, Loc);
1815 case MacroDirective::MD_Undefine: {
1816 MD = PP.AllocateUndefMacroDirective(Loc);
1819 case MacroDirective::MD_Visibility:
1820 bool isPublic = Record[Idx++];
1821 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1828 Earliest->setPrevious(MD);
1833 PP.setLoadedMacroDirective(II, Latest);
1836 ASTReader::InputFileInfo
1837 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
1838 // Go find this input file.
1839 BitstreamCursor &Cursor = F.InputFilesCursor;
1840 SavedStreamPosition SavedPosition(Cursor);
1841 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1843 unsigned Code = Cursor.ReadCode();
1847 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1848 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1849 "invalid record type for input file");
1852 std::string Filename;
1857 assert(Record[0] == ID && "Bogus stored ID or offset");
1858 StoredSize = static_cast<off_t>(Record[1]);
1859 StoredTime = static_cast<time_t>(Record[2]);
1860 Overridden = static_cast<bool>(Record[3]);
1862 ResolveImportedPath(F, Filename);
1864 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
1868 std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
1869 return readInputFileInfo(F, ID).Filename;
1872 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1873 // If this ID is bogus, just return an empty input file.
1874 if (ID == 0 || ID > F.InputFilesLoaded.size())
1877 // If we've already loaded this input file, return it.
1878 if (F.InputFilesLoaded[ID-1].getFile())
1879 return F.InputFilesLoaded[ID-1];
1881 if (F.InputFilesLoaded[ID-1].isNotFound())
1884 // Go find this input file.
1885 BitstreamCursor &Cursor = F.InputFilesCursor;
1886 SavedStreamPosition SavedPosition(Cursor);
1887 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1889 InputFileInfo FI = readInputFileInfo(F, ID);
1890 off_t StoredSize = FI.StoredSize;
1891 time_t StoredTime = FI.StoredTime;
1892 bool Overridden = FI.Overridden;
1893 StringRef Filename = FI.Filename;
1895 const FileEntry *File
1896 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1897 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1899 // If we didn't find the file, resolve it relative to the
1900 // original directory from which this AST file was created.
1901 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1902 F.OriginalDir != CurrentDir) {
1903 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1906 if (!Resolved.empty())
1907 File = FileMgr.getFile(Resolved);
1910 // For an overridden file, create a virtual file with the stored
1912 if (Overridden && File == nullptr) {
1913 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1916 if (File == nullptr) {
1918 std::string ErrorStr = "could not find file '";
1919 ErrorStr += Filename;
1920 ErrorStr += "' referenced by AST file";
1921 Error(ErrorStr.c_str());
1923 // Record that we didn't find the file.
1924 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1928 // Check if there was a request to override the contents of the file
1929 // that was part of the precompiled header. Overridding such a file
1930 // can lead to problems when lexing using the source locations from the
1932 SourceManager &SM = getSourceManager();
1933 if (!Overridden && SM.isFileOverridden(File)) {
1935 Error(diag::err_fe_pch_file_overridden, Filename);
1936 // After emitting the diagnostic, recover by disabling the override so
1937 // that the original file will be used.
1938 SM.disableFileContentsOverride(File);
1939 // The FileEntry is a virtual file entry with the size of the contents
1940 // that would override the original contents. Set it to the original's
1942 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1943 StoredSize, StoredTime);
1946 bool IsOutOfDate = false;
1948 // For an overridden file, there is nothing to validate.
1949 if (!Overridden && //
1950 (StoredSize != File->getSize() ||
1951 #if defined(LLVM_ON_WIN32)
1954 // In our regression testing, the Windows file system seems to
1955 // have inconsistent modification times that sometimes
1956 // erroneously trigger this error-handling path.
1958 // This also happens in networked file systems, so disable this
1959 // check if validation is disabled or if we have an explicitly
1962 // FIXME: Should we also do this for PCH files? They could also
1963 // reasonably get shared across a network during a distributed build.
1964 (StoredTime != File->getModificationTime() && !DisableValidation &&
1965 F.Kind != MK_ExplicitModule)
1969 // Build a list of the PCH imports that got us here (in reverse).
1970 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
1971 while (ImportStack.back()->ImportedBy.size() > 0)
1972 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
1974 // The top-level PCH is stale.
1975 StringRef TopLevelPCHName(ImportStack.back()->FileName);
1976 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
1978 // Print the import stack.
1979 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
1980 Diag(diag::note_pch_required_by)
1981 << Filename << ImportStack[0]->FileName;
1982 for (unsigned I = 1; I < ImportStack.size(); ++I)
1983 Diag(diag::note_pch_required_by)
1984 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
1987 if (!Diags.isDiagnosticInFlight())
1988 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
1994 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1996 // Note that we've loaded this input file.
1997 F.InputFilesLoaded[ID-1] = IF;
2001 /// \brief If we are loading a relocatable PCH or module file, and the filename
2002 /// is not an absolute path, add the system or module root to the beginning of
2004 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2005 // Resolve relative to the base directory, if we have one.
2006 if (!M.BaseDirectory.empty())
2007 return ResolveImportedPath(Filename, M.BaseDirectory);
2010 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2011 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2014 SmallString<128> Buffer;
2015 llvm::sys::path::append(Buffer, Prefix, Filename);
2016 Filename.assign(Buffer.begin(), Buffer.end());
2019 ASTReader::ASTReadResult
2020 ASTReader::ReadControlBlock(ModuleFile &F,
2021 SmallVectorImpl<ImportedModule> &Loaded,
2022 const ModuleFile *ImportedBy,
2023 unsigned ClientLoadCapabilities) {
2024 BitstreamCursor &Stream = F.Stream;
2026 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2027 Error("malformed block record in AST file");
2031 // Should we allow the configuration of the module file to differ from the
2032 // configuration of the current translation unit in a compatible way?
2034 // FIXME: Allow this for files explicitly specified with -include-pch too.
2035 bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2037 // Read all of the records and blocks in the control block.
2039 unsigned NumInputs = 0;
2040 unsigned NumUserInputs = 0;
2042 llvm::BitstreamEntry Entry = Stream.advance();
2044 switch (Entry.Kind) {
2045 case llvm::BitstreamEntry::Error:
2046 Error("malformed block record in AST file");
2048 case llvm::BitstreamEntry::EndBlock: {
2049 // Validate input files.
2050 const HeaderSearchOptions &HSOpts =
2051 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2053 // All user input files reside at the index range [0, NumUserInputs), and
2054 // system input files reside at [NumUserInputs, NumInputs).
2055 if (!DisableValidation) {
2056 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2058 // If we are reading a module, we will create a verification timestamp,
2059 // so we verify all input files. Otherwise, verify only user input
2062 unsigned N = NumUserInputs;
2063 if (ValidateSystemInputs ||
2064 (HSOpts.ModulesValidateOncePerBuildSession &&
2065 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2066 F.Kind == MK_ImplicitModule))
2069 for (unsigned I = 0; I < N; ++I) {
2070 InputFile IF = getInputFile(F, I+1, Complain);
2071 if (!IF.getFile() || IF.isOutOfDate())
2077 Listener->visitModuleFile(F.FileName);
2079 if (Listener && Listener->needsInputFileVisitation()) {
2080 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2082 for (unsigned I = 0; I < N; ++I) {
2083 bool IsSystem = I >= NumUserInputs;
2084 InputFileInfo FI = readInputFileInfo(F, I+1);
2085 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2092 case llvm::BitstreamEntry::SubBlock:
2094 case INPUT_FILES_BLOCK_ID:
2095 F.InputFilesCursor = Stream;
2096 if (Stream.SkipBlock() || // Skip with the main cursor
2097 // Read the abbreviations
2098 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2099 Error("malformed block record in AST file");
2105 if (Stream.SkipBlock()) {
2106 Error("malformed block record in AST file");
2112 case llvm::BitstreamEntry::Record:
2113 // The interesting case.
2117 // Read and process a record.
2120 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2122 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2123 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2124 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2125 : diag::err_pch_version_too_new);
2126 return VersionMismatch;
2129 bool hasErrors = Record[5];
2130 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2131 Diag(diag::err_pch_with_compiler_errors);
2135 F.RelocatablePCH = Record[4];
2136 // Relative paths in a relocatable PCH are relative to our sysroot.
2137 if (F.RelocatablePCH)
2138 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2140 const std::string &CurBranch = getClangFullRepositoryVersion();
2141 StringRef ASTBranch = Blob;
2142 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2143 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2144 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2145 return VersionMismatch;
2151 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2152 F.Signature = Record[0];
2156 // Load each of the imported PCH files.
2157 unsigned Idx = 0, N = Record.size();
2159 // Read information about the AST file.
2160 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2161 // The import location will be the local one for now; we will adjust
2162 // all import locations of module imports after the global source
2163 // location info are setup.
2164 SourceLocation ImportLoc =
2165 SourceLocation::getFromRawEncoding(Record[Idx++]);
2166 off_t StoredSize = (off_t)Record[Idx++];
2167 time_t StoredModTime = (time_t)Record[Idx++];
2168 ASTFileSignature StoredSignature = Record[Idx++];
2169 auto ImportedFile = ReadPath(F, Record, Idx);
2171 // Load the AST file.
2172 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
2173 StoredSize, StoredModTime, StoredSignature,
2174 ClientLoadCapabilities)) {
2175 case Failure: return Failure;
2176 // If we have to ignore the dependency, we'll have to ignore this too.
2178 case OutOfDate: return OutOfDate;
2179 case VersionMismatch: return VersionMismatch;
2180 case ConfigurationMismatch: return ConfigurationMismatch;
2181 case HadErrors: return HadErrors;
2182 case Success: break;
2188 case KNOWN_MODULE_FILES:
2191 case LANGUAGE_OPTIONS: {
2192 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2193 // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
2194 if (Listener && &F == *ModuleMgr.begin() &&
2195 ParseLanguageOptions(Record, Complain, *Listener,
2196 AllowCompatibleConfigurationMismatch) &&
2197 !DisableValidation && !AllowConfigurationMismatch)
2198 return ConfigurationMismatch;
2202 case TARGET_OPTIONS: {
2203 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2204 if (Listener && &F == *ModuleMgr.begin() &&
2205 ParseTargetOptions(Record, Complain, *Listener,
2206 AllowCompatibleConfigurationMismatch) &&
2207 !DisableValidation && !AllowConfigurationMismatch)
2208 return ConfigurationMismatch;
2212 case DIAGNOSTIC_OPTIONS: {
2213 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
2214 if (Listener && &F == *ModuleMgr.begin() &&
2215 !AllowCompatibleConfigurationMismatch &&
2216 ParseDiagnosticOptions(Record, Complain, *Listener) &&
2222 case FILE_SYSTEM_OPTIONS: {
2223 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2224 if (Listener && &F == *ModuleMgr.begin() &&
2225 !AllowCompatibleConfigurationMismatch &&
2226 ParseFileSystemOptions(Record, Complain, *Listener) &&
2227 !DisableValidation && !AllowConfigurationMismatch)
2228 return ConfigurationMismatch;
2232 case HEADER_SEARCH_OPTIONS: {
2233 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2234 if (Listener && &F == *ModuleMgr.begin() &&
2235 !AllowCompatibleConfigurationMismatch &&
2236 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2237 !DisableValidation && !AllowConfigurationMismatch)
2238 return ConfigurationMismatch;
2242 case PREPROCESSOR_OPTIONS: {
2243 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2244 if (Listener && &F == *ModuleMgr.begin() &&
2245 !AllowCompatibleConfigurationMismatch &&
2246 ParsePreprocessorOptions(Record, Complain, *Listener,
2247 SuggestedPredefines) &&
2248 !DisableValidation && !AllowConfigurationMismatch)
2249 return ConfigurationMismatch;
2254 F.OriginalSourceFileID = FileID::get(Record[0]);
2255 F.ActualOriginalSourceFileName = Blob;
2256 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2257 ResolveImportedPath(F, F.OriginalSourceFileName);
2260 case ORIGINAL_FILE_ID:
2261 F.OriginalSourceFileID = FileID::get(Record[0]);
2264 case ORIGINAL_PCH_DIR:
2265 F.OriginalDir = Blob;
2269 F.ModuleName = Blob;
2271 Listener->ReadModuleName(F.ModuleName);
2274 case MODULE_DIRECTORY: {
2275 assert(!F.ModuleName.empty() &&
2276 "MODULE_DIRECTORY found before MODULE_NAME");
2277 // If we've already loaded a module map file covering this module, we may
2278 // have a better path for it (relative to the current build).
2279 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2280 if (M && M->Directory) {
2281 // If we're implicitly loading a module, the base directory can't
2282 // change between the build and use.
2283 if (F.Kind != MK_ExplicitModule) {
2284 const DirectoryEntry *BuildDir =
2285 PP.getFileManager().getDirectory(Blob);
2286 if (!BuildDir || BuildDir != M->Directory) {
2287 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2288 Diag(diag::err_imported_module_relocated)
2289 << F.ModuleName << Blob << M->Directory->getName();
2293 F.BaseDirectory = M->Directory->getName();
2295 F.BaseDirectory = Blob;
2300 case MODULE_MAP_FILE:
2301 if (ASTReadResult Result =
2302 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2306 case INPUT_FILE_OFFSETS:
2307 NumInputs = Record[0];
2308 NumUserInputs = Record[1];
2309 F.InputFileOffsets = (const uint64_t *)Blob.data();
2310 F.InputFilesLoaded.resize(NumInputs);
2316 ASTReader::ASTReadResult
2317 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2318 BitstreamCursor &Stream = F.Stream;
2320 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2321 Error("malformed block record in AST file");
2325 // Read all of the records and blocks for the AST file.
2328 llvm::BitstreamEntry Entry = Stream.advance();
2330 switch (Entry.Kind) {
2331 case llvm::BitstreamEntry::Error:
2332 Error("error at end of module block in AST file");
2334 case llvm::BitstreamEntry::EndBlock: {
2335 // Outside of C++, we do not store a lookup map for the translation unit.
2336 // Instead, mark it as needing a lookup map to be built if this module
2337 // contains any declarations lexically within it (which it always does!).
2338 // This usually has no cost, since we very rarely need the lookup map for
2339 // the translation unit outside C++.
2340 DeclContext *DC = Context.getTranslationUnitDecl();
2341 if (DC->hasExternalLexicalStorage() &&
2342 !getContext().getLangOpts().CPlusPlus)
2343 DC->setMustBuildLookupTable();
2347 case llvm::BitstreamEntry::SubBlock:
2349 case DECLTYPES_BLOCK_ID:
2350 // We lazily load the decls block, but we want to set up the
2351 // DeclsCursor cursor to point into it. Clone our current bitcode
2352 // cursor to it, enter the block and read the abbrevs in that block.
2353 // With the main cursor, we just skip over it.
2354 F.DeclsCursor = Stream;
2355 if (Stream.SkipBlock() || // Skip with the main cursor.
2356 // Read the abbrevs.
2357 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2358 Error("malformed block record in AST file");
2363 case PREPROCESSOR_BLOCK_ID:
2364 F.MacroCursor = Stream;
2365 if (!PP.getExternalSource())
2366 PP.setExternalSource(this);
2368 if (Stream.SkipBlock() ||
2369 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2370 Error("malformed block record in AST file");
2373 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2376 case PREPROCESSOR_DETAIL_BLOCK_ID:
2377 F.PreprocessorDetailCursor = Stream;
2378 if (Stream.SkipBlock() ||
2379 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2380 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2381 Error("malformed preprocessor detail record in AST file");
2384 F.PreprocessorDetailStartOffset
2385 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2387 if (!PP.getPreprocessingRecord())
2388 PP.createPreprocessingRecord();
2389 if (!PP.getPreprocessingRecord()->getExternalSource())
2390 PP.getPreprocessingRecord()->SetExternalSource(*this);
2393 case SOURCE_MANAGER_BLOCK_ID:
2394 if (ReadSourceManagerBlock(F))
2398 case SUBMODULE_BLOCK_ID:
2399 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2403 case COMMENTS_BLOCK_ID: {
2404 BitstreamCursor C = Stream;
2405 if (Stream.SkipBlock() ||
2406 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2407 Error("malformed comments block in AST file");
2410 CommentsCursors.push_back(std::make_pair(C, &F));
2415 if (Stream.SkipBlock()) {
2416 Error("malformed block record in AST file");
2423 case llvm::BitstreamEntry::Record:
2424 // The interesting case.
2428 // Read and process a record.
2431 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2432 default: // Default behavior: ignore.
2436 if (F.LocalNumTypes != 0) {
2437 Error("duplicate TYPE_OFFSET record in AST file");
2440 F.TypeOffsets = (const uint32_t *)Blob.data();
2441 F.LocalNumTypes = Record[0];
2442 unsigned LocalBaseTypeIndex = Record[1];
2443 F.BaseTypeIndex = getTotalNumTypes();
2445 if (F.LocalNumTypes > 0) {
2446 // Introduce the global -> local mapping for types within this module.
2447 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2449 // Introduce the local -> global mapping for types within this module.
2450 F.TypeRemap.insertOrReplace(
2451 std::make_pair(LocalBaseTypeIndex,
2452 F.BaseTypeIndex - LocalBaseTypeIndex));
2454 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2460 if (F.LocalNumDecls != 0) {
2461 Error("duplicate DECL_OFFSET record in AST file");
2464 F.DeclOffsets = (const DeclOffset *)Blob.data();
2465 F.LocalNumDecls = Record[0];
2466 unsigned LocalBaseDeclID = Record[1];
2467 F.BaseDeclID = getTotalNumDecls();
2469 if (F.LocalNumDecls > 0) {
2470 // Introduce the global -> local mapping for declarations within this
2472 GlobalDeclMap.insert(
2473 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2475 // Introduce the local -> global mapping for declarations within this
2477 F.DeclRemap.insertOrReplace(
2478 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2480 // Introduce the global -> local mapping for declarations within this
2482 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2484 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2489 case TU_UPDATE_LEXICAL: {
2490 DeclContext *TU = Context.getTranslationUnitDecl();
2491 DeclContextInfo &Info = F.DeclContextInfos[TU];
2492 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2493 Info.NumLexicalDecls
2494 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2495 TU->setHasExternalLexicalStorage(true);
2499 case UPDATE_VISIBLE: {
2501 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2502 ASTDeclContextNameLookupTable *Table =
2503 ASTDeclContextNameLookupTable::Create(
2504 (const unsigned char *)Blob.data() + Record[Idx++],
2505 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2506 (const unsigned char *)Blob.data(),
2507 ASTDeclContextNameLookupTrait(*this, F));
2508 if (Decl *D = GetExistingDecl(ID)) {
2509 auto *DC = cast<DeclContext>(D);
2510 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2511 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2513 LookupTable = Table;
2515 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2519 case IDENTIFIER_TABLE:
2520 F.IdentifierTableData = Blob.data();
2522 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2523 (const unsigned char *)F.IdentifierTableData + Record[0],
2524 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2525 (const unsigned char *)F.IdentifierTableData,
2526 ASTIdentifierLookupTrait(*this, F));
2528 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2532 case IDENTIFIER_OFFSET: {
2533 if (F.LocalNumIdentifiers != 0) {
2534 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2537 F.IdentifierOffsets = (const uint32_t *)Blob.data();
2538 F.LocalNumIdentifiers = Record[0];
2539 unsigned LocalBaseIdentifierID = Record[1];
2540 F.BaseIdentifierID = getTotalNumIdentifiers();
2542 if (F.LocalNumIdentifiers > 0) {
2543 // Introduce the global -> local mapping for identifiers within this
2545 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2548 // Introduce the local -> global mapping for identifiers within this
2550 F.IdentifierRemap.insertOrReplace(
2551 std::make_pair(LocalBaseIdentifierID,
2552 F.BaseIdentifierID - LocalBaseIdentifierID));
2554 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2555 + F.LocalNumIdentifiers);
2560 case EAGERLY_DESERIALIZED_DECLS:
2561 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2562 // about "interesting" decls (for instance, if we're building a module).
2563 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2564 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2568 if (SpecialTypes.empty()) {
2569 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2570 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2574 if (SpecialTypes.size() != Record.size()) {
2575 Error("invalid special-types record");
2579 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2580 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2581 if (!SpecialTypes[I])
2582 SpecialTypes[I] = ID;
2583 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2589 TotalNumStatements += Record[0];
2590 TotalNumMacros += Record[1];
2591 TotalLexicalDeclContexts += Record[2];
2592 TotalVisibleDeclContexts += Record[3];
2595 case UNUSED_FILESCOPED_DECLS:
2596 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2597 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2600 case DELEGATING_CTORS:
2601 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2602 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2605 case WEAK_UNDECLARED_IDENTIFIERS:
2606 if (Record.size() % 4 != 0) {
2607 Error("invalid weak identifiers record");
2611 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2612 // files. This isn't the way to do it :)
2613 WeakUndeclaredIdentifiers.clear();
2615 // Translate the weak, undeclared identifiers into global IDs.
2616 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2617 WeakUndeclaredIdentifiers.push_back(
2618 getGlobalIdentifierID(F, Record[I++]));
2619 WeakUndeclaredIdentifiers.push_back(
2620 getGlobalIdentifierID(F, Record[I++]));
2621 WeakUndeclaredIdentifiers.push_back(
2622 ReadSourceLocation(F, Record, I).getRawEncoding());
2623 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2627 case SELECTOR_OFFSETS: {
2628 F.SelectorOffsets = (const uint32_t *)Blob.data();
2629 F.LocalNumSelectors = Record[0];
2630 unsigned LocalBaseSelectorID = Record[1];
2631 F.BaseSelectorID = getTotalNumSelectors();
2633 if (F.LocalNumSelectors > 0) {
2634 // Introduce the global -> local mapping for selectors within this
2636 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2638 // Introduce the local -> global mapping for selectors within this
2640 F.SelectorRemap.insertOrReplace(
2641 std::make_pair(LocalBaseSelectorID,
2642 F.BaseSelectorID - LocalBaseSelectorID));
2644 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2650 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2652 F.SelectorLookupTable
2653 = ASTSelectorLookupTable::Create(
2654 F.SelectorLookupTableData + Record[0],
2655 F.SelectorLookupTableData,
2656 ASTSelectorLookupTrait(*this, F));
2657 TotalNumMethodPoolEntries += Record[1];
2660 case REFERENCED_SELECTOR_POOL:
2661 if (!Record.empty()) {
2662 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2663 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2665 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2671 case PP_COUNTER_VALUE:
2672 if (!Record.empty() && Listener)
2673 Listener->ReadCounter(F, Record[0]);
2676 case FILE_SORTED_DECLS:
2677 F.FileSortedDecls = (const DeclID *)Blob.data();
2678 F.NumFileSortedDecls = Record[0];
2681 case SOURCE_LOCATION_OFFSETS: {
2682 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2683 F.LocalNumSLocEntries = Record[0];
2684 unsigned SLocSpaceSize = Record[1];
2685 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2686 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2688 // Make our entry in the range map. BaseID is negative and growing, so
2689 // we invert it. Because we invert it, though, we need the other end of
2691 unsigned RangeStart =
2692 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2693 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2694 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2696 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2697 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2698 GlobalSLocOffsetMap.insert(
2699 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2700 - SLocSpaceSize,&F));
2702 // Initialize the remapping table.
2703 // Invalid stays invalid.
2704 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
2705 // This module. Base was 2 when being compiled.
2706 F.SLocRemap.insertOrReplace(std::make_pair(2U,
2707 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2709 TotalNumSLocEntries += F.LocalNumSLocEntries;
2713 case MODULE_OFFSET_MAP: {
2714 // Additional remapping information.
2715 const unsigned char *Data = (const unsigned char*)Blob.data();
2716 const unsigned char *DataEnd = Data + Blob.size();
2718 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2719 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2720 F.SLocRemap.insert(std::make_pair(0U, 0));
2721 F.SLocRemap.insert(std::make_pair(2U, 1));
2724 // Continuous range maps we may be updating in our module.
2725 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2727 RemapBuilder SLocRemap(F.SLocRemap);
2728 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2729 RemapBuilder MacroRemap(F.MacroRemap);
2730 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2731 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2732 RemapBuilder SelectorRemap(F.SelectorRemap);
2733 RemapBuilder DeclRemap(F.DeclRemap);
2734 RemapBuilder TypeRemap(F.TypeRemap);
2736 while(Data < DataEnd) {
2737 using namespace llvm::support;
2738 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
2739 StringRef Name = StringRef((const char*)Data, Len);
2741 ModuleFile *OM = ModuleMgr.lookup(Name);
2743 Error("SourceLocation remap refers to unknown module");
2747 uint32_t SLocOffset =
2748 endian::readNext<uint32_t, little, unaligned>(Data);
2749 uint32_t IdentifierIDOffset =
2750 endian::readNext<uint32_t, little, unaligned>(Data);
2751 uint32_t MacroIDOffset =
2752 endian::readNext<uint32_t, little, unaligned>(Data);
2753 uint32_t PreprocessedEntityIDOffset =
2754 endian::readNext<uint32_t, little, unaligned>(Data);
2755 uint32_t SubmoduleIDOffset =
2756 endian::readNext<uint32_t, little, unaligned>(Data);
2757 uint32_t SelectorIDOffset =
2758 endian::readNext<uint32_t, little, unaligned>(Data);
2759 uint32_t DeclIDOffset =
2760 endian::readNext<uint32_t, little, unaligned>(Data);
2761 uint32_t TypeIndexOffset =
2762 endian::readNext<uint32_t, little, unaligned>(Data);
2764 uint32_t None = std::numeric_limits<uint32_t>::max();
2766 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2767 RemapBuilder &Remap) {
2769 Remap.insert(std::make_pair(Offset,
2770 static_cast<int>(BaseOffset - Offset)));
2772 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2773 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2774 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2775 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2776 PreprocessedEntityRemap);
2777 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2778 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2779 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2780 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
2782 // Global -> local mappings.
2783 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2788 case SOURCE_MANAGER_LINE_TABLE:
2789 if (ParseLineTable(F, Record))
2793 case SOURCE_LOCATION_PRELOADS: {
2794 // Need to transform from the local view (1-based IDs) to the global view,
2795 // which is based off F.SLocEntryBaseID.
2796 if (!F.PreloadSLocEntries.empty()) {
2797 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2801 F.PreloadSLocEntries.swap(Record);
2805 case EXT_VECTOR_DECLS:
2806 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2807 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2811 if (Record.size() % 3 != 0) {
2812 Error("Invalid VTABLE_USES record");
2816 // Later tables overwrite earlier ones.
2817 // FIXME: Modules will have some trouble with this. This is clearly not
2818 // the right way to do this.
2821 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2822 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2823 VTableUses.push_back(
2824 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2825 VTableUses.push_back(Record[Idx++]);
2829 case PENDING_IMPLICIT_INSTANTIATIONS:
2830 if (PendingInstantiations.size() % 2 != 0) {
2831 Error("Invalid existing PendingInstantiations");
2835 if (Record.size() % 2 != 0) {
2836 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2840 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2841 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2842 PendingInstantiations.push_back(
2843 ReadSourceLocation(F, Record, I).getRawEncoding());
2847 case SEMA_DECL_REFS:
2848 if (Record.size() != 2) {
2849 Error("Invalid SEMA_DECL_REFS block");
2852 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2853 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2856 case PPD_ENTITIES_OFFSETS: {
2857 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2858 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2859 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
2861 unsigned LocalBasePreprocessedEntityID = Record[0];
2863 unsigned StartingID;
2864 if (!PP.getPreprocessingRecord())
2865 PP.createPreprocessingRecord();
2866 if (!PP.getPreprocessingRecord()->getExternalSource())
2867 PP.getPreprocessingRecord()->SetExternalSource(*this);
2869 = PP.getPreprocessingRecord()
2870 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2871 F.BasePreprocessedEntityID = StartingID;
2873 if (F.NumPreprocessedEntities > 0) {
2874 // Introduce the global -> local mapping for preprocessed entities in
2876 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2878 // Introduce the local -> global mapping for preprocessed entities in
2880 F.PreprocessedEntityRemap.insertOrReplace(
2881 std::make_pair(LocalBasePreprocessedEntityID,
2882 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2888 case DECL_UPDATE_OFFSETS: {
2889 if (Record.size() % 2 != 0) {
2890 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2893 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
2894 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
2895 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
2897 // If we've already loaded the decl, perform the updates when we finish
2898 // loading this block.
2899 if (Decl *D = GetExistingDecl(ID))
2900 PendingUpdateRecords.push_back(std::make_pair(ID, D));
2905 case DECL_REPLACEMENTS: {
2906 if (Record.size() % 3 != 0) {
2907 Error("invalid DECL_REPLACEMENTS block in AST file");
2910 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2911 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2912 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2916 case OBJC_CATEGORIES_MAP: {
2917 if (F.LocalNumObjCCategoriesInMap != 0) {
2918 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2922 F.LocalNumObjCCategoriesInMap = Record[0];
2923 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
2927 case OBJC_CATEGORIES:
2928 F.ObjCCategories.swap(Record);
2931 case CXX_BASE_SPECIFIER_OFFSETS: {
2932 if (F.LocalNumCXXBaseSpecifiers != 0) {
2933 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2937 F.LocalNumCXXBaseSpecifiers = Record[0];
2938 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
2942 case CXX_CTOR_INITIALIZERS_OFFSETS: {
2943 if (F.LocalNumCXXCtorInitializers != 0) {
2944 Error("duplicate CXX_CTOR_INITIALIZERS_OFFSETS record in AST file");
2948 F.LocalNumCXXCtorInitializers = Record[0];
2949 F.CXXCtorInitializersOffsets = (const uint32_t *)Blob.data();
2953 case DIAG_PRAGMA_MAPPINGS:
2954 if (F.PragmaDiagMappings.empty())
2955 F.PragmaDiagMappings.swap(Record);
2957 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2958 Record.begin(), Record.end());
2961 case CUDA_SPECIAL_DECL_REFS:
2962 // Later tables overwrite earlier ones.
2963 // FIXME: Modules will have trouble with this.
2964 CUDASpecialDeclRefs.clear();
2965 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2966 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2969 case HEADER_SEARCH_TABLE: {
2970 F.HeaderFileInfoTableData = Blob.data();
2971 F.LocalNumHeaderFileInfos = Record[1];
2973 F.HeaderFileInfoTable
2974 = HeaderFileInfoLookupTable::Create(
2975 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2976 (const unsigned char *)F.HeaderFileInfoTableData,
2977 HeaderFileInfoTrait(*this, F,
2978 &PP.getHeaderSearchInfo(),
2979 Blob.data() + Record[2]));
2981 PP.getHeaderSearchInfo().SetExternalSource(this);
2982 if (!PP.getHeaderSearchInfo().getExternalLookup())
2983 PP.getHeaderSearchInfo().SetExternalLookup(this);
2988 case FP_PRAGMA_OPTIONS:
2989 // Later tables overwrite earlier ones.
2990 FPPragmaOptions.swap(Record);
2993 case OPENCL_EXTENSIONS:
2994 // Later tables overwrite earlier ones.
2995 OpenCLExtensions.swap(Record);
2998 case TENTATIVE_DEFINITIONS:
2999 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3000 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3003 case KNOWN_NAMESPACES:
3004 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3005 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3008 case UNDEFINED_BUT_USED:
3009 if (UndefinedButUsed.size() % 2 != 0) {
3010 Error("Invalid existing UndefinedButUsed");
3014 if (Record.size() % 2 != 0) {
3015 Error("invalid undefined-but-used record");
3018 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3019 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3020 UndefinedButUsed.push_back(
3021 ReadSourceLocation(F, Record, I).getRawEncoding());
3024 case DELETE_EXPRS_TO_ANALYZE:
3025 for (unsigned I = 0, N = Record.size(); I != N;) {
3026 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3027 const uint64_t Count = Record[I++];
3028 DelayedDeleteExprs.push_back(Count);
3029 for (uint64_t C = 0; C < Count; ++C) {
3030 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3031 bool IsArrayForm = Record[I++] == 1;
3032 DelayedDeleteExprs.push_back(IsArrayForm);
3037 case IMPORTED_MODULES: {
3038 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
3039 // If we aren't loading a module (which has its own exports), make
3040 // all of the imported modules visible.
3041 // FIXME: Deal with macros-only imports.
3042 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3043 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3044 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3046 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3052 case LOCAL_REDECLARATIONS: {
3053 F.RedeclarationChains.swap(Record);
3057 case LOCAL_REDECLARATIONS_MAP: {
3058 if (F.LocalNumRedeclarationsInMap != 0) {
3059 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3063 F.LocalNumRedeclarationsInMap = Record[0];
3064 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
3068 case MACRO_OFFSET: {
3069 if (F.LocalNumMacros != 0) {
3070 Error("duplicate MACRO_OFFSET record in AST file");
3073 F.MacroOffsets = (const uint32_t *)Blob.data();
3074 F.LocalNumMacros = Record[0];
3075 unsigned LocalBaseMacroID = Record[1];
3076 F.BaseMacroID = getTotalNumMacros();
3078 if (F.LocalNumMacros > 0) {
3079 // Introduce the global -> local mapping for macros within this module.
3080 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3082 // Introduce the local -> global mapping for macros within this module.
3083 F.MacroRemap.insertOrReplace(
3084 std::make_pair(LocalBaseMacroID,
3085 F.BaseMacroID - LocalBaseMacroID));
3087 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3092 case LATE_PARSED_TEMPLATE: {
3093 LateParsedTemplates.append(Record.begin(), Record.end());
3097 case OPTIMIZE_PRAGMA_OPTIONS:
3098 if (Record.size() != 1) {
3099 Error("invalid pragma optimize record");
3102 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3105 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3106 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3107 UnusedLocalTypedefNameCandidates.push_back(
3108 getGlobalDeclID(F, Record[I]));
3114 ASTReader::ASTReadResult
3115 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3116 const ModuleFile *ImportedBy,
3117 unsigned ClientLoadCapabilities) {
3119 F.ModuleMapPath = ReadPath(F, Record, Idx);
3121 if (F.Kind == MK_ExplicitModule) {
3122 // For an explicitly-loaded module, we don't care whether the original
3123 // module map file exists or matches.
3127 // Try to resolve ModuleName in the current header search context and
3128 // verify that it is found in the same module map file as we saved. If the
3129 // top-level AST file is a main file, skip this check because there is no
3130 // usable header search context.
3131 assert(!F.ModuleName.empty() &&
3132 "MODULE_NAME should come before MODULE_MAP_FILE");
3133 if (F.Kind == MK_ImplicitModule &&
3134 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3135 // An implicitly-loaded module file should have its module listed in some
3136 // module map file that we've already loaded.
3137 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3138 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3139 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3141 assert(ImportedBy && "top-level import should be verified");
3142 if ((ClientLoadCapabilities & ARR_Missing) == 0)
3143 Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3144 << ImportedBy->FileName
3149 assert(M->Name == F.ModuleName && "found module with different name");
3151 // Check the primary module map file.
3152 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3153 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3154 assert(ModMap && "found module is missing module map file");
3155 assert(ImportedBy && "top-level import should be verified");
3156 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3157 Diag(diag::err_imported_module_modmap_changed)
3158 << F.ModuleName << ImportedBy->FileName
3159 << ModMap->getName() << F.ModuleMapPath;
3163 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3164 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3165 // FIXME: we should use input files rather than storing names.
3166 std::string Filename = ReadPath(F, Record, Idx);
3167 const FileEntry *F =
3168 FileMgr.getFile(Filename, false, false);
3170 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3171 Error("could not find file '" + Filename +"' referenced by AST file");
3174 AdditionalStoredMaps.insert(F);
3177 // Check any additional module map files (e.g. module.private.modulemap)
3178 // that are not in the pcm.
3179 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3180 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3181 // Remove files that match
3182 // Note: SmallPtrSet::erase is really remove
3183 if (!AdditionalStoredMaps.erase(ModMap)) {
3184 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3185 Diag(diag::err_module_different_modmap)
3186 << F.ModuleName << /*new*/0 << ModMap->getName();
3192 // Check any additional module map files that are in the pcm, but not
3193 // found in header search. Cases that match are already removed.
3194 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3195 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3196 Diag(diag::err_module_different_modmap)
3197 << F.ModuleName << /*not new*/1 << ModMap->getName();
3203 Listener->ReadModuleMapFile(F.ModuleMapPath);
3208 /// \brief Move the given method to the back of the global list of methods.
3209 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3210 // Find the entry for this selector in the method pool.
3211 Sema::GlobalMethodPool::iterator Known
3212 = S.MethodPool.find(Method->getSelector());
3213 if (Known == S.MethodPool.end())
3216 // Retrieve the appropriate method list.
3217 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3218 : Known->second.second;
3220 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3222 if (List->getMethod() == Method) {
3230 if (List->getNext())
3231 List->setMethod(List->getNext()->getMethod());
3233 List->setMethod(Method);
3237 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3238 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3239 for (Decl *D : Names) {
3240 bool wasHidden = D->Hidden;
3243 if (wasHidden && SemaObj) {
3244 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3245 moveMethodToBackOfGlobalList(*SemaObj, Method);
3251 void ASTReader::makeModuleVisible(Module *Mod,
3252 Module::NameVisibilityKind NameVisibility,
3253 SourceLocation ImportLoc) {
3254 llvm::SmallPtrSet<Module *, 4> Visited;
3255 SmallVector<Module *, 4> Stack;
3256 Stack.push_back(Mod);
3257 while (!Stack.empty()) {
3258 Mod = Stack.pop_back_val();
3260 if (NameVisibility <= Mod->NameVisibility) {
3261 // This module already has this level of visibility (or greater), so
3262 // there is nothing more to do.
3266 if (!Mod->isAvailable()) {
3267 // Modules that aren't available cannot be made visible.
3271 // Update the module's name visibility.
3272 Mod->NameVisibility = NameVisibility;
3274 // If we've already deserialized any names from this module,
3275 // mark them as visible.
3276 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3277 if (Hidden != HiddenNamesMap.end()) {
3278 auto HiddenNames = std::move(*Hidden);
3279 HiddenNamesMap.erase(Hidden);
3280 makeNamesVisible(HiddenNames.second, HiddenNames.first);
3281 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3282 "making names visible added hidden names");
3285 // Push any exported modules onto the stack to be marked as visible.
3286 SmallVector<Module *, 16> Exports;
3287 Mod->getExportedModules(Exports);
3288 for (SmallVectorImpl<Module *>::iterator
3289 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3290 Module *Exported = *I;
3291 if (Visited.insert(Exported).second)
3292 Stack.push_back(Exported);
3297 bool ASTReader::loadGlobalIndex() {
3301 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3302 !Context.getLangOpts().Modules)
3305 // Try to load the global index.
3306 TriedLoadingGlobalIndex = true;
3307 StringRef ModuleCachePath
3308 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3309 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3310 = GlobalModuleIndex::readIndex(ModuleCachePath);
3314 GlobalIndex.reset(Result.first);
3315 ModuleMgr.setGlobalIndex(GlobalIndex.get());
3319 bool ASTReader::isGlobalIndexUnavailable() const {
3320 return Context.getLangOpts().Modules && UseGlobalIndex &&
3321 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3324 static void updateModuleTimestamp(ModuleFile &MF) {
3325 // Overwrite the timestamp file contents so that file's mtime changes.
3326 std::string TimestampFilename = MF.getTimestampFilename();
3328 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3331 OS << "Timestamp file\n";
3334 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3336 SourceLocation ImportLoc,
3337 unsigned ClientLoadCapabilities) {
3338 llvm::SaveAndRestore<SourceLocation>
3339 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3341 // Defer any pending actions until we get to the end of reading the AST file.
3342 Deserializing AnASTFile(this);
3344 // Bump the generation number.
3345 unsigned PreviousGeneration = incrementGeneration(Context);
3347 unsigned NumModules = ModuleMgr.size();
3348 SmallVector<ImportedModule, 4> Loaded;
3349 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3350 /*ImportedBy=*/nullptr, Loaded,
3352 ClientLoadCapabilities)) {
3356 case VersionMismatch:
3357 case ConfigurationMismatch:
3359 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3360 for (const ImportedModule &IM : Loaded)
3361 LoadedSet.insert(IM.Mod);
3363 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3365 Context.getLangOpts().Modules
3366 ? &PP.getHeaderSearchInfo().getModuleMap()
3369 // If we find that any modules are unusable, the global index is going
3370 // to be out-of-date. Just remove it.
3371 GlobalIndex.reset();
3372 ModuleMgr.setGlobalIndex(nullptr);
3379 // Here comes stuff that we only do once the entire chain is loaded.
3381 // Load the AST blocks of all of the modules that we loaded.
3382 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3383 MEnd = Loaded.end();
3385 ModuleFile &F = *M->Mod;
3387 // Read the AST block.
3388 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3391 // Once read, set the ModuleFile bit base offset and update the size in
3392 // bits of all files we've seen.
3393 F.GlobalBitOffset = TotalModulesSizeInBits;
3394 TotalModulesSizeInBits += F.SizeInBits;
3395 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3397 // Preload SLocEntries.
3398 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3399 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3400 // Load it through the SourceManager and don't call ReadSLocEntry()
3401 // directly because the entry may have already been loaded in which case
3402 // calling ReadSLocEntry() directly would trigger an assertion in
3404 SourceMgr.getLoadedSLocEntryByID(Index);
3408 // Setup the import locations and notify the module manager that we've
3409 // committed to these module files.
3410 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3411 MEnd = Loaded.end();
3413 ModuleFile &F = *M->Mod;
3415 ModuleMgr.moduleFileAccepted(&F);
3417 // Set the import location.
3418 F.DirectImportLoc = ImportLoc;
3420 F.ImportLoc = M->ImportLoc;
3422 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3423 M->ImportLoc.getRawEncoding());
3426 // Mark all of the identifiers in the identifier table as being out of date,
3427 // so that various accessors know to check the loaded modules when the
3428 // identifier is used.
3429 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3430 IdEnd = PP.getIdentifierTable().end();
3432 Id->second->setOutOfDate(true);
3434 // Resolve any unresolved module exports.
3435 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3436 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3437 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3438 Module *ResolvedMod = getSubmodule(GlobalID);
3440 switch (Unresolved.Kind) {
3441 case UnresolvedModuleRef::Conflict:
3443 Module::Conflict Conflict;
3444 Conflict.Other = ResolvedMod;
3445 Conflict.Message = Unresolved.String.str();
3446 Unresolved.Mod->Conflicts.push_back(Conflict);
3450 case UnresolvedModuleRef::Import:
3452 Unresolved.Mod->Imports.insert(ResolvedMod);
3455 case UnresolvedModuleRef::Export:
3456 if (ResolvedMod || Unresolved.IsWildcard)
3457 Unresolved.Mod->Exports.push_back(
3458 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3462 UnresolvedModuleRefs.clear();
3464 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3465 // Might be unnecessary as use declarations are only used to build the
3468 InitializeContext();
3473 if (DeserializationListener)
3474 DeserializationListener->ReaderInitialized(this);
3476 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3477 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3478 PrimaryModule.OriginalSourceFileID
3479 = FileID::get(PrimaryModule.SLocEntryBaseID
3480 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3482 // If this AST file is a precompiled preamble, then set the
3483 // preamble file ID of the source manager to the file source file
3484 // from which the preamble was built.
3485 if (Type == MK_Preamble) {
3486 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3487 } else if (Type == MK_MainFile) {
3488 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3492 // For any Objective-C class definitions we have already loaded, make sure
3493 // that we load any additional categories.
3494 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3495 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3496 ObjCClassesLoaded[I],
3497 PreviousGeneration);
3500 if (PP.getHeaderSearchInfo()
3501 .getHeaderSearchOpts()
3502 .ModulesValidateOncePerBuildSession) {
3503 // Now we are certain that the module and all modules it depends on are
3504 // up to date. Create or update timestamp files for modules that are
3505 // located in the module cache (not for PCH files that could be anywhere
3506 // in the filesystem).
3507 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3508 ImportedModule &M = Loaded[I];
3509 if (M.Mod->Kind == MK_ImplicitModule) {
3510 updateModuleTimestamp(*M.Mod);
3518 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3520 /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3521 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3522 return Stream.Read(8) == 'C' &&
3523 Stream.Read(8) == 'P' &&
3524 Stream.Read(8) == 'C' &&
3525 Stream.Read(8) == 'H';
3528 ASTReader::ASTReadResult
3529 ASTReader::ReadASTCore(StringRef FileName,
3531 SourceLocation ImportLoc,
3532 ModuleFile *ImportedBy,
3533 SmallVectorImpl<ImportedModule> &Loaded,
3534 off_t ExpectedSize, time_t ExpectedModTime,
3535 ASTFileSignature ExpectedSignature,
3536 unsigned ClientLoadCapabilities) {
3538 std::string ErrorStr;
3539 ModuleManager::AddModuleResult AddResult
3540 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3541 getGeneration(), ExpectedSize, ExpectedModTime,
3542 ExpectedSignature, readASTFileSignature,
3545 switch (AddResult) {
3546 case ModuleManager::AlreadyLoaded:
3549 case ModuleManager::NewlyLoaded:
3550 // Load module file below.
3553 case ModuleManager::Missing:
3554 // The module file was missing; if the client can handle that, return
3556 if (ClientLoadCapabilities & ARR_Missing)
3559 // Otherwise, return an error.
3561 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3567 case ModuleManager::OutOfDate:
3568 // We couldn't load the module file because it is out-of-date. If the
3569 // client can handle out-of-date, return it.
3570 if (ClientLoadCapabilities & ARR_OutOfDate)
3573 // Otherwise, return an error.
3575 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3582 assert(M && "Missing module file");
3584 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3586 if (FileName != "-") {
3587 CurrentDir = llvm::sys::path::parent_path(FileName);
3588 if (CurrentDir.empty()) CurrentDir = ".";
3592 BitstreamCursor &Stream = F.Stream;
3593 Stream.init(&F.StreamFile);
3594 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3596 // Sniff for the signature.
3597 if (!startsWithASTFileMagic(Stream)) {
3598 Diag(diag::err_not_a_pch_file) << FileName;
3602 // This is used for compatibility with older PCH formats.
3603 bool HaveReadControlBlock = false;
3606 llvm::BitstreamEntry Entry = Stream.advance();
3608 switch (Entry.Kind) {
3609 case llvm::BitstreamEntry::Error:
3610 case llvm::BitstreamEntry::EndBlock:
3611 case llvm::BitstreamEntry::Record:
3612 Error("invalid record at top-level of AST file");
3615 case llvm::BitstreamEntry::SubBlock:
3619 // We only know the control subblock ID.
3621 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3622 if (Stream.ReadBlockInfoBlock()) {
3623 Error("malformed BlockInfoBlock in AST file");
3627 case CONTROL_BLOCK_ID:
3628 HaveReadControlBlock = true;
3629 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
3633 case Failure: return Failure;
3634 case Missing: return Missing;
3635 case OutOfDate: return OutOfDate;
3636 case VersionMismatch: return VersionMismatch;
3637 case ConfigurationMismatch: return ConfigurationMismatch;
3638 case HadErrors: return HadErrors;
3642 if (!HaveReadControlBlock) {
3643 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3644 Diag(diag::err_pch_version_too_old);
3645 return VersionMismatch;
3648 // Record that we've loaded this module.
3649 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3653 if (Stream.SkipBlock()) {
3654 Error("malformed block record in AST file");
3664 void ASTReader::InitializeContext() {
3665 // If there's a listener, notify them that we "read" the translation unit.
3666 if (DeserializationListener)
3667 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3668 Context.getTranslationUnitDecl());
3670 // FIXME: Find a better way to deal with collisions between these
3671 // built-in types. Right now, we just ignore the problem.
3673 // Load the special types.
3674 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3675 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3676 if (!Context.CFConstantStringTypeDecl)
3677 Context.setCFConstantStringType(GetType(String));
3680 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3681 QualType FileType = GetType(File);
3682 if (FileType.isNull()) {
3683 Error("FILE type is NULL");
3687 if (!Context.FILEDecl) {
3688 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3689 Context.setFILEDecl(Typedef->getDecl());
3691 const TagType *Tag = FileType->getAs<TagType>();
3693 Error("Invalid FILE type in AST file");
3696 Context.setFILEDecl(Tag->getDecl());
3701 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3702 QualType Jmp_bufType = GetType(Jmp_buf);
3703 if (Jmp_bufType.isNull()) {
3704 Error("jmp_buf type is NULL");
3708 if (!Context.jmp_bufDecl) {
3709 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3710 Context.setjmp_bufDecl(Typedef->getDecl());
3712 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3714 Error("Invalid jmp_buf type in AST file");
3717 Context.setjmp_bufDecl(Tag->getDecl());
3722 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3723 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3724 if (Sigjmp_bufType.isNull()) {
3725 Error("sigjmp_buf type is NULL");
3729 if (!Context.sigjmp_bufDecl) {
3730 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3731 Context.setsigjmp_bufDecl(Typedef->getDecl());
3733 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3734 assert(Tag && "Invalid sigjmp_buf type in AST file");
3735 Context.setsigjmp_bufDecl(Tag->getDecl());
3740 if (unsigned ObjCIdRedef
3741 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3742 if (Context.ObjCIdRedefinitionType.isNull())
3743 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3746 if (unsigned ObjCClassRedef
3747 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3748 if (Context.ObjCClassRedefinitionType.isNull())
3749 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3752 if (unsigned ObjCSelRedef
3753 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3754 if (Context.ObjCSelRedefinitionType.isNull())
3755 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3758 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3759 QualType Ucontext_tType = GetType(Ucontext_t);
3760 if (Ucontext_tType.isNull()) {
3761 Error("ucontext_t type is NULL");
3765 if (!Context.ucontext_tDecl) {
3766 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3767 Context.setucontext_tDecl(Typedef->getDecl());
3769 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3770 assert(Tag && "Invalid ucontext_t type in AST file");
3771 Context.setucontext_tDecl(Tag->getDecl());
3777 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3779 // If there were any CUDA special declarations, deserialize them.
3780 if (!CUDASpecialDeclRefs.empty()) {
3781 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3782 Context.setcudaConfigureCallDecl(
3783 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3786 // Re-export any modules that were imported by a non-module AST file.
3787 // FIXME: This does not make macro-only imports visible again.
3788 for (auto &Import : ImportedModules) {
3789 if (Module *Imported = getSubmodule(Import.ID)) {
3790 makeModuleVisible(Imported, Module::AllVisible,
3791 /*ImportLoc=*/Import.ImportLoc);
3792 PP.makeModuleVisible(Imported, Import.ImportLoc);
3795 ImportedModules.clear();
3798 void ASTReader::finalizeForWriting() {
3799 // Nothing to do for now.
3802 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3803 /// cursor into the start of the given block ID, returning false on success and
3804 /// true on failure.
3805 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3807 llvm::BitstreamEntry Entry = Cursor.advance();
3808 switch (Entry.Kind) {
3809 case llvm::BitstreamEntry::Error:
3810 case llvm::BitstreamEntry::EndBlock:
3813 case llvm::BitstreamEntry::Record:
3814 // Ignore top-level records.
3815 Cursor.skipRecord(Entry.ID);
3818 case llvm::BitstreamEntry::SubBlock:
3819 if (Entry.ID == BlockID) {
3820 if (Cursor.EnterSubBlock(BlockID))
3826 if (Cursor.SkipBlock())
3832 /// \brief Reads and return the signature record from \p StreamFile's control
3833 /// block, or else returns 0.
3834 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
3835 BitstreamCursor Stream(StreamFile);
3836 if (!startsWithASTFileMagic(Stream))
3839 // Scan for the CONTROL_BLOCK_ID block.
3840 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3843 // Scan for SIGNATURE inside the control block.
3844 ASTReader::RecordData Record;
3846 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3847 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
3848 Entry.Kind != llvm::BitstreamEntry::Record)
3853 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
3858 /// \brief Retrieve the name of the original source file name
3859 /// directly from the AST file, without actually loading the AST
3861 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3862 FileManager &FileMgr,
3863 DiagnosticsEngine &Diags) {
3864 // Open the AST file.
3865 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
3867 Diags.Report(diag::err_fe_unable_to_read_pch_file)
3868 << ASTFileName << Buffer.getError().message();
3869 return std::string();
3872 // Initialize the stream
3873 llvm::BitstreamReader StreamFile;
3874 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
3875 (const unsigned char *)(*Buffer)->getBufferEnd());
3876 BitstreamCursor Stream(StreamFile);
3878 // Sniff for the signature.
3879 if (!startsWithASTFileMagic(Stream)) {
3880 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3881 return std::string();
3884 // Scan for the CONTROL_BLOCK_ID block.
3885 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
3886 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3887 return std::string();
3890 // Scan for ORIGINAL_FILE inside the control block.
3893 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3894 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3895 return std::string();
3897 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3898 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3899 return std::string();
3904 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3910 class SimplePCHValidator : public ASTReaderListener {
3911 const LangOptions &ExistingLangOpts;
3912 const TargetOptions &ExistingTargetOpts;
3913 const PreprocessorOptions &ExistingPPOpts;
3914 std::string ExistingModuleCachePath;
3915 FileManager &FileMgr;
3918 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3919 const TargetOptions &ExistingTargetOpts,
3920 const PreprocessorOptions &ExistingPPOpts,
3921 StringRef ExistingModuleCachePath,
3922 FileManager &FileMgr)
3923 : ExistingLangOpts(ExistingLangOpts),
3924 ExistingTargetOpts(ExistingTargetOpts),
3925 ExistingPPOpts(ExistingPPOpts),
3926 ExistingModuleCachePath(ExistingModuleCachePath),
3931 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
3932 bool AllowCompatibleDifferences) override {
3933 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
3934 AllowCompatibleDifferences);
3936 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
3937 bool AllowCompatibleDifferences) override {
3938 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
3939 AllowCompatibleDifferences);
3941 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
3942 StringRef SpecificModuleCachePath,
3943 bool Complain) override {
3944 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
3945 ExistingModuleCachePath,
3946 nullptr, ExistingLangOpts);
3948 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3950 std::string &SuggestedPredefines) override {
3951 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
3952 SuggestedPredefines, ExistingLangOpts);
3957 bool ASTReader::readASTFileControlBlock(StringRef Filename,
3958 FileManager &FileMgr,
3959 ASTReaderListener &Listener) {
3960 // Open the AST file.
3961 // FIXME: This allows use of the VFS; we do not allow use of the
3962 // VFS when actually loading a module.
3963 auto Buffer = FileMgr.getBufferForFile(Filename);
3968 // Initialize the stream
3969 llvm::BitstreamReader StreamFile;
3970 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
3971 (const unsigned char *)(*Buffer)->getBufferEnd());
3972 BitstreamCursor Stream(StreamFile);
3974 // Sniff for the signature.
3975 if (!startsWithASTFileMagic(Stream))
3978 // Scan for the CONTROL_BLOCK_ID block.
3979 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3982 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3983 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
3984 bool NeedsImports = Listener.needsImportVisitation();
3985 BitstreamCursor InputFilesCursor;
3986 if (NeedsInputFiles) {
3987 InputFilesCursor = Stream;
3988 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3991 // Read the abbreviations
3993 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3994 unsigned Code = InputFilesCursor.ReadCode();
3996 // We expect all abbrevs to be at the start of the block.
3997 if (Code != llvm::bitc::DEFINE_ABBREV) {
3998 InputFilesCursor.JumpToBit(Offset);
4001 InputFilesCursor.ReadAbbrevRecord();
4005 // Scan for ORIGINAL_FILE inside the control block.
4007 std::string ModuleDir;
4009 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4010 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4013 if (Entry.Kind != llvm::BitstreamEntry::Record)
4018 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4019 switch ((ControlRecordTypes)RecCode) {
4021 if (Record[0] != VERSION_MAJOR)
4024 if (Listener.ReadFullVersionInformation(Blob))
4030 Listener.ReadModuleName(Blob);
4032 case MODULE_DIRECTORY:
4035 case MODULE_MAP_FILE: {
4037 auto Path = ReadString(Record, Idx);
4038 ResolveImportedPath(Path, ModuleDir);
4039 Listener.ReadModuleMapFile(Path);
4042 case LANGUAGE_OPTIONS:
4043 if (ParseLanguageOptions(Record, false, Listener,
4044 /*AllowCompatibleConfigurationMismatch*/false))
4048 case TARGET_OPTIONS:
4049 if (ParseTargetOptions(Record, false, Listener,
4050 /*AllowCompatibleConfigurationMismatch*/ false))
4054 case DIAGNOSTIC_OPTIONS:
4055 if (ParseDiagnosticOptions(Record, false, Listener))
4059 case FILE_SYSTEM_OPTIONS:
4060 if (ParseFileSystemOptions(Record, false, Listener))
4064 case HEADER_SEARCH_OPTIONS:
4065 if (ParseHeaderSearchOptions(Record, false, Listener))
4069 case PREPROCESSOR_OPTIONS: {
4070 std::string IgnoredSuggestedPredefines;
4071 if (ParsePreprocessorOptions(Record, false, Listener,
4072 IgnoredSuggestedPredefines))
4077 case INPUT_FILE_OFFSETS: {
4078 if (!NeedsInputFiles)
4081 unsigned NumInputFiles = Record[0];
4082 unsigned NumUserFiles = Record[1];
4083 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
4084 for (unsigned I = 0; I != NumInputFiles; ++I) {
4085 // Go find this input file.
4086 bool isSystemFile = I >= NumUserFiles;
4088 if (isSystemFile && !NeedsSystemInputFiles)
4089 break; // the rest are system input files
4091 BitstreamCursor &Cursor = InputFilesCursor;
4092 SavedStreamPosition SavedPosition(Cursor);
4093 Cursor.JumpToBit(InputFileOffs[I]);
4095 unsigned Code = Cursor.ReadCode();
4098 bool shouldContinue = false;
4099 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4101 bool Overridden = static_cast<bool>(Record[3]);
4102 std::string Filename = Blob;
4103 ResolveImportedPath(Filename, ModuleDir);
4105 Listener.visitInputFile(Filename, isSystemFile, Overridden);
4108 if (!shouldContinue)
4118 unsigned Idx = 0, N = Record.size();
4120 // Read information about the AST file.
4121 Idx += 5; // ImportLoc, Size, ModTime, Signature
4122 std::string Filename = ReadString(Record, Idx);
4123 ResolveImportedPath(Filename, ModuleDir);
4124 Listener.visitImport(Filename);
4129 case KNOWN_MODULE_FILES: {
4130 // Known-but-not-technically-used module files are treated as imports.
4134 unsigned Idx = 0, N = Record.size();
4136 std::string Filename = ReadString(Record, Idx);
4137 ResolveImportedPath(Filename, ModuleDir);
4138 Listener.visitImport(Filename);
4144 // No other validation to perform.
4151 bool ASTReader::isAcceptableASTFile(StringRef Filename,
4152 FileManager &FileMgr,
4153 const LangOptions &LangOpts,
4154 const TargetOptions &TargetOpts,
4155 const PreprocessorOptions &PPOpts,
4156 std::string ExistingModuleCachePath) {
4157 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4158 ExistingModuleCachePath, FileMgr);
4159 return !readASTFileControlBlock(Filename, FileMgr, validator);
4162 ASTReader::ASTReadResult
4163 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4164 // Enter the submodule block.
4165 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4166 Error("malformed submodule block record in AST file");
4170 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4172 Module *CurrentModule = nullptr;
4175 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4177 switch (Entry.Kind) {
4178 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4179 case llvm::BitstreamEntry::Error:
4180 Error("malformed block record in AST file");
4182 case llvm::BitstreamEntry::EndBlock:
4184 case llvm::BitstreamEntry::Record:
4185 // The interesting case.
4192 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4194 if ((Kind == SUBMODULE_METADATA) != First) {
4195 Error("submodule metadata record should be at beginning of block");
4200 // Submodule information is only valid if we have a current module.
4201 // FIXME: Should we error on these cases?
4202 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4203 Kind != SUBMODULE_DEFINITION)
4207 default: // Default behavior: ignore.
4210 case SUBMODULE_DEFINITION: {
4211 if (Record.size() < 8) {
4212 Error("malformed module definition");
4216 StringRef Name = Blob;
4218 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4219 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4220 bool IsFramework = Record[Idx++];
4221 bool IsExplicit = Record[Idx++];
4222 bool IsSystem = Record[Idx++];
4223 bool IsExternC = Record[Idx++];
4224 bool InferSubmodules = Record[Idx++];
4225 bool InferExplicitSubmodules = Record[Idx++];
4226 bool InferExportWildcard = Record[Idx++];
4227 bool ConfigMacrosExhaustive = Record[Idx++];
4229 Module *ParentModule = nullptr;
4231 ParentModule = getSubmodule(Parent);
4233 // Retrieve this (sub)module from the module map, creating it if
4235 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
4238 // FIXME: set the definition loc for CurrentModule, or call
4239 // ModMap.setInferredModuleAllowedBy()
4241 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4242 if (GlobalIndex >= SubmodulesLoaded.size() ||
4243 SubmodulesLoaded[GlobalIndex]) {
4244 Error("too many submodules");
4248 if (!ParentModule) {
4249 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4250 if (CurFile != F.File) {
4251 if (!Diags.isDiagnosticInFlight()) {
4252 Diag(diag::err_module_file_conflict)
4253 << CurrentModule->getTopLevelModuleName()
4254 << CurFile->getName()
4255 << F.File->getName();
4261 CurrentModule->setASTFile(F.File);
4264 CurrentModule->IsFromModuleFile = true;
4265 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
4266 CurrentModule->IsExternC = IsExternC;
4267 CurrentModule->InferSubmodules = InferSubmodules;
4268 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4269 CurrentModule->InferExportWildcard = InferExportWildcard;
4270 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
4271 if (DeserializationListener)
4272 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4274 SubmodulesLoaded[GlobalIndex] = CurrentModule;
4276 // Clear out data that will be replaced by what is the module file.
4277 CurrentModule->LinkLibraries.clear();
4278 CurrentModule->ConfigMacros.clear();
4279 CurrentModule->UnresolvedConflicts.clear();
4280 CurrentModule->Conflicts.clear();
4284 case SUBMODULE_UMBRELLA_HEADER: {
4285 std::string Filename = Blob;
4286 ResolveImportedPath(F, Filename);
4287 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
4288 if (!CurrentModule->getUmbrellaHeader())
4289 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4290 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
4291 // This can be a spurious difference caused by changing the VFS to
4292 // point to a different copy of the file, and it is too late to
4293 // to rebuild safely.
4294 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4295 // after input file validation only real problems would remain and we
4296 // could just error. For now, assume it's okay.
4303 case SUBMODULE_HEADER:
4304 case SUBMODULE_EXCLUDED_HEADER:
4305 case SUBMODULE_PRIVATE_HEADER:
4306 // We lazily associate headers with their modules via the HeaderInfo table.
4307 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4308 // of complete filenames or remove it entirely.
4311 case SUBMODULE_TEXTUAL_HEADER:
4312 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4313 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4317 case SUBMODULE_TOPHEADER: {
4318 CurrentModule->addTopHeaderFilename(Blob);
4322 case SUBMODULE_UMBRELLA_DIR: {
4323 std::string Dirname = Blob;
4324 ResolveImportedPath(F, Dirname);
4325 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
4326 if (!CurrentModule->getUmbrellaDir())
4327 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4328 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
4329 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4330 Error("mismatched umbrella directories in submodule");
4337 case SUBMODULE_METADATA: {
4338 F.BaseSubmoduleID = getTotalNumSubmodules();
4339 F.LocalNumSubmodules = Record[0];
4340 unsigned LocalBaseSubmoduleID = Record[1];
4341 if (F.LocalNumSubmodules > 0) {
4342 // Introduce the global -> local mapping for submodules within this
4344 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4346 // Introduce the local -> global mapping for submodules within this
4348 F.SubmoduleRemap.insertOrReplace(
4349 std::make_pair(LocalBaseSubmoduleID,
4350 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4352 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4357 case SUBMODULE_IMPORTS: {
4358 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
4359 UnresolvedModuleRef Unresolved;
4360 Unresolved.File = &F;
4361 Unresolved.Mod = CurrentModule;
4362 Unresolved.ID = Record[Idx];
4363 Unresolved.Kind = UnresolvedModuleRef::Import;
4364 Unresolved.IsWildcard = false;
4365 UnresolvedModuleRefs.push_back(Unresolved);
4370 case SUBMODULE_EXPORTS: {
4371 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
4372 UnresolvedModuleRef Unresolved;
4373 Unresolved.File = &F;
4374 Unresolved.Mod = CurrentModule;
4375 Unresolved.ID = Record[Idx];
4376 Unresolved.Kind = UnresolvedModuleRef::Export;
4377 Unresolved.IsWildcard = Record[Idx + 1];
4378 UnresolvedModuleRefs.push_back(Unresolved);
4381 // Once we've loaded the set of exports, there's no reason to keep
4382 // the parsed, unresolved exports around.
4383 CurrentModule->UnresolvedExports.clear();
4386 case SUBMODULE_REQUIRES: {
4387 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
4388 Context.getTargetInfo());
4392 case SUBMODULE_LINK_LIBRARY:
4393 CurrentModule->LinkLibraries.push_back(
4394 Module::LinkLibrary(Blob, Record[0]));
4397 case SUBMODULE_CONFIG_MACRO:
4398 CurrentModule->ConfigMacros.push_back(Blob.str());
4401 case SUBMODULE_CONFLICT: {
4402 UnresolvedModuleRef Unresolved;
4403 Unresolved.File = &F;
4404 Unresolved.Mod = CurrentModule;
4405 Unresolved.ID = Record[0];
4406 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4407 Unresolved.IsWildcard = false;
4408 Unresolved.String = Blob;
4409 UnresolvedModuleRefs.push_back(Unresolved);
4416 /// \brief Parse the record that corresponds to a LangOptions data
4419 /// This routine parses the language options from the AST file and then gives
4420 /// them to the AST listener if one is set.
4422 /// \returns true if the listener deems the file unacceptable, false otherwise.
4423 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4425 ASTReaderListener &Listener,
4426 bool AllowCompatibleDifferences) {
4427 LangOptions LangOpts;
4429 #define LANGOPT(Name, Bits, Default, Description) \
4430 LangOpts.Name = Record[Idx++];
4431 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4432 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4433 #include "clang/Basic/LangOptions.def"
4434 #define SANITIZER(NAME, ID) \
4435 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
4436 #include "clang/Basic/Sanitizers.def"
4438 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4439 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4440 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4442 unsigned Length = Record[Idx++];
4443 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4444 Record.begin() + Idx + Length);
4449 for (unsigned N = Record[Idx++]; N; --N) {
4450 LangOpts.CommentOpts.BlockCommandNames.push_back(
4451 ReadString(Record, Idx));
4453 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4455 return Listener.ReadLanguageOptions(LangOpts, Complain,
4456 AllowCompatibleDifferences);
4459 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4460 ASTReaderListener &Listener,
4461 bool AllowCompatibleDifferences) {
4463 TargetOptions TargetOpts;
4464 TargetOpts.Triple = ReadString(Record, Idx);
4465 TargetOpts.CPU = ReadString(Record, Idx);
4466 TargetOpts.ABI = ReadString(Record, Idx);
4467 for (unsigned N = Record[Idx++]; N; --N) {
4468 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4470 for (unsigned N = Record[Idx++]; N; --N) {
4471 TargetOpts.Features.push_back(ReadString(Record, Idx));
4474 return Listener.ReadTargetOptions(TargetOpts, Complain,
4475 AllowCompatibleDifferences);
4478 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4479 ASTReaderListener &Listener) {
4480 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
4482 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
4483 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4484 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
4485 #include "clang/Basic/DiagnosticOptions.def"
4487 for (unsigned N = Record[Idx++]; N; --N)
4488 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
4489 for (unsigned N = Record[Idx++]; N; --N)
4490 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
4492 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4495 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4496 ASTReaderListener &Listener) {
4497 FileSystemOptions FSOpts;
4499 FSOpts.WorkingDir = ReadString(Record, Idx);
4500 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4503 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4505 ASTReaderListener &Listener) {
4506 HeaderSearchOptions HSOpts;
4508 HSOpts.Sysroot = ReadString(Record, Idx);
4511 for (unsigned N = Record[Idx++]; N; --N) {
4512 std::string Path = ReadString(Record, Idx);
4513 frontend::IncludeDirGroup Group
4514 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4515 bool IsFramework = Record[Idx++];
4516 bool IgnoreSysRoot = Record[Idx++];
4517 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4521 // System header prefixes.
4522 for (unsigned N = Record[Idx++]; N; --N) {
4523 std::string Prefix = ReadString(Record, Idx);
4524 bool IsSystemHeader = Record[Idx++];
4525 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
4528 HSOpts.ResourceDir = ReadString(Record, Idx);
4529 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4530 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
4531 HSOpts.DisableModuleHash = Record[Idx++];
4532 HSOpts.UseBuiltinIncludes = Record[Idx++];
4533 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4534 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4535 HSOpts.UseLibcxx = Record[Idx++];
4536 std::string SpecificModuleCachePath = ReadString(Record, Idx);
4538 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4542 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4544 ASTReaderListener &Listener,
4545 std::string &SuggestedPredefines) {
4546 PreprocessorOptions PPOpts;
4549 // Macro definitions/undefs
4550 for (unsigned N = Record[Idx++]; N; --N) {
4551 std::string Macro = ReadString(Record, Idx);
4552 bool IsUndef = Record[Idx++];
4553 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4557 for (unsigned N = Record[Idx++]; N; --N) {
4558 PPOpts.Includes.push_back(ReadString(Record, Idx));
4562 for (unsigned N = Record[Idx++]; N; --N) {
4563 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4566 PPOpts.UsePredefines = Record[Idx++];
4567 PPOpts.DetailedRecord = Record[Idx++];
4568 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4569 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4570 PPOpts.ObjCXXARCStandardLibrary =
4571 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4572 SuggestedPredefines.clear();
4573 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4574 SuggestedPredefines);
4577 std::pair<ModuleFile *, unsigned>
4578 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4579 GlobalPreprocessedEntityMapType::iterator
4580 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4581 assert(I != GlobalPreprocessedEntityMap.end() &&
4582 "Corrupted global preprocessed entity map");
4583 ModuleFile *M = I->second;
4584 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4585 return std::make_pair(M, LocalIndex);
4588 llvm::iterator_range<PreprocessingRecord::iterator>
4589 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4590 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4591 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4592 Mod.NumPreprocessedEntities);
4594 return llvm::make_range(PreprocessingRecord::iterator(),
4595 PreprocessingRecord::iterator());
4598 llvm::iterator_range<ASTReader::ModuleDeclIterator>
4599 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4600 return llvm::make_range(
4601 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4602 ModuleDeclIterator(this, &Mod,
4603 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4606 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4607 PreprocessedEntityID PPID = Index+1;
4608 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4609 ModuleFile &M = *PPInfo.first;
4610 unsigned LocalIndex = PPInfo.second;
4611 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4613 if (!PP.getPreprocessingRecord()) {
4614 Error("no preprocessing record");
4618 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4619 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4621 llvm::BitstreamEntry Entry =
4622 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4623 if (Entry.Kind != llvm::BitstreamEntry::Record)
4627 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4628 ReadSourceLocation(M, PPOffs.End));
4629 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4632 PreprocessorDetailRecordTypes RecType =
4633 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4634 Entry.ID, Record, &Blob);
4636 case PPD_MACRO_EXPANSION: {
4637 bool isBuiltin = Record[0];
4638 IdentifierInfo *Name = nullptr;
4639 MacroDefinitionRecord *Def = nullptr;
4641 Name = getLocalIdentifier(M, Record[1]);
4643 PreprocessedEntityID GlobalID =
4644 getGlobalPreprocessedEntityID(M, Record[1]);
4645 Def = cast<MacroDefinitionRecord>(
4646 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
4651 ME = new (PPRec) MacroExpansion(Name, Range);
4653 ME = new (PPRec) MacroExpansion(Def, Range);
4658 case PPD_MACRO_DEFINITION: {
4659 // Decode the identifier info and then check again; if the macro is
4660 // still defined and associated with the identifier,
4661 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4662 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
4664 if (DeserializationListener)
4665 DeserializationListener->MacroDefinitionRead(PPID, MD);
4670 case PPD_INCLUSION_DIRECTIVE: {
4671 const char *FullFileNameStart = Blob.data() + Record[0];
4672 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4673 const FileEntry *File = nullptr;
4674 if (!FullFileName.empty())
4675 File = PP.getFileManager().getFile(FullFileName);
4677 // FIXME: Stable encoding
4678 InclusionDirective::InclusionKind Kind
4679 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4680 InclusionDirective *ID
4681 = new (PPRec) InclusionDirective(PPRec, Kind,
4682 StringRef(Blob.data(), Record[0]),
4683 Record[1], Record[3],
4690 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4693 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4694 /// preprocessed entities or the entities it contains are not the ones we are
4695 /// looking for. Find the next module that contains entities and return the ID
4696 /// of the first entry.
4697 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4698 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4700 for (GlobalSLocOffsetMapType::const_iterator
4701 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4702 ModuleFile &M = *SLocMapI->second;
4703 if (M.NumPreprocessedEntities)
4704 return M.BasePreprocessedEntityID;
4707 return getTotalNumPreprocessedEntities();
4712 template <unsigned PPEntityOffset::*PPLoc>
4713 struct PPEntityComp {
4714 const ASTReader &Reader;
4717 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4719 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4720 SourceLocation LHS = getLoc(L);
4721 SourceLocation RHS = getLoc(R);
4722 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4725 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4726 SourceLocation LHS = getLoc(L);
4727 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4730 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4731 SourceLocation RHS = getLoc(R);
4732 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4735 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4736 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4742 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4743 bool EndsAfter) const {
4744 if (SourceMgr.isLocalSourceLocation(Loc))
4745 return getTotalNumPreprocessedEntities();
4747 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4748 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
4749 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4750 "Corrupted global sloc offset map");
4752 if (SLocMapI->second->NumPreprocessedEntities == 0)
4753 return findNextPreprocessedEntity(SLocMapI);
4755 ModuleFile &M = *SLocMapI->second;
4756 typedef const PPEntityOffset *pp_iterator;
4757 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4758 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4760 size_t Count = M.NumPreprocessedEntities;
4762 pp_iterator First = pp_begin;
4766 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4767 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4769 // Do a binary search manually instead of using std::lower_bound because
4770 // The end locations of entities may be unordered (when a macro expansion
4771 // is inside another macro argument), but for this case it is not important
4772 // whether we get the first macro expansion or its containing macro.
4776 std::advance(PPI, Half);
4777 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4781 Count = Count - Half - 1;
4788 return findNextPreprocessedEntity(SLocMapI);
4790 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4793 /// \brief Returns a pair of [Begin, End) indices of preallocated
4794 /// preprocessed entities that \arg Range encompasses.
4795 std::pair<unsigned, unsigned>
4796 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4797 if (Range.isInvalid())
4798 return std::make_pair(0,0);
4799 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4801 PreprocessedEntityID BeginID =
4802 findPreprocessedEntity(Range.getBegin(), false);
4803 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
4804 return std::make_pair(BeginID, EndID);
4807 /// \brief Optionally returns true or false if the preallocated preprocessed
4808 /// entity with index \arg Index came from file \arg FID.
4809 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4811 if (FID.isInvalid())
4814 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4815 ModuleFile &M = *PPInfo.first;
4816 unsigned LocalIndex = PPInfo.second;
4817 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4819 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4820 if (Loc.isInvalid())
4823 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4830 /// \brief Visitor used to search for information about a header file.
4831 class HeaderFileInfoVisitor {
4832 const FileEntry *FE;
4834 Optional<HeaderFileInfo> HFI;
4837 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4840 static bool visit(ModuleFile &M, void *UserData) {
4841 HeaderFileInfoVisitor *This
4842 = static_cast<HeaderFileInfoVisitor *>(UserData);
4844 HeaderFileInfoLookupTable *Table
4845 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4849 // Look in the on-disk hash table for an entry for this file name.
4850 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
4851 if (Pos == Table->end())
4858 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4862 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4863 HeaderFileInfoVisitor Visitor(FE);
4864 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4865 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
4868 return HeaderFileInfo();
4871 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4872 // FIXME: Make it work properly with modules.
4873 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
4874 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4875 ModuleFile &F = *(*I);
4878 assert(!Diag.DiagStates.empty());
4879 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4880 while (Idx < F.PragmaDiagMappings.size()) {
4881 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4882 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4883 if (DiagStateID != 0) {
4884 Diag.DiagStatePoints.push_back(
4885 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4886 FullSourceLoc(Loc, SourceMgr)));
4890 assert(DiagStateID == 0);
4891 // A new DiagState was created here.
4892 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4893 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4894 DiagStates.push_back(NewState);
4895 Diag.DiagStatePoints.push_back(
4896 DiagnosticsEngine::DiagStatePoint(NewState,
4897 FullSourceLoc(Loc, SourceMgr)));
4899 assert(Idx < F.PragmaDiagMappings.size() &&
4900 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4901 if (Idx >= F.PragmaDiagMappings.size()) {
4902 break; // Something is messed up but at least avoid infinite loop in
4905 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4906 if (DiagID == (unsigned)-1) {
4907 break; // no more diag/map pairs for this location.
4909 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
4910 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
4911 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
4917 /// \brief Get the correct cursor and offset for loading a type.
4918 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4919 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4920 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4921 ModuleFile *M = I->second;
4922 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4925 /// \brief Read and return the type with the given index..
4927 /// The index is the type ID, shifted and minus the number of predefs. This
4928 /// routine actually reads the record corresponding to the type at the given
4929 /// location. It is a helper routine for GetType, which deals with reading type
4931 QualType ASTReader::readTypeRecord(unsigned Index) {
4932 RecordLocation Loc = TypeCursorForIndex(Index);
4933 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
4935 // Keep track of where we are in the stream, then jump back there
4936 // after reading this type.
4937 SavedStreamPosition SavedPosition(DeclsCursor);
4939 ReadingKindTracker ReadingKind(Read_Type, *this);
4941 // Note that we are loading a type record.
4942 Deserializing AType(this);
4945 DeclsCursor.JumpToBit(Loc.Offset);
4947 unsigned Code = DeclsCursor.ReadCode();
4948 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
4949 case TYPE_EXT_QUAL: {
4950 if (Record.size() != 2) {
4951 Error("Incorrect encoding of extended qualifier type");
4954 QualType Base = readType(*Loc.F, Record, Idx);
4955 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4956 return Context.getQualifiedType(Base, Quals);
4959 case TYPE_COMPLEX: {
4960 if (Record.size() != 1) {
4961 Error("Incorrect encoding of complex type");
4964 QualType ElemType = readType(*Loc.F, Record, Idx);
4965 return Context.getComplexType(ElemType);
4968 case TYPE_POINTER: {
4969 if (Record.size() != 1) {
4970 Error("Incorrect encoding of pointer type");
4973 QualType PointeeType = readType(*Loc.F, Record, Idx);
4974 return Context.getPointerType(PointeeType);
4977 case TYPE_DECAYED: {
4978 if (Record.size() != 1) {
4979 Error("Incorrect encoding of decayed type");
4982 QualType OriginalType = readType(*Loc.F, Record, Idx);
4983 QualType DT = Context.getAdjustedParameterType(OriginalType);
4984 if (!isa<DecayedType>(DT))
4985 Error("Decayed type does not decay");
4989 case TYPE_ADJUSTED: {
4990 if (Record.size() != 2) {
4991 Error("Incorrect encoding of adjusted type");
4994 QualType OriginalTy = readType(*Loc.F, Record, Idx);
4995 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
4996 return Context.getAdjustedType(OriginalTy, AdjustedTy);
4999 case TYPE_BLOCK_POINTER: {
5000 if (Record.size() != 1) {
5001 Error("Incorrect encoding of block pointer type");
5004 QualType PointeeType = readType(*Loc.F, Record, Idx);
5005 return Context.getBlockPointerType(PointeeType);
5008 case TYPE_LVALUE_REFERENCE: {
5009 if (Record.size() != 2) {
5010 Error("Incorrect encoding of lvalue reference type");
5013 QualType PointeeType = readType(*Loc.F, Record, Idx);
5014 return Context.getLValueReferenceType(PointeeType, Record[1]);
5017 case TYPE_RVALUE_REFERENCE: {
5018 if (Record.size() != 1) {
5019 Error("Incorrect encoding of rvalue reference type");
5022 QualType PointeeType = readType(*Loc.F, Record, Idx);
5023 return Context.getRValueReferenceType(PointeeType);
5026 case TYPE_MEMBER_POINTER: {
5027 if (Record.size() != 2) {
5028 Error("Incorrect encoding of member pointer type");
5031 QualType PointeeType = readType(*Loc.F, Record, Idx);
5032 QualType ClassType = readType(*Loc.F, Record, Idx);
5033 if (PointeeType.isNull() || ClassType.isNull())
5036 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5039 case TYPE_CONSTANT_ARRAY: {
5040 QualType ElementType = readType(*Loc.F, Record, Idx);
5041 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5042 unsigned IndexTypeQuals = Record[2];
5044 llvm::APInt Size = ReadAPInt(Record, Idx);
5045 return Context.getConstantArrayType(ElementType, Size,
5046 ASM, IndexTypeQuals);
5049 case TYPE_INCOMPLETE_ARRAY: {
5050 QualType ElementType = readType(*Loc.F, Record, Idx);
5051 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5052 unsigned IndexTypeQuals = Record[2];
5053 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5056 case TYPE_VARIABLE_ARRAY: {
5057 QualType ElementType = readType(*Loc.F, Record, Idx);
5058 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5059 unsigned IndexTypeQuals = Record[2];
5060 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5061 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5062 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5063 ASM, IndexTypeQuals,
5064 SourceRange(LBLoc, RBLoc));
5068 if (Record.size() != 3) {
5069 Error("incorrect encoding of vector type in AST file");
5073 QualType ElementType = readType(*Loc.F, Record, Idx);
5074 unsigned NumElements = Record[1];
5075 unsigned VecKind = Record[2];
5076 return Context.getVectorType(ElementType, NumElements,
5077 (VectorType::VectorKind)VecKind);
5080 case TYPE_EXT_VECTOR: {
5081 if (Record.size() != 3) {
5082 Error("incorrect encoding of extended vector type in AST file");
5086 QualType ElementType = readType(*Loc.F, Record, Idx);
5087 unsigned NumElements = Record[1];
5088 return Context.getExtVectorType(ElementType, NumElements);
5091 case TYPE_FUNCTION_NO_PROTO: {
5092 if (Record.size() != 6) {
5093 Error("incorrect encoding of no-proto function type");
5096 QualType ResultType = readType(*Loc.F, Record, Idx);
5097 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5098 (CallingConv)Record[4], Record[5]);
5099 return Context.getFunctionNoProtoType(ResultType, Info);
5102 case TYPE_FUNCTION_PROTO: {
5103 QualType ResultType = readType(*Loc.F, Record, Idx);
5105 FunctionProtoType::ExtProtoInfo EPI;
5106 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5107 /*hasregparm*/ Record[2],
5108 /*regparm*/ Record[3],
5109 static_cast<CallingConv>(Record[4]),
5110 /*produces*/ Record[5]);
5114 EPI.Variadic = Record[Idx++];
5115 EPI.HasTrailingReturn = Record[Idx++];
5116 EPI.TypeQuals = Record[Idx++];
5117 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5118 SmallVector<QualType, 8> ExceptionStorage;
5119 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
5121 unsigned NumParams = Record[Idx++];
5122 SmallVector<QualType, 16> ParamTypes;
5123 for (unsigned I = 0; I != NumParams; ++I)
5124 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5126 return Context.getFunctionType(ResultType, ParamTypes, EPI);
5129 case TYPE_UNRESOLVED_USING: {
5131 return Context.getTypeDeclType(
5132 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5135 case TYPE_TYPEDEF: {
5136 if (Record.size() != 2) {
5137 Error("incorrect encoding of typedef type");
5141 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5142 QualType Canonical = readType(*Loc.F, Record, Idx);
5143 if (!Canonical.isNull())
5144 Canonical = Context.getCanonicalType(Canonical);
5145 return Context.getTypedefType(Decl, Canonical);
5148 case TYPE_TYPEOF_EXPR:
5149 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5152 if (Record.size() != 1) {
5153 Error("incorrect encoding of typeof(type) in AST file");
5156 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5157 return Context.getTypeOfType(UnderlyingType);
5160 case TYPE_DECLTYPE: {
5161 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5162 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5165 case TYPE_UNARY_TRANSFORM: {
5166 QualType BaseType = readType(*Loc.F, Record, Idx);
5167 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5168 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5169 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5173 QualType Deduced = readType(*Loc.F, Record, Idx);
5174 bool IsDecltypeAuto = Record[Idx++];
5175 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
5176 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
5180 if (Record.size() != 2) {
5181 Error("incorrect encoding of record type");
5185 bool IsDependent = Record[Idx++];
5186 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5187 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5188 QualType T = Context.getRecordType(RD);
5189 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5194 if (Record.size() != 2) {
5195 Error("incorrect encoding of enum type");
5199 bool IsDependent = Record[Idx++];
5201 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5202 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5206 case TYPE_ATTRIBUTED: {
5207 if (Record.size() != 3) {
5208 Error("incorrect encoding of attributed type");
5211 QualType modifiedType = readType(*Loc.F, Record, Idx);
5212 QualType equivalentType = readType(*Loc.F, Record, Idx);
5213 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5214 return Context.getAttributedType(kind, modifiedType, equivalentType);
5218 if (Record.size() != 1) {
5219 Error("incorrect encoding of paren type");
5222 QualType InnerType = readType(*Loc.F, Record, Idx);
5223 return Context.getParenType(InnerType);
5226 case TYPE_PACK_EXPANSION: {
5227 if (Record.size() != 2) {
5228 Error("incorrect encoding of pack expansion type");
5231 QualType Pattern = readType(*Loc.F, Record, Idx);
5232 if (Pattern.isNull())
5234 Optional<unsigned> NumExpansions;
5236 NumExpansions = Record[1] - 1;
5237 return Context.getPackExpansionType(Pattern, NumExpansions);
5240 case TYPE_ELABORATED: {
5242 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5243 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5244 QualType NamedType = readType(*Loc.F, Record, Idx);
5245 return Context.getElaboratedType(Keyword, NNS, NamedType);
5248 case TYPE_OBJC_INTERFACE: {
5250 ObjCInterfaceDecl *ItfD
5251 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5252 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5255 case TYPE_OBJC_OBJECT: {
5257 QualType Base = readType(*Loc.F, Record, Idx);
5258 unsigned NumProtos = Record[Idx++];
5259 SmallVector<ObjCProtocolDecl*, 4> Protos;
5260 for (unsigned I = 0; I != NumProtos; ++I)
5261 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5262 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5265 case TYPE_OBJC_OBJECT_POINTER: {
5267 QualType Pointee = readType(*Loc.F, Record, Idx);
5268 return Context.getObjCObjectPointerType(Pointee);
5271 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5273 QualType Parm = readType(*Loc.F, Record, Idx);
5274 QualType Replacement = readType(*Loc.F, Record, Idx);
5275 return Context.getSubstTemplateTypeParmType(
5276 cast<TemplateTypeParmType>(Parm),
5277 Context.getCanonicalType(Replacement));
5280 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5282 QualType Parm = readType(*Loc.F, Record, Idx);
5283 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5284 return Context.getSubstTemplateTypeParmPackType(
5285 cast<TemplateTypeParmType>(Parm),
5289 case TYPE_INJECTED_CLASS_NAME: {
5290 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5291 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5292 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5293 // for AST reading, too much interdependencies.
5294 const Type *T = nullptr;
5295 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5296 if (const Type *Existing = DI->getTypeForDecl()) {
5302 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5303 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5304 DI->setTypeForDecl(T);
5306 return QualType(T, 0);
5309 case TYPE_TEMPLATE_TYPE_PARM: {
5311 unsigned Depth = Record[Idx++];
5312 unsigned Index = Record[Idx++];
5313 bool Pack = Record[Idx++];
5314 TemplateTypeParmDecl *D
5315 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5316 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5319 case TYPE_DEPENDENT_NAME: {
5321 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5322 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5323 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5324 QualType Canon = readType(*Loc.F, Record, Idx);
5325 if (!Canon.isNull())
5326 Canon = Context.getCanonicalType(Canon);
5327 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5330 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5332 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5333 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5334 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5335 unsigned NumArgs = Record[Idx++];
5336 SmallVector<TemplateArgument, 8> Args;
5337 Args.reserve(NumArgs);
5339 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5340 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5341 Args.size(), Args.data());
5344 case TYPE_DEPENDENT_SIZED_ARRAY: {
5348 QualType ElementType = readType(*Loc.F, Record, Idx);
5349 ArrayType::ArraySizeModifier ASM
5350 = (ArrayType::ArraySizeModifier)Record[Idx++];
5351 unsigned IndexTypeQuals = Record[Idx++];
5353 // DependentSizedArrayType
5354 Expr *NumElts = ReadExpr(*Loc.F);
5355 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5357 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5358 IndexTypeQuals, Brackets);
5361 case TYPE_TEMPLATE_SPECIALIZATION: {
5363 bool IsDependent = Record[Idx++];
5364 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5365 SmallVector<TemplateArgument, 8> Args;
5366 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5367 QualType Underlying = readType(*Loc.F, Record, Idx);
5369 if (Underlying.isNull())
5370 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5373 T = Context.getTemplateSpecializationType(Name, Args.data(),
5374 Args.size(), Underlying);
5375 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5380 if (Record.size() != 1) {
5381 Error("Incorrect encoding of atomic type");
5384 QualType ValueType = readType(*Loc.F, Record, Idx);
5385 return Context.getAtomicType(ValueType);
5388 llvm_unreachable("Invalid TypeCode!");
5391 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5392 SmallVectorImpl<QualType> &Exceptions,
5393 FunctionProtoType::ExceptionSpecInfo &ESI,
5394 const RecordData &Record, unsigned &Idx) {
5395 ExceptionSpecificationType EST =
5396 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5398 if (EST == EST_Dynamic) {
5399 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
5400 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5401 ESI.Exceptions = Exceptions;
5402 } else if (EST == EST_ComputedNoexcept) {
5403 ESI.NoexceptExpr = ReadExpr(ModuleFile);
5404 } else if (EST == EST_Uninstantiated) {
5405 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5406 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5407 } else if (EST == EST_Unevaluated) {
5408 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5412 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5415 const ASTReader::RecordData &Record;
5418 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5420 return Reader.ReadSourceLocation(F, R, I);
5423 template<typename T>
5424 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5425 return Reader.ReadDeclAs<T>(F, Record, Idx);
5429 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5430 const ASTReader::RecordData &Record, unsigned &Idx)
5431 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5434 // We want compile-time assurance that we've enumerated all of
5435 // these, so unfortunately we have to declare them first, then
5436 // define them out-of-line.
5437 #define ABSTRACT_TYPELOC(CLASS, PARENT)
5438 #define TYPELOC(CLASS, PARENT) \
5439 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5440 #include "clang/AST/TypeLocNodes.def"
5442 void VisitFunctionTypeLoc(FunctionTypeLoc);
5443 void VisitArrayTypeLoc(ArrayTypeLoc);
5446 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5449 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5450 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5451 if (TL.needsExtraLocalData()) {
5452 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5453 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5454 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5455 TL.setModeAttr(Record[Idx++]);
5458 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5459 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5461 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5462 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5464 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5467 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5470 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5471 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5473 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5474 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5476 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5477 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5479 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5480 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5481 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5483 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5484 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5485 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5487 TL.setSizeExpr(Reader.ReadExpr(F));
5489 TL.setSizeExpr(nullptr);
5491 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5492 VisitArrayTypeLoc(TL);
5494 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5495 VisitArrayTypeLoc(TL);
5497 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5498 VisitArrayTypeLoc(TL);
5500 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5501 DependentSizedArrayTypeLoc TL) {
5502 VisitArrayTypeLoc(TL);
5504 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5505 DependentSizedExtVectorTypeLoc TL) {
5506 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5508 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5509 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5511 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5512 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5514 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5515 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5516 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5517 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5518 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5519 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5520 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5523 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5524 VisitFunctionTypeLoc(TL);
5526 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5527 VisitFunctionTypeLoc(TL);
5529 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5530 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5532 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5533 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5535 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5536 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5537 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5538 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5540 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5541 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5542 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5543 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5544 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5546 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5547 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5549 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5550 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5551 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5552 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5553 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5555 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5556 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5558 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5559 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5561 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5562 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5564 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5565 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5566 if (TL.hasAttrOperand()) {
5568 range.setBegin(ReadSourceLocation(Record, Idx));
5569 range.setEnd(ReadSourceLocation(Record, Idx));
5570 TL.setAttrOperandParensRange(range);
5572 if (TL.hasAttrExprOperand()) {
5574 TL.setAttrExprOperand(Reader.ReadExpr(F));
5576 TL.setAttrExprOperand(nullptr);
5577 } else if (TL.hasAttrEnumOperand())
5578 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5580 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5581 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5583 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5584 SubstTemplateTypeParmTypeLoc TL) {
5585 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5587 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5588 SubstTemplateTypeParmPackTypeLoc TL) {
5589 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5591 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5592 TemplateSpecializationTypeLoc TL) {
5593 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5594 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5595 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5596 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5597 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5599 Reader.GetTemplateArgumentLocInfo(F,
5600 TL.getTypePtr()->getArg(i).getKind(),
5603 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5604 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5605 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5607 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5608 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5609 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5611 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5612 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5614 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5615 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5616 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5617 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5619 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5620 DependentTemplateSpecializationTypeLoc TL) {
5621 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5622 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5623 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5624 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5625 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5626 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5627 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5629 Reader.GetTemplateArgumentLocInfo(F,
5630 TL.getTypePtr()->getArg(I).getKind(),
5633 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5634 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5636 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5637 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5639 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5640 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5641 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5642 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5643 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5644 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5646 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5647 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5649 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5650 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5651 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5652 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5655 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5656 const RecordData &Record,
5658 QualType InfoTy = readType(F, Record, Idx);
5659 if (InfoTy.isNull())
5662 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5663 TypeLocReader TLR(*this, F, Record, Idx);
5664 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5669 QualType ASTReader::GetType(TypeID ID) {
5670 unsigned FastQuals = ID & Qualifiers::FastMask;
5671 unsigned Index = ID >> Qualifiers::FastWidth;
5673 if (Index < NUM_PREDEF_TYPE_IDS) {
5675 switch ((PredefinedTypeIDs)Index) {
5676 case PREDEF_TYPE_NULL_ID: return QualType();
5677 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5678 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5680 case PREDEF_TYPE_CHAR_U_ID:
5681 case PREDEF_TYPE_CHAR_S_ID:
5682 // FIXME: Check that the signedness of CharTy is correct!
5686 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5687 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5688 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5689 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5690 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5691 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5692 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5693 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5694 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5695 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5696 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5697 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5698 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5699 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5700 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5701 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5702 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5703 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5704 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5705 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5706 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5707 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5708 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5709 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5710 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5711 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5712 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5713 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5714 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5715 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5716 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5717 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5718 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5719 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
5720 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
5721 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
5722 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5724 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5725 T = Context.getAutoRRefDeductType();
5728 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5729 T = Context.ARCUnbridgedCastTy;
5732 case PREDEF_TYPE_VA_LIST_TAG:
5733 T = Context.getVaListTagType();
5736 case PREDEF_TYPE_BUILTIN_FN:
5737 T = Context.BuiltinFnTy;
5741 assert(!T.isNull() && "Unknown predefined type");
5742 return T.withFastQualifiers(FastQuals);
5745 Index -= NUM_PREDEF_TYPE_IDS;
5746 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5747 if (TypesLoaded[Index].isNull()) {
5748 TypesLoaded[Index] = readTypeRecord(Index);
5749 if (TypesLoaded[Index].isNull())
5752 TypesLoaded[Index]->setFromAST();
5753 if (DeserializationListener)
5754 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5755 TypesLoaded[Index]);
5758 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5761 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5762 return GetType(getGlobalTypeID(F, LocalID));
5765 serialization::TypeID
5766 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5767 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5768 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5770 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5773 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5774 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5775 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5777 unsigned GlobalIndex = LocalIndex + I->second;
5778 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5781 TemplateArgumentLocInfo
5782 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5783 TemplateArgument::ArgKind Kind,
5784 const RecordData &Record,
5787 case TemplateArgument::Expression:
5789 case TemplateArgument::Type:
5790 return GetTypeSourceInfo(F, Record, Index);
5791 case TemplateArgument::Template: {
5792 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5794 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5795 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5798 case TemplateArgument::TemplateExpansion: {
5799 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5801 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5802 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5803 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5806 case TemplateArgument::Null:
5807 case TemplateArgument::Integral:
5808 case TemplateArgument::Declaration:
5809 case TemplateArgument::NullPtr:
5810 case TemplateArgument::Pack:
5811 // FIXME: Is this right?
5812 return TemplateArgumentLocInfo();
5814 llvm_unreachable("unexpected template argument loc");
5818 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5819 const RecordData &Record, unsigned &Index) {
5820 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5822 if (Arg.getKind() == TemplateArgument::Expression) {
5823 if (Record[Index++]) // bool InfoHasSameExpr.
5824 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5826 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5830 const ASTTemplateArgumentListInfo*
5831 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5832 const RecordData &Record,
5834 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5835 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5836 unsigned NumArgsAsWritten = Record[Index++];
5837 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5838 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5839 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5840 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5843 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5847 template<typename TemplateSpecializationDecl>
5848 static void completeRedeclChainForTemplateSpecialization(Decl *D) {
5849 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
5850 TSD->getSpecializedTemplate()->LoadLazySpecializations();
5853 void ASTReader::CompleteRedeclChain(const Decl *D) {
5854 if (NumCurrentElementsDeserializing) {
5855 // We arrange to not care about the complete redeclaration chain while we're
5856 // deserializing. Just remember that the AST has marked this one as complete
5857 // but that it's not actually complete yet, so we know we still need to
5858 // complete it later.
5859 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
5863 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
5865 // If this is a named declaration, complete it by looking it up
5866 // within its context.
5868 // FIXME: Merging a function definition should merge
5869 // all mergeable entities within it.
5870 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
5871 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
5872 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
5873 auto *II = Name.getAsIdentifierInfo();
5874 if (isa<TranslationUnitDecl>(DC) && II) {
5875 // Outside of C++, we don't have a lookup table for the TU, so update
5876 // the identifier instead. In C++, either way should work fine.
5877 if (II->isOutOfDate())
5878 updateOutOfDateIdentifier(*II);
5881 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
5882 // FIXME: It'd be nice to do something a bit more targeted here.
5883 D->getDeclContext()->decls_begin();
5887 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
5888 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
5889 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
5890 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
5891 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
5892 if (auto *Template = FD->getPrimaryTemplate())
5893 Template->LoadLazySpecializations();
5897 uint64_t ASTReader::ReadCXXCtorInitializersRef(ModuleFile &M,
5898 const RecordData &Record,
5900 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXCtorInitializers) {
5901 Error("malformed AST file: missing C++ ctor initializers");
5905 unsigned LocalID = Record[Idx++];
5906 return getGlobalBitOffset(M, M.CXXCtorInitializersOffsets[LocalID - 1]);
5909 CXXCtorInitializer **
5910 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
5911 RecordLocation Loc = getLocalBitOffset(Offset);
5912 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5913 SavedStreamPosition SavedPosition(Cursor);
5914 Cursor.JumpToBit(Loc.Offset);
5915 ReadingKindTracker ReadingKind(Read_Decl, *this);
5918 unsigned Code = Cursor.ReadCode();
5919 unsigned RecCode = Cursor.readRecord(Code, Record);
5920 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
5921 Error("malformed AST file: missing C++ ctor initializers");
5926 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
5929 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
5930 const RecordData &Record,
5932 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
5933 Error("malformed AST file: missing C++ base specifier");
5937 unsigned LocalID = Record[Idx++];
5938 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5941 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5942 RecordLocation Loc = getLocalBitOffset(Offset);
5943 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5944 SavedStreamPosition SavedPosition(Cursor);
5945 Cursor.JumpToBit(Loc.Offset);
5946 ReadingKindTracker ReadingKind(Read_Decl, *this);
5948 unsigned Code = Cursor.ReadCode();
5949 unsigned RecCode = Cursor.readRecord(Code, Record);
5950 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5951 Error("malformed AST file: missing C++ base specifiers");
5956 unsigned NumBases = Record[Idx++];
5957 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5958 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5959 for (unsigned I = 0; I != NumBases; ++I)
5960 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5964 serialization::DeclID
5965 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5966 if (LocalID < NUM_PREDEF_DECL_IDS)
5969 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5970 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5971 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5973 return LocalID + I->second;
5976 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5977 ModuleFile &M) const {
5978 // Predefined decls aren't from any module.
5979 if (ID < NUM_PREDEF_DECL_IDS)
5982 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5983 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5984 return &M == I->second;
5987 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
5988 if (!D->isFromASTFile())
5990 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5991 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5995 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5996 if (ID < NUM_PREDEF_DECL_IDS)
5997 return SourceLocation();
5999 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6001 if (Index > DeclsLoaded.size()) {
6002 Error("declaration ID out-of-range for AST file");
6003 return SourceLocation();
6006 if (Decl *D = DeclsLoaded[Index])
6007 return D->getLocation();
6009 unsigned RawLocation = 0;
6010 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6011 return ReadSourceLocation(*Rec.F, RawLocation);
6014 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6016 case PREDEF_DECL_NULL_ID:
6019 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6020 return Context.getTranslationUnitDecl();
6022 case PREDEF_DECL_OBJC_ID_ID:
6023 return Context.getObjCIdDecl();
6025 case PREDEF_DECL_OBJC_SEL_ID:
6026 return Context.getObjCSelDecl();
6028 case PREDEF_DECL_OBJC_CLASS_ID:
6029 return Context.getObjCClassDecl();
6031 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6032 return Context.getObjCProtocolDecl();
6034 case PREDEF_DECL_INT_128_ID:
6035 return Context.getInt128Decl();
6037 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6038 return Context.getUInt128Decl();
6040 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6041 return Context.getObjCInstanceTypeDecl();
6043 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6044 return Context.getBuiltinVaListDecl();
6046 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6047 return Context.getExternCContextDecl();
6049 llvm_unreachable("PredefinedDeclIDs unknown enum value");
6052 Decl *ASTReader::GetExistingDecl(DeclID ID) {
6053 if (ID < NUM_PREDEF_DECL_IDS) {
6054 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6056 // Track that we have merged the declaration with ID \p ID into the
6057 // pre-existing predefined declaration \p D.
6058 auto &Merged = MergedDecls[D->getCanonicalDecl()];
6060 Merged.push_back(ID);
6065 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6067 if (Index >= DeclsLoaded.size()) {
6068 assert(0 && "declaration ID out-of-range for AST file");
6069 Error("declaration ID out-of-range for AST file");
6073 return DeclsLoaded[Index];
6076 Decl *ASTReader::GetDecl(DeclID ID) {
6077 if (ID < NUM_PREDEF_DECL_IDS)
6078 return GetExistingDecl(ID);
6080 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6082 if (Index >= DeclsLoaded.size()) {
6083 assert(0 && "declaration ID out-of-range for AST file");
6084 Error("declaration ID out-of-range for AST file");
6088 if (!DeclsLoaded[Index]) {
6090 if (DeserializationListener)
6091 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6094 return DeclsLoaded[Index];
6097 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6099 if (GlobalID < NUM_PREDEF_DECL_IDS)
6102 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6103 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6104 ModuleFile *Owner = I->second;
6106 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6107 = M.GlobalToLocalDeclIDs.find(Owner);
6108 if (Pos == M.GlobalToLocalDeclIDs.end())
6111 return GlobalID - Owner->BaseDeclID + Pos->second;
6114 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6115 const RecordData &Record,
6117 if (Idx >= Record.size()) {
6118 Error("Corrupted AST file");
6122 return getGlobalDeclID(F, Record[Idx++]);
6125 /// \brief Resolve the offset of a statement into a statement.
6127 /// This operation will read a new statement from the external
6128 /// source each time it is called, and is meant to be used via a
6129 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6130 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6131 // Switch case IDs are per Decl.
6132 ClearSwitchCaseIDs();
6134 // Offset here is a global offset across the entire chain.
6135 RecordLocation Loc = getLocalBitOffset(Offset);
6136 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6137 return ReadStmtFromStream(*Loc.F);
6141 class FindExternalLexicalDeclsVisitor {
6143 const DeclContext *DC;
6144 bool (*isKindWeWant)(Decl::Kind);
6146 SmallVectorImpl<Decl*> &Decls;
6147 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6150 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6151 bool (*isKindWeWant)(Decl::Kind),
6152 SmallVectorImpl<Decl*> &Decls)
6153 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6155 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6156 PredefsVisited[I] = false;
6159 static bool visitPostorder(ModuleFile &M, void *UserData) {
6160 FindExternalLexicalDeclsVisitor *This
6161 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6163 ModuleFile::DeclContextInfosMap::iterator Info
6164 = M.DeclContextInfos.find(This->DC);
6165 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6168 // Load all of the declaration IDs
6169 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6170 *IDE = ID + Info->second.NumLexicalDecls;
6172 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6175 // Don't add predefined declarations to the lexical context more
6177 if (ID->second < NUM_PREDEF_DECL_IDS) {
6178 if (This->PredefsVisited[ID->second])
6181 This->PredefsVisited[ID->second] = true;
6184 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6185 if (!This->DC->isDeclInLexicalTraversal(D))
6186 This->Decls.push_back(D);
6195 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6196 bool (*isKindWeWant)(Decl::Kind),
6197 SmallVectorImpl<Decl*> &Decls) {
6198 // There might be lexical decls in multiple modules, for the TU at
6199 // least. Walk all of the modules in the order they were loaded.
6200 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6201 ModuleMgr.visitDepthFirst(
6202 nullptr, &FindExternalLexicalDeclsVisitor::visitPostorder, &Visitor);
6203 ++NumLexicalDeclContextsRead;
6214 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6216 bool operator()(LocalDeclID L, LocalDeclID R) const {
6217 SourceLocation LHS = getLocation(L);
6218 SourceLocation RHS = getLocation(R);
6219 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6222 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6223 SourceLocation RHS = getLocation(R);
6224 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6227 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6228 SourceLocation LHS = getLocation(L);
6229 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6232 SourceLocation getLocation(LocalDeclID ID) const {
6233 return Reader.getSourceManager().getFileLoc(
6234 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6240 void ASTReader::FindFileRegionDecls(FileID File,
6241 unsigned Offset, unsigned Length,
6242 SmallVectorImpl<Decl *> &Decls) {
6243 SourceManager &SM = getSourceManager();
6245 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6246 if (I == FileDeclIDs.end())
6249 FileDeclsInfo &DInfo = I->second;
6250 if (DInfo.Decls.empty())
6254 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6255 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6257 DeclIDComp DIDComp(*this, *DInfo.Mod);
6258 ArrayRef<serialization::LocalDeclID>::iterator
6259 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6261 if (BeginIt != DInfo.Decls.begin())
6264 // If we are pointing at a top-level decl inside an objc container, we need
6265 // to backtrack until we find it otherwise we will fail to report that the
6266 // region overlaps with an objc container.
6267 while (BeginIt != DInfo.Decls.begin() &&
6268 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6269 ->isTopLevelDeclInObjCContainer())
6272 ArrayRef<serialization::LocalDeclID>::iterator
6273 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6275 if (EndIt != DInfo.Decls.end())
6278 for (ArrayRef<serialization::LocalDeclID>::iterator
6279 DIt = BeginIt; DIt != EndIt; ++DIt)
6280 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6284 /// \brief ModuleFile visitor used to perform name lookup into a
6285 /// declaration context.
6286 class DeclContextNameLookupVisitor {
6288 ArrayRef<const DeclContext *> Contexts;
6289 DeclarationName Name;
6290 SmallVectorImpl<NamedDecl *> &Decls;
6291 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet;
6294 DeclContextNameLookupVisitor(ASTReader &Reader,
6295 ArrayRef<const DeclContext *> Contexts,
6296 DeclarationName Name,
6297 SmallVectorImpl<NamedDecl *> &Decls,
6298 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet)
6299 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls),
6300 DeclSet(DeclSet) { }
6302 static bool visit(ModuleFile &M, void *UserData) {
6303 DeclContextNameLookupVisitor *This
6304 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6306 // Check whether we have any visible declaration information for
6307 // this context in this module.
6308 ModuleFile::DeclContextInfosMap::iterator Info;
6309 bool FoundInfo = false;
6310 for (auto *DC : This->Contexts) {
6311 Info = M.DeclContextInfos.find(DC);
6312 if (Info != M.DeclContextInfos.end() &&
6313 Info->second.NameLookupTableData) {
6322 // Look for this name within this module.
6323 ASTDeclContextNameLookupTable *LookupTable =
6324 Info->second.NameLookupTableData;
6325 ASTDeclContextNameLookupTable::iterator Pos
6326 = LookupTable->find(This->Name);
6327 if (Pos == LookupTable->end())
6330 bool FoundAnything = false;
6331 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6332 for (; Data.first != Data.second; ++Data.first) {
6333 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6337 if (ND->getDeclName() != This->Name) {
6338 // A name might be null because the decl's redeclarable part is
6339 // currently read before reading its name. The lookup is triggered by
6340 // building that decl (likely indirectly), and so it is later in the
6341 // sense of "already existing" and can be ignored here.
6342 // FIXME: This should not happen; deserializing declarations should
6343 // not perform lookups since that can lead to deserialization cycles.
6347 // Record this declaration.
6348 FoundAnything = true;
6349 if (This->DeclSet.insert(ND).second)
6350 This->Decls.push_back(ND);
6353 return FoundAnything;
6358 /// \brief Retrieve the "definitive" module file for the definition of the
6359 /// given declaration context, if there is one.
6361 /// The "definitive" module file is the only place where we need to look to
6362 /// find information about the declarations within the given declaration
6363 /// context. For example, C++ and Objective-C classes, C structs/unions, and
6364 /// Objective-C protocols, categories, and extensions are all defined in a
6365 /// single place in the source code, so they have definitive module files
6366 /// associated with them. C++ namespaces, on the other hand, can have
6367 /// definitions in multiple different module files.
6369 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6370 /// NDEBUG checking.
6371 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6372 ASTReader &Reader) {
6373 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6374 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
6380 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6381 DeclarationName Name) {
6382 assert(DC->hasExternalVisibleStorage() &&
6383 "DeclContext has no visible decls in storage");
6387 Deserializing LookupResults(this);
6389 SmallVector<NamedDecl *, 64> Decls;
6390 llvm::SmallPtrSet<NamedDecl*, 64> DeclSet;
6392 // Compute the declaration contexts we need to look into. Multiple such
6393 // declaration contexts occur when two declaration contexts from disjoint
6394 // modules get merged, e.g., when two namespaces with the same name are
6395 // independently defined in separate modules.
6396 SmallVector<const DeclContext *, 2> Contexts;
6397 Contexts.push_back(DC);
6399 if (DC->isNamespace()) {
6400 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6401 if (Merged != MergedDecls.end()) {
6402 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6403 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6407 auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
6408 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls, DeclSet);
6410 // If we can definitively determine which module file to look into,
6411 // only look there. Otherwise, look in all module files.
6412 ModuleFile *Definitive;
6413 if (Contexts.size() == 1 &&
6414 (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6415 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6417 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6421 LookUpInContexts(Contexts);
6423 // If this might be an implicit special member function, then also search
6424 // all merged definitions of the surrounding class. We need to search them
6425 // individually, because finding an entity in one of them doesn't imply that
6426 // we can't find a different entity in another one.
6427 if (isa<CXXRecordDecl>(DC)) {
6428 auto Merged = MergedLookups.find(DC);
6429 if (Merged != MergedLookups.end()) {
6430 for (unsigned I = 0; I != Merged->second.size(); ++I) {
6431 const DeclContext *Context = Merged->second[I];
6432 LookUpInContexts(Context);
6433 // We might have just added some more merged lookups. If so, our
6434 // iterator is now invalid, so grab a fresh one before continuing.
6435 Merged = MergedLookups.find(DC);
6440 ++NumVisibleDeclContextsRead;
6441 SetExternalVisibleDeclsForName(DC, Name, Decls);
6442 return !Decls.empty();
6446 /// \brief ModuleFile visitor used to retrieve all visible names in a
6447 /// declaration context.
6448 class DeclContextAllNamesVisitor {
6450 SmallVectorImpl<const DeclContext *> &Contexts;
6452 llvm::SmallPtrSet<NamedDecl *, 256> DeclSet;
6456 DeclContextAllNamesVisitor(ASTReader &Reader,
6457 SmallVectorImpl<const DeclContext *> &Contexts,
6458 DeclsMap &Decls, bool VisitAll)
6459 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
6461 static bool visit(ModuleFile &M, void *UserData) {
6462 DeclContextAllNamesVisitor *This
6463 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6465 // Check whether we have any visible declaration information for
6466 // this context in this module.
6467 ModuleFile::DeclContextInfosMap::iterator Info;
6468 bool FoundInfo = false;
6469 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6470 Info = M.DeclContextInfos.find(This->Contexts[I]);
6471 if (Info != M.DeclContextInfos.end() &&
6472 Info->second.NameLookupTableData) {
6481 ASTDeclContextNameLookupTable *LookupTable =
6482 Info->second.NameLookupTableData;
6483 bool FoundAnything = false;
6484 for (ASTDeclContextNameLookupTable::data_iterator
6485 I = LookupTable->data_begin(), E = LookupTable->data_end();
6488 ASTDeclContextNameLookupTrait::data_type Data = *I;
6489 for (; Data.first != Data.second; ++Data.first) {
6490 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6495 // Record this declaration.
6496 FoundAnything = true;
6497 if (This->DeclSet.insert(ND).second)
6498 This->Decls[ND->getDeclName()].push_back(ND);
6502 return FoundAnything && !This->VisitAll;
6507 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6508 if (!DC->hasExternalVisibleStorage())
6512 // Compute the declaration contexts we need to look into. Multiple such
6513 // declaration contexts occur when two declaration contexts from disjoint
6514 // modules get merged, e.g., when two namespaces with the same name are
6515 // independently defined in separate modules.
6516 SmallVector<const DeclContext *, 2> Contexts;
6517 Contexts.push_back(DC);
6519 if (DC->isNamespace()) {
6520 MergedDeclsMap::iterator Merged
6521 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6522 if (Merged != MergedDecls.end()) {
6523 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6524 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6528 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6529 /*VisitAll=*/DC->isFileContext());
6530 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6531 ++NumVisibleDeclContextsRead;
6533 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
6534 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6536 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6539 /// \brief Under non-PCH compilation the consumer receives the objc methods
6540 /// before receiving the implementation, and codegen depends on this.
6541 /// We simulate this by deserializing and passing to consumer the methods of the
6542 /// implementation before passing the deserialized implementation decl.
6543 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6544 ASTConsumer *Consumer) {
6545 assert(ImplD && Consumer);
6547 for (auto *I : ImplD->methods())
6548 Consumer->HandleInterestingDecl(DeclGroupRef(I));
6550 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6553 void ASTReader::PassInterestingDeclsToConsumer() {
6556 if (PassingDeclsToConsumer)
6559 // Guard variable to avoid recursively redoing the process of passing
6560 // decls to consumer.
6561 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6564 // Ensure that we've loaded all potentially-interesting declarations
6565 // that need to be eagerly loaded.
6566 for (auto ID : EagerlyDeserializedDecls)
6568 EagerlyDeserializedDecls.clear();
6570 while (!InterestingDecls.empty()) {
6571 Decl *D = InterestingDecls.front();
6572 InterestingDecls.pop_front();
6574 PassInterestingDeclToConsumer(D);
6578 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6579 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6580 PassObjCImplDeclToConsumer(ImplD, Consumer);
6582 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6585 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6586 this->Consumer = Consumer;
6589 PassInterestingDeclsToConsumer();
6591 if (DeserializationListener)
6592 DeserializationListener->ReaderInitialized(this);
6595 void ASTReader::PrintStats() {
6596 std::fprintf(stderr, "*** AST File Statistics:\n");
6598 unsigned NumTypesLoaded
6599 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6601 unsigned NumDeclsLoaded
6602 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6604 unsigned NumIdentifiersLoaded
6605 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6606 IdentifiersLoaded.end(),
6607 (IdentifierInfo *)nullptr);
6608 unsigned NumMacrosLoaded
6609 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6611 (MacroInfo *)nullptr);
6612 unsigned NumSelectorsLoaded
6613 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6614 SelectorsLoaded.end(),
6617 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6618 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6619 NumSLocEntriesRead, TotalNumSLocEntries,
6620 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6621 if (!TypesLoaded.empty())
6622 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6623 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6624 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6625 if (!DeclsLoaded.empty())
6626 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6627 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6628 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6629 if (!IdentifiersLoaded.empty())
6630 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6631 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6632 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6633 if (!MacrosLoaded.empty())
6634 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6635 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6636 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6637 if (!SelectorsLoaded.empty())
6638 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6639 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6640 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6641 if (TotalNumStatements)
6642 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6643 NumStatementsRead, TotalNumStatements,
6644 ((float)NumStatementsRead/TotalNumStatements * 100));
6646 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6647 NumMacrosRead, TotalNumMacros,
6648 ((float)NumMacrosRead/TotalNumMacros * 100));
6649 if (TotalLexicalDeclContexts)
6650 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6651 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6652 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6654 if (TotalVisibleDeclContexts)
6655 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6656 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6657 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6659 if (TotalNumMethodPoolEntries) {
6660 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6661 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6662 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6665 if (NumMethodPoolLookups) {
6666 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6667 NumMethodPoolHits, NumMethodPoolLookups,
6668 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6670 if (NumMethodPoolTableLookups) {
6671 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6672 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6673 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6677 if (NumIdentifierLookupHits) {
6678 std::fprintf(stderr,
6679 " %u / %u identifier table lookups succeeded (%f%%)\n",
6680 NumIdentifierLookupHits, NumIdentifierLookups,
6681 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6685 std::fprintf(stderr, "\n");
6686 GlobalIndex->printStats();
6689 std::fprintf(stderr, "\n");
6691 std::fprintf(stderr, "\n");
6694 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6696 dumpModuleIDMap(StringRef Name,
6697 const ContinuousRangeMap<Key, ModuleFile *,
6698 InitialCapacity> &Map) {
6699 if (Map.begin() == Map.end())
6702 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6703 llvm::errs() << Name << ":\n";
6704 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6706 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6711 void ASTReader::dump() {
6712 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6713 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6714 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6715 dumpModuleIDMap("Global type map", GlobalTypeMap);
6716 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6717 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6718 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6719 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6720 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6721 dumpModuleIDMap("Global preprocessed entity map",
6722 GlobalPreprocessedEntityMap);
6724 llvm::errs() << "\n*** PCH/Modules Loaded:";
6725 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6726 MEnd = ModuleMgr.end();
6731 /// Return the amount of memory used by memory buffers, breaking down
6732 /// by heap-backed versus mmap'ed memory.
6733 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6734 for (ModuleConstIterator I = ModuleMgr.begin(),
6735 E = ModuleMgr.end(); I != E; ++I) {
6736 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6737 size_t bytes = buf->getBufferSize();
6738 switch (buf->getBufferKind()) {
6739 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6740 sizes.malloc_bytes += bytes;
6742 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6743 sizes.mmap_bytes += bytes;
6750 void ASTReader::InitializeSema(Sema &S) {
6752 S.addExternalSource(this);
6754 // Makes sure any declarations that were deserialized "too early"
6755 // still get added to the identifier's declaration chains.
6756 for (uint64_t ID : PreloadedDeclIDs) {
6757 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6758 pushExternalDeclIntoScope(D, D->getDeclName());
6760 PreloadedDeclIDs.clear();
6762 // FIXME: What happens if these are changed by a module import?
6763 if (!FPPragmaOptions.empty()) {
6764 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6765 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6768 // FIXME: What happens if these are changed by a module import?
6769 if (!OpenCLExtensions.empty()) {
6771 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6772 #include "clang/Basic/OpenCLExtensions.def"
6774 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6780 void ASTReader::UpdateSema() {
6781 assert(SemaObj && "no Sema to update");
6783 // Load the offsets of the declarations that Sema references.
6784 // They will be lazily deserialized when needed.
6785 if (!SemaDeclRefs.empty()) {
6786 assert(SemaDeclRefs.size() % 2 == 0);
6787 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6788 if (!SemaObj->StdNamespace)
6789 SemaObj->StdNamespace = SemaDeclRefs[I];
6790 if (!SemaObj->StdBadAlloc)
6791 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6793 SemaDeclRefs.clear();
6796 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6797 // encountered the pragma in the source.
6798 if(OptimizeOffPragmaLocation.isValid())
6799 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
6802 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6803 // Note that we are loading an identifier.
6804 Deserializing AnIdentifier(this);
6805 StringRef Name(NameStart, NameEnd - NameStart);
6807 // If there is a global index, look there first to determine which modules
6808 // provably do not have any results for this identifier.
6809 GlobalModuleIndex::HitSet Hits;
6810 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
6811 if (!loadGlobalIndex()) {
6812 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6816 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6817 NumIdentifierLookups,
6818 NumIdentifierLookupHits);
6819 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6820 IdentifierInfo *II = Visitor.getIdentifierInfo();
6821 markIdentifierUpToDate(II);
6826 /// \brief An identifier-lookup iterator that enumerates all of the
6827 /// identifiers stored within a set of AST files.
6828 class ASTIdentifierIterator : public IdentifierIterator {
6829 /// \brief The AST reader whose identifiers are being enumerated.
6830 const ASTReader &Reader;
6832 /// \brief The current index into the chain of AST files stored in
6836 /// \brief The current position within the identifier lookup table
6837 /// of the current AST file.
6838 ASTIdentifierLookupTable::key_iterator Current;
6840 /// \brief The end position within the identifier lookup table of
6841 /// the current AST file.
6842 ASTIdentifierLookupTable::key_iterator End;
6845 explicit ASTIdentifierIterator(const ASTReader &Reader);
6847 StringRef Next() override;
6851 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6852 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6853 ASTIdentifierLookupTable *IdTable
6854 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6855 Current = IdTable->key_begin();
6856 End = IdTable->key_end();
6859 StringRef ASTIdentifierIterator::Next() {
6860 while (Current == End) {
6861 // If we have exhausted all of our AST files, we're done.
6866 ASTIdentifierLookupTable *IdTable
6867 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6868 IdentifierLookupTable;
6869 Current = IdTable->key_begin();
6870 End = IdTable->key_end();
6873 // We have any identifiers remaining in the current AST file; return
6875 StringRef Result = *Current;
6880 IdentifierIterator *ASTReader::getIdentifiers() {
6881 if (!loadGlobalIndex())
6882 return GlobalIndex->createIdentifierIterator();
6884 return new ASTIdentifierIterator(*this);
6887 namespace clang { namespace serialization {
6888 class ReadMethodPoolVisitor {
6891 unsigned PriorGeneration;
6892 unsigned InstanceBits;
6893 unsigned FactoryBits;
6894 bool InstanceHasMoreThanOneDecl;
6895 bool FactoryHasMoreThanOneDecl;
6896 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6897 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
6900 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6901 unsigned PriorGeneration)
6902 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6903 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
6904 FactoryHasMoreThanOneDecl(false) {}
6906 static bool visit(ModuleFile &M, void *UserData) {
6907 ReadMethodPoolVisitor *This
6908 = static_cast<ReadMethodPoolVisitor *>(UserData);
6910 if (!M.SelectorLookupTable)
6913 // If we've already searched this module file, skip it now.
6914 if (M.Generation <= This->PriorGeneration)
6917 ++This->Reader.NumMethodPoolTableLookups;
6918 ASTSelectorLookupTable *PoolTable
6919 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6920 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6921 if (Pos == PoolTable->end())
6924 ++This->Reader.NumMethodPoolTableHits;
6925 ++This->Reader.NumSelectorsRead;
6926 // FIXME: Not quite happy with the statistics here. We probably should
6927 // disable this tracking when called via LoadSelector.
6928 // Also, should entries without methods count as misses?
6929 ++This->Reader.NumMethodPoolEntriesRead;
6930 ASTSelectorLookupTrait::data_type Data = *Pos;
6931 if (This->Reader.DeserializationListener)
6932 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6935 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6936 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6937 This->InstanceBits = Data.InstanceBits;
6938 This->FactoryBits = Data.FactoryBits;
6939 This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
6940 This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
6944 /// \brief Retrieve the instance methods found by this visitor.
6945 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6946 return InstanceMethods;
6949 /// \brief Retrieve the instance methods found by this visitor.
6950 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6951 return FactoryMethods;
6954 unsigned getInstanceBits() const { return InstanceBits; }
6955 unsigned getFactoryBits() const { return FactoryBits; }
6956 bool instanceHasMoreThanOneDecl() const {
6957 return InstanceHasMoreThanOneDecl;
6959 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
6961 } } // end namespace clang::serialization
6963 /// \brief Add the given set of methods to the method list.
6964 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6965 ObjCMethodList &List) {
6966 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6967 S.addMethodToGlobalList(&List, Methods[I]);
6971 void ASTReader::ReadMethodPool(Selector Sel) {
6972 // Get the selector generation and update it to the current generation.
6973 unsigned &Generation = SelectorGeneration[Sel];
6974 unsigned PriorGeneration = Generation;
6975 Generation = getGeneration();
6977 // Search for methods defined with this selector.
6978 ++NumMethodPoolLookups;
6979 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6980 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6982 if (Visitor.getInstanceMethods().empty() &&
6983 Visitor.getFactoryMethods().empty())
6986 ++NumMethodPoolHits;
6991 Sema &S = *getSema();
6992 Sema::GlobalMethodPool::iterator Pos
6993 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6995 Pos->second.first.setBits(Visitor.getInstanceBits());
6996 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
6997 Pos->second.second.setBits(Visitor.getFactoryBits());
6998 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
7000 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7001 // when building a module we keep every method individually and may need to
7002 // update hasMoreThanOneDecl as we add the methods.
7003 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7004 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
7007 void ASTReader::ReadKnownNamespaces(
7008 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7011 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7012 if (NamespaceDecl *Namespace
7013 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7014 Namespaces.push_back(Namespace);
7018 void ASTReader::ReadUndefinedButUsed(
7019 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
7020 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7021 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
7022 SourceLocation Loc =
7023 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
7024 Undefined.insert(std::make_pair(D, Loc));
7028 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7029 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7031 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7032 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7033 uint64_t Count = DelayedDeleteExprs[Idx++];
7034 for (uint64_t C = 0; C < Count; ++C) {
7035 SourceLocation DeleteLoc =
7036 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7037 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7038 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7043 void ASTReader::ReadTentativeDefinitions(
7044 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7045 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7046 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7048 TentativeDefs.push_back(Var);
7050 TentativeDefinitions.clear();
7053 void ASTReader::ReadUnusedFileScopedDecls(
7054 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7055 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7057 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7061 UnusedFileScopedDecls.clear();
7064 void ASTReader::ReadDelegatingConstructors(
7065 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7066 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7067 CXXConstructorDecl *D
7068 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7072 DelegatingCtorDecls.clear();
7075 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7076 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7078 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7082 ExtVectorDecls.clear();
7085 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7086 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7087 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7089 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7090 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7094 UnusedLocalTypedefNameCandidates.clear();
7097 void ASTReader::ReadReferencedSelectors(
7098 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7099 if (ReferencedSelectorsData.empty())
7102 // If there are @selector references added them to its pool. This is for
7103 // implementation of -Wselector.
7104 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7106 while (I < DataSize) {
7107 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7108 SourceLocation SelLoc
7109 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7110 Sels.push_back(std::make_pair(Sel, SelLoc));
7112 ReferencedSelectorsData.clear();
7115 void ASTReader::ReadWeakUndeclaredIdentifiers(
7116 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7117 if (WeakUndeclaredIdentifiers.empty())
7120 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7121 IdentifierInfo *WeakId
7122 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7123 IdentifierInfo *AliasId
7124 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7126 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7127 bool Used = WeakUndeclaredIdentifiers[I++];
7128 WeakInfo WI(AliasId, Loc);
7130 WeakIDs.push_back(std::make_pair(WeakId, WI));
7132 WeakUndeclaredIdentifiers.clear();
7135 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7136 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7137 ExternalVTableUse VT;
7138 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7139 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7140 VT.DefinitionRequired = VTableUses[Idx++];
7141 VTables.push_back(VT);
7147 void ASTReader::ReadPendingInstantiations(
7148 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7149 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7150 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7152 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7154 Pending.push_back(std::make_pair(D, Loc));
7156 PendingInstantiations.clear();
7159 void ASTReader::ReadLateParsedTemplates(
7160 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7161 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7163 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7165 LateParsedTemplate *LT = new LateParsedTemplate;
7166 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7168 ModuleFile *F = getOwningModuleFile(LT->D);
7169 assert(F && "No module");
7171 unsigned TokN = LateParsedTemplates[Idx++];
7172 LT->Toks.reserve(TokN);
7173 for (unsigned T = 0; T < TokN; ++T)
7174 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7176 LPTMap.insert(std::make_pair(FD, LT));
7179 LateParsedTemplates.clear();
7182 void ASTReader::LoadSelector(Selector Sel) {
7183 // It would be complicated to avoid reading the methods anyway. So don't.
7184 ReadMethodPool(Sel);
7187 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7188 assert(ID && "Non-zero identifier ID required");
7189 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7190 IdentifiersLoaded[ID - 1] = II;
7191 if (DeserializationListener)
7192 DeserializationListener->IdentifierRead(ID, II);
7195 /// \brief Set the globally-visible declarations associated with the given
7198 /// If the AST reader is currently in a state where the given declaration IDs
7199 /// cannot safely be resolved, they are queued until it is safe to resolve
7202 /// \param II an IdentifierInfo that refers to one or more globally-visible
7205 /// \param DeclIDs the set of declaration IDs with the name @p II that are
7206 /// visible at global scope.
7208 /// \param Decls if non-null, this vector will be populated with the set of
7209 /// deserialized declarations. These declarations will not be pushed into
7212 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7213 const SmallVectorImpl<uint32_t> &DeclIDs,
7214 SmallVectorImpl<Decl *> *Decls) {
7215 if (NumCurrentElementsDeserializing && !Decls) {
7216 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
7220 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7222 // Queue this declaration so that it will be added to the
7223 // translation unit scope and identifier's declaration chain
7224 // once a Sema object is known.
7225 PreloadedDeclIDs.push_back(DeclIDs[I]);
7229 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7231 // If we're simply supposed to record the declarations, do so now.
7233 Decls->push_back(D);
7237 // Introduce this declaration into the translation-unit scope
7238 // and add it to the declaration chain for this identifier, so
7239 // that (unqualified) name lookup will find it.
7240 pushExternalDeclIntoScope(D, II);
7244 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
7248 if (IdentifiersLoaded.empty()) {
7249 Error("no identifier table in AST file");
7254 if (!IdentifiersLoaded[ID]) {
7255 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7256 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7257 ModuleFile *M = I->second;
7258 unsigned Index = ID - M->BaseIdentifierID;
7259 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7261 // All of the strings in the AST file are preceded by a 16-bit length.
7262 // Extract that 16-bit length to avoid having to execute strlen().
7263 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7264 // unsigned integers. This is important to avoid integer overflow when
7265 // we cast them to 'unsigned'.
7266 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7267 unsigned StrLen = (((unsigned) StrLenPtr[0])
7268 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
7269 IdentifiersLoaded[ID]
7270 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
7271 if (DeserializationListener)
7272 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7275 return IdentifiersLoaded[ID];
7278 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7279 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
7282 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7283 if (LocalID < NUM_PREDEF_IDENT_IDS)
7286 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7287 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7288 assert(I != M.IdentifierRemap.end()
7289 && "Invalid index into identifier index remap");
7291 return LocalID + I->second;
7294 MacroInfo *ASTReader::getMacro(MacroID ID) {
7298 if (MacrosLoaded.empty()) {
7299 Error("no macro table in AST file");
7303 ID -= NUM_PREDEF_MACRO_IDS;
7304 if (!MacrosLoaded[ID]) {
7305 GlobalMacroMapType::iterator I
7306 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7307 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7308 ModuleFile *M = I->second;
7309 unsigned Index = ID - M->BaseMacroID;
7310 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7312 if (DeserializationListener)
7313 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7317 return MacrosLoaded[ID];
7320 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7321 if (LocalID < NUM_PREDEF_MACRO_IDS)
7324 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7325 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7326 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7328 return LocalID + I->second;
7331 serialization::SubmoduleID
7332 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7333 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7336 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7337 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7338 assert(I != M.SubmoduleRemap.end()
7339 && "Invalid index into submodule index remap");
7341 return LocalID + I->second;
7344 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7345 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7346 assert(GlobalID == 0 && "Unhandled global submodule ID");
7350 if (GlobalID > SubmodulesLoaded.size()) {
7351 Error("submodule ID out of range in AST file");
7355 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7358 Module *ASTReader::getModule(unsigned ID) {
7359 return getSubmodule(ID);
7362 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7363 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7366 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7370 if (ID > SelectorsLoaded.size()) {
7371 Error("selector ID out of range in AST file");
7375 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
7376 // Load this selector from the selector table.
7377 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7378 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7379 ModuleFile &M = *I->second;
7380 ASTSelectorLookupTrait Trait(*this, M);
7381 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7382 SelectorsLoaded[ID - 1] =
7383 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7384 if (DeserializationListener)
7385 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7388 return SelectorsLoaded[ID - 1];
7391 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7392 return DecodeSelector(ID);
7395 uint32_t ASTReader::GetNumExternalSelectors() {
7396 // ID 0 (the null selector) is considered an external selector.
7397 return getTotalNumSelectors() + 1;
7400 serialization::SelectorID
7401 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7402 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7405 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7406 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7407 assert(I != M.SelectorRemap.end()
7408 && "Invalid index into selector index remap");
7410 return LocalID + I->second;
7414 ASTReader::ReadDeclarationName(ModuleFile &F,
7415 const RecordData &Record, unsigned &Idx) {
7416 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7418 case DeclarationName::Identifier:
7419 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
7421 case DeclarationName::ObjCZeroArgSelector:
7422 case DeclarationName::ObjCOneArgSelector:
7423 case DeclarationName::ObjCMultiArgSelector:
7424 return DeclarationName(ReadSelector(F, Record, Idx));
7426 case DeclarationName::CXXConstructorName:
7427 return Context.DeclarationNames.getCXXConstructorName(
7428 Context.getCanonicalType(readType(F, Record, Idx)));
7430 case DeclarationName::CXXDestructorName:
7431 return Context.DeclarationNames.getCXXDestructorName(
7432 Context.getCanonicalType(readType(F, Record, Idx)));
7434 case DeclarationName::CXXConversionFunctionName:
7435 return Context.DeclarationNames.getCXXConversionFunctionName(
7436 Context.getCanonicalType(readType(F, Record, Idx)));
7438 case DeclarationName::CXXOperatorName:
7439 return Context.DeclarationNames.getCXXOperatorName(
7440 (OverloadedOperatorKind)Record[Idx++]);
7442 case DeclarationName::CXXLiteralOperatorName:
7443 return Context.DeclarationNames.getCXXLiteralOperatorName(
7444 GetIdentifierInfo(F, Record, Idx));
7446 case DeclarationName::CXXUsingDirective:
7447 return DeclarationName::getUsingDirectiveName();
7450 llvm_unreachable("Invalid NameKind!");
7453 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7454 DeclarationNameLoc &DNLoc,
7455 DeclarationName Name,
7456 const RecordData &Record, unsigned &Idx) {
7457 switch (Name.getNameKind()) {
7458 case DeclarationName::CXXConstructorName:
7459 case DeclarationName::CXXDestructorName:
7460 case DeclarationName::CXXConversionFunctionName:
7461 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7464 case DeclarationName::CXXOperatorName:
7465 DNLoc.CXXOperatorName.BeginOpNameLoc
7466 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7467 DNLoc.CXXOperatorName.EndOpNameLoc
7468 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7471 case DeclarationName::CXXLiteralOperatorName:
7472 DNLoc.CXXLiteralOperatorName.OpNameLoc
7473 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7476 case DeclarationName::Identifier:
7477 case DeclarationName::ObjCZeroArgSelector:
7478 case DeclarationName::ObjCOneArgSelector:
7479 case DeclarationName::ObjCMultiArgSelector:
7480 case DeclarationName::CXXUsingDirective:
7485 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7486 DeclarationNameInfo &NameInfo,
7487 const RecordData &Record, unsigned &Idx) {
7488 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7489 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7490 DeclarationNameLoc DNLoc;
7491 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7492 NameInfo.setInfo(DNLoc);
7495 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7496 const RecordData &Record, unsigned &Idx) {
7497 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7498 unsigned NumTPLists = Record[Idx++];
7499 Info.NumTemplParamLists = NumTPLists;
7501 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7502 for (unsigned i=0; i != NumTPLists; ++i)
7503 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7508 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7510 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7512 case TemplateName::Template:
7513 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7515 case TemplateName::OverloadedTemplate: {
7516 unsigned size = Record[Idx++];
7517 UnresolvedSet<8> Decls;
7519 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7521 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7524 case TemplateName::QualifiedTemplate: {
7525 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7526 bool hasTemplKeyword = Record[Idx++];
7527 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7528 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7531 case TemplateName::DependentTemplate: {
7532 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7533 if (Record[Idx++]) // isIdentifier
7534 return Context.getDependentTemplateName(NNS,
7535 GetIdentifierInfo(F, Record,
7537 return Context.getDependentTemplateName(NNS,
7538 (OverloadedOperatorKind)Record[Idx++]);
7541 case TemplateName::SubstTemplateTemplateParm: {
7542 TemplateTemplateParmDecl *param
7543 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7544 if (!param) return TemplateName();
7545 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7546 return Context.getSubstTemplateTemplateParm(param, replacement);
7549 case TemplateName::SubstTemplateTemplateParmPack: {
7550 TemplateTemplateParmDecl *Param
7551 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7553 return TemplateName();
7555 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7556 if (ArgPack.getKind() != TemplateArgument::Pack)
7557 return TemplateName();
7559 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7563 llvm_unreachable("Unhandled template name kind!");
7567 ASTReader::ReadTemplateArgument(ModuleFile &F,
7568 const RecordData &Record, unsigned &Idx) {
7569 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7571 case TemplateArgument::Null:
7572 return TemplateArgument();
7573 case TemplateArgument::Type:
7574 return TemplateArgument(readType(F, Record, Idx));
7575 case TemplateArgument::Declaration: {
7576 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7577 return TemplateArgument(D, readType(F, Record, Idx));
7579 case TemplateArgument::NullPtr:
7580 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7581 case TemplateArgument::Integral: {
7582 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7583 QualType T = readType(F, Record, Idx);
7584 return TemplateArgument(Context, Value, T);
7586 case TemplateArgument::Template:
7587 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7588 case TemplateArgument::TemplateExpansion: {
7589 TemplateName Name = ReadTemplateName(F, Record, Idx);
7590 Optional<unsigned> NumTemplateExpansions;
7591 if (unsigned NumExpansions = Record[Idx++])
7592 NumTemplateExpansions = NumExpansions - 1;
7593 return TemplateArgument(Name, NumTemplateExpansions);
7595 case TemplateArgument::Expression:
7596 return TemplateArgument(ReadExpr(F));
7597 case TemplateArgument::Pack: {
7598 unsigned NumArgs = Record[Idx++];
7599 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7600 for (unsigned I = 0; I != NumArgs; ++I)
7601 Args[I] = ReadTemplateArgument(F, Record, Idx);
7602 return TemplateArgument(Args, NumArgs);
7606 llvm_unreachable("Unhandled template argument kind!");
7609 TemplateParameterList *
7610 ASTReader::ReadTemplateParameterList(ModuleFile &F,
7611 const RecordData &Record, unsigned &Idx) {
7612 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7613 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7614 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7616 unsigned NumParams = Record[Idx++];
7617 SmallVector<NamedDecl *, 16> Params;
7618 Params.reserve(NumParams);
7620 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7622 TemplateParameterList* TemplateParams =
7623 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7624 Params.data(), Params.size(), RAngleLoc);
7625 return TemplateParams;
7630 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
7631 ModuleFile &F, const RecordData &Record,
7633 unsigned NumTemplateArgs = Record[Idx++];
7634 TemplArgs.reserve(NumTemplateArgs);
7635 while (NumTemplateArgs--)
7636 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7639 /// \brief Read a UnresolvedSet structure.
7640 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
7641 const RecordData &Record, unsigned &Idx) {
7642 unsigned NumDecls = Record[Idx++];
7643 Set.reserve(Context, NumDecls);
7644 while (NumDecls--) {
7645 DeclID ID = ReadDeclID(F, Record, Idx);
7646 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
7647 Set.addLazyDecl(Context, ID, AS);
7652 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7653 const RecordData &Record, unsigned &Idx) {
7654 bool isVirtual = static_cast<bool>(Record[Idx++]);
7655 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7656 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7657 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7658 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7659 SourceRange Range = ReadSourceRange(F, Record, Idx);
7660 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7661 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7663 Result.setInheritConstructors(inheritConstructors);
7667 CXXCtorInitializer **
7668 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7670 unsigned NumInitializers = Record[Idx++];
7671 assert(NumInitializers && "wrote ctor initializers but have no inits");
7672 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7673 for (unsigned i = 0; i != NumInitializers; ++i) {
7674 TypeSourceInfo *TInfo = nullptr;
7675 bool IsBaseVirtual = false;
7676 FieldDecl *Member = nullptr;
7677 IndirectFieldDecl *IndirectMember = nullptr;
7679 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7681 case CTOR_INITIALIZER_BASE:
7682 TInfo = GetTypeSourceInfo(F, Record, Idx);
7683 IsBaseVirtual = Record[Idx++];
7686 case CTOR_INITIALIZER_DELEGATING:
7687 TInfo = GetTypeSourceInfo(F, Record, Idx);
7690 case CTOR_INITIALIZER_MEMBER:
7691 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7694 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7695 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7699 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7700 Expr *Init = ReadExpr(F);
7701 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7702 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7703 bool IsWritten = Record[Idx++];
7704 unsigned SourceOrderOrNumArrayIndices;
7705 SmallVector<VarDecl *, 8> Indices;
7707 SourceOrderOrNumArrayIndices = Record[Idx++];
7709 SourceOrderOrNumArrayIndices = Record[Idx++];
7710 Indices.reserve(SourceOrderOrNumArrayIndices);
7711 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7712 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7715 CXXCtorInitializer *BOMInit;
7716 if (Type == CTOR_INITIALIZER_BASE) {
7717 BOMInit = new (Context)
7718 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7719 RParenLoc, MemberOrEllipsisLoc);
7720 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7721 BOMInit = new (Context)
7722 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
7723 } else if (IsWritten) {
7725 BOMInit = new (Context) CXXCtorInitializer(
7726 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
7728 BOMInit = new (Context)
7729 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7730 LParenLoc, Init, RParenLoc);
7732 if (IndirectMember) {
7733 assert(Indices.empty() && "Indirect field improperly initialized");
7734 BOMInit = new (Context)
7735 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7736 LParenLoc, Init, RParenLoc);
7738 BOMInit = CXXCtorInitializer::Create(
7739 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
7740 Indices.data(), Indices.size());
7745 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7746 CtorInitializers[i] = BOMInit;
7749 return CtorInitializers;
7752 NestedNameSpecifier *
7753 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7754 const RecordData &Record, unsigned &Idx) {
7755 unsigned N = Record[Idx++];
7756 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
7757 for (unsigned I = 0; I != N; ++I) {
7758 NestedNameSpecifier::SpecifierKind Kind
7759 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7761 case NestedNameSpecifier::Identifier: {
7762 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7763 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7767 case NestedNameSpecifier::Namespace: {
7768 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7769 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7773 case NestedNameSpecifier::NamespaceAlias: {
7774 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7775 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7779 case NestedNameSpecifier::TypeSpec:
7780 case NestedNameSpecifier::TypeSpecWithTemplate: {
7781 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7785 bool Template = Record[Idx++];
7786 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7790 case NestedNameSpecifier::Global: {
7791 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7792 // No associated value, and there can't be a prefix.
7796 case NestedNameSpecifier::Super: {
7797 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7798 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
7807 NestedNameSpecifierLoc
7808 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7810 unsigned N = Record[Idx++];
7811 NestedNameSpecifierLocBuilder Builder;
7812 for (unsigned I = 0; I != N; ++I) {
7813 NestedNameSpecifier::SpecifierKind Kind
7814 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7816 case NestedNameSpecifier::Identifier: {
7817 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7818 SourceRange Range = ReadSourceRange(F, Record, Idx);
7819 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7823 case NestedNameSpecifier::Namespace: {
7824 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7825 SourceRange Range = ReadSourceRange(F, Record, Idx);
7826 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7830 case NestedNameSpecifier::NamespaceAlias: {
7831 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7832 SourceRange Range = ReadSourceRange(F, Record, Idx);
7833 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7837 case NestedNameSpecifier::TypeSpec:
7838 case NestedNameSpecifier::TypeSpecWithTemplate: {
7839 bool Template = Record[Idx++];
7840 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7842 return NestedNameSpecifierLoc();
7843 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7845 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7846 Builder.Extend(Context,
7847 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7848 T->getTypeLoc(), ColonColonLoc);
7852 case NestedNameSpecifier::Global: {
7853 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7854 Builder.MakeGlobal(Context, ColonColonLoc);
7858 case NestedNameSpecifier::Super: {
7859 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7860 SourceRange Range = ReadSourceRange(F, Record, Idx);
7861 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
7867 return Builder.getWithLocInContext(Context);
7871 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7873 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7874 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7875 return SourceRange(beg, end);
7878 /// \brief Read an integral value
7879 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7880 unsigned BitWidth = Record[Idx++];
7881 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7882 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7887 /// \brief Read a signed integral value
7888 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7889 bool isUnsigned = Record[Idx++];
7890 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7893 /// \brief Read a floating-point value
7894 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7895 const llvm::fltSemantics &Sem,
7897 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
7900 // \brief Read a string
7901 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7902 unsigned Len = Record[Idx++];
7903 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7908 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
7910 std::string Filename = ReadString(Record, Idx);
7911 ResolveImportedPath(F, Filename);
7915 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7917 unsigned Major = Record[Idx++];
7918 unsigned Minor = Record[Idx++];
7919 unsigned Subminor = Record[Idx++];
7921 return VersionTuple(Major);
7923 return VersionTuple(Major, Minor - 1);
7924 return VersionTuple(Major, Minor - 1, Subminor - 1);
7927 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7928 const RecordData &Record,
7930 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7931 return CXXTemporary::Create(Context, Decl);
7934 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7935 return Diag(CurrentImportLoc, DiagID);
7938 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7939 return Diags.Report(Loc, DiagID);
7942 /// \brief Retrieve the identifier table associated with the
7944 IdentifierTable &ASTReader::getIdentifierTable() {
7945 return PP.getIdentifierTable();
7948 /// \brief Record that the given ID maps to the given switch-case
7950 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7951 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
7952 "Already have a SwitchCase with this ID");
7953 (*CurrSwitchCaseStmts)[ID] = SC;
7956 /// \brief Retrieve the switch-case statement with the given ID.
7957 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7958 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
7959 return (*CurrSwitchCaseStmts)[ID];
7962 void ASTReader::ClearSwitchCaseIDs() {
7963 CurrSwitchCaseStmts->clear();
7966 void ASTReader::ReadComments() {
7967 std::vector<RawComment *> Comments;
7968 for (SmallVectorImpl<std::pair<BitstreamCursor,
7969 serialization::ModuleFile *> >::iterator
7970 I = CommentsCursors.begin(),
7971 E = CommentsCursors.end();
7974 BitstreamCursor &Cursor = I->first;
7975 serialization::ModuleFile &F = *I->second;
7976 SavedStreamPosition SavedPosition(Cursor);
7980 llvm::BitstreamEntry Entry =
7981 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7983 switch (Entry.Kind) {
7984 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7985 case llvm::BitstreamEntry::Error:
7986 Error("malformed block record in AST file");
7988 case llvm::BitstreamEntry::EndBlock:
7990 case llvm::BitstreamEntry::Record:
7991 // The interesting case.
7997 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
7998 case COMMENTS_RAW_COMMENT: {
8000 SourceRange SR = ReadSourceRange(F, Record, Idx);
8001 RawComment::CommentKind Kind =
8002 (RawComment::CommentKind) Record[Idx++];
8003 bool IsTrailingComment = Record[Idx++];
8004 bool IsAlmostTrailingComment = Record[Idx++];
8005 Comments.push_back(new (Context) RawComment(
8006 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8007 Context.getLangOpts().CommentOpts.ParseAllComments));
8013 Context.Comments.addDeserializedComments(Comments);
8017 void ASTReader::getInputFiles(ModuleFile &F,
8018 SmallVectorImpl<serialization::InputFile> &Files) {
8019 for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8021 Files.push_back(getInputFile(F, ID));
8025 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8026 // If we know the owning module, use it.
8027 if (Module *M = D->getImportedOwningModule())
8028 return M->getFullModuleName();
8030 // Otherwise, use the name of the top-level module the decl is within.
8031 if (ModuleFile *M = getOwningModuleFile(D))
8032 return M->ModuleName;
8034 // Not from a module.
8038 void ASTReader::finishPendingActions() {
8039 while (!PendingIdentifierInfos.empty() ||
8040 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
8041 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
8042 !PendingUpdateRecords.empty()) {
8043 // If any identifiers with corresponding top-level declarations have
8044 // been loaded, load those declarations now.
8045 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8047 TopLevelDeclsMap TopLevelDecls;
8049 while (!PendingIdentifierInfos.empty()) {
8050 IdentifierInfo *II = PendingIdentifierInfos.back().first;
8051 SmallVector<uint32_t, 4> DeclIDs =
8052 std::move(PendingIdentifierInfos.back().second);
8053 PendingIdentifierInfos.pop_back();
8055 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
8058 // For each decl chain that we wanted to complete while deserializing, mark
8059 // it as "still needs to be completed".
8060 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8061 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8063 PendingIncompleteDeclChains.clear();
8065 // Load pending declaration chains.
8066 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8067 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8068 loadPendingDeclChain(PendingDeclChains[I]);
8070 assert(PendingDeclChainsKnown.empty());
8071 PendingDeclChains.clear();
8073 // Make the most recent of the top-level declarations visible.
8074 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8075 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
8076 IdentifierInfo *II = TLD->first;
8077 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
8078 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
8082 // Load any pending macro definitions.
8083 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
8084 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8085 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8086 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8087 // Initialize the macro history from chained-PCHs ahead of module imports.
8088 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8090 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8091 if (Info.M->Kind != MK_ImplicitModule &&
8092 Info.M->Kind != MK_ExplicitModule)
8093 resolvePendingMacro(II, Info);
8095 // Handle module imports.
8096 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8098 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8099 if (Info.M->Kind == MK_ImplicitModule ||
8100 Info.M->Kind == MK_ExplicitModule)
8101 resolvePendingMacro(II, Info);
8104 PendingMacroIDs.clear();
8106 // Wire up the DeclContexts for Decls that we delayed setting until
8107 // recursive loading is completed.
8108 while (!PendingDeclContextInfos.empty()) {
8109 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8110 PendingDeclContextInfos.pop_front();
8111 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8112 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8113 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8116 // Perform any pending declaration updates.
8117 while (!PendingUpdateRecords.empty()) {
8118 auto Update = PendingUpdateRecords.pop_back_val();
8119 ReadingKindTracker ReadingKind(Read_Decl, *this);
8120 loadDeclUpdateRecords(Update.first, Update.second);
8124 // At this point, all update records for loaded decls are in place, so any
8125 // fake class definitions should have become real.
8126 assert(PendingFakeDefinitionData.empty() &&
8127 "faked up a class definition but never saw the real one");
8129 // If we deserialized any C++ or Objective-C class definitions, any
8130 // Objective-C protocol definitions, or any redeclarable templates, make sure
8131 // that all redeclarations point to the definitions. Note that this can only
8132 // happen now, after the redeclaration chains have been fully wired.
8133 for (Decl *D : PendingDefinitions) {
8134 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
8135 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
8136 // Make sure that the TagType points at the definition.
8137 const_cast<TagType*>(TagT)->decl = TD;
8140 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
8141 for (auto *R = getMostRecentExistingDecl(RD); R;
8142 R = R->getPreviousDecl()) {
8144 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
8145 "declaration thinks it's the definition but it isn't");
8146 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
8153 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
8154 // Make sure that the ObjCInterfaceType points at the definition.
8155 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8158 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8159 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8164 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
8165 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8166 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8171 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
8172 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8173 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
8175 PendingDefinitions.clear();
8177 // Load the bodies of any functions or methods we've encountered. We do
8178 // this now (delayed) so that we can be sure that the declaration chains
8179 // have been fully wired up.
8180 // FIXME: There seems to be no point in delaying this, it does not depend
8181 // on the redecl chains having been wired up.
8182 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8183 PBEnd = PendingBodies.end();
8184 PB != PBEnd; ++PB) {
8185 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8186 // FIXME: Check for =delete/=default?
8187 // FIXME: Complain about ODR violations here?
8188 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8189 FD->setLazyBody(PB->second);
8193 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8194 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8195 MD->setLazyBody(PB->second);
8197 PendingBodies.clear();
8200 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8201 getContext().deduplicateMergedDefinitonsFor(ND);
8202 PendingMergedDefinitionsToDeduplicate.clear();
8205 void ASTReader::diagnoseOdrViolations() {
8206 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8209 // Trigger the import of the full definition of each class that had any
8210 // odr-merging problems, so we can produce better diagnostics for them.
8211 // These updates may in turn find and diagnose some ODR failures, so take
8212 // ownership of the set first.
8213 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8214 PendingOdrMergeFailures.clear();
8215 for (auto &Merge : OdrMergeFailures) {
8216 Merge.first->buildLookup();
8217 Merge.first->decls_begin();
8218 Merge.first->bases_begin();
8219 Merge.first->vbases_begin();
8220 for (auto *RD : Merge.second) {
8227 // For each declaration from a merged context, check that the canonical
8228 // definition of that context also contains a declaration of the same
8231 // Caution: this loop does things that might invalidate iterators into
8232 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8233 while (!PendingOdrMergeChecks.empty()) {
8234 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8236 // FIXME: Skip over implicit declarations for now. This matters for things
8237 // like implicitly-declared special member functions. This isn't entirely
8238 // correct; we can end up with multiple unmerged declarations of the same
8240 if (D->isImplicit())
8243 DeclContext *CanonDef = D->getDeclContext();
8246 const Decl *DCanon = D->getCanonicalDecl();
8248 for (auto RI : D->redecls()) {
8249 if (RI->getLexicalDeclContext() == CanonDef) {
8257 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8258 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8259 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8260 !Found && I != E; ++I) {
8261 for (auto RI : (*I)->redecls()) {
8262 if (RI->getLexicalDeclContext() == CanonDef) {
8263 // This declaration is present in the canonical definition. If it's
8264 // in the same redecl chain, it's the one we're looking for.
8265 if (RI->getCanonicalDecl() == DCanon)
8268 Candidates.push_back(cast<NamedDecl>(RI));
8275 // The AST doesn't like TagDecls becoming invalid after they've been
8276 // completed. We only really need to mark FieldDecls as invalid here.
8277 if (!isa<TagDecl>(D))
8278 D->setInvalidDecl();
8280 // Ensure we don't accidentally recursively enter deserialization while
8281 // we're producing our diagnostic.
8282 Deserializing RecursionGuard(this);
8284 std::string CanonDefModule =
8285 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8286 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8287 << D << getOwningModuleNameForDiagnostic(D)
8288 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8290 if (Candidates.empty())
8291 Diag(cast<Decl>(CanonDef)->getLocation(),
8292 diag::note_module_odr_violation_no_possible_decls) << D;
8294 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8295 Diag(Candidates[I]->getLocation(),
8296 diag::note_module_odr_violation_possible_decl)
8300 DiagnosedOdrMergeFailures.insert(CanonDef);
8304 if (OdrMergeFailures.empty())
8307 // Ensure we don't accidentally recursively enter deserialization while
8308 // we're producing our diagnostics.
8309 Deserializing RecursionGuard(this);
8311 // Issue any pending ODR-failure diagnostics.
8312 for (auto &Merge : OdrMergeFailures) {
8313 // If we've already pointed out a specific problem with this class, don't
8314 // bother issuing a general "something's different" diagnostic.
8315 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
8318 bool Diagnosed = false;
8319 for (auto *RD : Merge.second) {
8320 // Multiple different declarations got merged together; tell the user
8321 // where they came from.
8322 if (Merge.first != RD) {
8323 // FIXME: Walk the definition, figure out what's different,
8324 // and diagnose that.
8326 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8327 Diag(Merge.first->getLocation(),
8328 diag::err_module_odr_violation_different_definitions)
8329 << Merge.first << Module.empty() << Module;
8333 Diag(RD->getLocation(),
8334 diag::note_module_odr_violation_different_definitions)
8335 << getOwningModuleNameForDiagnostic(RD);
8340 // All definitions are updates to the same declaration. This happens if a
8341 // module instantiates the declaration of a class template specialization
8342 // and two or more other modules instantiate its definition.
8344 // FIXME: Indicate which modules had instantiations of this definition.
8345 // FIXME: How can this even happen?
8346 Diag(Merge.first->getLocation(),
8347 diag::err_module_odr_violation_different_instantiations)
8353 void ASTReader::FinishedDeserializing() {
8354 assert(NumCurrentElementsDeserializing &&
8355 "FinishedDeserializing not paired with StartedDeserializing");
8356 if (NumCurrentElementsDeserializing == 1) {
8357 // We decrease NumCurrentElementsDeserializing only after pending actions
8358 // are finished, to avoid recursively re-calling finishPendingActions().
8359 finishPendingActions();
8361 --NumCurrentElementsDeserializing;
8363 if (NumCurrentElementsDeserializing == 0) {
8364 // Propagate exception specification updates along redeclaration chains.
8365 while (!PendingExceptionSpecUpdates.empty()) {
8366 auto Updates = std::move(PendingExceptionSpecUpdates);
8367 PendingExceptionSpecUpdates.clear();
8368 for (auto Update : Updates) {
8369 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
8370 SemaObj->UpdateExceptionSpec(Update.second,
8371 FPT->getExtProtoInfo().ExceptionSpec);
8375 diagnoseOdrViolations();
8377 // We are not in recursive loading, so it's safe to pass the "interesting"
8378 // decls to the consumer.
8380 PassInterestingDeclsToConsumer();
8384 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
8385 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8386 // Remove any fake results before adding any real ones.
8387 auto It = PendingFakeLookupResults.find(II);
8388 if (It != PendingFakeLookupResults.end()) {
8389 for (auto *ND : PendingFakeLookupResults[II])
8390 SemaObj->IdResolver.RemoveDecl(ND);
8391 // FIXME: this works around module+PCH performance issue.
8392 // Rather than erase the result from the map, which is O(n), just clear
8393 // the vector of NamedDecls.
8398 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8399 SemaObj->TUScope->AddDecl(D);
8400 } else if (SemaObj->TUScope) {
8401 // Adding the decl to IdResolver may have failed because it was already in
8402 // (even though it was not added in scope). If it is already in, make sure
8403 // it gets in the scope as well.
8404 if (std::find(SemaObj->IdResolver.begin(Name),
8405 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8406 SemaObj->TUScope->AddDecl(D);
8410 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8411 bool DisableValidation, bool AllowASTWithCompilerErrors,
8412 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8413 bool UseGlobalIndex)
8414 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8415 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8416 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8417 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8418 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8419 DisableValidation(DisableValidation),
8420 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8421 AllowConfigurationMismatch(AllowConfigurationMismatch),
8422 ValidateSystemInputs(ValidateSystemInputs),
8423 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8424 CurrSwitchCaseStmts(&SwitchCaseStmts),
8425 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8426 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8427 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8428 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8429 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8430 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8431 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8432 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8433 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8434 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
8435 SourceMgr.setExternalSLocEntrySource(this);
8438 ASTReader::~ASTReader() {
8439 if (OwnsDeserializationListener)
8440 delete DeserializationListener;
8442 for (DeclContextVisibleUpdatesPending::iterator
8443 I = PendingVisibleUpdates.begin(),
8444 E = PendingVisibleUpdates.end();
8446 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8447 F = I->second.end();