1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the ASTReader class, which reads AST files.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Serialization/ASTReader.h"
14 #include "ASTCommon.h"
15 #include "ASTReaderInternals.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ASTMutationListener.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/TypeLocVisitor.h"
39 #include "clang/AST/UnresolvedSet.h"
40 #include "clang/Basic/CommentOptions.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/DiagnosticOptions.h"
43 #include "clang/Basic/ExceptionSpecificationType.h"
44 #include "clang/Basic/FileManager.h"
45 #include "clang/Basic/FileSystemOptions.h"
46 #include "clang/Basic/IdentifierTable.h"
47 #include "clang/Basic/LLVM.h"
48 #include "clang/Basic/LangOptions.h"
49 #include "clang/Basic/Module.h"
50 #include "clang/Basic/ObjCRuntime.h"
51 #include "clang/Basic/OperatorKinds.h"
52 #include "clang/Basic/PragmaKinds.h"
53 #include "clang/Basic/Sanitizers.h"
54 #include "clang/Basic/SourceLocation.h"
55 #include "clang/Basic/SourceManager.h"
56 #include "clang/Basic/SourceManagerInternals.h"
57 #include "clang/Basic/Specifiers.h"
58 #include "clang/Basic/TargetInfo.h"
59 #include "clang/Basic/TargetOptions.h"
60 #include "clang/Basic/TokenKinds.h"
61 #include "clang/Basic/Version.h"
62 #include "clang/Lex/HeaderSearch.h"
63 #include "clang/Lex/HeaderSearchOptions.h"
64 #include "clang/Lex/MacroInfo.h"
65 #include "clang/Lex/ModuleMap.h"
66 #include "clang/Lex/PreprocessingRecord.h"
67 #include "clang/Lex/Preprocessor.h"
68 #include "clang/Lex/PreprocessorOptions.h"
69 #include "clang/Lex/Token.h"
70 #include "clang/Sema/ObjCMethodList.h"
71 #include "clang/Sema/Scope.h"
72 #include "clang/Sema/Sema.h"
73 #include "clang/Sema/Weak.h"
74 #include "clang/Serialization/ASTBitCodes.h"
75 #include "clang/Serialization/ASTDeserializationListener.h"
76 #include "clang/Serialization/ContinuousRangeMap.h"
77 #include "clang/Serialization/GlobalModuleIndex.h"
78 #include "clang/Serialization/InMemoryModuleCache.h"
79 #include "clang/Serialization/Module.h"
80 #include "clang/Serialization/ModuleFileExtension.h"
81 #include "clang/Serialization/ModuleManager.h"
82 #include "clang/Serialization/PCHContainerOperations.h"
83 #include "clang/Serialization/SerializationDiagnostic.h"
84 #include "llvm/ADT/APFloat.h"
85 #include "llvm/ADT/APInt.h"
86 #include "llvm/ADT/APSInt.h"
87 #include "llvm/ADT/ArrayRef.h"
88 #include "llvm/ADT/DenseMap.h"
89 #include "llvm/ADT/FoldingSet.h"
90 #include "llvm/ADT/Hashing.h"
91 #include "llvm/ADT/IntrusiveRefCntPtr.h"
92 #include "llvm/ADT/None.h"
93 #include "llvm/ADT/Optional.h"
94 #include "llvm/ADT/STLExtras.h"
95 #include "llvm/ADT/ScopeExit.h"
96 #include "llvm/ADT/SmallPtrSet.h"
97 #include "llvm/ADT/SmallString.h"
98 #include "llvm/ADT/SmallVector.h"
99 #include "llvm/ADT/StringExtras.h"
100 #include "llvm/ADT/StringMap.h"
101 #include "llvm/ADT/StringRef.h"
102 #include "llvm/ADT/Triple.h"
103 #include "llvm/ADT/iterator_range.h"
104 #include "llvm/Bitstream/BitstreamReader.h"
105 #include "llvm/Support/Casting.h"
106 #include "llvm/Support/Compiler.h"
107 #include "llvm/Support/Compression.h"
108 #include "llvm/Support/DJB.h"
109 #include "llvm/Support/Endian.h"
110 #include "llvm/Support/Error.h"
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FileSystem.h"
113 #include "llvm/Support/MemoryBuffer.h"
114 #include "llvm/Support/Path.h"
115 #include "llvm/Support/SaveAndRestore.h"
116 #include "llvm/Support/Timer.h"
117 #include "llvm/Support/VersionTuple.h"
118 #include "llvm/Support/raw_ostream.h"
130 #include <system_error>
135 using namespace clang;
136 using namespace clang::serialization;
137 using namespace clang::serialization::reader;
138 using llvm::BitstreamCursor;
140 //===----------------------------------------------------------------------===//
141 // ChainedASTReaderListener implementation
142 //===----------------------------------------------------------------------===//
145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146 return First->ReadFullVersionInformation(FullVersion) ||
147 Second->ReadFullVersionInformation(FullVersion);
150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151 First->ReadModuleName(ModuleName);
152 Second->ReadModuleName(ModuleName);
155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156 First->ReadModuleMapFile(ModuleMapPath);
157 Second->ReadModuleMapFile(ModuleMapPath);
161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
163 bool AllowCompatibleDifferences) {
164 return First->ReadLanguageOptions(LangOpts, Complain,
165 AllowCompatibleDifferences) ||
166 Second->ReadLanguageOptions(LangOpts, Complain,
167 AllowCompatibleDifferences);
170 bool ChainedASTReaderListener::ReadTargetOptions(
171 const TargetOptions &TargetOpts, bool Complain,
172 bool AllowCompatibleDifferences) {
173 return First->ReadTargetOptions(TargetOpts, Complain,
174 AllowCompatibleDifferences) ||
175 Second->ReadTargetOptions(TargetOpts, Complain,
176 AllowCompatibleDifferences);
179 bool ChainedASTReaderListener::ReadDiagnosticOptions(
180 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
181 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182 Second->ReadDiagnosticOptions(DiagOpts, Complain);
186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
188 return First->ReadFileSystemOptions(FSOpts, Complain) ||
189 Second->ReadFileSystemOptions(FSOpts, Complain);
192 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
193 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
195 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
197 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201 bool ChainedASTReaderListener::ReadPreprocessorOptions(
202 const PreprocessorOptions &PPOpts, bool Complain,
203 std::string &SuggestedPredefines) {
204 return First->ReadPreprocessorOptions(PPOpts, Complain,
205 SuggestedPredefines) ||
206 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
211 First->ReadCounter(M, Value);
212 Second->ReadCounter(M, Value);
215 bool ChainedASTReaderListener::needsInputFileVisitation() {
216 return First->needsInputFileVisitation() ||
217 Second->needsInputFileVisitation();
220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221 return First->needsSystemInputFileVisitation() ||
222 Second->needsSystemInputFileVisitation();
225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
227 First->visitModuleFile(Filename, Kind);
228 Second->visitModuleFile(Filename, Kind);
231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
234 bool isExplicitModule) {
235 bool Continue = false;
236 if (First->needsInputFileVisitation() &&
237 (!isSystem || First->needsSystemInputFileVisitation()))
238 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
240 if (Second->needsInputFileVisitation() &&
241 (!isSystem || Second->needsSystemInputFileVisitation()))
242 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
247 void ChainedASTReaderListener::readModuleFileExtension(
248 const ModuleFileExtensionMetadata &Metadata) {
249 First->readModuleFileExtension(Metadata);
250 Second->readModuleFileExtension(Metadata);
253 //===----------------------------------------------------------------------===//
254 // PCH validator implementation
255 //===----------------------------------------------------------------------===//
257 ASTReaderListener::~ASTReaderListener() = default;
259 /// Compare the given set of language options against an existing set of
260 /// language options.
262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
263 /// \param AllowCompatibleDifferences If true, differences between compatible
264 /// language options will be permitted.
266 /// \returns true if the languagae options mis-match, false otherwise.
267 static bool checkLanguageOptions(const LangOptions &LangOpts,
268 const LangOptions &ExistingLangOpts,
269 DiagnosticsEngine *Diags,
270 bool AllowCompatibleDifferences = true) {
271 #define LANGOPT(Name, Bits, Default, Description) \
272 if (ExistingLangOpts.Name != LangOpts.Name) { \
274 Diags->Report(diag::err_pch_langopt_mismatch) \
275 << Description << LangOpts.Name << ExistingLangOpts.Name; \
279 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
280 if (ExistingLangOpts.Name != LangOpts.Name) { \
282 Diags->Report(diag::err_pch_langopt_value_mismatch) \
287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
288 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
290 Diags->Report(diag::err_pch_langopt_value_mismatch) \
295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
296 if (!AllowCompatibleDifferences) \
297 LANGOPT(Name, Bits, Default, Description)
299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
300 if (!AllowCompatibleDifferences) \
301 ENUM_LANGOPT(Name, Bits, Default, Description)
303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304 if (!AllowCompatibleDifferences) \
305 VALUE_LANGOPT(Name, Bits, Default, Description)
307 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
310 #include "clang/Basic/LangOptions.def"
312 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
314 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
318 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
320 Diags->Report(diag::err_pch_langopt_value_mismatch)
321 << "target Objective-C runtime";
325 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326 LangOpts.CommentOpts.BlockCommandNames) {
328 Diags->Report(diag::err_pch_langopt_value_mismatch)
329 << "block command names";
333 // Sanitizer feature mismatches are treated as compatible differences. If
334 // compatible differences aren't allowed, we still only want to check for
335 // mismatches of non-modular sanitizers (the only ones which can affect AST
337 if (!AllowCompatibleDifferences) {
338 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341 ExistingSanitizers.clear(ModularSanitizers);
342 ImportedSanitizers.clear(ModularSanitizers);
343 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344 const std::string Flag = "-fsanitize=";
346 #define SANITIZER(NAME, ID) \
348 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
349 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
350 if (InExistingModule != InImportedModule) \
351 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
352 << InExistingModule << (Flag + NAME); \
354 #include "clang/Basic/Sanitizers.def"
363 /// Compare the given set of target options against an existing set of
366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
368 /// \returns true if the target options mis-match, false otherwise.
369 static bool checkTargetOptions(const TargetOptions &TargetOpts,
370 const TargetOptions &ExistingTargetOpts,
371 DiagnosticsEngine *Diags,
372 bool AllowCompatibleDifferences = true) {
373 #define CHECK_TARGET_OPT(Field, Name) \
374 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
376 Diags->Report(diag::err_pch_targetopt_mismatch) \
377 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
381 // The triple and ABI must match exactly.
382 CHECK_TARGET_OPT(Triple, "target");
383 CHECK_TARGET_OPT(ABI, "target ABI");
385 // We can tolerate different CPUs in many cases, notably when one CPU
386 // supports a strict superset of another. When allowing compatible
387 // differences skip this check.
388 if (!AllowCompatibleDifferences)
389 CHECK_TARGET_OPT(CPU, "target CPU");
391 #undef CHECK_TARGET_OPT
393 // Compare feature sets.
394 SmallVector<StringRef, 4> ExistingFeatures(
395 ExistingTargetOpts.FeaturesAsWritten.begin(),
396 ExistingTargetOpts.FeaturesAsWritten.end());
397 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398 TargetOpts.FeaturesAsWritten.end());
399 llvm::sort(ExistingFeatures);
400 llvm::sort(ReadFeatures);
402 // We compute the set difference in both directions explicitly so that we can
403 // diagnose the differences differently.
404 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
406 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409 ExistingFeatures.begin(), ExistingFeatures.end(),
410 std::back_inserter(UnmatchedReadFeatures));
412 // If we are allowing compatible differences and the read feature set is
413 // a strict subset of the existing feature set, there is nothing to diagnose.
414 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
418 for (StringRef Feature : UnmatchedReadFeatures)
419 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
420 << /* is-existing-feature */ false << Feature;
421 for (StringRef Feature : UnmatchedExistingFeatures)
422 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423 << /* is-existing-feature */ true << Feature;
426 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
432 bool AllowCompatibleDifferences) {
433 const LangOptions &ExistingLangOpts = PP.getLangOpts();
434 return checkLanguageOptions(LangOpts, ExistingLangOpts,
435 Complain ? &Reader.Diags : nullptr,
436 AllowCompatibleDifferences);
439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
441 bool AllowCompatibleDifferences) {
442 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
444 Complain ? &Reader.Diags : nullptr,
445 AllowCompatibleDifferences);
450 using MacroDefinitionsMap =
451 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457 DiagnosticsEngine &Diags,
459 using Level = DiagnosticsEngine::Level;
461 // Check current mappings for new -Werror mappings, and the stored mappings
462 // for cases that were explicitly mapped to *not* be errors that are now
463 // errors because of options like -Werror.
464 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
466 for (DiagnosticsEngine *MappingSource : MappingSources) {
467 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468 diag::kind DiagID = DiagIDMappingPair.first;
469 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470 if (CurLevel < DiagnosticsEngine::Error)
471 continue; // not significant
473 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474 if (StoredLevel < DiagnosticsEngine::Error) {
476 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
490 return Ext >= diag::Severity::Error;
493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494 DiagnosticsEngine &Diags,
495 bool IsSystem, bool Complain) {
498 if (Diags.getSuppressSystemWarnings())
500 // If -Wsystem-headers was not enabled before, be conservative
501 if (StoredDiags.getSuppressSystemWarnings()) {
503 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
508 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
514 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515 !StoredDiags.getEnableAllWarnings()) {
517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
521 if (isExtHandlingFromDiagsError(Diags) &&
522 !isExtHandlingFromDiagsError(StoredDiags)) {
524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
528 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
531 /// Return the top import module if it is implicit, nullptr otherwise.
532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
534 // If the original import came from a file explicitly generated by the user,
535 // don't check the diagnostic mappings.
536 // FIXME: currently this is approximated by checking whether this is not a
537 // module import of an implicitly-loaded module file.
538 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539 // the transitive closure of its imports, since unrelated modules cannot be
540 // imported until after this module finishes validation.
541 ModuleFile *TopImport = &*ModuleMgr.rbegin();
542 while (!TopImport->ImportedBy.empty())
543 TopImport = TopImport->ImportedBy[0];
544 if (TopImport->Kind != MK_ImplicitModule)
547 StringRef ModuleName = TopImport->ModuleName;
548 assert(!ModuleName.empty() && "diagnostic options read before module name");
550 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551 assert(M && "missing module");
555 bool PCHValidator::ReadDiagnosticOptions(
556 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
560 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
561 // This should never fail, because we would have processed these options
562 // before writing them to an ASTFile.
563 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
565 ModuleManager &ModuleMgr = Reader.getModuleManager();
566 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
568 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
572 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573 // contains the union of their flags.
574 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
578 /// Collect the macro definitions provided by the given preprocessor
581 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582 MacroDefinitionsMap &Macros,
583 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
584 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585 StringRef Macro = PPOpts.Macros[I].first;
586 bool IsUndef = PPOpts.Macros[I].second;
588 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589 StringRef MacroName = MacroPair.first;
590 StringRef MacroBody = MacroPair.second;
592 // For an #undef'd macro, we only care about the name.
594 if (MacroNames && !Macros.count(MacroName))
595 MacroNames->push_back(MacroName);
597 Macros[MacroName] = std::make_pair("", true);
601 // For a #define'd macro, figure out the actual definition.
602 if (MacroName.size() == Macro.size())
605 // Note: GCC drops anything following an end-of-line character.
606 StringRef::size_type End = MacroBody.find_first_of("\n\r");
607 MacroBody = MacroBody.substr(0, End);
610 if (MacroNames && !Macros.count(MacroName))
611 MacroNames->push_back(MacroName);
612 Macros[MacroName] = std::make_pair(MacroBody, false);
616 /// Check the preprocessor options deserialized from the control block
617 /// against the preprocessor options in an existing preprocessor.
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 /// \param Validate If true, validate preprocessor options. If false, allow
621 /// macros defined by \p ExistingPPOpts to override those defined by
622 /// \p PPOpts in SuggestedPredefines.
623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624 const PreprocessorOptions &ExistingPPOpts,
625 DiagnosticsEngine *Diags,
626 FileManager &FileMgr,
627 std::string &SuggestedPredefines,
628 const LangOptions &LangOpts,
629 bool Validate = true) {
630 // Check macro definitions.
631 MacroDefinitionsMap ASTFileMacros;
632 collectMacroDefinitions(PPOpts, ASTFileMacros);
633 MacroDefinitionsMap ExistingMacros;
634 SmallVector<StringRef, 4> ExistingMacroNames;
635 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
637 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
638 // Dig out the macro definition in the existing preprocessor options.
639 StringRef MacroName = ExistingMacroNames[I];
640 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
642 // Check whether we know anything about this macro name or not.
643 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644 ASTFileMacros.find(MacroName);
645 if (!Validate || Known == ASTFileMacros.end()) {
646 // FIXME: Check whether this identifier was referenced anywhere in the
647 // AST file. If so, we should reject the AST file. Unfortunately, this
648 // information isn't in the control block. What shall we do about it?
650 if (Existing.second) {
651 SuggestedPredefines += "#undef ";
652 SuggestedPredefines += MacroName.str();
653 SuggestedPredefines += '\n';
655 SuggestedPredefines += "#define ";
656 SuggestedPredefines += MacroName.str();
657 SuggestedPredefines += ' ';
658 SuggestedPredefines += Existing.first.str();
659 SuggestedPredefines += '\n';
664 // If the macro was defined in one but undef'd in the other, we have a
666 if (Existing.second != Known->second.second) {
668 Diags->Report(diag::err_pch_macro_def_undef)
669 << MacroName << Known->second.second;
674 // If the macro was #undef'd in both, or if the macro bodies are identical,
676 if (Existing.second || Existing.first == Known->second.first)
679 // The macro bodies differ; complain.
681 Diags->Report(diag::err_pch_macro_def_conflict)
682 << MacroName << Known->second.first << Existing.first;
687 // Check whether we're using predefines.
688 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
690 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
695 // Detailed record is important since it is used for the module cache hash.
696 if (LangOpts.Modules &&
697 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
699 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
704 // Compute the #include and #include_macros lines we need.
705 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706 StringRef File = ExistingPPOpts.Includes[I];
708 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
709 !ExistingPPOpts.PCHThroughHeader.empty()) {
710 // In case the through header is an include, we must add all the includes
711 // to the predefines so the start point can be determined.
712 SuggestedPredefines += "#include \"";
713 SuggestedPredefines += File;
714 SuggestedPredefines += "\"\n";
718 if (File == ExistingPPOpts.ImplicitPCHInclude)
721 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
722 != PPOpts.Includes.end())
725 SuggestedPredefines += "#include \"";
726 SuggestedPredefines += File;
727 SuggestedPredefines += "\"\n";
730 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
731 StringRef File = ExistingPPOpts.MacroIncludes[I];
732 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
734 != PPOpts.MacroIncludes.end())
737 SuggestedPredefines += "#__include_macros \"";
738 SuggestedPredefines += File;
739 SuggestedPredefines += "\"\n##\n";
745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
747 std::string &SuggestedPredefines) {
748 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
750 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
751 Complain? &Reader.Diags : nullptr,
757 bool SimpleASTReaderListener::ReadPreprocessorOptions(
758 const PreprocessorOptions &PPOpts,
760 std::string &SuggestedPredefines) {
761 return checkPreprocessorOptions(PPOpts,
762 PP.getPreprocessorOpts(),
770 /// Check the header search options deserialized from the control block
771 /// against the header search options in an existing preprocessor.
773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
775 StringRef SpecificModuleCachePath,
776 StringRef ExistingModuleCachePath,
777 DiagnosticsEngine *Diags,
778 const LangOptions &LangOpts) {
779 if (LangOpts.Modules) {
780 if (SpecificModuleCachePath != ExistingModuleCachePath) {
782 Diags->Report(diag::err_pch_modulecache_mismatch)
783 << SpecificModuleCachePath << ExistingModuleCachePath;
791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792 StringRef SpecificModuleCachePath,
794 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
795 PP.getHeaderSearchInfo().getModuleCachePath(),
796 Complain ? &Reader.Diags : nullptr,
800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
801 PP.setCounterValue(Value);
804 //===----------------------------------------------------------------------===//
805 // AST reader implementation
806 //===----------------------------------------------------------------------===//
808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
809 bool TakeOwnership) {
810 DeserializationListener = Listener;
811 OwnsDeserializationListener = TakeOwnership;
814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
815 return serialization::ComputeHash(Sel);
818 std::pair<unsigned, unsigned>
819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
820 using namespace llvm::support;
822 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
823 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
824 return std::make_pair(KeyLen, DataLen);
827 ASTSelectorLookupTrait::internal_key_type
828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
829 using namespace llvm::support;
831 SelectorTable &SelTable = Reader.getContext().Selectors;
832 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
833 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
834 F, endian::readNext<uint32_t, little, unaligned>(d));
836 return SelTable.getNullarySelector(FirstII);
838 return SelTable.getUnarySelector(FirstII);
840 SmallVector<IdentifierInfo *, 16> Args;
841 Args.push_back(FirstII);
842 for (unsigned I = 1; I != N; ++I)
843 Args.push_back(Reader.getLocalIdentifier(
844 F, endian::readNext<uint32_t, little, unaligned>(d)));
846 return SelTable.getSelector(N, Args.data());
849 ASTSelectorLookupTrait::data_type
850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
852 using namespace llvm::support;
856 Result.ID = Reader.getGlobalSelectorID(
857 F, endian::readNext<uint32_t, little, unaligned>(d));
858 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
859 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
860 Result.InstanceBits = FullInstanceBits & 0x3;
861 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
862 Result.FactoryBits = FullFactoryBits & 0x3;
863 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
864 unsigned NumInstanceMethods = FullInstanceBits >> 3;
865 unsigned NumFactoryMethods = FullFactoryBits >> 3;
867 // Load instance methods
868 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
869 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
870 F, endian::readNext<uint32_t, little, unaligned>(d)))
871 Result.Instance.push_back(Method);
874 // Load factory methods
875 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877 F, endian::readNext<uint32_t, little, unaligned>(d)))
878 Result.Factory.push_back(Method);
884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
885 return llvm::djbHash(a);
888 std::pair<unsigned, unsigned>
889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
890 using namespace llvm::support;
892 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
893 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
894 return std::make_pair(KeyLen, DataLen);
897 ASTIdentifierLookupTraitBase::internal_key_type
898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
899 assert(n >= 2 && d[n-1] == '\0');
900 return StringRef((const char*) d, n-1);
903 /// Whether the given identifier is "interesting".
904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
906 return II.hadMacroDefinition() ||
908 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
909 II.hasRevertedTokenIDToIdentifier() ||
910 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
911 II.getFETokenInfo());
914 static bool readBit(unsigned &Bits) {
915 bool Value = Bits & 0x1;
920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
921 using namespace llvm::support;
923 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
924 return Reader.getGlobalIdentifierID(F, RawID >> 1);
927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
928 if (!II.isFromAST()) {
930 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
931 if (isInterestingIdentifier(Reader, II, IsModule))
932 II.setChangedSinceDeserialization();
936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
937 const unsigned char* d,
939 using namespace llvm::support;
941 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
942 bool IsInteresting = RawID & 0x01;
944 // Wipe out the "is interesting" bit.
947 // Build the IdentifierInfo and link the identifier ID with it.
948 IdentifierInfo *II = KnownII;
950 II = &Reader.getIdentifierTable().getOwn(k);
953 markIdentifierFromAST(Reader, *II);
954 Reader.markIdentifierUpToDate(II);
956 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
957 if (!IsInteresting) {
958 // For uninteresting identifiers, there's nothing else to do. Just notify
959 // the reader that we've finished loading this identifier.
960 Reader.SetIdentifierInfo(ID, II);
964 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
965 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
966 bool CPlusPlusOperatorKeyword = readBit(Bits);
967 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
968 bool HasRevertedBuiltin = readBit(Bits);
969 bool Poisoned = readBit(Bits);
970 bool ExtensionToken = readBit(Bits);
971 bool HadMacroDefinition = readBit(Bits);
973 assert(Bits == 0 && "Extra bits in the identifier?");
976 // Set or check the various bits in the IdentifierInfo structure.
977 // Token IDs are read-only.
978 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
979 II->revertTokenIDToIdentifier();
981 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
982 else if (HasRevertedBuiltin && II->getBuiltinID()) {
984 assert((II->hasRevertedBuiltin() ||
985 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
986 "Incorrect ObjC keyword or builtin ID");
988 assert(II->isExtensionToken() == ExtensionToken &&
989 "Incorrect extension token flag");
990 (void)ExtensionToken;
992 II->setIsPoisoned(true);
993 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
994 "Incorrect C++ operator keyword flag");
995 (void)CPlusPlusOperatorKeyword;
997 // If this identifier is a macro, deserialize the macro
999 if (HadMacroDefinition) {
1000 uint32_t MacroDirectivesOffset =
1001 endian::readNext<uint32_t, little, unaligned>(d);
1004 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1007 Reader.SetIdentifierInfo(ID, II);
1009 // Read all of the declarations visible at global scope with this
1012 SmallVector<uint32_t, 4> DeclIDs;
1013 for (; DataLen > 0; DataLen -= 4)
1014 DeclIDs.push_back(Reader.getGlobalDeclID(
1015 F, endian::readNext<uint32_t, little, unaligned>(d)));
1016 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1023 : Kind(Name.getNameKind()) {
1025 case DeclarationName::Identifier:
1026 Data = (uint64_t)Name.getAsIdentifierInfo();
1028 case DeclarationName::ObjCZeroArgSelector:
1029 case DeclarationName::ObjCOneArgSelector:
1030 case DeclarationName::ObjCMultiArgSelector:
1031 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1033 case DeclarationName::CXXOperatorName:
1034 Data = Name.getCXXOverloadedOperator();
1036 case DeclarationName::CXXLiteralOperatorName:
1037 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1039 case DeclarationName::CXXDeductionGuideName:
1040 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1041 ->getDeclName().getAsIdentifierInfo();
1043 case DeclarationName::CXXConstructorName:
1044 case DeclarationName::CXXDestructorName:
1045 case DeclarationName::CXXConversionFunctionName:
1046 case DeclarationName::CXXUsingDirective:
1052 unsigned DeclarationNameKey::getHash() const {
1053 llvm::FoldingSetNodeID ID;
1054 ID.AddInteger(Kind);
1057 case DeclarationName::Identifier:
1058 case DeclarationName::CXXLiteralOperatorName:
1059 case DeclarationName::CXXDeductionGuideName:
1060 ID.AddString(((IdentifierInfo*)Data)->getName());
1062 case DeclarationName::ObjCZeroArgSelector:
1063 case DeclarationName::ObjCOneArgSelector:
1064 case DeclarationName::ObjCMultiArgSelector:
1065 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1067 case DeclarationName::CXXOperatorName:
1068 ID.AddInteger((OverloadedOperatorKind)Data);
1070 case DeclarationName::CXXConstructorName:
1071 case DeclarationName::CXXDestructorName:
1072 case DeclarationName::CXXConversionFunctionName:
1073 case DeclarationName::CXXUsingDirective:
1077 return ID.ComputeHash();
1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1082 using namespace llvm::support;
1084 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1085 return Reader.getLocalModuleFile(F, ModuleFileID);
1088 std::pair<unsigned, unsigned>
1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1090 using namespace llvm::support;
1092 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1093 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1094 return std::make_pair(KeyLen, DataLen);
1097 ASTDeclContextNameLookupTrait::internal_key_type
1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1099 using namespace llvm::support;
1101 auto Kind = (DeclarationName::NameKind)*d++;
1104 case DeclarationName::Identifier:
1105 case DeclarationName::CXXLiteralOperatorName:
1106 case DeclarationName::CXXDeductionGuideName:
1107 Data = (uint64_t)Reader.getLocalIdentifier(
1108 F, endian::readNext<uint32_t, little, unaligned>(d));
1110 case DeclarationName::ObjCZeroArgSelector:
1111 case DeclarationName::ObjCOneArgSelector:
1112 case DeclarationName::ObjCMultiArgSelector:
1114 (uint64_t)Reader.getLocalSelector(
1115 F, endian::readNext<uint32_t, little, unaligned>(
1116 d)).getAsOpaquePtr();
1118 case DeclarationName::CXXOperatorName:
1119 Data = *d++; // OverloadedOperatorKind
1121 case DeclarationName::CXXConstructorName:
1122 case DeclarationName::CXXDestructorName:
1123 case DeclarationName::CXXConversionFunctionName:
1124 case DeclarationName::CXXUsingDirective:
1129 return DeclarationNameKey(Kind, Data);
1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1133 const unsigned char *d,
1135 data_type_builder &Val) {
1136 using namespace llvm::support;
1138 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1139 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1140 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1145 BitstreamCursor &Cursor,
1148 assert(Offset != 0);
1150 SavedStreamPosition SavedPosition(Cursor);
1151 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1152 Error(std::move(Err));
1158 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1160 Error(MaybeCode.takeError());
1163 unsigned Code = MaybeCode.get();
1165 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1166 if (!MaybeRecCode) {
1167 Error(MaybeRecCode.takeError());
1170 unsigned RecCode = MaybeRecCode.get();
1171 if (RecCode != DECL_CONTEXT_LEXICAL) {
1172 Error("Expected lexical block");
1176 assert(!isa<TranslationUnitDecl>(DC) &&
1177 "expected a TU_UPDATE_LEXICAL record for TU");
1178 // If we are handling a C++ class template instantiation, we can see multiple
1179 // lexical updates for the same record. It's important that we select only one
1180 // of them, so that field numbering works properly. Just pick the first one we
1182 auto &Lex = LexicalDecls[DC];
1184 Lex = std::make_pair(
1185 &M, llvm::makeArrayRef(
1186 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1190 DC->setHasExternalLexicalStorage(true);
1194 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1195 BitstreamCursor &Cursor,
1198 assert(Offset != 0);
1200 SavedStreamPosition SavedPosition(Cursor);
1201 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1202 Error(std::move(Err));
1208 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1210 Error(MaybeCode.takeError());
1213 unsigned Code = MaybeCode.get();
1215 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1216 if (!MaybeRecCode) {
1217 Error(MaybeRecCode.takeError());
1220 unsigned RecCode = MaybeRecCode.get();
1221 if (RecCode != DECL_CONTEXT_VISIBLE) {
1222 Error("Expected visible lookup table block");
1226 // We can't safely determine the primary context yet, so delay attaching the
1227 // lookup table until we're done with recursive deserialization.
1228 auto *Data = (const unsigned char*)Blob.data();
1229 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1233 void ASTReader::Error(StringRef Msg) const {
1234 Error(diag::err_fe_pch_malformed, Msg);
1235 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1236 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1237 Diag(diag::note_module_cache_path)
1238 << PP.getHeaderSearchInfo().getModuleCachePath();
1242 void ASTReader::Error(unsigned DiagID,
1243 StringRef Arg1, StringRef Arg2) const {
1244 if (Diags.isDiagnosticInFlight())
1245 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1247 Diag(DiagID) << Arg1 << Arg2;
1250 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1251 unsigned Select) const {
1252 if (!Diags.isDiagnosticInFlight())
1253 Diag(DiagID) << Arg1 << Arg2 << Select;
1256 void ASTReader::Error(llvm::Error &&Err) const {
1257 Error(toString(std::move(Err)));
1260 //===----------------------------------------------------------------------===//
1261 // Source Manager Deserialization
1262 //===----------------------------------------------------------------------===//
1264 /// Read the line table in the source manager block.
1265 /// \returns true if there was an error.
1266 bool ASTReader::ParseLineTable(ModuleFile &F,
1267 const RecordData &Record) {
1269 LineTableInfo &LineTable = SourceMgr.getLineTable();
1271 // Parse the file names
1272 std::map<int, int> FileIDs;
1273 FileIDs[-1] = -1; // For unspecified filenames.
1274 for (unsigned I = 0; Record[Idx]; ++I) {
1275 // Extract the file name
1276 auto Filename = ReadPath(F, Record, Idx);
1277 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1281 // Parse the line entries
1282 std::vector<LineEntry> Entries;
1283 while (Idx < Record.size()) {
1284 int FID = Record[Idx++];
1285 assert(FID >= 0 && "Serialized line entries for non-local file.");
1286 // Remap FileID from 1-based old view.
1287 FID += F.SLocEntryBaseID - 1;
1289 // Extract the line entries
1290 unsigned NumEntries = Record[Idx++];
1291 assert(NumEntries && "no line entries for file ID");
1293 Entries.reserve(NumEntries);
1294 for (unsigned I = 0; I != NumEntries; ++I) {
1295 unsigned FileOffset = Record[Idx++];
1296 unsigned LineNo = Record[Idx++];
1297 int FilenameID = FileIDs[Record[Idx++]];
1298 SrcMgr::CharacteristicKind FileKind
1299 = (SrcMgr::CharacteristicKind)Record[Idx++];
1300 unsigned IncludeOffset = Record[Idx++];
1301 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1302 FileKind, IncludeOffset));
1304 LineTable.AddEntry(FileID::get(FID), Entries);
1310 /// Read a source manager block
1311 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1312 using namespace SrcMgr;
1314 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1316 // Set the source-location entry cursor to the current position in
1317 // the stream. This cursor will be used to read the contents of the
1318 // source manager block initially, and then lazily read
1319 // source-location entries as needed.
1320 SLocEntryCursor = F.Stream;
1322 // The stream itself is going to skip over the source manager block.
1323 if (llvm::Error Err = F.Stream.SkipBlock()) {
1324 Error(std::move(Err));
1328 // Enter the source manager block.
1329 if (llvm::Error Err =
1330 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1331 Error(std::move(Err));
1337 Expected<llvm::BitstreamEntry> MaybeE =
1338 SLocEntryCursor.advanceSkippingSubblocks();
1340 Error(MaybeE.takeError());
1343 llvm::BitstreamEntry E = MaybeE.get();
1346 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1347 case llvm::BitstreamEntry::Error:
1348 Error("malformed block record in AST file");
1350 case llvm::BitstreamEntry::EndBlock:
1352 case llvm::BitstreamEntry::Record:
1353 // The interesting case.
1360 Expected<unsigned> MaybeRecord =
1361 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1363 Error(MaybeRecord.takeError());
1366 switch (MaybeRecord.get()) {
1367 default: // Default behavior: ignore.
1370 case SM_SLOC_FILE_ENTRY:
1371 case SM_SLOC_BUFFER_ENTRY:
1372 case SM_SLOC_EXPANSION_ENTRY:
1373 // Once we hit one of the source location entries, we're done.
1379 /// If a header file is not found at the path that we expect it to be
1380 /// and the PCH file was moved from its original location, try to resolve the
1381 /// file by assuming that header+PCH were moved together and the header is in
1382 /// the same place relative to the PCH.
1384 resolveFileRelativeToOriginalDir(const std::string &Filename,
1385 const std::string &OriginalDir,
1386 const std::string &CurrDir) {
1387 assert(OriginalDir != CurrDir &&
1388 "No point trying to resolve the file if the PCH dir didn't change");
1390 using namespace llvm::sys;
1392 SmallString<128> filePath(Filename);
1393 fs::make_absolute(filePath);
1394 assert(path::is_absolute(OriginalDir));
1395 SmallString<128> currPCHPath(CurrDir);
1397 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1398 fileDirE = path::end(path::parent_path(filePath));
1399 path::const_iterator origDirI = path::begin(OriginalDir),
1400 origDirE = path::end(OriginalDir);
1401 // Skip the common path components from filePath and OriginalDir.
1402 while (fileDirI != fileDirE && origDirI != origDirE &&
1403 *fileDirI == *origDirI) {
1407 for (; origDirI != origDirE; ++origDirI)
1408 path::append(currPCHPath, "..");
1409 path::append(currPCHPath, fileDirI, fileDirE);
1410 path::append(currPCHPath, path::filename(Filename));
1411 return currPCHPath.str();
1414 bool ASTReader::ReadSLocEntry(int ID) {
1418 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1419 Error("source location entry ID out-of-range for AST file");
1423 // Local helper to read the (possibly-compressed) buffer data following the
1425 auto ReadBuffer = [this](
1426 BitstreamCursor &SLocEntryCursor,
1427 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1430 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1432 Error(MaybeCode.takeError());
1435 unsigned Code = MaybeCode.get();
1437 Expected<unsigned> MaybeRecCode =
1438 SLocEntryCursor.readRecord(Code, Record, &Blob);
1439 if (!MaybeRecCode) {
1440 Error(MaybeRecCode.takeError());
1443 unsigned RecCode = MaybeRecCode.get();
1445 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1446 if (!llvm::zlib::isAvailable()) {
1447 Error("zlib is not available");
1450 SmallString<0> Uncompressed;
1452 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1453 Error("could not decompress embedded file contents: " +
1454 llvm::toString(std::move(E)));
1457 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1458 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1459 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1461 Error("AST record has invalid code");
1466 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1467 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1468 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1469 Error(std::move(Err));
1473 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1474 unsigned BaseOffset = F->SLocEntryBaseOffset;
1476 ++NumSLocEntriesRead;
1477 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1479 Error(MaybeEntry.takeError());
1482 llvm::BitstreamEntry Entry = MaybeEntry.get();
1484 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1485 Error("incorrectly-formatted source location entry in AST file");
1491 Expected<unsigned> MaybeSLOC =
1492 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1494 Error(MaybeSLOC.takeError());
1497 switch (MaybeSLOC.get()) {
1499 Error("incorrectly-formatted source location entry in AST file");
1502 case SM_SLOC_FILE_ENTRY: {
1503 // We will detect whether a file changed and return 'Failure' for it, but
1504 // we will also try to fail gracefully by setting up the SLocEntry.
1505 unsigned InputID = Record[4];
1506 InputFile IF = getInputFile(*F, InputID);
1507 const FileEntry *File = IF.getFile();
1508 bool OverriddenBuffer = IF.isOverridden();
1510 // Note that we only check if a File was returned. If it was out-of-date
1511 // we have complained but we will continue creating a FileID to recover
1516 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1517 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1518 // This is the module's main file.
1519 IncludeLoc = getImportLocation(F);
1521 SrcMgr::CharacteristicKind
1522 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1523 // FIXME: The FileID should be created from the FileEntryRef.
1524 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1525 ID, BaseOffset + Record[0]);
1526 SrcMgr::FileInfo &FileInfo =
1527 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1528 FileInfo.NumCreatedFIDs = Record[5];
1530 FileInfo.setHasLineDirectives();
1532 unsigned NumFileDecls = Record[7];
1533 if (NumFileDecls && ContextObj) {
1534 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1535 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1536 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1540 const SrcMgr::ContentCache *ContentCache
1541 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1542 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1543 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1544 !ContentCache->getRawBuffer()) {
1545 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1548 SourceMgr.overrideFileContents(File, std::move(Buffer));
1554 case SM_SLOC_BUFFER_ENTRY: {
1555 const char *Name = Blob.data();
1556 unsigned Offset = Record[0];
1557 SrcMgr::CharacteristicKind
1558 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1559 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1560 if (IncludeLoc.isInvalid() && F->isModule()) {
1561 IncludeLoc = getImportLocation(F);
1564 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1567 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1568 BaseOffset + Offset, IncludeLoc);
1572 case SM_SLOC_EXPANSION_ENTRY: {
1573 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1574 SourceMgr.createExpansionLoc(SpellingLoc,
1575 ReadSourceLocation(*F, Record[2]),
1576 ReadSourceLocation(*F, Record[3]),
1580 BaseOffset + Record[0]);
1588 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1590 return std::make_pair(SourceLocation(), "");
1592 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1593 Error("source location entry ID out-of-range for AST file");
1594 return std::make_pair(SourceLocation(), "");
1597 // Find which module file this entry lands in.
1598 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1600 return std::make_pair(SourceLocation(), "");
1602 // FIXME: Can we map this down to a particular submodule? That would be
1604 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1607 /// Find the location where the module F is imported.
1608 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1609 if (F->ImportLoc.isValid())
1610 return F->ImportLoc;
1612 // Otherwise we have a PCH. It's considered to be "imported" at the first
1613 // location of its includer.
1614 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1615 // Main file is the importer.
1616 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1617 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1619 return F->ImportedBy[0]->FirstLoc;
1622 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1623 /// the abbreviations that are at the top of the block and then leave the cursor
1624 /// pointing into the block.
1625 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1626 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1627 // FIXME this drops errors on the floor.
1628 consumeError(std::move(Err));
1633 uint64_t Offset = Cursor.GetCurrentBitNo();
1634 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1636 // FIXME this drops errors on the floor.
1637 consumeError(MaybeCode.takeError());
1640 unsigned Code = MaybeCode.get();
1642 // We expect all abbrevs to be at the start of the block.
1643 if (Code != llvm::bitc::DEFINE_ABBREV) {
1644 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1645 // FIXME this drops errors on the floor.
1646 consumeError(std::move(Err));
1651 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1652 // FIXME this drops errors on the floor.
1653 consumeError(std::move(Err));
1659 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1663 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1664 Tok.setLength(Record[Idx++]);
1665 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1666 Tok.setIdentifierInfo(II);
1667 Tok.setKind((tok::TokenKind)Record[Idx++]);
1668 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1672 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1673 BitstreamCursor &Stream = F.MacroCursor;
1675 // Keep track of where we are in the stream, then jump back there
1676 // after reading this macro.
1677 SavedStreamPosition SavedPosition(Stream);
1679 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1680 // FIXME this drops errors on the floor.
1681 consumeError(std::move(Err));
1685 SmallVector<IdentifierInfo*, 16> MacroParams;
1686 MacroInfo *Macro = nullptr;
1689 // Advance to the next record, but if we get to the end of the block, don't
1690 // pop it (removing all the abbreviations from the cursor) since we want to
1691 // be able to reseek within the block and read entries.
1692 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1693 Expected<llvm::BitstreamEntry> MaybeEntry =
1694 Stream.advanceSkippingSubblocks(Flags);
1696 Error(MaybeEntry.takeError());
1699 llvm::BitstreamEntry Entry = MaybeEntry.get();
1701 switch (Entry.Kind) {
1702 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1703 case llvm::BitstreamEntry::Error:
1704 Error("malformed block record in AST file");
1706 case llvm::BitstreamEntry::EndBlock:
1708 case llvm::BitstreamEntry::Record:
1709 // The interesting case.
1715 PreprocessorRecordTypes RecType;
1716 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1717 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1719 Error(MaybeRecType.takeError());
1723 case PP_MODULE_MACRO:
1724 case PP_MACRO_DIRECTIVE_HISTORY:
1727 case PP_MACRO_OBJECT_LIKE:
1728 case PP_MACRO_FUNCTION_LIKE: {
1729 // If we already have a macro, that means that we've hit the end
1730 // of the definition of the macro we were looking for. We're
1735 unsigned NextIndex = 1; // Skip identifier ID.
1736 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1737 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1738 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1739 MI->setIsUsed(Record[NextIndex++]);
1740 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1742 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1743 // Decode function-like macro info.
1744 bool isC99VarArgs = Record[NextIndex++];
1745 bool isGNUVarArgs = Record[NextIndex++];
1746 bool hasCommaPasting = Record[NextIndex++];
1747 MacroParams.clear();
1748 unsigned NumArgs = Record[NextIndex++];
1749 for (unsigned i = 0; i != NumArgs; ++i)
1750 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1752 // Install function-like macro info.
1753 MI->setIsFunctionLike();
1754 if (isC99VarArgs) MI->setIsC99Varargs();
1755 if (isGNUVarArgs) MI->setIsGNUVarargs();
1756 if (hasCommaPasting) MI->setHasCommaPasting();
1757 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1760 // Remember that we saw this macro last so that we add the tokens that
1761 // form its body to it.
1764 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1765 Record[NextIndex]) {
1766 // We have a macro definition. Register the association
1767 PreprocessedEntityID
1768 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1769 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1770 PreprocessingRecord::PPEntityID PPID =
1771 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1772 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1773 PPRec.getPreprocessedEntity(PPID));
1775 PPRec.RegisterMacroDefinition(Macro, PPDef);
1783 // If we see a TOKEN before a PP_MACRO_*, then the file is
1784 // erroneous, just pretend we didn't see this.
1788 Token Tok = ReadToken(F, Record, Idx);
1789 Macro->AddTokenToBody(Tok);
1796 PreprocessedEntityID
1797 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1798 unsigned LocalID) const {
1799 if (!M.ModuleOffsetMap.empty())
1800 ReadModuleOffsetMap(M);
1802 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1803 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1804 assert(I != M.PreprocessedEntityRemap.end()
1805 && "Invalid index into preprocessed entity index remap");
1807 return LocalID + I->second;
1810 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1811 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1814 HeaderFileInfoTrait::internal_key_type
1815 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1816 internal_key_type ikey = {FE->getSize(),
1817 M.HasTimestamps ? FE->getModificationTime() : 0,
1818 FE->getName(), /*Imported*/ false};
1822 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1823 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1826 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1829 // Determine whether the actual files are equivalent.
1830 FileManager &FileMgr = Reader.getFileManager();
1831 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1832 if (!Key.Imported) {
1833 if (auto File = FileMgr.getFile(Key.Filename))
1838 std::string Resolved = Key.Filename;
1839 Reader.ResolveImportedPath(M, Resolved);
1840 if (auto File = FileMgr.getFile(Resolved))
1845 const FileEntry *FEA = GetFile(a);
1846 const FileEntry *FEB = GetFile(b);
1847 return FEA && FEA == FEB;
1850 std::pair<unsigned, unsigned>
1851 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1852 using namespace llvm::support;
1854 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1855 unsigned DataLen = (unsigned) *d++;
1856 return std::make_pair(KeyLen, DataLen);
1859 HeaderFileInfoTrait::internal_key_type
1860 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1861 using namespace llvm::support;
1863 internal_key_type ikey;
1864 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1865 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1866 ikey.Filename = (const char *)d;
1867 ikey.Imported = true;
1871 HeaderFileInfoTrait::data_type
1872 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1874 using namespace llvm::support;
1876 const unsigned char *End = d + DataLen;
1878 unsigned Flags = *d++;
1879 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1880 HFI.isImport |= (Flags >> 5) & 0x01;
1881 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1882 HFI.DirInfo = (Flags >> 1) & 0x07;
1883 HFI.IndexHeaderMapHeader = Flags & 0x01;
1884 // FIXME: Find a better way to handle this. Maybe just store a
1885 // "has been included" flag?
1886 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1888 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1889 M, endian::readNext<uint32_t, little, unaligned>(d));
1890 if (unsigned FrameworkOffset =
1891 endian::readNext<uint32_t, little, unaligned>(d)) {
1892 // The framework offset is 1 greater than the actual offset,
1893 // since 0 is used as an indicator for "no framework name".
1894 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1895 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1898 assert((End - d) % 4 == 0 &&
1899 "Wrong data length in HeaderFileInfo deserialization");
1901 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1902 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1905 // This header is part of a module. Associate it with the module to enable
1906 // implicit module import.
1907 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1908 Module *Mod = Reader.getSubmodule(GlobalSMID);
1909 FileManager &FileMgr = Reader.getFileManager();
1911 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1913 std::string Filename = key.Filename;
1915 Reader.ResolveImportedPath(M, Filename);
1916 // FIXME: This is not always the right filename-as-written, but we're not
1917 // going to use this information to rebuild the module, so it doesn't make
1918 // a lot of difference.
1919 Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
1920 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1921 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1924 // This HeaderFileInfo was externally loaded.
1925 HFI.External = true;
1930 void ASTReader::addPendingMacro(IdentifierInfo *II,
1932 uint64_t MacroDirectivesOffset) {
1933 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1934 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1937 void ASTReader::ReadDefinedMacros() {
1938 // Note that we are loading defined macros.
1939 Deserializing Macros(this);
1941 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1942 BitstreamCursor &MacroCursor = I.MacroCursor;
1944 // If there was no preprocessor block, skip this file.
1945 if (MacroCursor.getBitcodeBytes().empty())
1948 BitstreamCursor Cursor = MacroCursor;
1949 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1950 Error(std::move(Err));
1956 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1958 Error(MaybeE.takeError());
1961 llvm::BitstreamEntry E = MaybeE.get();
1964 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1965 case llvm::BitstreamEntry::Error:
1966 Error("malformed block record in AST file");
1968 case llvm::BitstreamEntry::EndBlock:
1971 case llvm::BitstreamEntry::Record: {
1973 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1975 Error(MaybeRecord.takeError());
1978 switch (MaybeRecord.get()) {
1979 default: // Default behavior: ignore.
1982 case PP_MACRO_OBJECT_LIKE:
1983 case PP_MACRO_FUNCTION_LIKE: {
1984 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1985 if (II->isOutOfDate())
1986 updateOutOfDateIdentifier(*II);
2004 /// Visitor class used to look up identifirs in an AST file.
2005 class IdentifierLookupVisitor {
2008 unsigned PriorGeneration;
2009 unsigned &NumIdentifierLookups;
2010 unsigned &NumIdentifierLookupHits;
2011 IdentifierInfo *Found = nullptr;
2014 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2015 unsigned &NumIdentifierLookups,
2016 unsigned &NumIdentifierLookupHits)
2017 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2018 PriorGeneration(PriorGeneration),
2019 NumIdentifierLookups(NumIdentifierLookups),
2020 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2022 bool operator()(ModuleFile &M) {
2023 // If we've already searched this module file, skip it now.
2024 if (M.Generation <= PriorGeneration)
2027 ASTIdentifierLookupTable *IdTable
2028 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2032 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2034 ++NumIdentifierLookups;
2035 ASTIdentifierLookupTable::iterator Pos =
2036 IdTable->find_hashed(Name, NameHash, &Trait);
2037 if (Pos == IdTable->end())
2040 // Dereferencing the iterator has the effect of building the
2041 // IdentifierInfo node and populating it with the various
2042 // declarations it needs.
2043 ++NumIdentifierLookupHits;
2048 // Retrieve the identifier info found within the module
2050 IdentifierInfo *getIdentifierInfo() const { return Found; }
2055 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2056 // Note that we are loading an identifier.
2057 Deserializing AnIdentifier(this);
2059 unsigned PriorGeneration = 0;
2060 if (getContext().getLangOpts().Modules)
2061 PriorGeneration = IdentifierGeneration[&II];
2063 // If there is a global index, look there first to determine which modules
2064 // provably do not have any results for this identifier.
2065 GlobalModuleIndex::HitSet Hits;
2066 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2067 if (!loadGlobalIndex()) {
2068 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2073 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2074 NumIdentifierLookups,
2075 NumIdentifierLookupHits);
2076 ModuleMgr.visit(Visitor, HitsPtr);
2077 markIdentifierUpToDate(&II);
2080 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2084 II->setOutOfDate(false);
2086 // Update the generation for this identifier.
2087 if (getContext().getLangOpts().Modules)
2088 IdentifierGeneration[II] = getGeneration();
2091 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2092 const PendingMacroInfo &PMInfo) {
2093 ModuleFile &M = *PMInfo.M;
2095 BitstreamCursor &Cursor = M.MacroCursor;
2096 SavedStreamPosition SavedPosition(Cursor);
2097 if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2098 Error(std::move(Err));
2102 struct ModuleMacroRecord {
2103 SubmoduleID SubModID;
2105 SmallVector<SubmoduleID, 8> Overrides;
2107 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2109 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2110 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2114 Expected<llvm::BitstreamEntry> MaybeEntry =
2115 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2117 Error(MaybeEntry.takeError());
2120 llvm::BitstreamEntry Entry = MaybeEntry.get();
2122 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2123 Error("malformed block record in AST file");
2128 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2130 Error(MaybePP.takeError());
2133 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2134 case PP_MACRO_DIRECTIVE_HISTORY:
2137 case PP_MODULE_MACRO: {
2138 ModuleMacros.push_back(ModuleMacroRecord());
2139 auto &Info = ModuleMacros.back();
2140 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2141 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2142 for (int I = 2, N = Record.size(); I != N; ++I)
2143 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2148 Error("malformed block record in AST file");
2152 // We found the macro directive history; that's the last record
2157 // Module macros are listed in reverse dependency order.
2159 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2160 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2161 for (auto &MMR : ModuleMacros) {
2163 for (unsigned ModID : MMR.Overrides) {
2164 Module *Mod = getSubmodule(ModID);
2165 auto *Macro = PP.getModuleMacro(Mod, II);
2166 assert(Macro && "missing definition for overridden macro");
2167 Overrides.push_back(Macro);
2170 bool Inserted = false;
2171 Module *Owner = getSubmodule(MMR.SubModID);
2172 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2176 // Don't read the directive history for a module; we don't have anywhere
2181 // Deserialize the macro directives history in reverse source-order.
2182 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2183 unsigned Idx = 0, N = Record.size();
2185 MacroDirective *MD = nullptr;
2186 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2187 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2189 case MacroDirective::MD_Define: {
2190 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2191 MD = PP.AllocateDefMacroDirective(MI, Loc);
2194 case MacroDirective::MD_Undefine:
2195 MD = PP.AllocateUndefMacroDirective(Loc);
2197 case MacroDirective::MD_Visibility:
2198 bool isPublic = Record[Idx++];
2199 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2206 Earliest->setPrevious(MD);
2211 PP.setLoadedMacroDirective(II, Earliest, Latest);
2214 ASTReader::InputFileInfo
2215 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2216 // Go find this input file.
2217 BitstreamCursor &Cursor = F.InputFilesCursor;
2218 SavedStreamPosition SavedPosition(Cursor);
2219 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2220 // FIXME this drops errors on the floor.
2221 consumeError(std::move(Err));
2224 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2226 // FIXME this drops errors on the floor.
2227 consumeError(MaybeCode.takeError());
2229 unsigned Code = MaybeCode.get();
2233 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2234 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2235 "invalid record type for input file");
2237 // FIXME this drops errors on the floor.
2238 consumeError(Maybe.takeError());
2241 assert(Record[0] == ID && "Bogus stored ID or offset");
2243 R.StoredSize = static_cast<off_t>(Record[1]);
2244 R.StoredTime = static_cast<time_t>(Record[2]);
2245 R.Overridden = static_cast<bool>(Record[3]);
2246 R.Transient = static_cast<bool>(Record[4]);
2247 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2249 ResolveImportedPath(F, R.Filename);
2251 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2252 if (!MaybeEntry) // FIXME this drops errors on the floor.
2253 consumeError(MaybeEntry.takeError());
2254 llvm::BitstreamEntry Entry = MaybeEntry.get();
2255 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2256 "expected record type for input file hash");
2259 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2260 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2261 "invalid record type for input file hash");
2263 // FIXME this drops errors on the floor.
2264 consumeError(Maybe.takeError());
2266 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2267 static_cast<uint64_t>(Record[0]);
2271 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2272 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2273 // If this ID is bogus, just return an empty input file.
2274 if (ID == 0 || ID > F.InputFilesLoaded.size())
2277 // If we've already loaded this input file, return it.
2278 if (F.InputFilesLoaded[ID-1].getFile())
2279 return F.InputFilesLoaded[ID-1];
2281 if (F.InputFilesLoaded[ID-1].isNotFound())
2284 // Go find this input file.
2285 BitstreamCursor &Cursor = F.InputFilesCursor;
2286 SavedStreamPosition SavedPosition(Cursor);
2287 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2288 // FIXME this drops errors on the floor.
2289 consumeError(std::move(Err));
2292 InputFileInfo FI = readInputFileInfo(F, ID);
2293 off_t StoredSize = FI.StoredSize;
2294 time_t StoredTime = FI.StoredTime;
2295 bool Overridden = FI.Overridden;
2296 bool Transient = FI.Transient;
2297 StringRef Filename = FI.Filename;
2298 uint64_t StoredContentHash = FI.ContentHash;
2300 const FileEntry *File = nullptr;
2301 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2304 // If we didn't find the file, resolve it relative to the
2305 // original directory from which this AST file was created.
2306 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2307 F.OriginalDir != F.BaseDirectory) {
2308 std::string Resolved = resolveFileRelativeToOriginalDir(
2309 Filename, F.OriginalDir, F.BaseDirectory);
2310 if (!Resolved.empty())
2311 if (auto FE = FileMgr.getFile(Resolved))
2315 // For an overridden file, create a virtual file with the stored
2317 if ((Overridden || Transient) && File == nullptr)
2318 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2320 if (File == nullptr) {
2322 std::string ErrorStr = "could not find file '";
2323 ErrorStr += Filename;
2324 ErrorStr += "' referenced by AST file '";
2325 ErrorStr += F.FileName;
2329 // Record that we didn't find the file.
2330 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2334 // Check if there was a request to override the contents of the file
2335 // that was part of the precompiled header. Overriding such a file
2336 // can lead to problems when lexing using the source locations from the
2338 SourceManager &SM = getSourceManager();
2339 // FIXME: Reject if the overrides are different.
2340 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2342 Error(diag::err_fe_pch_file_overridden, Filename);
2344 // After emitting the diagnostic, bypass the overriding file to recover
2345 // (this creates a separate FileEntry).
2346 File = SM.bypassFileContentsOverride(*File);
2348 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2353 enum ModificationType {
2359 auto HasInputFileChanged = [&]() {
2360 if (StoredSize != File->getSize())
2361 return ModificationType::Size;
2362 if (!DisableValidation && StoredTime &&
2363 StoredTime != File->getModificationTime()) {
2364 // In case the modification time changes but not the content,
2365 // accept the cached file as legit.
2366 if (ValidateASTInputFilesContent &&
2367 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2368 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2369 if (!MemBuffOrError) {
2371 return ModificationType::ModTime;
2372 std::string ErrorStr = "could not get buffer for file '";
2373 ErrorStr += File->getName();
2376 return ModificationType::ModTime;
2379 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2380 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2381 return ModificationType::None;
2382 return ModificationType::Content;
2384 return ModificationType::ModTime;
2386 return ModificationType::None;
2389 bool IsOutOfDate = false;
2390 auto FileChange = HasInputFileChanged();
2391 // For an overridden file, there is nothing to validate.
2392 if (!Overridden && FileChange != ModificationType::None) {
2394 // Build a list of the PCH imports that got us here (in reverse).
2395 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2396 while (!ImportStack.back()->ImportedBy.empty())
2397 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2399 // The top-level PCH is stale.
2400 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2401 unsigned DiagnosticKind =
2402 moduleKindForDiagnostic(ImportStack.back()->Kind);
2403 if (DiagnosticKind == 0)
2404 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2405 (unsigned)FileChange);
2406 else if (DiagnosticKind == 1)
2407 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2408 (unsigned)FileChange);
2410 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2411 (unsigned)FileChange);
2413 // Print the import stack.
2414 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2415 Diag(diag::note_pch_required_by)
2416 << Filename << ImportStack[0]->FileName;
2417 for (unsigned I = 1; I < ImportStack.size(); ++I)
2418 Diag(diag::note_pch_required_by)
2419 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2422 if (!Diags.isDiagnosticInFlight())
2423 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2428 // FIXME: If the file is overridden and we've already opened it,
2429 // issue an error (or split it into a separate FileEntry).
2431 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2433 // Note that we've loaded this input file.
2434 F.InputFilesLoaded[ID-1] = IF;
2438 /// If we are loading a relocatable PCH or module file, and the filename
2439 /// is not an absolute path, add the system or module root to the beginning of
2441 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2442 // Resolve relative to the base directory, if we have one.
2443 if (!M.BaseDirectory.empty())
2444 return ResolveImportedPath(Filename, M.BaseDirectory);
2447 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2448 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2451 SmallString<128> Buffer;
2452 llvm::sys::path::append(Buffer, Prefix, Filename);
2453 Filename.assign(Buffer.begin(), Buffer.end());
2456 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2458 case ASTReader::Failure: return true;
2459 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2460 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2461 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2462 case ASTReader::ConfigurationMismatch:
2463 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2464 case ASTReader::HadErrors: return true;
2465 case ASTReader::Success: return false;
2468 llvm_unreachable("unknown ASTReadResult");
2471 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2472 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2473 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2474 std::string &SuggestedPredefines) {
2475 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2476 // FIXME this drops errors on the floor.
2477 consumeError(std::move(Err));
2481 // Read all of the records in the options block.
2483 ASTReadResult Result = Success;
2485 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2487 // FIXME this drops errors on the floor.
2488 consumeError(MaybeEntry.takeError());
2491 llvm::BitstreamEntry Entry = MaybeEntry.get();
2493 switch (Entry.Kind) {
2494 case llvm::BitstreamEntry::Error:
2495 case llvm::BitstreamEntry::SubBlock:
2498 case llvm::BitstreamEntry::EndBlock:
2501 case llvm::BitstreamEntry::Record:
2502 // The interesting case.
2506 // Read and process a record.
2508 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2509 if (!MaybeRecordType) {
2510 // FIXME this drops errors on the floor.
2511 consumeError(MaybeRecordType.takeError());
2514 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2515 case LANGUAGE_OPTIONS: {
2516 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2517 if (ParseLanguageOptions(Record, Complain, Listener,
2518 AllowCompatibleConfigurationMismatch))
2519 Result = ConfigurationMismatch;
2523 case TARGET_OPTIONS: {
2524 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2525 if (ParseTargetOptions(Record, Complain, Listener,
2526 AllowCompatibleConfigurationMismatch))
2527 Result = ConfigurationMismatch;
2531 case FILE_SYSTEM_OPTIONS: {
2532 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2533 if (!AllowCompatibleConfigurationMismatch &&
2534 ParseFileSystemOptions(Record, Complain, Listener))
2535 Result = ConfigurationMismatch;
2539 case HEADER_SEARCH_OPTIONS: {
2540 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2541 if (!AllowCompatibleConfigurationMismatch &&
2542 ParseHeaderSearchOptions(Record, Complain, Listener))
2543 Result = ConfigurationMismatch;
2547 case PREPROCESSOR_OPTIONS:
2548 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2549 if (!AllowCompatibleConfigurationMismatch &&
2550 ParsePreprocessorOptions(Record, Complain, Listener,
2551 SuggestedPredefines))
2552 Result = ConfigurationMismatch;
2558 ASTReader::ASTReadResult
2559 ASTReader::ReadControlBlock(ModuleFile &F,
2560 SmallVectorImpl<ImportedModule> &Loaded,
2561 const ModuleFile *ImportedBy,
2562 unsigned ClientLoadCapabilities) {
2563 BitstreamCursor &Stream = F.Stream;
2564 ASTReadResult Result = Success;
2566 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2567 Error(std::move(Err));
2571 // Lambda to read the unhashed control block the first time it's called.
2573 // For PCM files, the unhashed control block cannot be read until after the
2574 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2575 // need to look ahead before reading the IMPORTS record. For consistency,
2576 // this block is always read somehow (see BitstreamEntry::EndBlock).
2577 bool HasReadUnhashedControlBlock = false;
2578 auto readUnhashedControlBlockOnce = [&]() {
2579 if (!HasReadUnhashedControlBlock) {
2580 HasReadUnhashedControlBlock = true;
2581 if (ASTReadResult Result =
2582 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2588 // Read all of the records and blocks in the control block.
2590 unsigned NumInputs = 0;
2591 unsigned NumUserInputs = 0;
2592 StringRef BaseDirectoryAsWritten;
2594 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2596 Error(MaybeEntry.takeError());
2599 llvm::BitstreamEntry Entry = MaybeEntry.get();
2601 switch (Entry.Kind) {
2602 case llvm::BitstreamEntry::Error:
2603 Error("malformed block record in AST file");
2605 case llvm::BitstreamEntry::EndBlock: {
2606 // Validate the module before returning. This call catches an AST with
2607 // no module name and no imports.
2608 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2611 // Validate input files.
2612 const HeaderSearchOptions &HSOpts =
2613 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2615 // All user input files reside at the index range [0, NumUserInputs), and
2616 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2617 // loaded module files, ignore missing inputs.
2618 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2619 F.Kind != MK_PrebuiltModule) {
2620 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2622 // If we are reading a module, we will create a verification timestamp,
2623 // so we verify all input files. Otherwise, verify only user input
2626 unsigned N = NumUserInputs;
2627 if (ValidateSystemInputs ||
2628 (HSOpts.ModulesValidateOncePerBuildSession &&
2629 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2630 F.Kind == MK_ImplicitModule))
2633 for (unsigned I = 0; I < N; ++I) {
2634 InputFile IF = getInputFile(F, I+1, Complain);
2635 if (!IF.getFile() || IF.isOutOfDate())
2641 Listener->visitModuleFile(F.FileName, F.Kind);
2643 if (Listener && Listener->needsInputFileVisitation()) {
2644 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2646 for (unsigned I = 0; I < N; ++I) {
2647 bool IsSystem = I >= NumUserInputs;
2648 InputFileInfo FI = readInputFileInfo(F, I+1);
2649 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2650 F.Kind == MK_ExplicitModule ||
2651 F.Kind == MK_PrebuiltModule);
2658 case llvm::BitstreamEntry::SubBlock:
2660 case INPUT_FILES_BLOCK_ID:
2661 F.InputFilesCursor = Stream;
2662 if (llvm::Error Err = Stream.SkipBlock()) {
2663 Error(std::move(Err));
2666 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2667 Error("malformed block record in AST file");
2672 case OPTIONS_BLOCK_ID:
2673 // If we're reading the first module for this group, check its options
2674 // are compatible with ours. For modules it imports, no further checking
2675 // is required, because we checked them when we built it.
2676 if (Listener && !ImportedBy) {
2677 // Should we allow the configuration of the module file to differ from
2678 // the configuration of the current translation unit in a compatible
2681 // FIXME: Allow this for files explicitly specified with -include-pch.
2682 bool AllowCompatibleConfigurationMismatch =
2683 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2685 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2686 AllowCompatibleConfigurationMismatch,
2687 *Listener, SuggestedPredefines);
2688 if (Result == Failure) {
2689 Error("malformed block record in AST file");
2693 if (DisableValidation ||
2694 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2697 // If we can't load the module, exit early since we likely
2698 // will rebuild the module anyway. The stream may be in the
2699 // middle of a block.
2700 if (Result != Success)
2702 } else if (llvm::Error Err = Stream.SkipBlock()) {
2703 Error(std::move(Err));
2709 if (llvm::Error Err = Stream.SkipBlock()) {
2710 Error(std::move(Err));
2716 case llvm::BitstreamEntry::Record:
2717 // The interesting case.
2721 // Read and process a record.
2724 Expected<unsigned> MaybeRecordType =
2725 Stream.readRecord(Entry.ID, Record, &Blob);
2726 if (!MaybeRecordType) {
2727 Error(MaybeRecordType.takeError());
2730 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2732 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2733 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2734 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2735 : diag::err_pch_version_too_new);
2736 return VersionMismatch;
2739 bool hasErrors = Record[7];
2740 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2741 Diag(diag::err_pch_with_compiler_errors);
2745 Diags.ErrorOccurred = true;
2746 Diags.UncompilableErrorOccurred = true;
2747 Diags.UnrecoverableErrorOccurred = true;
2750 F.RelocatablePCH = Record[4];
2751 // Relative paths in a relocatable PCH are relative to our sysroot.
2752 if (F.RelocatablePCH)
2753 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2755 F.HasTimestamps = Record[5];
2757 F.PCHHasObjectFile = Record[6];
2759 const std::string &CurBranch = getClangFullRepositoryVersion();
2760 StringRef ASTBranch = Blob;
2761 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2762 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2763 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2764 return VersionMismatch;
2770 // Validate the AST before processing any imports (otherwise, untangling
2771 // them can be error-prone and expensive). A module will have a name and
2772 // will already have been validated, but this catches the PCH case.
2773 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2776 // Load each of the imported PCH files.
2777 unsigned Idx = 0, N = Record.size();
2779 // Read information about the AST file.
2780 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2781 // The import location will be the local one for now; we will adjust
2782 // all import locations of module imports after the global source
2783 // location info are setup, in ReadAST.
2784 SourceLocation ImportLoc =
2785 ReadUntranslatedSourceLocation(Record[Idx++]);
2786 off_t StoredSize = (off_t)Record[Idx++];
2787 time_t StoredModTime = (time_t)Record[Idx++];
2788 ASTFileSignature StoredSignature = {
2789 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2790 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2791 (uint32_t)Record[Idx++]}}};
2793 std::string ImportedName = ReadString(Record, Idx);
2794 std::string ImportedFile;
2796 // For prebuilt and explicit modules first consult the file map for
2797 // an override. Note that here we don't search prebuilt module
2798 // directories, only the explicit name to file mappings. Also, we will
2799 // still verify the size/signature making sure it is essentially the
2800 // same file but perhaps in a different location.
2801 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2802 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2803 ImportedName, /*FileMapOnly*/ true);
2805 if (ImportedFile.empty())
2806 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2807 // ModuleCache as when writing.
2808 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2810 SkipPath(Record, Idx);
2812 // If our client can't cope with us being out of date, we can't cope with
2813 // our dependency being missing.
2814 unsigned Capabilities = ClientLoadCapabilities;
2815 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2816 Capabilities &= ~ARR_Missing;
2818 // Load the AST file.
2819 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2820 Loaded, StoredSize, StoredModTime,
2821 StoredSignature, Capabilities);
2823 // If we diagnosed a problem, produce a backtrace.
2824 if (isDiagnosedResult(Result, Capabilities))
2825 Diag(diag::note_module_file_imported_by)
2826 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2829 case Failure: return Failure;
2830 // If we have to ignore the dependency, we'll have to ignore this too.
2832 case OutOfDate: return OutOfDate;
2833 case VersionMismatch: return VersionMismatch;
2834 case ConfigurationMismatch: return ConfigurationMismatch;
2835 case HadErrors: return HadErrors;
2836 case Success: break;
2843 F.OriginalSourceFileID = FileID::get(Record[0]);
2844 F.ActualOriginalSourceFileName = Blob;
2845 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2846 ResolveImportedPath(F, F.OriginalSourceFileName);
2849 case ORIGINAL_FILE_ID:
2850 F.OriginalSourceFileID = FileID::get(Record[0]);
2853 case ORIGINAL_PCH_DIR:
2854 F.OriginalDir = Blob;
2858 F.ModuleName = Blob;
2859 Diag(diag::remark_module_import)
2860 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2861 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2863 Listener->ReadModuleName(F.ModuleName);
2865 // Validate the AST as soon as we have a name so we can exit early on
2867 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2872 case MODULE_DIRECTORY: {
2873 // Save the BaseDirectory as written in the PCM for computing the module
2874 // filename for the ModuleCache.
2875 BaseDirectoryAsWritten = Blob;
2876 assert(!F.ModuleName.empty() &&
2877 "MODULE_DIRECTORY found before MODULE_NAME");
2878 // If we've already loaded a module map file covering this module, we may
2879 // have a better path for it (relative to the current build).
2880 Module *M = PP.getHeaderSearchInfo().lookupModule(
2881 F.ModuleName, /*AllowSearch*/ true,
2882 /*AllowExtraModuleMapSearch*/ true);
2883 if (M && M->Directory) {
2884 // If we're implicitly loading a module, the base directory can't
2885 // change between the build and use.
2886 // Don't emit module relocation error if we have -fno-validate-pch
2887 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2888 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2889 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2890 if (!BuildDir || *BuildDir != M->Directory) {
2891 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2892 Diag(diag::err_imported_module_relocated)
2893 << F.ModuleName << Blob << M->Directory->getName();
2897 F.BaseDirectory = M->Directory->getName();
2899 F.BaseDirectory = Blob;
2904 case MODULE_MAP_FILE:
2905 if (ASTReadResult Result =
2906 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2910 case INPUT_FILE_OFFSETS:
2911 NumInputs = Record[0];
2912 NumUserInputs = Record[1];
2913 F.InputFileOffsets =
2914 (const llvm::support::unaligned_uint64_t *)Blob.data();
2915 F.InputFilesLoaded.resize(NumInputs);
2916 F.NumUserInputFiles = NumUserInputs;
2922 ASTReader::ASTReadResult
2923 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2924 BitstreamCursor &Stream = F.Stream;
2926 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2927 Error(std::move(Err));
2931 // Read all of the records and blocks for the AST file.
2934 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2936 Error(MaybeEntry.takeError());
2939 llvm::BitstreamEntry Entry = MaybeEntry.get();
2941 switch (Entry.Kind) {
2942 case llvm::BitstreamEntry::Error:
2943 Error("error at end of module block in AST file");
2945 case llvm::BitstreamEntry::EndBlock:
2946 // Outside of C++, we do not store a lookup map for the translation unit.
2947 // Instead, mark it as needing a lookup map to be built if this module
2948 // contains any declarations lexically within it (which it always does!).
2949 // This usually has no cost, since we very rarely need the lookup map for
2950 // the translation unit outside C++.
2951 if (ASTContext *Ctx = ContextObj) {
2952 DeclContext *DC = Ctx->getTranslationUnitDecl();
2953 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2954 DC->setMustBuildLookupTable();
2958 case llvm::BitstreamEntry::SubBlock:
2960 case DECLTYPES_BLOCK_ID:
2961 // We lazily load the decls block, but we want to set up the
2962 // DeclsCursor cursor to point into it. Clone our current bitcode
2963 // cursor to it, enter the block and read the abbrevs in that block.
2964 // With the main cursor, we just skip over it.
2965 F.DeclsCursor = Stream;
2966 if (llvm::Error Err = Stream.SkipBlock()) {
2967 Error(std::move(Err));
2970 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2971 Error("malformed block record in AST file");
2976 case PREPROCESSOR_BLOCK_ID:
2977 F.MacroCursor = Stream;
2978 if (!PP.getExternalSource())
2979 PP.setExternalSource(this);
2981 if (llvm::Error Err = Stream.SkipBlock()) {
2982 Error(std::move(Err));
2985 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2986 Error("malformed block record in AST file");
2989 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2992 case PREPROCESSOR_DETAIL_BLOCK_ID:
2993 F.PreprocessorDetailCursor = Stream;
2995 if (llvm::Error Err = Stream.SkipBlock()) {
2996 Error(std::move(Err));
2999 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3000 PREPROCESSOR_DETAIL_BLOCK_ID)) {
3001 Error("malformed preprocessor detail record in AST file");
3004 F.PreprocessorDetailStartOffset
3005 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3007 if (!PP.getPreprocessingRecord())
3008 PP.createPreprocessingRecord();
3009 if (!PP.getPreprocessingRecord()->getExternalSource())
3010 PP.getPreprocessingRecord()->SetExternalSource(*this);
3013 case SOURCE_MANAGER_BLOCK_ID:
3014 if (ReadSourceManagerBlock(F))
3018 case SUBMODULE_BLOCK_ID:
3019 if (ASTReadResult Result =
3020 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3024 case COMMENTS_BLOCK_ID: {
3025 BitstreamCursor C = Stream;
3027 if (llvm::Error Err = Stream.SkipBlock()) {
3028 Error(std::move(Err));
3031 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3032 Error("malformed comments block in AST file");
3035 CommentsCursors.push_back(std::make_pair(C, &F));
3040 if (llvm::Error Err = Stream.SkipBlock()) {
3041 Error(std::move(Err));
3048 case llvm::BitstreamEntry::Record:
3049 // The interesting case.
3053 // Read and process a record.
3056 Expected<unsigned> MaybeRecordType =
3057 Stream.readRecord(Entry.ID, Record, &Blob);
3058 if (!MaybeRecordType) {
3059 Error(MaybeRecordType.takeError());
3062 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3064 // If we're not loading an AST context, we don't care about most records.
3066 switch (RecordType) {
3067 case IDENTIFIER_TABLE:
3068 case IDENTIFIER_OFFSET:
3069 case INTERESTING_IDENTIFIERS:
3071 case PP_CONDITIONAL_STACK:
3072 case PP_COUNTER_VALUE:
3073 case SOURCE_LOCATION_OFFSETS:
3074 case MODULE_OFFSET_MAP:
3075 case SOURCE_MANAGER_LINE_TABLE:
3076 case SOURCE_LOCATION_PRELOADS:
3077 case PPD_ENTITIES_OFFSETS:
3078 case HEADER_SEARCH_TABLE:
3079 case IMPORTED_MODULES:
3087 switch (RecordType) {
3088 default: // Default behavior: ignore.
3092 if (F.LocalNumTypes != 0) {
3093 Error("duplicate TYPE_OFFSET record in AST file");
3096 F.TypeOffsets = (const uint32_t *)Blob.data();
3097 F.LocalNumTypes = Record[0];
3098 unsigned LocalBaseTypeIndex = Record[1];
3099 F.BaseTypeIndex = getTotalNumTypes();
3101 if (F.LocalNumTypes > 0) {
3102 // Introduce the global -> local mapping for types within this module.
3103 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3105 // Introduce the local -> global mapping for types within this module.
3106 F.TypeRemap.insertOrReplace(
3107 std::make_pair(LocalBaseTypeIndex,
3108 F.BaseTypeIndex - LocalBaseTypeIndex));
3110 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3116 if (F.LocalNumDecls != 0) {
3117 Error("duplicate DECL_OFFSET record in AST file");
3120 F.DeclOffsets = (const DeclOffset *)Blob.data();
3121 F.LocalNumDecls = Record[0];
3122 unsigned LocalBaseDeclID = Record[1];
3123 F.BaseDeclID = getTotalNumDecls();
3125 if (F.LocalNumDecls > 0) {
3126 // Introduce the global -> local mapping for declarations within this
3128 GlobalDeclMap.insert(
3129 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3131 // Introduce the local -> global mapping for declarations within this
3133 F.DeclRemap.insertOrReplace(
3134 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3136 // Introduce the global -> local mapping for declarations within this
3138 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3140 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3145 case TU_UPDATE_LEXICAL: {
3146 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3147 LexicalContents Contents(
3148 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3150 static_cast<unsigned int>(Blob.size() / 4));
3151 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3152 TU->setHasExternalLexicalStorage(true);
3156 case UPDATE_VISIBLE: {
3158 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3159 auto *Data = (const unsigned char*)Blob.data();
3160 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3161 // If we've already loaded the decl, perform the updates when we finish
3162 // loading this block.
3163 if (Decl *D = GetExistingDecl(ID))
3164 PendingUpdateRecords.push_back(
3165 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3169 case IDENTIFIER_TABLE:
3170 F.IdentifierTableData = Blob.data();
3172 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3173 (const unsigned char *)F.IdentifierTableData + Record[0],
3174 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3175 (const unsigned char *)F.IdentifierTableData,
3176 ASTIdentifierLookupTrait(*this, F));
3178 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3182 case IDENTIFIER_OFFSET: {
3183 if (F.LocalNumIdentifiers != 0) {
3184 Error("duplicate IDENTIFIER_OFFSET record in AST file");
3187 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3188 F.LocalNumIdentifiers = Record[0];
3189 unsigned LocalBaseIdentifierID = Record[1];
3190 F.BaseIdentifierID = getTotalNumIdentifiers();
3192 if (F.LocalNumIdentifiers > 0) {
3193 // Introduce the global -> local mapping for identifiers within this
3195 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3198 // Introduce the local -> global mapping for identifiers within this
3200 F.IdentifierRemap.insertOrReplace(
3201 std::make_pair(LocalBaseIdentifierID,
3202 F.BaseIdentifierID - LocalBaseIdentifierID));
3204 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3205 + F.LocalNumIdentifiers);
3210 case INTERESTING_IDENTIFIERS:
3211 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3214 case EAGERLY_DESERIALIZED_DECLS:
3215 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3216 // about "interesting" decls (for instance, if we're building a module).
3217 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3218 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3221 case MODULAR_CODEGEN_DECLS:
3222 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3223 // them (ie: if we're not codegenerating this module).
3224 if (F.Kind == MK_MainFile)
3225 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3226 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3230 if (SpecialTypes.empty()) {
3231 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3236 if (SpecialTypes.size() != Record.size()) {
3237 Error("invalid special-types record");
3241 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3242 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3243 if (!SpecialTypes[I])
3244 SpecialTypes[I] = ID;
3245 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3251 TotalNumStatements += Record[0];
3252 TotalNumMacros += Record[1];
3253 TotalLexicalDeclContexts += Record[2];
3254 TotalVisibleDeclContexts += Record[3];
3257 case UNUSED_FILESCOPED_DECLS:
3258 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3259 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3262 case DELEGATING_CTORS:
3263 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3264 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3267 case WEAK_UNDECLARED_IDENTIFIERS:
3268 if (Record.size() % 4 != 0) {
3269 Error("invalid weak identifiers record");
3273 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3274 // files. This isn't the way to do it :)
3275 WeakUndeclaredIdentifiers.clear();
3277 // Translate the weak, undeclared identifiers into global IDs.
3278 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3279 WeakUndeclaredIdentifiers.push_back(
3280 getGlobalIdentifierID(F, Record[I++]));
3281 WeakUndeclaredIdentifiers.push_back(
3282 getGlobalIdentifierID(F, Record[I++]));
3283 WeakUndeclaredIdentifiers.push_back(
3284 ReadSourceLocation(F, Record, I).getRawEncoding());
3285 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3289 case SELECTOR_OFFSETS: {
3290 F.SelectorOffsets = (const uint32_t *)Blob.data();
3291 F.LocalNumSelectors = Record[0];
3292 unsigned LocalBaseSelectorID = Record[1];
3293 F.BaseSelectorID = getTotalNumSelectors();
3295 if (F.LocalNumSelectors > 0) {
3296 // Introduce the global -> local mapping for selectors within this
3298 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3300 // Introduce the local -> global mapping for selectors within this
3302 F.SelectorRemap.insertOrReplace(
3303 std::make_pair(LocalBaseSelectorID,
3304 F.BaseSelectorID - LocalBaseSelectorID));
3306 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3312 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3314 F.SelectorLookupTable
3315 = ASTSelectorLookupTable::Create(
3316 F.SelectorLookupTableData + Record[0],
3317 F.SelectorLookupTableData,
3318 ASTSelectorLookupTrait(*this, F));
3319 TotalNumMethodPoolEntries += Record[1];
3322 case REFERENCED_SELECTOR_POOL:
3323 if (!Record.empty()) {
3324 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3325 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3327 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3333 case PP_CONDITIONAL_STACK:
3334 if (!Record.empty()) {
3335 unsigned Idx = 0, End = Record.size() - 1;
3336 bool ReachedEOFWhileSkipping = Record[Idx++];
3337 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3338 if (ReachedEOFWhileSkipping) {
3339 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3340 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3341 bool FoundNonSkipPortion = Record[Idx++];
3342 bool FoundElse = Record[Idx++];
3343 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3344 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3345 FoundElse, ElseLoc);
3347 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3349 auto Loc = ReadSourceLocation(F, Record, Idx);
3350 bool WasSkipping = Record[Idx++];
3351 bool FoundNonSkip = Record[Idx++];
3352 bool FoundElse = Record[Idx++];
3353 ConditionalStack.push_back(
3354 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3356 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3360 case PP_COUNTER_VALUE:
3361 if (!Record.empty() && Listener)
3362 Listener->ReadCounter(F, Record[0]);
3365 case FILE_SORTED_DECLS:
3366 F.FileSortedDecls = (const DeclID *)Blob.data();
3367 F.NumFileSortedDecls = Record[0];
3370 case SOURCE_LOCATION_OFFSETS: {
3371 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3372 F.LocalNumSLocEntries = Record[0];
3373 unsigned SLocSpaceSize = Record[1];
3374 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3375 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3377 if (!F.SLocEntryBaseID) {
3378 Error("ran out of source locations");
3381 // Make our entry in the range map. BaseID is negative and growing, so
3382 // we invert it. Because we invert it, though, we need the other end of
3384 unsigned RangeStart =
3385 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3386 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3387 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3389 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3390 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3391 GlobalSLocOffsetMap.insert(
3392 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3393 - SLocSpaceSize,&F));
3395 // Initialize the remapping table.
3396 // Invalid stays invalid.
3397 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3398 // This module. Base was 2 when being compiled.
3399 F.SLocRemap.insertOrReplace(std::make_pair(2U,
3400 static_cast<int>(F.SLocEntryBaseOffset - 2)));
3402 TotalNumSLocEntries += F.LocalNumSLocEntries;
3406 case MODULE_OFFSET_MAP:
3407 F.ModuleOffsetMap = Blob;
3410 case SOURCE_MANAGER_LINE_TABLE:
3411 if (ParseLineTable(F, Record))
3415 case SOURCE_LOCATION_PRELOADS: {
3416 // Need to transform from the local view (1-based IDs) to the global view,
3417 // which is based off F.SLocEntryBaseID.
3418 if (!F.PreloadSLocEntries.empty()) {
3419 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3423 F.PreloadSLocEntries.swap(Record);
3427 case EXT_VECTOR_DECLS:
3428 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3429 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3433 if (Record.size() % 3 != 0) {
3434 Error("Invalid VTABLE_USES record");
3438 // Later tables overwrite earlier ones.
3439 // FIXME: Modules will have some trouble with this. This is clearly not
3440 // the right way to do this.
3443 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3444 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3445 VTableUses.push_back(
3446 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3447 VTableUses.push_back(Record[Idx++]);
3451 case PENDING_IMPLICIT_INSTANTIATIONS:
3452 if (PendingInstantiations.size() % 2 != 0) {
3453 Error("Invalid existing PendingInstantiations");
3457 if (Record.size() % 2 != 0) {
3458 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3462 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3463 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3464 PendingInstantiations.push_back(
3465 ReadSourceLocation(F, Record, I).getRawEncoding());
3469 case SEMA_DECL_REFS:
3470 if (Record.size() != 3) {
3471 Error("Invalid SEMA_DECL_REFS block");
3474 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3475 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3478 case PPD_ENTITIES_OFFSETS: {
3479 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3480 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3481 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3483 unsigned LocalBasePreprocessedEntityID = Record[0];
3485 unsigned StartingID;
3486 if (!PP.getPreprocessingRecord())
3487 PP.createPreprocessingRecord();
3488 if (!PP.getPreprocessingRecord()->getExternalSource())
3489 PP.getPreprocessingRecord()->SetExternalSource(*this);
3491 = PP.getPreprocessingRecord()
3492 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3493 F.BasePreprocessedEntityID = StartingID;
3495 if (F.NumPreprocessedEntities > 0) {
3496 // Introduce the global -> local mapping for preprocessed entities in
3498 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3500 // Introduce the local -> global mapping for preprocessed entities in
3502 F.PreprocessedEntityRemap.insertOrReplace(
3503 std::make_pair(LocalBasePreprocessedEntityID,
3504 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3510 case PPD_SKIPPED_RANGES: {
3511 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3512 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3513 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3515 if (!PP.getPreprocessingRecord())
3516 PP.createPreprocessingRecord();
3517 if (!PP.getPreprocessingRecord()->getExternalSource())
3518 PP.getPreprocessingRecord()->SetExternalSource(*this);
3519 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3520 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3522 if (F.NumPreprocessedSkippedRanges > 0)
3523 GlobalSkippedRangeMap.insert(
3524 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3528 case DECL_UPDATE_OFFSETS:
3529 if (Record.size() % 2 != 0) {
3530 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3533 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3534 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3535 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3537 // If we've already loaded the decl, perform the updates when we finish
3538 // loading this block.
3539 if (Decl *D = GetExistingDecl(ID))
3540 PendingUpdateRecords.push_back(
3541 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3545 case OBJC_CATEGORIES_MAP:
3546 if (F.LocalNumObjCCategoriesInMap != 0) {
3547 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3551 F.LocalNumObjCCategoriesInMap = Record[0];
3552 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3555 case OBJC_CATEGORIES:
3556 F.ObjCCategories.swap(Record);
3559 case CUDA_SPECIAL_DECL_REFS:
3560 // Later tables overwrite earlier ones.
3561 // FIXME: Modules will have trouble with this.
3562 CUDASpecialDeclRefs.clear();
3563 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3564 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3567 case HEADER_SEARCH_TABLE:
3568 F.HeaderFileInfoTableData = Blob.data();
3569 F.LocalNumHeaderFileInfos = Record[1];
3571 F.HeaderFileInfoTable
3572 = HeaderFileInfoLookupTable::Create(
3573 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3574 (const unsigned char *)F.HeaderFileInfoTableData,
3575 HeaderFileInfoTrait(*this, F,
3576 &PP.getHeaderSearchInfo(),
3577 Blob.data() + Record[2]));
3579 PP.getHeaderSearchInfo().SetExternalSource(this);
3580 if (!PP.getHeaderSearchInfo().getExternalLookup())
3581 PP.getHeaderSearchInfo().SetExternalLookup(this);
3585 case FP_PRAGMA_OPTIONS:
3586 // Later tables overwrite earlier ones.
3587 FPPragmaOptions.swap(Record);
3590 case OPENCL_EXTENSIONS:
3591 for (unsigned I = 0, E = Record.size(); I != E; ) {
3592 auto Name = ReadString(Record, I);
3593 auto &Opt = OpenCLExtensions.OptMap[Name];
3594 Opt.Supported = Record[I++] != 0;
3595 Opt.Enabled = Record[I++] != 0;
3596 Opt.Avail = Record[I++];
3597 Opt.Core = Record[I++];
3601 case OPENCL_EXTENSION_TYPES:
3602 for (unsigned I = 0, E = Record.size(); I != E;) {
3603 auto TypeID = static_cast<::TypeID>(Record[I++]);
3604 auto *Type = GetType(TypeID).getTypePtr();
3605 auto NumExt = static_cast<unsigned>(Record[I++]);
3606 for (unsigned II = 0; II != NumExt; ++II) {
3607 auto Ext = ReadString(Record, I);
3608 OpenCLTypeExtMap[Type].insert(Ext);
3613 case OPENCL_EXTENSION_DECLS:
3614 for (unsigned I = 0, E = Record.size(); I != E;) {
3615 auto DeclID = static_cast<::DeclID>(Record[I++]);
3616 auto *Decl = GetDecl(DeclID);
3617 auto NumExt = static_cast<unsigned>(Record[I++]);
3618 for (unsigned II = 0; II != NumExt; ++II) {
3619 auto Ext = ReadString(Record, I);
3620 OpenCLDeclExtMap[Decl].insert(Ext);
3625 case TENTATIVE_DEFINITIONS:
3626 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3627 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3630 case KNOWN_NAMESPACES:
3631 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3632 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3635 case UNDEFINED_BUT_USED:
3636 if (UndefinedButUsed.size() % 2 != 0) {
3637 Error("Invalid existing UndefinedButUsed");
3641 if (Record.size() % 2 != 0) {
3642 Error("invalid undefined-but-used record");
3645 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3646 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3647 UndefinedButUsed.push_back(
3648 ReadSourceLocation(F, Record, I).getRawEncoding());
3652 case DELETE_EXPRS_TO_ANALYZE:
3653 for (unsigned I = 0, N = Record.size(); I != N;) {
3654 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3655 const uint64_t Count = Record[I++];
3656 DelayedDeleteExprs.push_back(Count);
3657 for (uint64_t C = 0; C < Count; ++C) {
3658 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3659 bool IsArrayForm = Record[I++] == 1;
3660 DelayedDeleteExprs.push_back(IsArrayForm);
3665 case IMPORTED_MODULES:
3666 if (!F.isModule()) {
3667 // If we aren't loading a module (which has its own exports), make
3668 // all of the imported modules visible.
3669 // FIXME: Deal with macros-only imports.
3670 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3671 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3672 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3674 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3675 if (DeserializationListener)
3676 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3682 case MACRO_OFFSET: {
3683 if (F.LocalNumMacros != 0) {
3684 Error("duplicate MACRO_OFFSET record in AST file");
3687 F.MacroOffsets = (const uint32_t *)Blob.data();
3688 F.LocalNumMacros = Record[0];
3689 unsigned LocalBaseMacroID = Record[1];
3690 F.BaseMacroID = getTotalNumMacros();
3692 if (F.LocalNumMacros > 0) {
3693 // Introduce the global -> local mapping for macros within this module.
3694 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3696 // Introduce the local -> global mapping for macros within this module.
3697 F.MacroRemap.insertOrReplace(
3698 std::make_pair(LocalBaseMacroID,
3699 F.BaseMacroID - LocalBaseMacroID));
3701 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3706 case LATE_PARSED_TEMPLATE:
3707 LateParsedTemplates.append(Record.begin(), Record.end());
3710 case OPTIMIZE_PRAGMA_OPTIONS:
3711 if (Record.size() != 1) {
3712 Error("invalid pragma optimize record");
3715 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3718 case MSSTRUCT_PRAGMA_OPTIONS:
3719 if (Record.size() != 1) {
3720 Error("invalid pragma ms_struct record");
3723 PragmaMSStructState = Record[0];
3726 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3727 if (Record.size() != 2) {
3728 Error("invalid pragma ms_struct record");
3731 PragmaMSPointersToMembersState = Record[0];
3732 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3735 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3736 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3737 UnusedLocalTypedefNameCandidates.push_back(
3738 getGlobalDeclID(F, Record[I]));
3741 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3742 if (Record.size() != 1) {
3743 Error("invalid cuda pragma options record");
3746 ForceCUDAHostDeviceDepth = Record[0];
3749 case PACK_PRAGMA_OPTIONS: {
3750 if (Record.size() < 3) {
3751 Error("invalid pragma pack record");
3754 PragmaPackCurrentValue = Record[0];
3755 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3756 unsigned NumStackEntries = Record[2];
3758 // Reset the stack when importing a new module.
3759 PragmaPackStack.clear();
3760 for (unsigned I = 0; I < NumStackEntries; ++I) {
3761 PragmaPackStackEntry Entry;
3762 Entry.Value = Record[Idx++];
3763 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3764 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3765 PragmaPackStrings.push_back(ReadString(Record, Idx));
3766 Entry.SlotLabel = PragmaPackStrings.back();
3767 PragmaPackStack.push_back(Entry);
3775 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3776 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3778 // Additional remapping information.
3779 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3780 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3781 F.ModuleOffsetMap = StringRef();
3783 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3784 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3785 F.SLocRemap.insert(std::make_pair(0U, 0));
3786 F.SLocRemap.insert(std::make_pair(2U, 1));
3789 // Continuous range maps we may be updating in our module.
3790 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3791 RemapBuilder SLocRemap(F.SLocRemap);
3792 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3793 RemapBuilder MacroRemap(F.MacroRemap);
3794 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3795 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3796 RemapBuilder SelectorRemap(F.SelectorRemap);
3797 RemapBuilder DeclRemap(F.DeclRemap);
3798 RemapBuilder TypeRemap(F.TypeRemap);
3800 while (Data < DataEnd) {
3801 // FIXME: Looking up dependency modules by filename is horrible. Let's
3802 // start fixing this with prebuilt and explicit modules and see how it
3804 using namespace llvm::support;
3805 ModuleKind Kind = static_cast<ModuleKind>(
3806 endian::readNext<uint8_t, little, unaligned>(Data));
3807 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3808 StringRef Name = StringRef((const char*)Data, Len);
3810 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3811 ? ModuleMgr.lookupByModuleName(Name)
3812 : ModuleMgr.lookupByFileName(Name));
3815 "SourceLocation remap refers to unknown module, cannot find ";
3821 uint32_t SLocOffset =
3822 endian::readNext<uint32_t, little, unaligned>(Data);
3823 uint32_t IdentifierIDOffset =
3824 endian::readNext<uint32_t, little, unaligned>(Data);
3825 uint32_t MacroIDOffset =
3826 endian::readNext<uint32_t, little, unaligned>(Data);
3827 uint32_t PreprocessedEntityIDOffset =
3828 endian::readNext<uint32_t, little, unaligned>(Data);
3829 uint32_t SubmoduleIDOffset =
3830 endian::readNext<uint32_t, little, unaligned>(Data);
3831 uint32_t SelectorIDOffset =
3832 endian::readNext<uint32_t, little, unaligned>(Data);
3833 uint32_t DeclIDOffset =
3834 endian::readNext<uint32_t, little, unaligned>(Data);
3835 uint32_t TypeIndexOffset =
3836 endian::readNext<uint32_t, little, unaligned>(Data);
3838 uint32_t None = std::numeric_limits<uint32_t>::max();
3840 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3841 RemapBuilder &Remap) {
3843 Remap.insert(std::make_pair(Offset,
3844 static_cast<int>(BaseOffset - Offset)));
3846 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3847 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3848 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3849 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3850 PreprocessedEntityRemap);
3851 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3852 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3853 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3854 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3856 // Global -> local mappings.
3857 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3861 ASTReader::ASTReadResult
3862 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3863 const ModuleFile *ImportedBy,
3864 unsigned ClientLoadCapabilities) {
3866 F.ModuleMapPath = ReadPath(F, Record, Idx);
3868 // Try to resolve ModuleName in the current header search context and
3869 // verify that it is found in the same module map file as we saved. If the
3870 // top-level AST file is a main file, skip this check because there is no
3871 // usable header search context.
3872 assert(!F.ModuleName.empty() &&
3873 "MODULE_NAME should come before MODULE_MAP_FILE");
3874 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3875 // An implicitly-loaded module file should have its module listed in some
3876 // module map file that we've already loaded.
3877 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3878 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3879 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3880 // Don't emit module relocation error if we have -fno-validate-pch
3881 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3882 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3883 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3884 // This module was defined by an imported (explicit) module.
3885 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3886 << ASTFE->getName();
3888 // This module was built with a different module map.
3889 Diag(diag::err_imported_module_not_found)
3890 << F.ModuleName << F.FileName
3891 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3893 // In case it was imported by a PCH, there's a chance the user is
3894 // just missing to include the search path to the directory containing
3896 if (ImportedBy && ImportedBy->Kind == MK_PCH)
3897 Diag(diag::note_imported_by_pch_module_not_found)
3898 << llvm::sys::path::parent_path(F.ModuleMapPath);
3904 assert(M->Name == F.ModuleName && "found module with different name");
3906 // Check the primary module map file.
3907 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3908 if (!StoredModMap || *StoredModMap != ModMap) {
3909 assert(ModMap && "found module is missing module map file");
3910 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3911 "top-level import should be verified");
3912 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3913 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3914 Diag(diag::err_imported_module_modmap_changed)
3915 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3916 << ModMap->getName() << F.ModuleMapPath << NotImported;
3920 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3921 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3922 // FIXME: we should use input files rather than storing names.
3923 std::string Filename = ReadPath(F, Record, Idx);
3924 auto F = FileMgr.getFile(Filename, false, false);
3926 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3927 Error("could not find file '" + Filename +"' referenced by AST file");
3930 AdditionalStoredMaps.insert(*F);
3933 // Check any additional module map files (e.g. module.private.modulemap)
3934 // that are not in the pcm.
3935 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3936 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3937 // Remove files that match
3938 // Note: SmallPtrSet::erase is really remove
3939 if (!AdditionalStoredMaps.erase(ModMap)) {
3940 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3941 Diag(diag::err_module_different_modmap)
3942 << F.ModuleName << /*new*/0 << ModMap->getName();
3948 // Check any additional module map files that are in the pcm, but not
3949 // found in header search. Cases that match are already removed.
3950 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3951 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3952 Diag(diag::err_module_different_modmap)
3953 << F.ModuleName << /*not new*/1 << ModMap->getName();
3959 Listener->ReadModuleMapFile(F.ModuleMapPath);
3963 /// Move the given method to the back of the global list of methods.
3964 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3965 // Find the entry for this selector in the method pool.
3966 Sema::GlobalMethodPool::iterator Known
3967 = S.MethodPool.find(Method->getSelector());
3968 if (Known == S.MethodPool.end())
3971 // Retrieve the appropriate method list.
3972 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3973 : Known->second.second;
3975 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3977 if (List->getMethod() == Method) {
3985 if (List->getNext())
3986 List->setMethod(List->getNext()->getMethod());
3988 List->setMethod(Method);
3992 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3993 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3994 for (Decl *D : Names) {
3995 bool wasHidden = D->isHidden();
3996 D->setVisibleDespiteOwningModule();
3998 if (wasHidden && SemaObj) {
3999 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4000 moveMethodToBackOfGlobalList(*SemaObj, Method);
4006 void ASTReader::makeModuleVisible(Module *Mod,
4007 Module::NameVisibilityKind NameVisibility,
4008 SourceLocation ImportLoc) {
4009 llvm::SmallPtrSet<Module *, 4> Visited;
4010 SmallVector<Module *, 4> Stack;
4011 Stack.push_back(Mod);
4012 while (!Stack.empty()) {
4013 Mod = Stack.pop_back_val();
4015 if (NameVisibility <= Mod->NameVisibility) {
4016 // This module already has this level of visibility (or greater), so
4017 // there is nothing more to do.
4021 if (!Mod->isAvailable()) {
4022 // Modules that aren't available cannot be made visible.
4026 // Update the module's name visibility.
4027 Mod->NameVisibility = NameVisibility;
4029 // If we've already deserialized any names from this module,
4030 // mark them as visible.
4031 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4032 if (Hidden != HiddenNamesMap.end()) {
4033 auto HiddenNames = std::move(*Hidden);
4034 HiddenNamesMap.erase(Hidden);
4035 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4036 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4037 "making names visible added hidden names");
4040 // Push any exported modules onto the stack to be marked as visible.
4041 SmallVector<Module *, 16> Exports;
4042 Mod->getExportedModules(Exports);
4043 for (SmallVectorImpl<Module *>::iterator
4044 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4045 Module *Exported = *I;
4046 if (Visited.insert(Exported).second)
4047 Stack.push_back(Exported);
4052 /// We've merged the definition \p MergedDef into the existing definition
4053 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4055 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4056 NamedDecl *MergedDef) {
4057 if (Def->isHidden()) {
4058 // If MergedDef is visible or becomes visible, make the definition visible.
4059 if (!MergedDef->isHidden())
4060 Def->setVisibleDespiteOwningModule();
4062 getContext().mergeDefinitionIntoModule(
4063 Def, MergedDef->getImportedOwningModule(),
4064 /*NotifyListeners*/ false);
4065 PendingMergedDefinitionsToDeduplicate.insert(Def);
4070 bool ASTReader::loadGlobalIndex() {
4074 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4075 !PP.getLangOpts().Modules)
4078 // Try to load the global index.
4079 TriedLoadingGlobalIndex = true;
4080 StringRef ModuleCachePath
4081 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4082 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4083 GlobalModuleIndex::readIndex(ModuleCachePath);
4084 if (llvm::Error Err = std::move(Result.second)) {
4085 assert(!Result.first);
4086 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4090 GlobalIndex.reset(Result.first);
4091 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4095 bool ASTReader::isGlobalIndexUnavailable() const {
4096 return PP.getLangOpts().Modules && UseGlobalIndex &&
4097 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4100 static void updateModuleTimestamp(ModuleFile &MF) {
4101 // Overwrite the timestamp file contents so that file's mtime changes.
4102 std::string TimestampFilename = MF.getTimestampFilename();
4104 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4107 OS << "Timestamp file\n";
4109 OS.clear_error(); // Avoid triggering a fatal error.
4112 /// Given a cursor at the start of an AST file, scan ahead and drop the
4113 /// cursor into the start of the given block ID, returning false on success and
4114 /// true on failure.
4115 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4117 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4119 // FIXME this drops errors on the floor.
4120 consumeError(MaybeEntry.takeError());
4123 llvm::BitstreamEntry Entry = MaybeEntry.get();
4125 switch (Entry.Kind) {
4126 case llvm::BitstreamEntry::Error:
4127 case llvm::BitstreamEntry::EndBlock:
4130 case llvm::BitstreamEntry::Record:
4131 // Ignore top-level records.
4132 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4135 // FIXME this drops errors on the floor.
4136 consumeError(Skipped.takeError());
4140 case llvm::BitstreamEntry::SubBlock:
4141 if (Entry.ID == BlockID) {
4142 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4143 // FIXME this drops the error on the floor.
4144 consumeError(std::move(Err));
4151 if (llvm::Error Err = Cursor.SkipBlock()) {
4152 // FIXME this drops the error on the floor.
4153 consumeError(std::move(Err));
4160 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4162 SourceLocation ImportLoc,
4163 unsigned ClientLoadCapabilities,
4164 SmallVectorImpl<ImportedSubmodule> *Imported) {
4165 llvm::SaveAndRestore<SourceLocation>
4166 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4168 // Defer any pending actions until we get to the end of reading the AST file.
4169 Deserializing AnASTFile(this);
4171 // Bump the generation number.
4172 unsigned PreviousGeneration = 0;
4174 PreviousGeneration = incrementGeneration(*ContextObj);
4176 unsigned NumModules = ModuleMgr.size();
4177 SmallVector<ImportedModule, 4> Loaded;
4178 switch (ASTReadResult ReadResult =
4179 ReadASTCore(FileName, Type, ImportLoc,
4180 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4181 ASTFileSignature(), ClientLoadCapabilities)) {
4185 case VersionMismatch:
4186 case ConfigurationMismatch:
4188 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
4189 for (const ImportedModule &IM : Loaded)
4190 LoadedSet.insert(IM.Mod);
4192 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
4193 PP.getLangOpts().Modules
4194 ? &PP.getHeaderSearchInfo().getModuleMap()
4197 // If we find that any modules are unusable, the global index is going
4198 // to be out-of-date. Just remove it.
4199 GlobalIndex.reset();
4200 ModuleMgr.setGlobalIndex(nullptr);
4207 // Here comes stuff that we only do once the entire chain is loaded.
4209 // Load the AST blocks of all of the modules that we loaded.
4210 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4211 MEnd = Loaded.end();
4213 ModuleFile &F = *M->Mod;
4215 // Read the AST block.
4216 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4219 // Read the extension blocks.
4220 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4221 if (ASTReadResult Result = ReadExtensionBlock(F))
4225 // Once read, set the ModuleFile bit base offset and update the size in
4226 // bits of all files we've seen.
4227 F.GlobalBitOffset = TotalModulesSizeInBits;
4228 TotalModulesSizeInBits += F.SizeInBits;
4229 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4231 // Preload SLocEntries.
4232 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4233 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4234 // Load it through the SourceManager and don't call ReadSLocEntry()
4235 // directly because the entry may have already been loaded in which case
4236 // calling ReadSLocEntry() directly would trigger an assertion in
4238 SourceMgr.getLoadedSLocEntryByID(Index);
4241 // Map the original source file ID into the ID space of the current
4243 if (F.OriginalSourceFileID.isValid()) {
4244 F.OriginalSourceFileID = FileID::get(
4245 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4248 // Preload all the pending interesting identifiers by marking them out of
4250 for (auto Offset : F.PreloadIdentifierOffsets) {
4251 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4252 F.IdentifierTableData + Offset);
4254 ASTIdentifierLookupTrait Trait(*this, F);
4255 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4256 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4257 auto &II = PP.getIdentifierTable().getOwn(Key);
4258 II.setOutOfDate(true);
4260 // Mark this identifier as being from an AST file so that we can track
4261 // whether we need to serialize it.
4262 markIdentifierFromAST(*this, II);
4264 // Associate the ID with the identifier so that the writer can reuse it.
4265 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4266 SetIdentifierInfo(ID, &II);
4270 // Setup the import locations and notify the module manager that we've
4271 // committed to these module files.
4272 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4273 MEnd = Loaded.end();
4275 ModuleFile &F = *M->Mod;
4277 ModuleMgr.moduleFileAccepted(&F);
4279 // Set the import location.
4280 F.DirectImportLoc = ImportLoc;
4281 // FIXME: We assume that locations from PCH / preamble do not need
4284 F.ImportLoc = M->ImportLoc;
4286 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4289 if (!PP.getLangOpts().CPlusPlus ||
4290 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4291 Type != MK_PrebuiltModule)) {
4292 // Mark all of the identifiers in the identifier table as being out of date,
4293 // so that various accessors know to check the loaded modules when the
4294 // identifier is used.
4296 // For C++ modules, we don't need information on many identifiers (just
4297 // those that provide macros or are poisoned), so we mark all of
4298 // the interesting ones via PreloadIdentifierOffsets.
4299 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4300 IdEnd = PP.getIdentifierTable().end();
4302 Id->second->setOutOfDate(true);
4304 // Mark selectors as out of date.
4305 for (auto Sel : SelectorGeneration)
4306 SelectorOutOfDate[Sel.first] = true;
4308 // Resolve any unresolved module exports.
4309 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4310 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4311 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4312 Module *ResolvedMod = getSubmodule(GlobalID);
4314 switch (Unresolved.Kind) {
4315 case UnresolvedModuleRef::Conflict:
4317 Module::Conflict Conflict;
4318 Conflict.Other = ResolvedMod;
4319 Conflict.Message = Unresolved.String.str();
4320 Unresolved.Mod->Conflicts.push_back(Conflict);
4324 case UnresolvedModuleRef::Import:
4326 Unresolved.Mod->Imports.insert(ResolvedMod);
4329 case UnresolvedModuleRef::Export:
4330 if (ResolvedMod || Unresolved.IsWildcard)
4331 Unresolved.Mod->Exports.push_back(
4332 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4336 UnresolvedModuleRefs.clear();
4339 Imported->append(ImportedModules.begin(),
4340 ImportedModules.end());
4342 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4343 // Might be unnecessary as use declarations are only used to build the
4347 InitializeContext();
4352 if (DeserializationListener)
4353 DeserializationListener->ReaderInitialized(this);
4355 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4356 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4357 // If this AST file is a precompiled preamble, then set the
4358 // preamble file ID of the source manager to the file source file
4359 // from which the preamble was built.
4360 if (Type == MK_Preamble) {
4361 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4362 } else if (Type == MK_MainFile) {
4363 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4367 // For any Objective-C class definitions we have already loaded, make sure
4368 // that we load any additional categories.
4370 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4371 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4372 ObjCClassesLoaded[I],
4373 PreviousGeneration);
4377 if (PP.getHeaderSearchInfo()
4378 .getHeaderSearchOpts()
4379 .ModulesValidateOncePerBuildSession) {
4380 // Now we are certain that the module and all modules it depends on are
4381 // up to date. Create or update timestamp files for modules that are
4382 // located in the module cache (not for PCH files that could be anywhere
4383 // in the filesystem).
4384 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4385 ImportedModule &M = Loaded[I];
4386 if (M.Mod->Kind == MK_ImplicitModule) {
4387 updateModuleTimestamp(*M.Mod);
4395 static ASTFileSignature readASTFileSignature(StringRef PCH);
4397 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4398 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4399 // FIXME checking magic headers is done in other places such as
4400 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4401 // always done the same. Unify it all with a helper.
4402 if (!Stream.canSkipToPos(4))
4403 return llvm::createStringError(std::errc::illegal_byte_sequence,
4404 "file too small to contain AST file magic");
4405 for (unsigned C : {'C', 'P', 'C', 'H'})
4406 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4408 return llvm::createStringError(
4409 std::errc::illegal_byte_sequence,
4410 "file doesn't start with AST file magic");
4412 return Res.takeError();
4413 return llvm::Error::success();
4416 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4420 case MK_ImplicitModule:
4421 case MK_ExplicitModule:
4422 case MK_PrebuiltModule:
4426 return 2; // main source file
4428 llvm_unreachable("unknown module kind");
4431 ASTReader::ASTReadResult
4432 ASTReader::ReadASTCore(StringRef FileName,
4434 SourceLocation ImportLoc,
4435 ModuleFile *ImportedBy,
4436 SmallVectorImpl<ImportedModule> &Loaded,
4437 off_t ExpectedSize, time_t ExpectedModTime,
4438 ASTFileSignature ExpectedSignature,
4439 unsigned ClientLoadCapabilities) {
4441 std::string ErrorStr;
4442 ModuleManager::AddModuleResult AddResult
4443 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4444 getGeneration(), ExpectedSize, ExpectedModTime,
4445 ExpectedSignature, readASTFileSignature,
4448 switch (AddResult) {
4449 case ModuleManager::AlreadyLoaded:
4450 Diag(diag::remark_module_import)
4451 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4452 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4455 case ModuleManager::NewlyLoaded:
4456 // Load module file below.
4459 case ModuleManager::Missing:
4460 // The module file was missing; if the client can handle that, return
4462 if (ClientLoadCapabilities & ARR_Missing)
4465 // Otherwise, return an error.
4466 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4467 << FileName << !ErrorStr.empty()
4471 case ModuleManager::OutOfDate:
4472 // We couldn't load the module file because it is out-of-date. If the
4473 // client can handle out-of-date, return it.
4474 if (ClientLoadCapabilities & ARR_OutOfDate)
4477 // Otherwise, return an error.
4478 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4479 << FileName << !ErrorStr.empty()
4484 assert(M && "Missing module file");
4486 bool ShouldFinalizePCM = false;
4487 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4488 auto &MC = getModuleManager().getModuleCache();
4489 if (ShouldFinalizePCM)
4490 MC.finalizePCM(FileName);
4492 MC.tryToDropPCM(FileName);
4495 BitstreamCursor &Stream = F.Stream;
4496 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4497 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4499 // Sniff for the signature.
4500 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4501 Diag(diag::err_module_file_invalid)
4502 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4506 // This is used for compatibility with older PCH formats.
4507 bool HaveReadControlBlock = false;
4509 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4511 Error(MaybeEntry.takeError());
4514 llvm::BitstreamEntry Entry = MaybeEntry.get();
4516 switch (Entry.Kind) {
4517 case llvm::BitstreamEntry::Error:
4518 case llvm::BitstreamEntry::Record:
4519 case llvm::BitstreamEntry::EndBlock:
4520 Error("invalid record at top-level of AST file");
4523 case llvm::BitstreamEntry::SubBlock:
4528 case CONTROL_BLOCK_ID:
4529 HaveReadControlBlock = true;
4530 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4532 // Check that we didn't try to load a non-module AST file as a module.
4534 // FIXME: Should we also perform the converse check? Loading a module as
4535 // a PCH file sort of works, but it's a bit wonky.
4536 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4537 Type == MK_PrebuiltModule) &&
4538 F.ModuleName.empty()) {
4539 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4540 if (Result != OutOfDate ||
4541 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4542 Diag(diag::err_module_file_not_module) << FileName;
4547 case Failure: return Failure;
4548 case Missing: return Missing;
4549 case OutOfDate: return OutOfDate;
4550 case VersionMismatch: return VersionMismatch;
4551 case ConfigurationMismatch: return ConfigurationMismatch;
4552 case HadErrors: return HadErrors;
4557 if (!HaveReadControlBlock) {
4558 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4559 Diag(diag::err_pch_version_too_old);
4560 return VersionMismatch;
4563 // Record that we've loaded this module.
4564 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4565 ShouldFinalizePCM = true;
4568 case UNHASHED_CONTROL_BLOCK_ID:
4569 // This block is handled using look-ahead during ReadControlBlock. We
4570 // shouldn't get here!
4571 Error("malformed block record in AST file");
4575 if (llvm::Error Err = Stream.SkipBlock()) {
4576 Error(std::move(Err));
4583 llvm_unreachable("unexpected break; expected return");
4586 ASTReader::ASTReadResult
4587 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4588 unsigned ClientLoadCapabilities) {
4589 const HeaderSearchOptions &HSOpts =
4590 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4591 bool AllowCompatibleConfigurationMismatch =
4592 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4594 ASTReadResult Result = readUnhashedControlBlockImpl(
4595 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4597 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4599 // If F was directly imported by another module, it's implicitly validated by
4600 // the importing module.
4601 if (DisableValidation || WasImportedBy ||
4602 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4605 if (Result == Failure) {
4606 Error("malformed block record in AST file");
4610 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4611 // If this module has already been finalized in the ModuleCache, we're stuck
4612 // with it; we can only load a single version of each module.
4614 // This can happen when a module is imported in two contexts: in one, as a
4615 // user module; in another, as a system module (due to an import from
4616 // another module marked with the [system] flag). It usually indicates a
4617 // bug in the module map: this module should also be marked with [system].
4619 // If -Wno-system-headers (the default), and the first import is as a
4620 // system module, then validation will fail during the as-user import,
4621 // since -Werror flags won't have been validated. However, it's reasonable
4622 // to treat this consistently as a system module.
4624 // If -Wsystem-headers, the PCM on disk was built with
4625 // -Wno-system-headers, and the first import is as a user module, then
4626 // validation will fail during the as-system import since the PCM on disk
4627 // doesn't guarantee that -Werror was respected. However, the -Werror
4628 // flags were checked during the initial as-user import.
4629 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4630 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4638 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4639 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4640 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4641 bool ValidateDiagnosticOptions) {
4642 // Initialize a stream.
4643 BitstreamCursor Stream(StreamData);
4645 // Sniff for the signature.
4646 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4647 // FIXME this drops the error on the floor.
4648 consumeError(std::move(Err));
4652 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4653 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4656 // Read all of the records in the options block.
4658 ASTReadResult Result = Success;
4660 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4662 // FIXME this drops the error on the floor.
4663 consumeError(MaybeEntry.takeError());
4666 llvm::BitstreamEntry Entry = MaybeEntry.get();
4668 switch (Entry.Kind) {
4669 case llvm::BitstreamEntry::Error:
4670 case llvm::BitstreamEntry::SubBlock:
4673 case llvm::BitstreamEntry::EndBlock:
4676 case llvm::BitstreamEntry::Record:
4677 // The interesting case.
4681 // Read and process a record.
4683 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4684 if (!MaybeRecordType) {
4685 // FIXME this drops the error.
4688 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4691 std::copy(Record.begin(), Record.end(), F->Signature.data());
4693 case DIAGNOSTIC_OPTIONS: {
4694 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4695 if (Listener && ValidateDiagnosticOptions &&
4696 !AllowCompatibleConfigurationMismatch &&
4697 ParseDiagnosticOptions(Record, Complain, *Listener))
4698 Result = OutOfDate; // Don't return early. Read the signature.
4701 case DIAG_PRAGMA_MAPPINGS:
4704 if (F->PragmaDiagMappings.empty())
4705 F->PragmaDiagMappings.swap(Record);
4707 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4708 Record.begin(), Record.end());
4714 /// Parse a record and blob containing module file extension metadata.
4715 static bool parseModuleFileExtensionMetadata(
4716 const SmallVectorImpl<uint64_t> &Record,
4718 ModuleFileExtensionMetadata &Metadata) {
4719 if (Record.size() < 4) return true;
4721 Metadata.MajorVersion = Record[0];
4722 Metadata.MinorVersion = Record[1];
4724 unsigned BlockNameLen = Record[2];
4725 unsigned UserInfoLen = Record[3];
4727 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4729 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4730 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4731 Blob.data() + BlockNameLen + UserInfoLen);
4735 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4736 BitstreamCursor &Stream = F.Stream;
4740 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4742 Error(MaybeEntry.takeError());
4745 llvm::BitstreamEntry Entry = MaybeEntry.get();
4747 switch (Entry.Kind) {
4748 case llvm::BitstreamEntry::SubBlock:
4749 if (llvm::Error Err = Stream.SkipBlock()) {
4750 Error(std::move(Err));
4755 case llvm::BitstreamEntry::EndBlock:
4758 case llvm::BitstreamEntry::Error:
4761 case llvm::BitstreamEntry::Record:
4767 Expected<unsigned> MaybeRecCode =
4768 Stream.readRecord(Entry.ID, Record, &Blob);
4769 if (!MaybeRecCode) {
4770 Error(MaybeRecCode.takeError());
4773 switch (MaybeRecCode.get()) {
4774 case EXTENSION_METADATA: {
4775 ModuleFileExtensionMetadata Metadata;
4776 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4779 // Find a module file extension with this block name.
4780 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4781 if (Known == ModuleFileExtensions.end()) break;
4784 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4786 F.ExtensionReaders.push_back(std::move(Reader));
4797 void ASTReader::InitializeContext() {
4798 assert(ContextObj && "no context to initialize");
4799 ASTContext &Context = *ContextObj;
4801 // If there's a listener, notify them that we "read" the translation unit.
4802 if (DeserializationListener)
4803 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4804 Context.getTranslationUnitDecl());
4806 // FIXME: Find a better way to deal with collisions between these
4807 // built-in types. Right now, we just ignore the problem.
4809 // Load the special types.
4810 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4811 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4812 if (!Context.CFConstantStringTypeDecl)
4813 Context.setCFConstantStringType(GetType(String));
4816 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4817 QualType FileType = GetType(File);
4818 if (FileType.isNull()) {
4819 Error("FILE type is NULL");
4823 if (!Context.FILEDecl) {
4824 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4825 Context.setFILEDecl(Typedef->getDecl());
4827 const TagType *Tag = FileType->getAs<TagType>();
4829 Error("Invalid FILE type in AST file");
4832 Context.setFILEDecl(Tag->getDecl());
4837 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4838 QualType Jmp_bufType = GetType(Jmp_buf);
4839 if (Jmp_bufType.isNull()) {
4840 Error("jmp_buf type is NULL");
4844 if (!Context.jmp_bufDecl) {
4845 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4846 Context.setjmp_bufDecl(Typedef->getDecl());
4848 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4850 Error("Invalid jmp_buf type in AST file");
4853 Context.setjmp_bufDecl(Tag->getDecl());
4858 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4859 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4860 if (Sigjmp_bufType.isNull()) {
4861 Error("sigjmp_buf type is NULL");
4865 if (!Context.sigjmp_bufDecl) {
4866 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4867 Context.setsigjmp_bufDecl(Typedef->getDecl());
4869 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4870 assert(Tag && "Invalid sigjmp_buf type in AST file");
4871 Context.setsigjmp_bufDecl(Tag->getDecl());
4876 if (unsigned ObjCIdRedef
4877 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4878 if (Context.ObjCIdRedefinitionType.isNull())
4879 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4882 if (unsigned ObjCClassRedef
4883 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4884 if (Context.ObjCClassRedefinitionType.isNull())
4885 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4888 if (unsigned ObjCSelRedef
4889 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4890 if (Context.ObjCSelRedefinitionType.isNull())
4891 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4894 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4895 QualType Ucontext_tType = GetType(Ucontext_t);
4896 if (Ucontext_tType.isNull()) {
4897 Error("ucontext_t type is NULL");
4901 if (!Context.ucontext_tDecl) {
4902 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4903 Context.setucontext_tDecl(Typedef->getDecl());
4905 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4906 assert(Tag && "Invalid ucontext_t type in AST file");
4907 Context.setucontext_tDecl(Tag->getDecl());
4913 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4915 // If there were any CUDA special declarations, deserialize them.
4916 if (!CUDASpecialDeclRefs.empty()) {
4917 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4918 Context.setcudaConfigureCallDecl(
4919 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4922 // Re-export any modules that were imported by a non-module AST file.
4923 // FIXME: This does not make macro-only imports visible again.
4924 for (auto &Import : ImportedModules) {
4925 if (Module *Imported = getSubmodule(Import.ID)) {
4926 makeModuleVisible(Imported, Module::AllVisible,
4927 /*ImportLoc=*/Import.ImportLoc);
4928 if (Import.ImportLoc.isValid())
4929 PP.makeModuleVisible(Imported, Import.ImportLoc);
4930 // FIXME: should we tell Sema to make the module visible too?
4933 ImportedModules.clear();
4936 void ASTReader::finalizeForWriting() {
4937 // Nothing to do for now.
4940 /// Reads and return the signature record from \p PCH's control block, or
4942 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4943 BitstreamCursor Stream(PCH);
4944 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4945 // FIXME this drops the error on the floor.
4946 consumeError(std::move(Err));
4947 return ASTFileSignature();
4950 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4951 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4952 return ASTFileSignature();
4954 // Scan for SIGNATURE inside the diagnostic options block.
4955 ASTReader::RecordData Record;
4957 Expected<llvm::BitstreamEntry> MaybeEntry =
4958 Stream.advanceSkippingSubblocks();
4960 // FIXME this drops the error on the floor.
4961 consumeError(MaybeEntry.takeError());
4962 return ASTFileSignature();
4964 llvm::BitstreamEntry Entry = MaybeEntry.get();
4966 if (Entry.Kind != llvm::BitstreamEntry::Record)
4967 return ASTFileSignature();
4971 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4973 // FIXME this drops the error on the floor.
4974 consumeError(MaybeRecord.takeError());
4975 return ASTFileSignature();
4977 if (SIGNATURE == MaybeRecord.get())
4978 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4979 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4983 /// Retrieve the name of the original source file name
4984 /// directly from the AST file, without actually loading the AST
4986 std::string ASTReader::getOriginalSourceFile(
4987 const std::string &ASTFileName, FileManager &FileMgr,
4988 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4989 // Open the AST file.
4990 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4992 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4993 << ASTFileName << Buffer.getError().message();
4994 return std::string();
4997 // Initialize the stream
4998 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5000 // Sniff for the signature.
5001 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5002 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5003 return std::string();
5006 // Scan for the CONTROL_BLOCK_ID block.
5007 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5008 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5009 return std::string();
5012 // Scan for ORIGINAL_FILE inside the control block.
5015 Expected<llvm::BitstreamEntry> MaybeEntry =
5016 Stream.advanceSkippingSubblocks();
5018 // FIXME this drops errors on the floor.
5019 consumeError(MaybeEntry.takeError());
5020 return std::string();
5022 llvm::BitstreamEntry Entry = MaybeEntry.get();
5024 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5025 return std::string();
5027 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5028 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5029 return std::string();
5034 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5036 // FIXME this drops the errors on the floor.
5037 consumeError(MaybeRecord.takeError());
5038 return std::string();
5040 if (ORIGINAL_FILE == MaybeRecord.get())
5047 class SimplePCHValidator : public ASTReaderListener {
5048 const LangOptions &ExistingLangOpts;
5049 const TargetOptions &ExistingTargetOpts;
5050 const PreprocessorOptions &ExistingPPOpts;
5051 std::string ExistingModuleCachePath;
5052 FileManager &FileMgr;
5055 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5056 const TargetOptions &ExistingTargetOpts,
5057 const PreprocessorOptions &ExistingPPOpts,
5058 StringRef ExistingModuleCachePath,
5059 FileManager &FileMgr)
5060 : ExistingLangOpts(ExistingLangOpts),
5061 ExistingTargetOpts(ExistingTargetOpts),
5062 ExistingPPOpts(ExistingPPOpts),
5063 ExistingModuleCachePath(ExistingModuleCachePath),
5066 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5067 bool AllowCompatibleDifferences) override {
5068 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5069 AllowCompatibleDifferences);
5072 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5073 bool AllowCompatibleDifferences) override {
5074 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5075 AllowCompatibleDifferences);
5078 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5079 StringRef SpecificModuleCachePath,
5080 bool Complain) override {
5081 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5082 ExistingModuleCachePath,
5083 nullptr, ExistingLangOpts);
5086 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5088 std::string &SuggestedPredefines) override {
5089 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5090 SuggestedPredefines, ExistingLangOpts);
5096 bool ASTReader::readASTFileControlBlock(
5097 StringRef Filename, FileManager &FileMgr,
5098 const PCHContainerReader &PCHContainerRdr,
5099 bool FindModuleFileExtensions,
5100 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5101 // Open the AST file.
5102 // FIXME: This allows use of the VFS; we do not allow use of the
5103 // VFS when actually loading a module.
5104 auto Buffer = FileMgr.getBufferForFile(Filename);
5109 // Initialize the stream
5110 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5111 BitstreamCursor Stream(Bytes);
5113 // Sniff for the signature.
5114 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5115 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5119 // Scan for the CONTROL_BLOCK_ID block.
5120 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5123 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5124 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5125 bool NeedsImports = Listener.needsImportVisitation();
5126 BitstreamCursor InputFilesCursor;
5129 std::string ModuleDir;
5130 bool DoneWithControlBlock = false;
5131 while (!DoneWithControlBlock) {
5132 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5134 // FIXME this drops the error on the floor.
5135 consumeError(MaybeEntry.takeError());
5138 llvm::BitstreamEntry Entry = MaybeEntry.get();
5140 switch (Entry.Kind) {
5141 case llvm::BitstreamEntry::SubBlock: {
5143 case OPTIONS_BLOCK_ID: {
5144 std::string IgnoredSuggestedPredefines;
5145 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5146 /*AllowCompatibleConfigurationMismatch*/ false,
5147 Listener, IgnoredSuggestedPredefines) != Success)
5152 case INPUT_FILES_BLOCK_ID:
5153 InputFilesCursor = Stream;
5154 if (llvm::Error Err = Stream.SkipBlock()) {
5155 // FIXME this drops the error on the floor.
5156 consumeError(std::move(Err));
5159 if (NeedsInputFiles &&
5160 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5165 if (llvm::Error Err = Stream.SkipBlock()) {
5166 // FIXME this drops the error on the floor.
5167 consumeError(std::move(Err));
5176 case llvm::BitstreamEntry::EndBlock:
5177 DoneWithControlBlock = true;
5180 case llvm::BitstreamEntry::Error:
5183 case llvm::BitstreamEntry::Record:
5187 if (DoneWithControlBlock) break;
5191 Expected<unsigned> MaybeRecCode =
5192 Stream.readRecord(Entry.ID, Record, &Blob);
5193 if (!MaybeRecCode) {
5194 // FIXME this drops the error.
5197 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5199 if (Record[0] != VERSION_MAJOR)
5201 if (Listener.ReadFullVersionInformation(Blob))
5205 Listener.ReadModuleName(Blob);
5207 case MODULE_DIRECTORY:
5210 case MODULE_MAP_FILE: {
5212 auto Path = ReadString(Record, Idx);
5213 ResolveImportedPath(Path, ModuleDir);
5214 Listener.ReadModuleMapFile(Path);
5217 case INPUT_FILE_OFFSETS: {
5218 if (!NeedsInputFiles)
5221 unsigned NumInputFiles = Record[0];
5222 unsigned NumUserFiles = Record[1];
5223 const llvm::support::unaligned_uint64_t *InputFileOffs =
5224 (const llvm::support::unaligned_uint64_t *)Blob.data();
5225 for (unsigned I = 0; I != NumInputFiles; ++I) {
5226 // Go find this input file.
5227 bool isSystemFile = I >= NumUserFiles;
5229 if (isSystemFile && !NeedsSystemInputFiles)
5230 break; // the rest are system input files
5232 BitstreamCursor &Cursor = InputFilesCursor;
5233 SavedStreamPosition SavedPosition(Cursor);
5234 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5235 // FIXME this drops errors on the floor.
5236 consumeError(std::move(Err));
5239 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5241 // FIXME this drops errors on the floor.
5242 consumeError(MaybeCode.takeError());
5244 unsigned Code = MaybeCode.get();
5248 bool shouldContinue = false;
5249 Expected<unsigned> MaybeRecordType =
5250 Cursor.readRecord(Code, Record, &Blob);
5251 if (!MaybeRecordType) {
5252 // FIXME this drops errors on the floor.
5253 consumeError(MaybeRecordType.takeError());
5255 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5256 case INPUT_FILE_HASH:
5259 bool Overridden = static_cast<bool>(Record[3]);
5260 std::string Filename = Blob;
5261 ResolveImportedPath(Filename, ModuleDir);
5262 shouldContinue = Listener.visitInputFile(
5263 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5266 if (!shouldContinue)
5276 unsigned Idx = 0, N = Record.size();
5278 // Read information about the AST file.
5279 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5280 std::string ModuleName = ReadString(Record, Idx);
5281 std::string Filename = ReadString(Record, Idx);
5282 ResolveImportedPath(Filename, ModuleDir);
5283 Listener.visitImport(ModuleName, Filename);
5289 // No other validation to perform.
5294 // Look for module file extension blocks, if requested.
5295 if (FindModuleFileExtensions) {
5296 BitstreamCursor SavedStream = Stream;
5297 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5298 bool DoneWithExtensionBlock = false;
5299 while (!DoneWithExtensionBlock) {
5300 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5302 // FIXME this drops the error.
5305 llvm::BitstreamEntry Entry = MaybeEntry.get();
5307 switch (Entry.Kind) {
5308 case llvm::BitstreamEntry::SubBlock:
5309 if (llvm::Error Err = Stream.SkipBlock()) {
5310 // FIXME this drops the error on the floor.
5311 consumeError(std::move(Err));
5316 case llvm::BitstreamEntry::EndBlock:
5317 DoneWithExtensionBlock = true;
5320 case llvm::BitstreamEntry::Error:
5323 case llvm::BitstreamEntry::Record:
5329 Expected<unsigned> MaybeRecCode =
5330 Stream.readRecord(Entry.ID, Record, &Blob);
5331 if (!MaybeRecCode) {
5332 // FIXME this drops the error.
5335 switch (MaybeRecCode.get()) {
5336 case EXTENSION_METADATA: {
5337 ModuleFileExtensionMetadata Metadata;
5338 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5341 Listener.readModuleFileExtension(Metadata);
5347 Stream = SavedStream;
5350 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5351 if (readUnhashedControlBlockImpl(
5352 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5353 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5354 ValidateDiagnosticOptions) != Success)
5360 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5361 const PCHContainerReader &PCHContainerRdr,
5362 const LangOptions &LangOpts,
5363 const TargetOptions &TargetOpts,
5364 const PreprocessorOptions &PPOpts,
5365 StringRef ExistingModuleCachePath) {
5366 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5367 ExistingModuleCachePath, FileMgr);
5368 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5369 /*FindModuleFileExtensions=*/false,
5371 /*ValidateDiagnosticOptions=*/true);
5374 ASTReader::ASTReadResult
5375 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5376 // Enter the submodule block.
5377 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5378 Error(std::move(Err));
5382 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5384 Module *CurrentModule = nullptr;
5387 Expected<llvm::BitstreamEntry> MaybeEntry =
5388 F.Stream.advanceSkippingSubblocks();
5390 Error(MaybeEntry.takeError());
5393 llvm::BitstreamEntry Entry = MaybeEntry.get();
5395 switch (Entry.Kind) {
5396 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5397 case llvm::BitstreamEntry::Error:
5398 Error("malformed block record in AST file");
5400 case llvm::BitstreamEntry::EndBlock:
5402 case llvm::BitstreamEntry::Record:
5403 // The interesting case.
5410 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5412 Error(MaybeKind.takeError());
5415 unsigned Kind = MaybeKind.get();
5417 if ((Kind == SUBMODULE_METADATA) != First) {
5418 Error("submodule metadata record should be at beginning of block");
5423 // Submodule information is only valid if we have a current module.
5424 // FIXME: Should we error on these cases?
5425 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5426 Kind != SUBMODULE_DEFINITION)
5430 default: // Default behavior: ignore.
5433 case SUBMODULE_DEFINITION: {
5434 if (Record.size() < 12) {
5435 Error("malformed module definition");
5439 StringRef Name = Blob;
5441 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5442 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5443 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5444 bool IsFramework = Record[Idx++];
5445 bool IsExplicit = Record[Idx++];
5446 bool IsSystem = Record[Idx++];
5447 bool IsExternC = Record[Idx++];
5448 bool InferSubmodules = Record[Idx++];
5449 bool InferExplicitSubmodules = Record[Idx++];
5450 bool InferExportWildcard = Record[Idx++];
5451 bool ConfigMacrosExhaustive = Record[Idx++];
5452 bool ModuleMapIsPrivate = Record[Idx++];
5454 Module *ParentModule = nullptr;
5456 ParentModule = getSubmodule(Parent);
5458 // Retrieve this (sub)module from the module map, creating it if
5461 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5464 // FIXME: set the definition loc for CurrentModule, or call
5465 // ModMap.setInferredModuleAllowedBy()
5467 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5468 if (GlobalIndex >= SubmodulesLoaded.size() ||
5469 SubmodulesLoaded[GlobalIndex]) {
5470 Error("too many submodules");
5474 if (!ParentModule) {
5475 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5476 // Don't emit module relocation error if we have -fno-validate-pch
5477 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5478 CurFile != F.File) {
5479 if (!Diags.isDiagnosticInFlight()) {
5480 Diag(diag::err_module_file_conflict)
5481 << CurrentModule->getTopLevelModuleName()
5482 << CurFile->getName()
5483 << F.File->getName();
5489 CurrentModule->setASTFile(F.File);
5490 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5493 CurrentModule->Kind = Kind;
5494 CurrentModule->Signature = F.Signature;
5495 CurrentModule->IsFromModuleFile = true;
5496 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5497 CurrentModule->IsExternC = IsExternC;
5498 CurrentModule->InferSubmodules = InferSubmodules;
5499 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5500 CurrentModule->InferExportWildcard = InferExportWildcard;
5501 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5502 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5503 if (DeserializationListener)
5504 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5506 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5508 // Clear out data that will be replaced by what is in the module file.
5509 CurrentModule->LinkLibraries.clear();
5510 CurrentModule->ConfigMacros.clear();
5511 CurrentModule->UnresolvedConflicts.clear();
5512 CurrentModule->Conflicts.clear();
5514 // The module is available unless it's missing a requirement; relevant
5515 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5516 // Missing headers that were present when the module was built do not
5517 // make it unavailable -- if we got this far, this must be an explicitly
5518 // imported module file.
5519 CurrentModule->Requirements.clear();
5520 CurrentModule->MissingHeaders.clear();
5521 CurrentModule->IsMissingRequirement =
5522 ParentModule && ParentModule->IsMissingRequirement;
5523 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5527 case SUBMODULE_UMBRELLA_HEADER: {
5528 std::string Filename = Blob;
5529 ResolveImportedPath(F, Filename);
5530 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5531 if (!CurrentModule->getUmbrellaHeader())
5532 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5533 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5534 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5535 Error("mismatched umbrella headers in submodule");
5542 case SUBMODULE_HEADER:
5543 case SUBMODULE_EXCLUDED_HEADER:
5544 case SUBMODULE_PRIVATE_HEADER:
5545 // We lazily associate headers with their modules via the HeaderInfo table.
5546 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5547 // of complete filenames or remove it entirely.
5550 case SUBMODULE_TEXTUAL_HEADER:
5551 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5552 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5556 case SUBMODULE_TOPHEADER:
5557 CurrentModule->addTopHeaderFilename(Blob);
5560 case SUBMODULE_UMBRELLA_DIR: {
5561 std::string Dirname = Blob;
5562 ResolveImportedPath(F, Dirname);
5563 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5564 if (!CurrentModule->getUmbrellaDir())
5565 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5566 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5567 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5568 Error("mismatched umbrella directories in submodule");
5575 case SUBMODULE_METADATA: {
5576 F.BaseSubmoduleID = getTotalNumSubmodules();
5577 F.LocalNumSubmodules = Record[0];
5578 unsigned LocalBaseSubmoduleID = Record[1];
5579 if (F.LocalNumSubmodules > 0) {
5580 // Introduce the global -> local mapping for submodules within this
5582 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5584 // Introduce the local -> global mapping for submodules within this
5586 F.SubmoduleRemap.insertOrReplace(
5587 std::make_pair(LocalBaseSubmoduleID,
5588 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5590 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5595 case SUBMODULE_IMPORTS:
5596 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5597 UnresolvedModuleRef Unresolved;
5598 Unresolved.File = &F;
5599 Unresolved.Mod = CurrentModule;
5600 Unresolved.ID = Record[Idx];
5601 Unresolved.Kind = UnresolvedModuleRef::Import;
5602 Unresolved.IsWildcard = false;
5603 UnresolvedModuleRefs.push_back(Unresolved);
5607 case SUBMODULE_EXPORTS:
5608 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5609 UnresolvedModuleRef Unresolved;
5610 Unresolved.File = &F;
5611 Unresolved.Mod = CurrentModule;
5612 Unresolved.ID = Record[Idx];
5613 Unresolved.Kind = UnresolvedModuleRef::Export;
5614 Unresolved.IsWildcard = Record[Idx + 1];
5615 UnresolvedModuleRefs.push_back(Unresolved);
5618 // Once we've loaded the set of exports, there's no reason to keep
5619 // the parsed, unresolved exports around.
5620 CurrentModule->UnresolvedExports.clear();
5623 case SUBMODULE_REQUIRES:
5624 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5625 PP.getTargetInfo());
5628 case SUBMODULE_LINK_LIBRARY:
5629 ModMap.resolveLinkAsDependencies(CurrentModule);
5630 CurrentModule->LinkLibraries.push_back(
5631 Module::LinkLibrary(Blob, Record[0]));
5634 case SUBMODULE_CONFIG_MACRO:
5635 CurrentModule->ConfigMacros.push_back(Blob.str());
5638 case SUBMODULE_CONFLICT: {
5639 UnresolvedModuleRef Unresolved;
5640 Unresolved.File = &F;
5641 Unresolved.Mod = CurrentModule;
5642 Unresolved.ID = Record[0];
5643 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5644 Unresolved.IsWildcard = false;
5645 Unresolved.String = Blob;
5646 UnresolvedModuleRefs.push_back(Unresolved);
5650 case SUBMODULE_INITIALIZERS: {
5653 SmallVector<uint32_t, 16> Inits;
5654 for (auto &ID : Record)
5655 Inits.push_back(getGlobalDeclID(F, ID));
5656 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5660 case SUBMODULE_EXPORT_AS:
5661 CurrentModule->ExportAsModule = Blob.str();
5662 ModMap.addLinkAsDependency(CurrentModule);
5668 /// Parse the record that corresponds to a LangOptions data
5671 /// This routine parses the language options from the AST file and then gives
5672 /// them to the AST listener if one is set.
5674 /// \returns true if the listener deems the file unacceptable, false otherwise.
5675 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5677 ASTReaderListener &Listener,
5678 bool AllowCompatibleDifferences) {
5679 LangOptions LangOpts;
5681 #define LANGOPT(Name, Bits, Default, Description) \
5682 LangOpts.Name = Record[Idx++];
5683 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5684 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5685 #include "clang/Basic/LangOptions.def"
5686 #define SANITIZER(NAME, ID) \
5687 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5688 #include "clang/Basic/Sanitizers.def"
5690 for (unsigned N = Record[Idx++]; N; --N)
5691 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5693 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5694 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5695 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5697 LangOpts.CurrentModule = ReadString(Record, Idx);
5700 for (unsigned N = Record[Idx++]; N; --N) {
5701 LangOpts.CommentOpts.BlockCommandNames.push_back(
5702 ReadString(Record, Idx));
5704 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5706 // OpenMP offloading options.
5707 for (unsigned N = Record[Idx++]; N; --N) {
5708 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5711 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5713 return Listener.ReadLanguageOptions(LangOpts, Complain,
5714 AllowCompatibleDifferences);
5717 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5718 ASTReaderListener &Listener,
5719 bool AllowCompatibleDifferences) {
5721 TargetOptions TargetOpts;
5722 TargetOpts.Triple = ReadString(Record, Idx);
5723 TargetOpts.CPU = ReadString(Record, Idx);
5724 TargetOpts.ABI = ReadString(Record, Idx);
5725 for (unsigned N = Record[Idx++]; N; --N) {
5726 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5728 for (unsigned N = Record[Idx++]; N; --N) {
5729 TargetOpts.Features.push_back(ReadString(Record, Idx));
5732 return Listener.ReadTargetOptions(TargetOpts, Complain,
5733 AllowCompatibleDifferences);
5736 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5737 ASTReaderListener &Listener) {
5738 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5740 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5741 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5742 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5743 #include "clang/Basic/DiagnosticOptions.def"
5745 for (unsigned N = Record[Idx++]; N; --N)
5746 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5747 for (unsigned N = Record[Idx++]; N; --N)
5748 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5750 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5753 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5754 ASTReaderListener &Listener) {
5755 FileSystemOptions FSOpts;
5757 FSOpts.WorkingDir = ReadString(Record, Idx);
5758 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5761 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5763 ASTReaderListener &Listener) {
5764 HeaderSearchOptions HSOpts;
5766 HSOpts.Sysroot = ReadString(Record, Idx);
5769 for (unsigned N = Record[Idx++]; N; --N) {
5770 std::string Path = ReadString(Record, Idx);
5771 frontend::IncludeDirGroup Group
5772 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5773 bool IsFramework = Record[Idx++];
5774 bool IgnoreSysRoot = Record[Idx++];
5775 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5779 // System header prefixes.
5780 for (unsigned N = Record[Idx++]; N; --N) {
5781 std::string Prefix = ReadString(Record, Idx);
5782 bool IsSystemHeader = Record[Idx++];
5783 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5786 HSOpts.ResourceDir = ReadString(Record, Idx);
5787 HSOpts.ModuleCachePath = ReadString(Record, Idx);
5788 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5789 HSOpts.DisableModuleHash = Record[Idx++];
5790 HSOpts.ImplicitModuleMaps = Record[Idx++];
5791 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5792 HSOpts.UseBuiltinIncludes = Record[Idx++];
5793 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5794 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5795 HSOpts.UseLibcxx = Record[Idx++];
5796 std::string SpecificModuleCachePath = ReadString(Record, Idx);
5798 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5802 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5804 ASTReaderListener &Listener,
5805 std::string &SuggestedPredefines) {
5806 PreprocessorOptions PPOpts;
5809 // Macro definitions/undefs
5810 for (unsigned N = Record[Idx++]; N; --N) {
5811 std::string Macro = ReadString(Record, Idx);
5812 bool IsUndef = Record[Idx++];
5813 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5817 for (unsigned N = Record[Idx++]; N; --N) {
5818 PPOpts.Includes.push_back(ReadString(Record, Idx));
5822 for (unsigned N = Record[Idx++]; N; --N) {
5823 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5826 PPOpts.UsePredefines = Record[Idx++];
5827 PPOpts.DetailedRecord = Record[Idx++];
5828 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5829 PPOpts.ObjCXXARCStandardLibrary =
5830 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5831 SuggestedPredefines.clear();
5832 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5833 SuggestedPredefines);
5836 std::pair<ModuleFile *, unsigned>
5837 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5838 GlobalPreprocessedEntityMapType::iterator
5839 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5840 assert(I != GlobalPreprocessedEntityMap.end() &&
5841 "Corrupted global preprocessed entity map");
5842 ModuleFile *M = I->second;
5843 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5844 return std::make_pair(M, LocalIndex);
5847 llvm::iterator_range<PreprocessingRecord::iterator>
5848 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5849 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5850 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5851 Mod.NumPreprocessedEntities);
5853 return llvm::make_range(PreprocessingRecord::iterator(),
5854 PreprocessingRecord::iterator());
5857 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5858 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5859 return llvm::make_range(
5860 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5861 ModuleDeclIterator(this, &Mod,
5862 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5865 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5866 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5867 assert(I != GlobalSkippedRangeMap.end() &&
5868 "Corrupted global skipped range map");
5869 ModuleFile *M = I->second;
5870 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5871 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5872 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5873 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5874 TranslateSourceLocation(*M, RawRange.getEnd()));
5875 assert(Range.isValid());
5879 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5880 PreprocessedEntityID PPID = Index+1;
5881 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5882 ModuleFile &M = *PPInfo.first;
5883 unsigned LocalIndex = PPInfo.second;
5884 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5886 if (!PP.getPreprocessingRecord()) {
5887 Error("no preprocessing record");
5891 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5892 if (llvm::Error Err =
5893 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5894 Error(std::move(Err));
5898 Expected<llvm::BitstreamEntry> MaybeEntry =
5899 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5901 Error(MaybeEntry.takeError());
5904 llvm::BitstreamEntry Entry = MaybeEntry.get();
5906 if (Entry.Kind != llvm::BitstreamEntry::Record)
5910 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5911 TranslateSourceLocation(M, PPOffs.getEnd()));
5912 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5915 Expected<unsigned> MaybeRecType =
5916 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5917 if (!MaybeRecType) {
5918 Error(MaybeRecType.takeError());
5921 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5922 case PPD_MACRO_EXPANSION: {
5923 bool isBuiltin = Record[0];
5924 IdentifierInfo *Name = nullptr;
5925 MacroDefinitionRecord *Def = nullptr;
5927 Name = getLocalIdentifier(M, Record[1]);
5929 PreprocessedEntityID GlobalID =
5930 getGlobalPreprocessedEntityID(M, Record[1]);
5931 Def = cast<MacroDefinitionRecord>(
5932 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5937 ME = new (PPRec) MacroExpansion(Name, Range);
5939 ME = new (PPRec) MacroExpansion(Def, Range);
5944 case PPD_MACRO_DEFINITION: {
5945 // Decode the identifier info and then check again; if the macro is
5946 // still defined and associated with the identifier,
5947 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5948 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5950 if (DeserializationListener)
5951 DeserializationListener->MacroDefinitionRead(PPID, MD);
5956 case PPD_INCLUSION_DIRECTIVE: {
5957 const char *FullFileNameStart = Blob.data() + Record[0];
5958 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5959 const FileEntry *File = nullptr;
5960 if (!FullFileName.empty())
5961 if (auto FE = PP.getFileManager().getFile(FullFileName))
5964 // FIXME: Stable encoding
5965 InclusionDirective::InclusionKind Kind
5966 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5967 InclusionDirective *ID
5968 = new (PPRec) InclusionDirective(PPRec, Kind,
5969 StringRef(Blob.data(), Record[0]),
5970 Record[1], Record[3],
5977 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5980 /// Find the next module that contains entities and return the ID
5981 /// of the first entry.
5983 /// \param SLocMapI points at a chunk of a module that contains no
5984 /// preprocessed entities or the entities it contains are not the ones we are
5986 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5987 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5989 for (GlobalSLocOffsetMapType::const_iterator
5990 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5991 ModuleFile &M = *SLocMapI->second;
5992 if (M.NumPreprocessedEntities)
5993 return M.BasePreprocessedEntityID;
5996 return getTotalNumPreprocessedEntities();
6001 struct PPEntityComp {
6002 const ASTReader &Reader;
6005 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6007 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6008 SourceLocation LHS = getLoc(L);
6009 SourceLocation RHS = getLoc(R);
6010 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6013 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6014 SourceLocation LHS = getLoc(L);
6015 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6018 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6019 SourceLocation RHS = getLoc(R);
6020 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6023 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6024 return Reader.TranslateSourceLocation(M, PPE.getBegin());
6030 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6031 bool EndsAfter) const {
6032 if (SourceMgr.isLocalSourceLocation(Loc))
6033 return getTotalNumPreprocessedEntities();
6035 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6036 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6037 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6038 "Corrupted global sloc offset map");
6040 if (SLocMapI->second->NumPreprocessedEntities == 0)
6041 return findNextPreprocessedEntity(SLocMapI);
6043 ModuleFile &M = *SLocMapI->second;
6045 using pp_iterator = const PPEntityOffset *;
6047 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6048 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6050 size_t Count = M.NumPreprocessedEntities;
6052 pp_iterator First = pp_begin;
6056 PPI = std::upper_bound(pp_begin, pp_end, Loc,
6057 PPEntityComp(*this, M));
6059 // Do a binary search manually instead of using std::lower_bound because
6060 // The end locations of entities may be unordered (when a macro expansion
6061 // is inside another macro argument), but for this case it is not important
6062 // whether we get the first macro expansion or its containing macro.
6066 std::advance(PPI, Half);
6067 if (SourceMgr.isBeforeInTranslationUnit(
6068 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6071 Count = Count - Half - 1;
6078 return findNextPreprocessedEntity(SLocMapI);
6080 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6083 /// Returns a pair of [Begin, End) indices of preallocated
6084 /// preprocessed entities that \arg Range encompasses.
6085 std::pair<unsigned, unsigned>
6086 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6087 if (Range.isInvalid())
6088 return std::make_pair(0,0);
6089 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6091 PreprocessedEntityID BeginID =
6092 findPreprocessedEntity(Range.getBegin(), false);
6093 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6094 return std::make_pair(BeginID, EndID);
6097 /// Optionally returns true or false if the preallocated preprocessed
6098 /// entity with index \arg Index came from file \arg FID.
6099 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6101 if (FID.isInvalid())
6104 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6105 ModuleFile &M = *PPInfo.first;
6106 unsigned LocalIndex = PPInfo.second;
6107 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6109 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6110 if (Loc.isInvalid())
6113 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6121 /// Visitor used to search for information about a header file.
6122 class HeaderFileInfoVisitor {
6123 const FileEntry *FE;
6124 Optional<HeaderFileInfo> HFI;
6127 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6129 bool operator()(ModuleFile &M) {
6130 HeaderFileInfoLookupTable *Table
6131 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6135 // Look in the on-disk hash table for an entry for this file name.
6136 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6137 if (Pos == Table->end())
6144 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6149 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6150 HeaderFileInfoVisitor Visitor(FE);
6151 ModuleMgr.visit(Visitor);
6152 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6155 return HeaderFileInfo();
6158 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6159 using DiagState = DiagnosticsEngine::DiagState;
6160 SmallVector<DiagState *, 32> DiagStates;
6162 for (ModuleFile &F : ModuleMgr) {
6164 auto &Record = F.PragmaDiagMappings;
6170 auto ReadDiagState =
6171 [&](const DiagState &BasedOn, SourceLocation Loc,
6172 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6173 unsigned BackrefID = Record[Idx++];
6175 return DiagStates[BackrefID - 1];
6177 // A new DiagState was created here.
6178 Diag.DiagStates.push_back(BasedOn);
6179 DiagState *NewState = &Diag.DiagStates.back();
6180 DiagStates.push_back(NewState);
6181 unsigned Size = Record[Idx++];
6182 assert(Idx + Size * 2 <= Record.size() &&
6183 "Invalid data, not enough diag/map pairs");
6185 unsigned DiagID = Record[Idx++];
6186 DiagnosticMapping NewMapping =
6187 DiagnosticMapping::deserialize(Record[Idx++]);
6188 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6191 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6193 // If this mapping was specified as a warning but the severity was
6194 // upgraded due to diagnostic settings, simulate the current diagnostic
6195 // settings (and use a warning).
6196 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6197 NewMapping.setSeverity(diag::Severity::Warning);
6198 NewMapping.setUpgradedFromWarning(false);
6201 Mapping = NewMapping;
6206 // Read the first state.
6207 DiagState *FirstState;
6208 if (F.Kind == MK_ImplicitModule) {
6209 // Implicitly-built modules are reused with different diagnostic
6210 // settings. Use the initial diagnostic state from Diag to simulate this
6211 // compilation's diagnostic settings.
6212 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6213 DiagStates.push_back(FirstState);
6215 // Skip the initial diagnostic state from the serialized module.
6216 assert(Record[1] == 0 &&
6217 "Invalid data, unexpected backref in initial state");
6218 Idx = 3 + Record[2] * 2;
6219 assert(Idx < Record.size() &&
6220 "Invalid data, not enough state change pairs in initial state");
6221 } else if (F.isModule()) {
6222 // For an explicit module, preserve the flags from the module build
6223 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6225 unsigned Flags = Record[Idx++];
6227 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6228 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6229 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6230 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6231 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6232 Initial.ExtBehavior = (diag::Severity)Flags;
6233 FirstState = ReadDiagState(Initial, SourceLocation(), true);
6235 assert(F.OriginalSourceFileID.isValid());
6237 // Set up the root buffer of the module to start with the initial
6238 // diagnostic state of the module itself, to cover files that contain no
6239 // explicit transitions (for which we did not serialize anything).
6240 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6241 .StateTransitions.push_back({FirstState, 0});
6243 // For prefix ASTs, start with whatever the user configured on the
6245 Idx++; // Skip flags.
6246 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6247 SourceLocation(), false);
6250 // Read the state transitions.
6251 unsigned NumLocations = Record[Idx++];
6252 while (NumLocations--) {
6253 assert(Idx < Record.size() &&
6254 "Invalid data, missing pragma diagnostic states");
6255 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6256 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6257 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6258 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6259 unsigned Transitions = Record[Idx++];
6261 // Note that we don't need to set up Parent/ParentOffset here, because
6262 // we won't be changing the diagnostic state within imported FileIDs
6263 // (other than perhaps appending to the main source file, which has no
6265 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6266 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6267 for (unsigned I = 0; I != Transitions; ++I) {
6268 unsigned Offset = Record[Idx++];
6270 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6271 F.StateTransitions.push_back({State, Offset});
6275 // Read the final state.
6276 assert(Idx < Record.size() &&
6277 "Invalid data, missing final pragma diagnostic state");
6278 SourceLocation CurStateLoc =
6279 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6280 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6282 if (!F.isModule()) {
6283 Diag.DiagStatesByLoc.CurDiagState = CurState;
6284 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6286 // Preserve the property that the imaginary root file describes the
6289 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6291 T.push_back({CurState, 0});
6293 T[0].State = CurState;
6296 // Don't try to read these mappings again.
6301 /// Get the correct cursor and offset for loading a type.
6302 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6303 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6304 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6305 ModuleFile *M = I->second;
6306 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6309 /// Read and return the type with the given index..
6311 /// The index is the type ID, shifted and minus the number of predefs. This
6312 /// routine actually reads the record corresponding to the type at the given
6313 /// location. It is a helper routine for GetType, which deals with reading type
6315 QualType ASTReader::readTypeRecord(unsigned Index) {
6316 assert(ContextObj && "reading type with no AST context");
6317 ASTContext &Context = *ContextObj;
6318 RecordLocation Loc = TypeCursorForIndex(Index);
6319 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6321 // Keep track of where we are in the stream, then jump back there
6322 // after reading this type.
6323 SavedStreamPosition SavedPosition(DeclsCursor);
6325 ReadingKindTracker ReadingKind(Read_Type, *this);
6327 // Note that we are loading a type record.
6328 Deserializing AType(this);
6331 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6332 Error(std::move(Err));
6336 Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
6338 Error(MaybeCode.takeError());
6341 unsigned Code = MaybeCode.get();
6343 Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
6344 if (!MaybeTypeCode) {
6345 Error(MaybeTypeCode.takeError());
6348 switch ((TypeCode)MaybeTypeCode.get()) {
6349 case TYPE_EXT_QUAL: {
6350 if (Record.size() != 2) {
6351 Error("Incorrect encoding of extended qualifier type");
6354 QualType Base = readType(*Loc.F, Record, Idx);
6355 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6356 return Context.getQualifiedType(Base, Quals);
6359 case TYPE_COMPLEX: {
6360 if (Record.size() != 1) {
6361 Error("Incorrect encoding of complex type");
6364 QualType ElemType = readType(*Loc.F, Record, Idx);
6365 return Context.getComplexType(ElemType);
6368 case TYPE_POINTER: {
6369 if (Record.size() != 1) {
6370 Error("Incorrect encoding of pointer type");
6373 QualType PointeeType = readType(*Loc.F, Record, Idx);
6374 return Context.getPointerType(PointeeType);
6377 case TYPE_DECAYED: {
6378 if (Record.size() != 1) {
6379 Error("Incorrect encoding of decayed type");
6382 QualType OriginalType = readType(*Loc.F, Record, Idx);
6383 QualType DT = Context.getAdjustedParameterType(OriginalType);
6384 if (!isa<DecayedType>(DT))
6385 Error("Decayed type does not decay");
6389 case TYPE_ADJUSTED: {
6390 if (Record.size() != 2) {
6391 Error("Incorrect encoding of adjusted type");
6394 QualType OriginalTy = readType(*Loc.F, Record, Idx);
6395 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
6396 return Context.getAdjustedType(OriginalTy, AdjustedTy);
6399 case TYPE_BLOCK_POINTER: {
6400 if (Record.size() != 1) {
6401 Error("Incorrect encoding of block pointer type");
6404 QualType PointeeType = readType(*Loc.F, Record, Idx);
6405 return Context.getBlockPointerType(PointeeType);
6408 case TYPE_LVALUE_REFERENCE: {
6409 if (Record.size() != 2) {
6410 Error("Incorrect encoding of lvalue reference type");
6413 QualType PointeeType = readType(*Loc.F, Record, Idx);
6414 return Context.getLValueReferenceType(PointeeType, Record[1]);
6417 case TYPE_RVALUE_REFERENCE: {
6418 if (Record.size() != 1) {
6419 Error("Incorrect encoding of rvalue reference type");
6422 QualType PointeeType = readType(*Loc.F, Record, Idx);
6423 return Context.getRValueReferenceType(PointeeType);
6426 case TYPE_MEMBER_POINTER: {
6427 if (Record.size() != 2) {
6428 Error("Incorrect encoding of member pointer type");
6431 QualType PointeeType = readType(*Loc.F, Record, Idx);
6432 QualType ClassType = readType(*Loc.F, Record, Idx);
6433 if (PointeeType.isNull() || ClassType.isNull())
6436 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
6439 case TYPE_CONSTANT_ARRAY: {
6440 QualType ElementType = readType(*Loc.F, Record, Idx);
6441 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6442 unsigned IndexTypeQuals = Record[2];
6444 llvm::APInt Size = ReadAPInt(Record, Idx);
6445 Expr *SizeExpr = ReadExpr(*Loc.F);
6446 return Context.getConstantArrayType(ElementType, Size, SizeExpr,
6447 ASM, IndexTypeQuals);
6450 case TYPE_INCOMPLETE_ARRAY: {
6451 QualType ElementType = readType(*Loc.F, Record, Idx);
6452 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6453 unsigned IndexTypeQuals = Record[2];
6454 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6457 case TYPE_VARIABLE_ARRAY: {
6458 QualType ElementType = readType(*Loc.F, Record, Idx);
6459 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6460 unsigned IndexTypeQuals = Record[2];
6461 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6462 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6463 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6464 ASM, IndexTypeQuals,
6465 SourceRange(LBLoc, RBLoc));
6469 if (Record.size() != 3) {
6470 Error("incorrect encoding of vector type in AST file");
6474 QualType ElementType = readType(*Loc.F, Record, Idx);
6475 unsigned NumElements = Record[1];
6476 unsigned VecKind = Record[2];
6477 return Context.getVectorType(ElementType, NumElements,
6478 (VectorType::VectorKind)VecKind);
6481 case TYPE_EXT_VECTOR: {
6482 if (Record.size() != 3) {
6483 Error("incorrect encoding of extended vector type in AST file");
6487 QualType ElementType = readType(*Loc.F, Record, Idx);
6488 unsigned NumElements = Record[1];
6489 return Context.getExtVectorType(ElementType, NumElements);
6492 case TYPE_FUNCTION_NO_PROTO: {
6493 if (Record.size() != 8) {
6494 Error("incorrect encoding of no-proto function type");
6497 QualType ResultType = readType(*Loc.F, Record, Idx);
6498 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6499 (CallingConv)Record[4], Record[5], Record[6],
6501 return Context.getFunctionNoProtoType(ResultType, Info);
6504 case TYPE_FUNCTION_PROTO: {
6505 QualType ResultType = readType(*Loc.F, Record, Idx);
6507 FunctionProtoType::ExtProtoInfo EPI;
6508 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6509 /*hasregparm*/ Record[2],
6510 /*regparm*/ Record[3],
6511 static_cast<CallingConv>(Record[4]),
6512 /*produces*/ Record[5],
6513 /*nocallersavedregs*/ Record[6],
6514 /*nocfcheck*/ Record[7]);
6518 EPI.Variadic = Record[Idx++];
6519 EPI.HasTrailingReturn = Record[Idx++];
6520 EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6521 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6522 SmallVector<QualType, 8> ExceptionStorage;
6523 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6525 unsigned NumParams = Record[Idx++];
6526 SmallVector<QualType, 16> ParamTypes;
6527 for (unsigned I = 0; I != NumParams; ++I)
6528 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6530 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6531 if (Idx != Record.size()) {
6532 for (unsigned I = 0; I != NumParams; ++I)
6533 ExtParameterInfos.push_back(
6534 FunctionProtoType::ExtParameterInfo
6535 ::getFromOpaqueValue(Record[Idx++]));
6536 EPI.ExtParameterInfos = ExtParameterInfos.data();
6539 assert(Idx == Record.size());
6541 return Context.getFunctionType(ResultType, ParamTypes, EPI);
6544 case TYPE_UNRESOLVED_USING: {
6546 return Context.getTypeDeclType(
6547 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6550 case TYPE_TYPEDEF: {
6551 if (Record.size() != 2) {
6552 Error("incorrect encoding of typedef type");
6556 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6557 QualType Canonical = readType(*Loc.F, Record, Idx);
6558 if (!Canonical.isNull())
6559 Canonical = Context.getCanonicalType(Canonical);
6560 return Context.getTypedefType(Decl, Canonical);
6563 case TYPE_TYPEOF_EXPR:
6564 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6567 if (Record.size() != 1) {
6568 Error("incorrect encoding of typeof(type) in AST file");
6571 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6572 return Context.getTypeOfType(UnderlyingType);
6575 case TYPE_DECLTYPE: {
6576 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6577 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6580 case TYPE_UNARY_TRANSFORM: {
6581 QualType BaseType = readType(*Loc.F, Record, Idx);
6582 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6583 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6584 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6588 QualType Deduced = readType(*Loc.F, Record, Idx);
6589 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6590 bool IsDependent = false, IsPack = false;
6591 if (Deduced.isNull()) {
6592 IsDependent = Record[Idx] > 0;
6593 IsPack = Record[Idx] > 1;
6596 return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
6599 case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6600 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6601 QualType Deduced = readType(*Loc.F, Record, Idx);
6602 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6603 return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6608 if (Record.size() != 2) {
6609 Error("incorrect encoding of record type");
6613 bool IsDependent = Record[Idx++];
6614 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6615 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6616 QualType T = Context.getRecordType(RD);
6617 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6622 if (Record.size() != 2) {
6623 Error("incorrect encoding of enum type");
6627 bool IsDependent = Record[Idx++];
6629 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6630 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6634 case TYPE_ATTRIBUTED: {
6635 if (Record.size() != 3) {
6636 Error("incorrect encoding of attributed type");
6639 QualType modifiedType = readType(*Loc.F, Record, Idx);
6640 QualType equivalentType = readType(*Loc.F, Record, Idx);
6641 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6642 return Context.getAttributedType(kind, modifiedType, equivalentType);
6646 if (Record.size() != 1) {
6647 Error("incorrect encoding of paren type");
6650 QualType InnerType = readType(*Loc.F, Record, Idx);
6651 return Context.getParenType(InnerType);
6654 case TYPE_MACRO_QUALIFIED: {
6655 if (Record.size() != 2) {
6656 Error("incorrect encoding of macro defined type");
6659 QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6660 IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6661 return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6664 case TYPE_PACK_EXPANSION: {
6665 if (Record.size() != 2) {
6666 Error("incorrect encoding of pack expansion type");
6669 QualType Pattern = readType(*Loc.F, Record, Idx);
6670 if (Pattern.isNull())
6672 Optional<unsigned> NumExpansions;
6674 NumExpansions = Record[1] - 1;
6675 return Context.getPackExpansionType(Pattern, NumExpansions);
6678 case TYPE_ELABORATED: {
6680 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6681 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6682 QualType NamedType = readType(*Loc.F, Record, Idx);
6683 TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6684 return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6687 case TYPE_OBJC_INTERFACE: {
6689 ObjCInterfaceDecl *ItfD
6690 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6691 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6694 case TYPE_OBJC_TYPE_PARAM: {
6696 ObjCTypeParamDecl *Decl
6697 = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6698 unsigned NumProtos = Record[Idx++];
6699 SmallVector<ObjCProtocolDecl*, 4> Protos;
6700 for (unsigned I = 0; I != NumProtos; ++I)
6701 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6702 return Context.getObjCTypeParamType(Decl, Protos);
6705 case TYPE_OBJC_OBJECT: {
6707 QualType Base = readType(*Loc.F, Record, Idx);
6708 unsigned NumTypeArgs = Record[Idx++];
6709 SmallVector<QualType, 4> TypeArgs;
6710 for (unsigned I = 0; I != NumTypeArgs; ++I)
6711 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6712 unsigned NumProtos = Record[Idx++];
6713 SmallVector<ObjCProtocolDecl*, 4> Protos;
6714 for (unsigned I = 0; I != NumProtos; ++I)
6715 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6716 bool IsKindOf = Record[Idx++];
6717 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6720 case TYPE_OBJC_OBJECT_POINTER: {
6722 QualType Pointee = readType(*Loc.F, Record, Idx);
6723 return Context.getObjCObjectPointerType(Pointee);
6726 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6728 QualType Parm = readType(*Loc.F, Record, Idx);
6729 QualType Replacement = readType(*Loc.F, Record, Idx);
6730 return Context.getSubstTemplateTypeParmType(
6731 cast<TemplateTypeParmType>(Parm),
6732 Context.getCanonicalType(Replacement));
6735 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6737 QualType Parm = readType(*Loc.F, Record, Idx);
6738 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6739 return Context.getSubstTemplateTypeParmPackType(
6740 cast<TemplateTypeParmType>(Parm),
6744 case TYPE_INJECTED_CLASS_NAME: {
6745 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6746 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6747 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6748 // for AST reading, too much interdependencies.
6749 const Type *T = nullptr;
6750 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6751 if (const Type *Existing = DI->getTypeForDecl()) {
6757 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6758 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6759 DI->setTypeForDecl(T);
6761 return QualType(T, 0);
6764 case TYPE_TEMPLATE_TYPE_PARM: {
6766 unsigned Depth = Record[Idx++];
6767 unsigned Index = Record[Idx++];
6768 bool Pack = Record[Idx++];
6769 TemplateTypeParmDecl *D
6770 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6771 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6774 case TYPE_DEPENDENT_NAME: {
6776 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6777 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6778 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6779 QualType Canon = readType(*Loc.F, Record, Idx);
6780 if (!Canon.isNull())
6781 Canon = Context.getCanonicalType(Canon);
6782 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6785 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6787 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6788 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6789 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6790 unsigned NumArgs = Record[Idx++];
6791 SmallVector<TemplateArgument, 8> Args;
6792 Args.reserve(NumArgs);
6794 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6795 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6799 case TYPE_DEPENDENT_SIZED_ARRAY: {
6803 QualType ElementType = readType(*Loc.F, Record, Idx);
6804 ArrayType::ArraySizeModifier ASM
6805 = (ArrayType::ArraySizeModifier)Record[Idx++];
6806 unsigned IndexTypeQuals = Record[Idx++];
6808 // DependentSizedArrayType
6809 Expr *NumElts = ReadExpr(*Loc.F);
6810 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6812 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6813 IndexTypeQuals, Brackets);
6816 case TYPE_TEMPLATE_SPECIALIZATION: {
6818 bool IsDependent = Record[Idx++];
6819 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6820 SmallVector<TemplateArgument, 8> Args;
6821 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6822 QualType Underlying = readType(*Loc.F, Record, Idx);
6824 if (Underlying.isNull())
6825 T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6827 T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6828 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6833 if (Record.size() != 1) {
6834 Error("Incorrect encoding of atomic type");
6837 QualType ValueType = readType(*Loc.F, Record, Idx);
6838 return Context.getAtomicType(ValueType);
6842 if (Record.size() != 2) {
6843 Error("Incorrect encoding of pipe type");
6847 // Reading the pipe element type.
6848 QualType ElementType = readType(*Loc.F, Record, Idx);
6849 unsigned ReadOnly = Record[1];
6850 return Context.getPipeType(ElementType, ReadOnly);
6853 case TYPE_DEPENDENT_SIZED_VECTOR: {
6855 QualType ElementType = readType(*Loc.F, Record, Idx);
6856 Expr *SizeExpr = ReadExpr(*Loc.F);
6857 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6858 unsigned VecKind = Record[Idx];
6860 return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6861 (VectorType::VectorKind)VecKind);
6864 case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6867 // DependentSizedExtVectorType
6868 QualType ElementType = readType(*Loc.F, Record, Idx);
6869 Expr *SizeExpr = ReadExpr(*Loc.F);
6870 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6872 return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6876 case TYPE_DEPENDENT_ADDRESS_SPACE: {
6879 // DependentAddressSpaceType
6880 QualType PointeeType = readType(*Loc.F, Record, Idx);
6881 Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6882 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6884 return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6888 llvm_unreachable("Invalid TypeCode!");
6891 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6892 SmallVectorImpl<QualType> &Exceptions,
6893 FunctionProtoType::ExceptionSpecInfo &ESI,
6894 const RecordData &Record, unsigned &Idx) {
6895 ExceptionSpecificationType EST =
6896 static_cast<ExceptionSpecificationType>(Record[Idx++]);
6898 if (EST == EST_Dynamic) {
6899 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6900 Exceptions.push_back(readType(ModuleFile, Record, Idx));
6901 ESI.Exceptions = Exceptions;
6902 } else if (isComputedNoexcept(EST)) {
6903 ESI.NoexceptExpr = ReadExpr(ModuleFile);
6904 } else if (EST == EST_Uninstantiated) {
6905 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6906 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6907 } else if (EST == EST_Unevaluated) {
6908 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6914 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6917 const ASTReader::RecordData &Record;
6920 SourceLocation ReadSourceLocation() {
6921 return Reader->ReadSourceLocation(*F, Record, Idx);
6924 TypeSourceInfo *GetTypeSourceInfo() {
6925 return Reader->GetTypeSourceInfo(*F, Record, Idx);
6928 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6929 return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6933 return Reader->ReadAttr(*F, Record, Idx);
6937 TypeLocReader(ModuleFile &F, ASTReader &Reader,
6938 const ASTReader::RecordData &Record, unsigned &Idx)
6939 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6941 // We want compile-time assurance that we've enumerated all of
6942 // these, so unfortunately we have to declare them first, then
6943 // define them out-of-line.
6944 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6945 #define TYPELOC(CLASS, PARENT) \
6946 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6947 #include "clang/AST/TypeLocNodes.def"
6949 void VisitFunctionTypeLoc(FunctionTypeLoc);
6950 void VisitArrayTypeLoc(ArrayTypeLoc);
6953 } // namespace clang
6955 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6959 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6960 TL.setBuiltinLoc(ReadSourceLocation());
6961 if (TL.needsExtraLocalData()) {
6962 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6963 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6964 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6965 TL.setModeAttr(Record[Idx++]);
6969 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6970 TL.setNameLoc(ReadSourceLocation());
6973 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6974 TL.setStarLoc(ReadSourceLocation());
6977 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6981 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6985 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6986 TL.setExpansionLoc(ReadSourceLocation());
6989 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6990 TL.setCaretLoc(ReadSourceLocation());
6993 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6994 TL.setAmpLoc(ReadSourceLocation());
6997 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6998 TL.setAmpAmpLoc(ReadSourceLocation());
7001 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7002 TL.setStarLoc(ReadSourceLocation());
7003 TL.setClassTInfo(GetTypeSourceInfo());
7006 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7007 TL.setLBracketLoc(ReadSourceLocation());
7008 TL.setRBracketLoc(ReadSourceLocation());
7010 TL.setSizeExpr(Reader->ReadExpr(*F));
7012 TL.setSizeExpr(nullptr);
7015 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7016 VisitArrayTypeLoc(TL);
7019 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7020 VisitArrayTypeLoc(TL);
7023 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7024 VisitArrayTypeLoc(TL);
7027 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7028 DependentSizedArrayTypeLoc TL) {
7029 VisitArrayTypeLoc(TL);
7032 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7033 DependentAddressSpaceTypeLoc TL) {
7035 TL.setAttrNameLoc(ReadSourceLocation());
7037 range.setBegin(ReadSourceLocation());
7038 range.setEnd(ReadSourceLocation());
7039 TL.setAttrOperandParensRange(range);
7040 TL.setAttrExprOperand(Reader->ReadExpr(*F));
7043 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7044 DependentSizedExtVectorTypeLoc TL) {
7045 TL.setNameLoc(ReadSourceLocation());
7048 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7049 TL.setNameLoc(ReadSourceLocation());
7052 void TypeLocReader::VisitDependentVectorTypeLoc(
7053 DependentVectorTypeLoc TL) {
7054 TL.setNameLoc(ReadSourceLocation());
7057 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7058 TL.setNameLoc(ReadSourceLocation());
7061 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7062 TL.setLocalRangeBegin(ReadSourceLocation());
7063 TL.setLParenLoc(ReadSourceLocation());
7064 TL.setRParenLoc(ReadSourceLocation());
7065 TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
7066 Reader->ReadSourceLocation(*F, Record, Idx)));
7067 TL.setLocalRangeEnd(ReadSourceLocation());
7068 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7069 TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
7073 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7074 VisitFunctionTypeLoc(TL);
7077 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7078 VisitFunctionTypeLoc(TL);
7081 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7082 TL.setNameLoc(ReadSourceLocation());
7085 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7086 TL.setNameLoc(ReadSourceLocation());
7089 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7090 TL.setTypeofLoc(ReadSourceLocation());
7091 TL.setLParenLoc(ReadSourceLocation());
7092 TL.setRParenLoc(ReadSourceLocation());
7095 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7096 TL.setTypeofLoc(ReadSourceLocation());
7097 TL.setLParenLoc(ReadSourceLocation());
7098 TL.setRParenLoc(ReadSourceLocation());
7099 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7102 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7103 TL.setNameLoc(ReadSourceLocation());
7106 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7107 TL.setKWLoc(ReadSourceLocation());
7108 TL.setLParenLoc(ReadSourceLocation());
7109 TL.setRParenLoc(ReadSourceLocation());
7110 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7113 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7114 TL.setNameLoc(ReadSourceLocation());
7117 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7118 DeducedTemplateSpecializationTypeLoc TL) {
7119 TL.setTemplateNameLoc(ReadSourceLocation());
7122 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7123 TL.setNameLoc(ReadSourceLocation());
7126 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7127 TL.setNameLoc(ReadSourceLocation());
7130 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7131 TL.setAttr(ReadAttr());
7134 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7135 TL.setNameLoc(ReadSourceLocation());
7138 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7139 SubstTemplateTypeParmTypeLoc TL) {
7140 TL.setNameLoc(ReadSourceLocation());
7143 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7144 SubstTemplateTypeParmPackTypeLoc TL) {
7145 TL.setNameLoc(ReadSourceLocation());
7148 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7149 TemplateSpecializationTypeLoc TL) {
7150 TL.setTemplateKeywordLoc(ReadSourceLocation());
7151 TL.setTemplateNameLoc(ReadSourceLocation());
7152 TL.setLAngleLoc(ReadSourceLocation());
7153 TL.setRAngleLoc(ReadSourceLocation());
7154 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7157 Reader->GetTemplateArgumentLocInfo(
7158 *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
7161 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7162 TL.setLParenLoc(ReadSourceLocation());
7163 TL.setRParenLoc(ReadSourceLocation());
7166 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7167 TL.setElaboratedKeywordLoc(ReadSourceLocation());
7168 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7171 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7172 TL.setNameLoc(ReadSourceLocation());
7175 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7176 TL.setElaboratedKeywordLoc(ReadSourceLocation());
7177 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7178 TL.setNameLoc(ReadSourceLocation());
7181 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7182 DependentTemplateSpecializationTypeLoc TL) {
7183 TL.setElaboratedKeywordLoc(ReadSourceLocation());
7184 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7185 TL.setTemplateKeywordLoc(ReadSourceLocation());
7186 TL.setTemplateNameLoc(ReadSourceLocation());
7187 TL.setLAngleLoc(ReadSourceLocation());
7188 TL.setRAngleLoc(ReadSourceLocation());
7189 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7192 Reader->GetTemplateArgumentLocInfo(
7193 *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
7196 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7197 TL.setEllipsisLoc(ReadSourceLocation());
7200 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7201 TL.setNameLoc(ReadSourceLocation());
7204 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7205 if (TL.getNumProtocols()) {
7206 TL.setProtocolLAngleLoc(ReadSourceLocation());
7207 TL.setProtocolRAngleLoc(ReadSourceLocation());
7209 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7210 TL.setProtocolLoc(i, ReadSourceLocation());
7213 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7214 TL.setHasBaseTypeAsWritten(Record[Idx++]);
7215 TL.setTypeArgsLAngleLoc(ReadSourceLocation());
7216 TL.setTypeArgsRAngleLoc(ReadSourceLocation());
7217 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7218 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7219 TL.setProtocolLAngleLoc(ReadSourceLocation());
7220 TL.setProtocolRAngleLoc(ReadSourceLocation());
7221 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7222 TL.setProtocolLoc(i, ReadSourceLocation());
7225 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7226 TL.setStarLoc(ReadSourceLocation());
7229 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7230 TL.setKWLoc(ReadSourceLocation());
7231 TL.setLParenLoc(ReadSourceLocation());
7232 TL.setRParenLoc(ReadSourceLocation());
7235 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7236 TL.setKWLoc(ReadSourceLocation());
7239 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
7240 unsigned &Idx, TypeLoc TL) {
7241 TypeLocReader TLR(F, *this, Record, Idx);
7242 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7247 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
7249 QualType InfoTy = readType(F, Record, Idx);
7250 if (InfoTy.isNull())
7253 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7254 ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
7258 QualType ASTReader::GetType(TypeID ID) {
7259 assert(ContextObj && "reading type with no AST context");
7260 ASTContext &Context = *ContextObj;
7262 unsigned FastQuals = ID & Qualifiers::FastMask;
7263 unsigned Index = ID >> Qualifiers::FastWidth;
7265 if (Index < NUM_PREDEF_TYPE_IDS) {
7267 switch ((PredefinedTypeIDs)Index) {
7268 case PREDEF_TYPE_NULL_ID:
7270 case PREDEF_TYPE_VOID_ID:
7273 case PREDEF_TYPE_BOOL_ID:
7276 case PREDEF_TYPE_CHAR_U_ID:
7277 case PREDEF_TYPE_CHAR_S_ID:
7278 // FIXME: Check that the signedness of CharTy is correct!
7281 case PREDEF_TYPE_UCHAR_ID:
7282 T = Context.UnsignedCharTy;
7284 case PREDEF_TYPE_USHORT_ID:
7285 T = Context.UnsignedShortTy;
7287 case PREDEF_TYPE_UINT_ID:
7288 T = Context.UnsignedIntTy;
7290 case PREDEF_TYPE_ULONG_ID:
7291 T = Context.UnsignedLongTy;
7293 case PREDEF_TYPE_ULONGLONG_ID:
7294 T = Context.UnsignedLongLongTy;
7296 case PREDEF_TYPE_UINT128_ID:
7297 T = Context.UnsignedInt128Ty;
7299 case PREDEF_TYPE_SCHAR_ID:
7300 T = Context.SignedCharTy;
7302 case PREDEF_TYPE_WCHAR_ID:
7303 T = Context.WCharTy;
7305 case PREDEF_TYPE_SHORT_ID:
7306 T = Context.ShortTy;
7308 case PREDEF_TYPE_INT_ID:
7311 case PREDEF_TYPE_LONG_ID:
7314 case PREDEF_TYPE_LONGLONG_ID:
7315 T = Context.LongLongTy;
7317 case PREDEF_TYPE_INT128_ID:
7318 T = Context.Int128Ty;
7320 case PREDEF_TYPE_HALF_ID:
7323 case PREDEF_TYPE_FLOAT_ID:
7324 T = Context.FloatTy;
7326 case PREDEF_TYPE_DOUBLE_ID:
7327 T = Context.DoubleTy;
7329 case PREDEF_TYPE_LONGDOUBLE_ID:
7330 T = Context.LongDoubleTy;
7332 case PREDEF_TYPE_SHORT_ACCUM_ID:
7333 T = Context.ShortAccumTy;
7335 case PREDEF_TYPE_ACCUM_ID:
7336 T = Context.AccumTy;
7338 case PREDEF_TYPE_LONG_ACCUM_ID:
7339 T = Context.LongAccumTy;
7341 case PREDEF_TYPE_USHORT_ACCUM_ID:
7342 T = Context.UnsignedShortAccumTy;
7344 case PREDEF_TYPE_UACCUM_ID:
7345 T = Context.UnsignedAccumTy;
7347 case PREDEF_TYPE_ULONG_ACCUM_ID:
7348 T = Context.UnsignedLongAccumTy;
7350 case PREDEF_TYPE_SHORT_FRACT_ID:
7351 T = Context.ShortFractTy;
7353 case PREDEF_TYPE_FRACT_ID:
7354 T = Context.FractTy;
7356 case PREDEF_TYPE_LONG_FRACT_ID:
7357 T = Context.LongFractTy;
7359 case PREDEF_TYPE_USHORT_FRACT_ID:
7360 T = Context.UnsignedShortFractTy;
7362 case PREDEF_TYPE_UFRACT_ID:
7363 T = Context.UnsignedFractTy;
7365 case PREDEF_TYPE_ULONG_FRACT_ID:
7366 T = Context.UnsignedLongFractTy;
7368 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7369 T = Context.SatShortAccumTy;
7371 case PREDEF_TYPE_SAT_ACCUM_ID:
7372 T = Context.SatAccumTy;
7374 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7375 T = Context.SatLongAccumTy;
7377 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7378 T = Context.SatUnsignedShortAccumTy;
7380 case PREDEF_TYPE_SAT_UACCUM_ID:
7381 T = Context.SatUnsignedAccumTy;
7383 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7384 T = Context.SatUnsignedLongAccumTy;
7386 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7387 T = Context.SatShortFractTy;
7389 case PREDEF_TYPE_SAT_FRACT_ID:
7390 T = Context.SatFractTy;
7392 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7393 T = Context.SatLongFractTy;
7395 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7396 T = Context.SatUnsignedShortFractTy;
7398 case PREDEF_TYPE_SAT_UFRACT_ID:
7399 T = Context.SatUnsignedFractTy;
7401 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7402 T = Context.SatUnsignedLongFractTy;
7404 case PREDEF_TYPE_FLOAT16_ID:
7405 T = Context.Float16Ty;
7407 case PREDEF_TYPE_FLOAT128_ID:
7408 T = Context.Float128Ty;
7410 case PREDEF_TYPE_OVERLOAD_ID:
7411 T = Context.OverloadTy;
7413 case PREDEF_TYPE_BOUND_MEMBER:
7414 T = Context.BoundMemberTy;
7416 case PREDEF_TYPE_PSEUDO_OBJECT:
7417 T = Context.PseudoObjectTy;
7419 case PREDEF_TYPE_DEPENDENT_ID:
7420 T = Context.DependentTy;
7422 case PREDEF_TYPE_UNKNOWN_ANY:
7423 T = Context.UnknownAnyTy;
7425 case PREDEF_TYPE_NULLPTR_ID:
7426 T = Context.NullPtrTy;
7428 case PREDEF_TYPE_CHAR8_ID:
7429 T = Context.Char8Ty;
7431 case PREDEF_TYPE_CHAR16_ID:
7432 T = Context.Char16Ty;
7434 case PREDEF_TYPE_CHAR32_ID:
7435 T = Context.Char32Ty;
7437 case PREDEF_TYPE_OBJC_ID:
7438 T = Context.ObjCBuiltinIdTy;
7440 case PREDEF_TYPE_OBJC_CLASS:
7441 T = Context.ObjCBuiltinClassTy;
7443 case PREDEF_TYPE_OBJC_SEL:
7444 T = Context.ObjCBuiltinSelTy;
7446 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7447 case PREDEF_TYPE_##Id##_ID: \
7448 T = Context.SingletonId; \
7450 #include "clang/Basic/OpenCLImageTypes.def"
7451 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7452 case PREDEF_TYPE_##Id##_ID: \
7453 T = Context.Id##Ty; \
7455 #include "clang/Basic/OpenCLExtensionTypes.def"
7456 case PREDEF_TYPE_SAMPLER_ID:
7457 T = Context.OCLSamplerTy;
7459 case PREDEF_TYPE_EVENT_ID:
7460 T = Context.OCLEventTy;
7462 case PREDEF_TYPE_CLK_EVENT_ID:
7463 T = Context.OCLClkEventTy;
7465 case PREDEF_TYPE_QUEUE_ID:
7466 T = Context.OCLQueueTy;
7468 case PREDEF_TYPE_RESERVE_ID_ID:
7469 T = Context.OCLReserveIDTy;
7471 case PREDEF_TYPE_AUTO_DEDUCT:
7472 T = Context.getAutoDeductType();
7474 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7475 T = Context.getAutoRRefDeductType();
7477 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7478 T = Context.ARCUnbridgedCastTy;
7480 case PREDEF_TYPE_BUILTIN_FN:
7481 T = Context.BuiltinFnTy;
7483 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7484 T = Context.OMPArraySectionTy;
7486 #define SVE_TYPE(Name, Id, SingletonId) \
7487 case PREDEF_TYPE_##Id##_ID: \
7488 T = Context.SingletonId; \
7490 #include "clang/Basic/AArch64SVEACLETypes.def"
7493 assert(!T.isNull() && "Unknown predefined type");
7494 return T.withFastQualifiers(FastQuals);
7497 Index -= NUM_PREDEF_TYPE_IDS;
7498 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7499 if (TypesLoaded[Index].isNull()) {
7500 TypesLoaded[Index] = readTypeRecord(Index);
7501 if (TypesLoaded[Index].isNull())
7504 TypesLoaded[Index]->setFromAST();
7505 if (DeserializationListener)
7506 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7507 TypesLoaded[Index]);
7510 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7513 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7514 return GetType(getGlobalTypeID(F, LocalID));
7517 serialization::TypeID
7518 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7519 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7520 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7522 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7525 if (!F.ModuleOffsetMap.empty())
7526 ReadModuleOffsetMap(F);
7528 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7529 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7530 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7532 unsigned GlobalIndex = LocalIndex + I->second;
7533 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7536 TemplateArgumentLocInfo
7537 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7538 TemplateArgument::ArgKind Kind,
7539 const RecordData &Record,
7542 case TemplateArgument::Expression:
7544 case TemplateArgument::Type:
7545 return GetTypeSourceInfo(F, Record, Index);
7546 case TemplateArgument::Template: {
7547 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7549 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7550 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7553 case TemplateArgument::TemplateExpansion: {
7554 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7556 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7557 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7558 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7561 case TemplateArgument::Null:
7562 case TemplateArgument::Integral:
7563 case TemplateArgument::Declaration:
7564 case TemplateArgument::NullPtr:
7565 case TemplateArgument::Pack:
7566 // FIXME: Is this right?
7567 return TemplateArgumentLocInfo();
7569 llvm_unreachable("unexpected template argument loc");
7573 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7574 const RecordData &Record, unsigned &Index) {
7575 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7577 if (Arg.getKind() == TemplateArgument::Expression) {
7578 if (Record[Index++]) // bool InfoHasSameExpr.
7579 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7581 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7585 const ASTTemplateArgumentListInfo*
7586 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7587 const RecordData &Record,
7589 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7590 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7591 unsigned NumArgsAsWritten = Record[Index++];
7592 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7593 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7594 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7595 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7598 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7602 void ASTReader::CompleteRedeclChain(const Decl *D) {
7603 if (NumCurrentElementsDeserializing) {
7604 // We arrange to not care about the complete redeclaration chain while we're
7605 // deserializing. Just remember that the AST has marked this one as complete
7606 // but that it's not actually complete yet, so we know we still need to
7607 // complete it later.
7608 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7612 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7614 // If this is a named declaration, complete it by looking it up
7615 // within its context.
7617 // FIXME: Merging a function definition should merge
7618 // all mergeable entities within it.
7619 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7620 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7621 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7622 if (!getContext().getLangOpts().CPlusPlus &&
7623 isa<TranslationUnitDecl>(DC)) {
7624 // Outside of C++, we don't have a lookup table for the TU, so update
7625 // the identifier instead. (For C++ modules, we don't store decls
7626 // in the serialized identifier table, so we do the lookup in the TU.)
7627 auto *II = Name.getAsIdentifierInfo();
7628 assert(II && "non-identifier name in C?");
7629 if (II->isOutOfDate())
7630 updateOutOfDateIdentifier(*II);
7633 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7634 // Find all declarations of this kind from the relevant context.
7635 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7636 auto *DC = cast<DeclContext>(DCDecl);
7637 SmallVector<Decl*, 8> Decls;
7638 FindExternalLexicalDecls(
7639 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7644 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7645 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7646 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7647 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7648 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7649 if (auto *Template = FD->getPrimaryTemplate())
7650 Template->LoadLazySpecializations();
7654 CXXCtorInitializer **
7655 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7656 RecordLocation Loc = getLocalBitOffset(Offset);
7657 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7658 SavedStreamPosition SavedPosition(Cursor);
7659 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7660 Error(std::move(Err));
7663 ReadingKindTracker ReadingKind(Read_Decl, *this);
7666 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7668 Error(MaybeCode.takeError());
7671 unsigned Code = MaybeCode.get();
7673 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7674 if (!MaybeRecCode) {
7675 Error(MaybeRecCode.takeError());
7678 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7679 Error("malformed AST file: missing C++ ctor initializers");
7684 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7687 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7688 assert(ContextObj && "reading base specifiers with no AST context");
7689 ASTContext &Context = *ContextObj;
7691 RecordLocation Loc = getLocalBitOffset(Offset);
7692 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7693 SavedStreamPosition SavedPosition(Cursor);
7694 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7695 Error(std::move(Err));
7698 ReadingKindTracker ReadingKind(Read_Decl, *this);
7701 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7703 Error(MaybeCode.takeError());
7706 unsigned Code = MaybeCode.get();
7708 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7709 if (!MaybeRecCode) {
7710 Error(MaybeCode.takeError());
7713 unsigned RecCode = MaybeRecCode.get();
7715 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7716 Error("malformed AST file: missing C++ base specifiers");
7721 unsigned NumBases = Record[Idx++];
7722 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7723 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7724 for (unsigned I = 0; I != NumBases; ++I)
7725 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7729 serialization::DeclID
7730 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7731 if (LocalID < NUM_PREDEF_DECL_IDS)
7734 if (!F.ModuleOffsetMap.empty())
7735 ReadModuleOffsetMap(F);
7737 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7738 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7739 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7741 return LocalID + I->second;
7744 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7745 ModuleFile &M) const {
7746 // Predefined decls aren't from any module.
7747 if (ID < NUM_PREDEF_DECL_IDS)
7750 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7751 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7754 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7755 if (!D->isFromASTFile())
7757 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7758 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7762 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7763 if (ID < NUM_PREDEF_DECL_IDS)
7764 return SourceLocation();
7766 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7768 if (Index > DeclsLoaded.size()) {
7769 Error("declaration ID out-of-range for AST file");
7770 return SourceLocation();
7773 if (Decl *D = DeclsLoaded[Index])
7774 return D->getLocation();
7777 DeclCursorForID(ID, Loc);
7781 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7783 case PREDEF_DECL_NULL_ID:
7786 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7787 return Context.getTranslationUnitDecl();
7789 case PREDEF_DECL_OBJC_ID_ID:
7790 return Context.getObjCIdDecl();
7792 case PREDEF_DECL_OBJC_SEL_ID:
7793 return Context.getObjCSelDecl();
7795 case PREDEF_DECL_OBJC_CLASS_ID:
7796 return Context.getObjCClassDecl();
7798 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7799 return Context.getObjCProtocolDecl();
7801 case PREDEF_DECL_INT_128_ID:
7802 return Context.getInt128Decl();
7804 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7805 return Context.getUInt128Decl();
7807 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7808 return Context.getObjCInstanceTypeDecl();
7810 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7811 return Context.getBuiltinVaListDecl();
7813 case PREDEF_DECL_VA_LIST_TAG:
7814 return Context.getVaListTagDecl();
7816 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7817 return Context.getBuiltinMSVaListDecl();
7819 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7820 return Context.getExternCContextDecl();
7822 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7823 return Context.getMakeIntegerSeqDecl();
7825 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7826 return Context.getCFConstantStringDecl();
7828 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7829 return Context.getCFConstantStringTagDecl();
7831 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7832 return Context.getTypePackElementDecl();
7834 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7837 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7838 assert(ContextObj && "reading decl with no AST context");
7839 if (ID < NUM_PREDEF_DECL_IDS) {
7840 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7842 // Track that we have merged the declaration with ID \p ID into the
7843 // pre-existing predefined declaration \p D.
7844 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7846 Merged.push_back(ID);
7851 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7853 if (Index >= DeclsLoaded.size()) {
7854 assert(0 && "declaration ID out-of-range for AST file");
7855 Error("declaration ID out-of-range for AST file");
7859 return DeclsLoaded[Index];
7862 Decl *ASTReader::GetDecl(DeclID ID) {
7863 if (ID < NUM_PREDEF_DECL_IDS)
7864 return GetExistingDecl(ID);
7866 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7868 if (Index >= DeclsLoaded.size()) {
7869 assert(0 && "declaration ID out-of-range for AST file");
7870 Error("declaration ID out-of-range for AST file");
7874 if (!DeclsLoaded[Index]) {
7876 if (DeserializationListener)
7877 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7880 return DeclsLoaded[Index];
7883 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7885 if (GlobalID < NUM_PREDEF_DECL_IDS)
7888 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7889 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7890 ModuleFile *Owner = I->second;
7892 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7893 = M.GlobalToLocalDeclIDs.find(Owner);
7894 if (Pos == M.GlobalToLocalDeclIDs.end())
7897 return GlobalID - Owner->BaseDeclID + Pos->second;
7900 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7901 const RecordData &Record,
7903 if (Idx >= Record.size()) {
7904 Error("Corrupted AST file");
7908 return getGlobalDeclID(F, Record[Idx++]);
7911 /// Resolve the offset of a statement into a statement.
7913 /// This operation will read a new statement from the external
7914 /// source each time it is called, and is meant to be used via a
7915 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7916 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7917 // Switch case IDs are per Decl.
7918 ClearSwitchCaseIDs();
7920 // Offset here is a global offset across the entire chain.
7921 RecordLocation Loc = getLocalBitOffset(Offset);
7922 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7923 Error(std::move(Err));
7926 assert(NumCurrentElementsDeserializing == 0 &&
7927 "should not be called while already deserializing");
7928 Deserializing D(this);
7929 return ReadStmtFromStream(*Loc.F);
7932 void ASTReader::FindExternalLexicalDecls(
7933 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7934 SmallVectorImpl<Decl *> &Decls) {
7935 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7937 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7938 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7939 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7940 auto K = (Decl::Kind)+LexicalDecls[I];
7941 if (!IsKindWeWant(K))
7944 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7946 // Don't add predefined declarations to the lexical context more
7948 if (ID < NUM_PREDEF_DECL_IDS) {
7949 if (PredefsVisited[ID])
7952 PredefsVisited[ID] = true;
7955 if (Decl *D = GetLocalDecl(*M, ID)) {
7956 assert(D->getKind() == K && "wrong kind for lexical decl");
7957 if (!DC->isDeclInLexicalTraversal(D))
7963 if (isa<TranslationUnitDecl>(DC)) {
7964 for (auto Lexical : TULexicalDecls)
7965 Visit(Lexical.first, Lexical.second);
7967 auto I = LexicalDecls.find(DC);
7968 if (I != LexicalDecls.end())
7969 Visit(I->second.first, I->second.second);
7972 ++NumLexicalDeclContextsRead;
7982 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7984 bool operator()(LocalDeclID L, LocalDeclID R) const {
7985 SourceLocation LHS = getLocation(L);
7986 SourceLocation RHS = getLocation(R);
7987 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7990 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7991 SourceLocation RHS = getLocation(R);
7992 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7995 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7996 SourceLocation LHS = getLocation(L);
7997 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8000 SourceLocation getLocation(LocalDeclID ID) const {
8001 return Reader.getSourceManager().getFileLoc(
8002 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
8008 void ASTReader::FindFileRegionDecls(FileID File,
8009 unsigned Offset, unsigned Length,
8010 SmallVectorImpl<Decl *> &Decls) {
8011 SourceManager &SM = getSourceManager();
8013 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
8014 if (I == FileDeclIDs.end())
8017 FileDeclsInfo &DInfo = I->second;
8018 if (DInfo.Decls.empty())
8022 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
8023 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
8025 DeclIDComp DIDComp(*this, *DInfo.Mod);
8026 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
8027 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
8028 if (BeginIt != DInfo.Decls.begin())
8031 // If we are pointing at a top-level decl inside an objc container, we need
8032 // to backtrack until we find it otherwise we will fail to report that the
8033 // region overlaps with an objc container.
8034 while (BeginIt != DInfo.Decls.begin() &&
8035 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
8036 ->isTopLevelDeclInObjCContainer())
8039 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
8040 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
8041 if (EndIt != DInfo.Decls.end())
8044 for (ArrayRef<serialization::LocalDeclID>::iterator
8045 DIt = BeginIt; DIt != EndIt; ++DIt)
8046 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
8050 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8051 DeclarationName Name) {
8052 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8053 "DeclContext has no visible decls in storage");
8057 auto It = Lookups.find(DC);
8058 if (It == Lookups.end())
8061 Deserializing LookupResults(this);
8063 // Load the list of declarations.
8064 SmallVector<NamedDecl *, 64> Decls;
8065 for (DeclID ID : It->second.Table.find(Name)) {
8066 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8067 if (ND->getDeclName() == Name)
8068 Decls.push_back(ND);
8071 ++NumVisibleDeclContextsRead;
8072 SetExternalVisibleDeclsForName(DC, Name, Decls);
8073 return !Decls.empty();
8076 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8077 if (!DC->hasExternalVisibleStorage())
8080 auto It = Lookups.find(DC);
8081 assert(It != Lookups.end() &&
8082 "have external visible storage but no lookup tables");
8086 for (DeclID ID : It->second.Table.findAll()) {
8087 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8088 Decls[ND->getDeclName()].push_back(ND);
8091 ++NumVisibleDeclContextsRead;
8093 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8094 SetExternalVisibleDeclsForName(DC, I->first, I->second);
8096 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8099 const serialization::reader::DeclContextLookupTable *
8100 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8101 auto I = Lookups.find(Primary);
8102 return I == Lookups.end() ? nullptr : &I->second;
8105 /// Under non-PCH compilation the consumer receives the objc methods
8106 /// before receiving the implementation, and codegen depends on this.
8107 /// We simulate this by deserializing and passing to consumer the methods of the
8108 /// implementation before passing the deserialized implementation decl.
8109 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8110 ASTConsumer *Consumer) {
8111 assert(ImplD && Consumer);
8113 for (auto *I : ImplD->methods())
8114 Consumer->HandleInterestingDecl(DeclGroupRef(I));
8116 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8119 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8120 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8121 PassObjCImplDeclToConsumer(ImplD, Consumer);
8123 Consumer->HandleInterestingDecl(DeclGroupRef(D));
8126 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8127 this->Consumer = Consumer;
8130 PassInterestingDeclsToConsumer();
8132 if (DeserializationListener)
8133 DeserializationListener->ReaderInitialized(this);
8136 void ASTReader::PrintStats() {
8137 std::fprintf(stderr, "*** AST File Statistics:\n");
8139 unsigned NumTypesLoaded
8140 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
8142 unsigned NumDeclsLoaded
8143 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
8145 unsigned NumIdentifiersLoaded
8146 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
8147 IdentifiersLoaded.end(),
8148 (IdentifierInfo *)nullptr);
8149 unsigned NumMacrosLoaded
8150 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
8152 (MacroInfo *)nullptr);
8153 unsigned NumSelectorsLoaded
8154 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
8155 SelectorsLoaded.end(),
8158 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8159 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
8160 NumSLocEntriesRead, TotalNumSLocEntries,
8161 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8162 if (!TypesLoaded.empty())
8163 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
8164 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8165 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8166 if (!DeclsLoaded.empty())
8167 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
8168 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8169 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8170 if (!IdentifiersLoaded.empty())
8171 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
8172 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8173 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8174 if (!MacrosLoaded.empty())
8175 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
8176 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8177 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8178 if (!SelectorsLoaded.empty())
8179 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
8180 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8181 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8182 if (TotalNumStatements)
8183 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
8184 NumStatementsRead, TotalNumStatements,
8185 ((float)NumStatementsRead/TotalNumStatements * 100));
8187 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
8188 NumMacrosRead, TotalNumMacros,
8189 ((float)NumMacrosRead/TotalNumMacros * 100));
8190 if (TotalLexicalDeclContexts)
8191 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
8192 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8193 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8195 if (TotalVisibleDeclContexts)
8196 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
8197 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8198 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8200 if (TotalNumMethodPoolEntries)
8201 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
8202 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8203 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8205 if (NumMethodPoolLookups)
8206 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
8207 NumMethodPoolHits, NumMethodPoolLookups,
8208 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8209 if (NumMethodPoolTableLookups)
8210 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
8211 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8212 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8214 if (NumIdentifierLookupHits)
8215 std::fprintf(stderr,
8216 " %u / %u identifier table lookups succeeded (%f%%)\n",
8217 NumIdentifierLookupHits, NumIdentifierLookups,
8218 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8221 std::fprintf(stderr, "\n");
8222 GlobalIndex->printStats();
8225 std::fprintf(stderr, "\n");
8227 std::fprintf(stderr, "\n");
8230 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8231 LLVM_DUMP_METHOD static void
8232 dumpModuleIDMap(StringRef Name,
8233 const ContinuousRangeMap<Key, ModuleFile *,
8234 InitialCapacity> &Map) {
8235 if (Map.begin() == Map.end())
8238 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8240 llvm::errs() << Name << ":\n";
8241 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8243 llvm::errs() << " " << I->first << " -> " << I->second->FileName
8248 LLVM_DUMP_METHOD void ASTReader::dump() {
8249 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8250 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8251 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8252 dumpModuleIDMap("Global type map", GlobalTypeMap);
8253 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8254 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8255 dumpModuleIDMap("Global macro map", GlobalMacroMap);
8256 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8257 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8258 dumpModuleIDMap("Global preprocessed entity map",
8259 GlobalPreprocessedEntityMap);
8261 llvm::errs() << "\n*** PCH/Modules Loaded:";
8262 for (ModuleFile &M : ModuleMgr)
8266 /// Return the amount of memory used by memory buffers, breaking down
8267 /// by heap-backed versus mmap'ed memory.
8268 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8269 for (ModuleFile &I : ModuleMgr) {
8270 if (llvm::MemoryBuffer *buf = I.Buffer) {
8271 size_t bytes = buf->getBufferSize();
8272 switch (buf->getBufferKind()) {
8273 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8274 sizes.malloc_bytes += bytes;
8276 case llvm::MemoryBuffer::MemoryBuffer_MMap:
8277 sizes.mmap_bytes += bytes;
8284 void ASTReader::InitializeSema(Sema &S) {
8286 S.addExternalSource(this);
8288 // Makes sure any declarations that were deserialized "too early"
8289 // still get added to the identifier's declaration chains.
8290 for (uint64_t ID : PreloadedDeclIDs) {
8291 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8292 pushExternalDeclIntoScope(D, D->getDeclName());
8294 PreloadedDeclIDs.clear();
8296 // FIXME: What happens if these are changed by a module import?
8297 if (!FPPragmaOptions.empty()) {
8298 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8299 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
8302 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
8303 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
8304 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
8309 void ASTReader::UpdateSema() {
8310 assert(SemaObj && "no Sema to update");
8312 // Load the offsets of the declarations that Sema references.
8313 // They will be lazily deserialized when needed.
8314 if (!SemaDeclRefs.empty()) {
8315 assert(SemaDeclRefs.size() % 3 == 0);
8316 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8317 if (!SemaObj->StdNamespace)
8318 SemaObj->StdNamespace = SemaDeclRefs[I];
8319 if (!SemaObj->StdBadAlloc)
8320 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8321 if (!SemaObj->StdAlignValT)
8322 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8324 SemaDeclRefs.clear();
8327 // Update the state of pragmas. Use the same API as if we had encountered the
8328 // pragma in the source.
8329 if(OptimizeOffPragmaLocation.isValid())
8330 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8331 if (PragmaMSStructState != -1)
8332 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8333 if (PointersToMembersPragmaLocation.isValid()) {
8334 SemaObj->ActOnPragmaMSPointersToMembers(
8335 (LangOptions::PragmaMSPointersToMembersKind)
8336 PragmaMSPointersToMembersState,
8337 PointersToMembersPragmaLocation);
8339 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8341 if (PragmaPackCurrentValue) {
8342 // The bottom of the stack might have a default value. It must be adjusted
8343 // to the current value to ensure that the packing state is preserved after
8344 // popping entries that were included/imported from a PCH/module.
8345 bool DropFirst = false;
8346 if (!PragmaPackStack.empty() &&
8347 PragmaPackStack.front().Location.isInvalid()) {
8348 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
8349 "Expected a default alignment value");
8350 SemaObj->PackStack.Stack.emplace_back(
8351 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
8352 SemaObj->PackStack.CurrentPragmaLocation,
8353 PragmaPackStack.front().PushLocation);
8356 for (const auto &Entry :
8357 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
8358 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
8359 Entry.Location, Entry.PushLocation);
8360 if (PragmaPackCurrentLocation.isInvalid()) {
8361 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
8362 "Expected a default alignment value");
8363 // Keep the current values.
8365 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
8366 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
8371 IdentifierInfo *ASTReader::get(StringRef Name) {
8372 // Note that we are loading an identifier.
8373 Deserializing AnIdentifier(this);
8375 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8376 NumIdentifierLookups,
8377 NumIdentifierLookupHits);
8379 // We don't need to do identifier table lookups in C++ modules (we preload
8380 // all interesting declarations, and don't need to use the scope for name
8381 // lookups). Perform the lookup in PCH files, though, since we don't build
8382 // a complete initial identifier table if we're carrying on from a PCH.
8383 if (PP.getLangOpts().CPlusPlus) {
8384 for (auto F : ModuleMgr.pch_modules())
8388 // If there is a global index, look there first to determine which modules
8389 // provably do not have any results for this identifier.
8390 GlobalModuleIndex::HitSet Hits;
8391 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8392 if (!loadGlobalIndex()) {
8393 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8398 ModuleMgr.visit(Visitor, HitsPtr);
8401 IdentifierInfo *II = Visitor.getIdentifierInfo();
8402 markIdentifierUpToDate(II);
8408 /// An identifier-lookup iterator that enumerates all of the
8409 /// identifiers stored within a set of AST files.
8410 class ASTIdentifierIterator : public IdentifierIterator {
8411 /// The AST reader whose identifiers are being enumerated.
8412 const ASTReader &Reader;
8414 /// The current index into the chain of AST files stored in
8418 /// The current position within the identifier lookup table
8419 /// of the current AST file.
8420 ASTIdentifierLookupTable::key_iterator Current;
8422 /// The end position within the identifier lookup table of
8423 /// the current AST file.
8424 ASTIdentifierLookupTable::key_iterator End;
8426 /// Whether to skip any modules in the ASTReader.
8430 explicit ASTIdentifierIterator(const ASTReader &Reader,
8431 bool SkipModules = false);
8433 StringRef Next() override;
8436 } // namespace clang
8438 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8440 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8443 StringRef ASTIdentifierIterator::Next() {
8444 while (Current == End) {
8445 // If we have exhausted all of our AST files, we're done.
8450 ModuleFile &F = Reader.ModuleMgr[Index];
8451 if (SkipModules && F.isModule())
8454 ASTIdentifierLookupTable *IdTable =
8455 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8456 Current = IdTable->key_begin();
8457 End = IdTable->key_end();
8460 // We have any identifiers remaining in the current AST file; return
8462 StringRef Result = *Current;
8469 /// A utility for appending two IdentifierIterators.
8470 class ChainedIdentifierIterator : public IdentifierIterator {
8471 std::unique_ptr<IdentifierIterator> Current;
8472 std::unique_ptr<IdentifierIterator> Queued;
8475 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8476 std::unique_ptr<IdentifierIterator> Second)
8477 : Current(std::move(First)), Queued(std::move(Second)) {}
8479 StringRef Next() override {
8483 StringRef result = Current->Next();
8484 if (!result.empty())
8487 // Try the queued iterator, which may itself be empty.
8489 std::swap(Current, Queued);
8496 IdentifierIterator *ASTReader::getIdentifiers() {
8497 if (!loadGlobalIndex()) {
8498 std::unique_ptr<IdentifierIterator> ReaderIter(
8499 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8500 std::unique_ptr<IdentifierIterator> ModulesIter(
8501 GlobalIndex->createIdentifierIterator());
8502 return new ChainedIdentifierIterator(std::move(ReaderIter),
8503 std::move(ModulesIter));
8506 return new ASTIdentifierIterator(*this);
8510 namespace serialization {
8512 class ReadMethodPoolVisitor {
8515 unsigned PriorGeneration;
8516 unsigned InstanceBits = 0;
8517 unsigned FactoryBits = 0;
8518 bool InstanceHasMoreThanOneDecl = false;
8519 bool FactoryHasMoreThanOneDecl = false;
8520 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8521 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8524 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8525 unsigned PriorGeneration)
8526 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8528 bool operator()(ModuleFile &M) {
8529 if (!M.SelectorLookupTable)
8532 // If we've already searched this module file, skip it now.
8533 if (M.Generation <= PriorGeneration)
8536 ++Reader.NumMethodPoolTableLookups;
8537 ASTSelectorLookupTable *PoolTable
8538 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8539 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8540 if (Pos == PoolTable->end())
8543 ++Reader.NumMethodPoolTableHits;
8544 ++Reader.NumSelectorsRead;
8545 // FIXME: Not quite happy with the statistics here. We probably should
8546 // disable this tracking when called via LoadSelector.
8547 // Also, should entries without methods count as misses?
8548 ++Reader.NumMethodPoolEntriesRead;
8549 ASTSelectorLookupTrait::data_type Data = *Pos;
8550 if (Reader.DeserializationListener)
8551 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8553 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8554 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8555 InstanceBits = Data.InstanceBits;
8556 FactoryBits = Data.FactoryBits;
8557 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8558 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8562 /// Retrieve the instance methods found by this visitor.
8563 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8564 return InstanceMethods;
8567 /// Retrieve the instance methods found by this visitor.
8568 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8569 return FactoryMethods;
8572 unsigned getInstanceBits() const { return InstanceBits; }
8573 unsigned getFactoryBits() const { return FactoryBits; }
8575 bool instanceHasMoreThanOneDecl() const {
8576 return InstanceHasMoreThanOneDecl;
8579 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8582 } // namespace serialization
8583 } // namespace clang
8585 /// Add the given set of methods to the method list.
8586 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8587 ObjCMethodList &List) {
8588 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8589 S.addMethodToGlobalList(&List, Methods[I]);
8593 void ASTReader::ReadMethodPool(Selector Sel) {
8594 // Get the selector generation and update it to the current generation.
8595 unsigned &Generation = SelectorGeneration[Sel];
8596 unsigned PriorGeneration = Generation;
8597 Generation = getGeneration();
8598 SelectorOutOfDate[Sel] = false;
8600 // Search for methods defined with this selector.
8601 ++NumMethodPoolLookups;
8602 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8603 ModuleMgr.visit(Visitor);
8605 if (Visitor.getInstanceMethods().empty() &&
8606 Visitor.getFactoryMethods().empty())
8609 ++NumMethodPoolHits;
8614 Sema &S = *getSema();
8615 Sema::GlobalMethodPool::iterator Pos
8616 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8618 Pos->second.first.setBits(Visitor.getInstanceBits());
8619 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8620 Pos->second.second.setBits(Visitor.getFactoryBits());
8621 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8623 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8624 // when building a module we keep every method individually and may need to
8625 // update hasMoreThanOneDecl as we add the methods.
8626 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8627 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8630 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8631 if (SelectorOutOfDate[Sel])
8632 ReadMethodPool(Sel);
8635 void ASTReader::ReadKnownNamespaces(
8636 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8639 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8640 if (NamespaceDecl *Namespace
8641 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8642 Namespaces.push_back(Namespace);
8646 void ASTReader::ReadUndefinedButUsed(
8647 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8648 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8649 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8650 SourceLocation Loc =
8651 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8652 Undefined.insert(std::make_pair(D, Loc));
8656 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8657 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8659 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8660 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8661 uint64_t Count = DelayedDeleteExprs[Idx++];
8662 for (uint64_t C = 0; C < Count; ++C) {
8663 SourceLocation DeleteLoc =
8664 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8665 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8666 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8671 void ASTReader::ReadTentativeDefinitions(
8672 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8673 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8674 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8676 TentativeDefs.push_back(Var);
8678 TentativeDefinitions.clear();
8681 void ASTReader::ReadUnusedFileScopedDecls(
8682 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8683 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8685 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8689 UnusedFileScopedDecls.clear();
8692 void ASTReader::ReadDelegatingConstructors(
8693 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8694 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8695 CXXConstructorDecl *D
8696 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8700 DelegatingCtorDecls.clear();
8703 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8704 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8706 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8710 ExtVectorDecls.clear();
8713 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8714 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8715 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8717 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8718 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8722 UnusedLocalTypedefNameCandidates.clear();
8725 void ASTReader::ReadReferencedSelectors(
8726 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8727 if (ReferencedSelectorsData.empty())
8730 // If there are @selector references added them to its pool. This is for
8731 // implementation of -Wselector.
8732 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8734 while (I < DataSize) {
8735 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8736 SourceLocation SelLoc
8737 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8738 Sels.push_back(std::make_pair(Sel, SelLoc));
8740 ReferencedSelectorsData.clear();
8743 void ASTReader::ReadWeakUndeclaredIdentifiers(
8744 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8745 if (WeakUndeclaredIdentifiers.empty())
8748 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8749 IdentifierInfo *WeakId
8750 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8751 IdentifierInfo *AliasId
8752 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8754 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8755 bool Used = WeakUndeclaredIdentifiers[I++];
8756 WeakInfo WI(AliasId, Loc);
8758 WeakIDs.push_back(std::make_pair(WeakId, WI));
8760 WeakUndeclaredIdentifiers.clear();
8763 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8764 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8765 ExternalVTableUse VT;
8766 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8767 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8768 VT.DefinitionRequired = VTableUses[Idx++];
8769 VTables.push_back(VT);
8775 void ASTReader::ReadPendingInstantiations(
8776 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8777 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8778 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8780 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8782 Pending.push_back(std::make_pair(D, Loc));
8784 PendingInstantiations.clear();
8787 void ASTReader::ReadLateParsedTemplates(
8788 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8790 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8792 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8794 auto LT = std::make_unique<LateParsedTemplate>();
8795 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8797 ModuleFile *F = getOwningModuleFile(LT->D);
8798 assert(F && "No module");
8800 unsigned TokN = LateParsedTemplates[Idx++];
8801 LT->Toks.reserve(TokN);
8802 for (unsigned T = 0; T < TokN; ++T)
8803 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8805 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8808 LateParsedTemplates.clear();
8811 void ASTReader::LoadSelector(Selector Sel) {
8812 // It would be complicated to avoid reading the methods anyway. So don't.
8813 ReadMethodPool(Sel);
8816 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8817 assert(ID && "Non-zero identifier ID required");
8818 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8819 IdentifiersLoaded[ID - 1] = II;
8820 if (DeserializationListener)
8821 DeserializationListener->IdentifierRead(ID, II);
8824 /// Set the globally-visible declarations associated with the given
8827 /// If the AST reader is currently in a state where the given declaration IDs
8828 /// cannot safely be resolved, they are queued until it is safe to resolve
8831 /// \param II an IdentifierInfo that refers to one or more globally-visible
8834 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8835 /// visible at global scope.
8837 /// \param Decls if non-null, this vector will be populated with the set of
8838 /// deserialized declarations. These declarations will not be pushed into
8841 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8842 const SmallVectorImpl<uint32_t> &DeclIDs,
8843 SmallVectorImpl<Decl *> *Decls) {
8844 if (NumCurrentElementsDeserializing && !Decls) {
8845 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8849 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8851 // Queue this declaration so that it will be added to the
8852 // translation unit scope and identifier's declaration chain
8853 // once a Sema object is known.
8854 PreloadedDeclIDs.push_back(DeclIDs[I]);
8858 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8860 // If we're simply supposed to record the declarations, do so now.
8862 Decls->push_back(D);
8866 // Introduce this declaration into the translation-unit scope
8867 // and add it to the declaration chain for this identifier, so
8868 // that (unqualified) name lookup will find it.
8869 pushExternalDeclIntoScope(D, II);
8873 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8877 if (IdentifiersLoaded.empty()) {
8878 Error("no identifier table in AST file");
8883 if (!IdentifiersLoaded[ID]) {
8884 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8885 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8886 ModuleFile *M = I->second;
8887 unsigned Index = ID - M->BaseIdentifierID;
8888 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8890 // All of the strings in the AST file are preceded by a 16-bit length.
8891 // Extract that 16-bit length to avoid having to execute strlen().
8892 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8893 // unsigned integers. This is important to avoid integer overflow when
8894 // we cast them to 'unsigned'.
8895 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8896 unsigned StrLen = (((unsigned) StrLenPtr[0])
8897 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8898 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8899 IdentifiersLoaded[ID] = &II;
8900 markIdentifierFromAST(*this, II);
8901 if (DeserializationListener)
8902 DeserializationListener->IdentifierRead(ID + 1, &II);
8905 return IdentifiersLoaded[ID];
8908 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8909 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8912 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8913 if (LocalID < NUM_PREDEF_IDENT_IDS)
8916 if (!M.ModuleOffsetMap.empty())
8917 ReadModuleOffsetMap(M);
8919 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8920 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8921 assert(I != M.IdentifierRemap.end()
8922 && "Invalid index into identifier index remap");
8924 return LocalID + I->second;
8927 MacroInfo *ASTReader::getMacro(MacroID ID) {
8931 if (MacrosLoaded.empty()) {
8932 Error("no macro table in AST file");
8936 ID -= NUM_PREDEF_MACRO_IDS;
8937 if (!MacrosLoaded[ID]) {
8938 GlobalMacroMapType::iterator I
8939 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8940 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8941 ModuleFile *M = I->second;
8942 unsigned Index = ID - M->BaseMacroID;
8943 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8945 if (DeserializationListener)
8946 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8950 return MacrosLoaded[ID];
8953 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8954 if (LocalID < NUM_PREDEF_MACRO_IDS)
8957 if (!M.ModuleOffsetMap.empty())
8958 ReadModuleOffsetMap(M);
8960 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8961 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8962 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8964 return LocalID + I->second;
8967 serialization::SubmoduleID
8968 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8969 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8972 if (!M.ModuleOffsetMap.empty())
8973 ReadModuleOffsetMap(M);
8975 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8976 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8977 assert(I != M.SubmoduleRemap.end()
8978 && "Invalid index into submodule index remap");
8980 return LocalID + I->second;
8983 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8984 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8985 assert(GlobalID == 0 && "Unhandled global submodule ID");
8989 if (GlobalID > SubmodulesLoaded.size()) {
8990 Error("submodule ID out of range in AST file");
8994 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8997 Module *ASTReader::getModule(unsigned ID) {
8998 return getSubmodule(ID);
9001 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
9002 ModuleFile *MF = getOwningModuleFile(D);
9003 return MF && MF->PCHHasObjectFile;
9006 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
9008 // It's a module, look it up by submodule ID.
9009 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
9010 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
9012 // It's a prefix (preamble, PCH, ...). Look it up by index.
9013 unsigned IndexFromEnd = ID >> 1;
9014 assert(IndexFromEnd && "got reference to unknown module file");
9015 return getModuleManager().pch_modules().end()[-IndexFromEnd];
9019 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
9023 // For a file representing a module, use the submodule ID of the top-level
9024 // module as the file ID. For any other kind of file, the number of such
9025 // files loaded beforehand will be the same on reload.
9026 // FIXME: Is this true even if we have an explicit module file and a PCH?
9028 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
9030 auto PCHModules = getModuleManager().pch_modules();
9031 auto I = llvm::find(PCHModules, F);
9032 assert(I != PCHModules.end() && "emitting reference to unknown file");
9033 return (I - PCHModules.end()) << 1;
9036 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
9037 ASTReader::getSourceDescriptor(unsigned ID) {
9038 if (const Module *M = getSubmodule(ID))
9039 return ExternalASTSource::ASTSourceDescriptor(*M);
9041 // If there is only a single PCH, return it instead.
9042 // Chained PCH are not supported.
9043 const auto &PCHChain = ModuleMgr.pch_modules();
9044 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
9045 ModuleFile &MF = ModuleMgr.getPrimaryModule();
9046 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
9047 StringRef FileName = llvm::sys::path::filename(MF.FileName);
9048 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
9054 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
9055 auto I = DefinitionSource.find(FD);
9056 if (I == DefinitionSource.end())
9057 return EK_ReplyHazy;
9058 return I->second ? EK_Never : EK_Always;
9061 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9062 return DecodeSelector(getGlobalSelectorID(M, LocalID));
9065 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9069 if (ID > SelectorsLoaded.size()) {
9070 Error("selector ID out of range in AST file");
9074 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9075 // Load this selector from the selector table.
9076 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9077 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9078 ModuleFile &M = *I->second;
9079 ASTSelectorLookupTrait Trait(*this, M);
9080 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9081 SelectorsLoaded[ID - 1] =
9082 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9083 if (DeserializationListener)
9084 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9087 return SelectorsLoaded[ID - 1];
9090 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9091 return DecodeSelector(ID);
9094 uint32_t ASTReader::GetNumExternalSelectors() {
9095 // ID 0 (the null selector) is considered an external selector.
9096 return getTotalNumSelectors() + 1;
9099 serialization::SelectorID
9100 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9101 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9104 if (!M.ModuleOffsetMap.empty())
9105 ReadModuleOffsetMap(M);
9107 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9108 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9109 assert(I != M.SelectorRemap.end()
9110 && "Invalid index into selector index remap");
9112 return LocalID + I->second;
9116 ASTReader::ReadDeclarationName(ModuleFile &F,
9117 const RecordData &Record, unsigned &Idx) {
9118 ASTContext &Context = getContext();
9119 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
9121 case DeclarationName::Identifier:
9122 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
9124 case DeclarationName::ObjCZeroArgSelector:
9125 case DeclarationName::ObjCOneArgSelector:
9126 case DeclarationName::ObjCMultiArgSelector:
9127 return DeclarationName(ReadSelector(F, Record, Idx));
9129 case DeclarationName::CXXConstructorName:
9130 return Context.DeclarationNames.getCXXConstructorName(
9131 Context.getCanonicalType(readType(F, Record, Idx)));
9133 case DeclarationName::CXXDestructorName:
9134 return Context.DeclarationNames.getCXXDestructorName(
9135 Context.getCanonicalType(readType(F, Record, Idx)));
9137 case DeclarationName::CXXDeductionGuideName:
9138 return Context.DeclarationNames.getCXXDeductionGuideName(
9139 ReadDeclAs<TemplateDecl>(F, Record, Idx));
9141 case DeclarationName::CXXConversionFunctionName:
9142 return Context.DeclarationNames.getCXXConversionFunctionName(
9143 Context.getCanonicalType(readType(F, Record, Idx)));
9145 case DeclarationName::CXXOperatorName:
9146 return Context.DeclarationNames.getCXXOperatorName(
9147 (OverloadedOperatorKind)Record[Idx++]);
9149 case DeclarationName::CXXLiteralOperatorName:
9150 return Context.DeclarationNames.getCXXLiteralOperatorName(
9151 GetIdentifierInfo(F, Record, Idx));
9153 case DeclarationName::CXXUsingDirective:
9154 return DeclarationName::getUsingDirectiveName();
9157 llvm_unreachable("Invalid NameKind!");
9160 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
9161 DeclarationNameLoc &DNLoc,
9162 DeclarationName Name,
9163 const RecordData &Record, unsigned &Idx) {
9164 switch (Name.getNameKind()) {
9165 case DeclarationName::CXXConstructorName:
9166 case DeclarationName::CXXDestructorName:
9167 case DeclarationName::CXXConversionFunctionName:
9168 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
9171 case DeclarationName::CXXOperatorName:
9172 DNLoc.CXXOperatorName.BeginOpNameLoc
9173 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9174 DNLoc.CXXOperatorName.EndOpNameLoc
9175 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9178 case DeclarationName::CXXLiteralOperatorName:
9179 DNLoc.CXXLiteralOperatorName.OpNameLoc
9180 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9183 case DeclarationName::Identifier:
9184 case DeclarationName::ObjCZeroArgSelector:
9185 case DeclarationName::ObjCOneArgSelector:
9186 case DeclarationName::ObjCMultiArgSelector:
9187 case DeclarationName::CXXUsingDirective:
9188 case DeclarationName::CXXDeductionGuideName:
9193 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
9194 DeclarationNameInfo &NameInfo,
9195 const RecordData &Record, unsigned &Idx) {
9196 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
9197 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
9198 DeclarationNameLoc DNLoc;
9199 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
9200 NameInfo.setInfo(DNLoc);
9203 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
9204 const RecordData &Record, unsigned &Idx) {
9205 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
9206 unsigned NumTPLists = Record[Idx++];
9207 Info.NumTemplParamLists = NumTPLists;
9209 Info.TemplParamLists =
9210 new (getContext()) TemplateParameterList *[NumTPLists];
9211 for (unsigned i = 0; i != NumTPLists; ++i)
9212 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
9217 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
9219 ASTContext &Context = getContext();
9220 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
9222 case TemplateName::Template:
9223 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
9225 case TemplateName::OverloadedTemplate: {
9226 unsigned size = Record[Idx++];
9227 UnresolvedSet<8> Decls;
9229 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
9231 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
9234 case TemplateName::AssumedTemplate: {
9235 DeclarationName Name = ReadDeclarationName(F, Record, Idx);
9236 return Context.getAssumedTemplateName(Name);
9239 case TemplateName::QualifiedTemplate: {
9240 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9241 bool hasTemplKeyword = Record[Idx++];
9242 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
9243 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
9246 case TemplateName::DependentTemplate: {
9247 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9248 if (Record[Idx++]) // isIdentifier
9249 return Context.getDependentTemplateName(NNS,
9250 GetIdentifierInfo(F, Record,
9252 return Context.getDependentTemplateName(NNS,
9253 (OverloadedOperatorKind)Record[Idx++]);
9256 case TemplateName::SubstTemplateTemplateParm: {
9257 TemplateTemplateParmDecl *param
9258 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9259 if (!param) return TemplateName();
9260 TemplateName replacement = ReadTemplateName(F, Record, Idx);
9261 return Context.getSubstTemplateTemplateParm(param, replacement);
9264 case TemplateName::SubstTemplateTemplateParmPack: {
9265 TemplateTemplateParmDecl *Param
9266 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9268 return TemplateName();
9270 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
9271 if (ArgPack.getKind() != TemplateArgument::Pack)
9272 return TemplateName();
9274 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
9278 llvm_unreachable("Unhandled template name kind!");
9281 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
9282 const RecordData &Record,
9284 bool Canonicalize) {
9285 ASTContext &Context = getContext();
9287 // The caller wants a canonical template argument. Sometimes the AST only
9288 // wants template arguments in canonical form (particularly as the template
9289 // argument lists of template specializations) so ensure we preserve that
9290 // canonical form across serialization.
9291 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
9292 return Context.getCanonicalTemplateArgument(Arg);
9295 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
9297 case TemplateArgument::Null:
9298 return TemplateArgument();
9299 case TemplateArgument::Type:
9300 return TemplateArgument(readType(F, Record, Idx));
9301 case TemplateArgument::Declaration: {
9302 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
9303 return TemplateArgument(D, readType(F, Record, Idx));
9305 case TemplateArgument::NullPtr:
9306 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
9307 case TemplateArgument::Integral: {
9308 llvm::APSInt Value = ReadAPSInt(Record, Idx);
9309 QualType T = readType(F, Record, Idx);
9310 return TemplateArgument(Context, Value, T);
9312 case TemplateArgument::Template:
9313 return TemplateArgument(ReadTemplateName(F, Record, Idx));
9314 case TemplateArgument::TemplateExpansion: {
9315 TemplateName Name = ReadTemplateName(F, Record, Idx);
9316 Optional<unsigned> NumTemplateExpansions;
9317 if (unsigned NumExpansions = Record[Idx++])
9318 NumTemplateExpansions = NumExpansions - 1;
9319 return TemplateArgument(Name, NumTemplateExpansions);
9321 case TemplateArgument::Expression:
9322 return TemplateArgument(ReadExpr(F));
9323 case TemplateArgument::Pack: {
9324 unsigned NumArgs = Record[Idx++];
9325 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
9326 for (unsigned I = 0; I != NumArgs; ++I)
9327 Args[I] = ReadTemplateArgument(F, Record, Idx);
9328 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
9332 llvm_unreachable("Unhandled template argument kind!");
9335 TemplateParameterList *
9336 ASTReader::ReadTemplateParameterList(ModuleFile &F,
9337 const RecordData &Record, unsigned &Idx) {
9338 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
9339 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
9340 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
9342 unsigned NumParams = Record[Idx++];
9343 SmallVector<NamedDecl *, 16> Params;
9344 Params.reserve(NumParams);
9346 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
9348 bool HasRequiresClause = Record[Idx++];
9349 Expr *RequiresClause = HasRequiresClause ? ReadExpr(F) : nullptr;
9351 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9352 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9353 return TemplateParams;
9358 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
9359 ModuleFile &F, const RecordData &Record,
9360 unsigned &Idx, bool Canonicalize) {
9361 unsigned NumTemplateArgs = Record[Idx++];
9362 TemplArgs.reserve(NumTemplateArgs);
9363 while (NumTemplateArgs--)
9364 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
9367 /// Read a UnresolvedSet structure.
9368 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
9369 const RecordData &Record, unsigned &Idx) {
9370 unsigned NumDecls = Record[Idx++];
9371 Set.reserve(getContext(), NumDecls);
9372 while (NumDecls--) {
9373 DeclID ID = ReadDeclID(F, Record, Idx);
9374 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
9375 Set.addLazyDecl(getContext(), ID, AS);
9380 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
9381 const RecordData &Record, unsigned &Idx) {
9382 bool isVirtual = static_cast<bool>(Record[Idx++]);
9383 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
9384 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
9385 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
9386 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
9387 SourceRange Range = ReadSourceRange(F, Record, Idx);
9388 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
9389 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9391 Result.setInheritConstructors(inheritConstructors);
9395 CXXCtorInitializer **
9396 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
9398 ASTContext &Context = getContext();
9399 unsigned NumInitializers = Record[Idx++];
9400 assert(NumInitializers && "wrote ctor initializers but have no inits");
9401 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9402 for (unsigned i = 0; i != NumInitializers; ++i) {
9403 TypeSourceInfo *TInfo = nullptr;
9404 bool IsBaseVirtual = false;
9405 FieldDecl *Member = nullptr;
9406 IndirectFieldDecl *IndirectMember = nullptr;
9408 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
9410 case CTOR_INITIALIZER_BASE:
9411 TInfo = GetTypeSourceInfo(F, Record, Idx);
9412 IsBaseVirtual = Record[Idx++];
9415 case CTOR_INITIALIZER_DELEGATING:
9416 TInfo = GetTypeSourceInfo(F, Record, Idx);
9419 case CTOR_INITIALIZER_MEMBER:
9420 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
9423 case CTOR_INITIALIZER_INDIRECT_MEMBER:
9424 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
9428 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
9429 Expr *Init = ReadExpr(F);
9430 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
9431 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
9433 CXXCtorInitializer *BOMInit;
9434 if (Type == CTOR_INITIALIZER_BASE)
9435 BOMInit = new (Context)
9436 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9437 RParenLoc, MemberOrEllipsisLoc);
9438 else if (Type == CTOR_INITIALIZER_DELEGATING)
9439 BOMInit = new (Context)
9440 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9442 BOMInit = new (Context)
9443 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9446 BOMInit = new (Context)
9447 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9448 LParenLoc, Init, RParenLoc);
9450 if (/*IsWritten*/Record[Idx++]) {
9451 unsigned SourceOrder = Record[Idx++];
9452 BOMInit->setSourceOrder(SourceOrder);
9455 CtorInitializers[i] = BOMInit;
9458 return CtorInitializers;
9461 NestedNameSpecifier *
9462 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
9463 const RecordData &Record, unsigned &Idx) {
9464 ASTContext &Context = getContext();
9465 unsigned N = Record[Idx++];
9466 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
9467 for (unsigned I = 0; I != N; ++I) {
9468 NestedNameSpecifier::SpecifierKind Kind
9469 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9471 case NestedNameSpecifier::Identifier: {
9472 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9473 NNS = NestedNameSpecifier::Create(Context, Prev, II);
9477 case NestedNameSpecifier::Namespace: {
9478 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9479 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
9483 case NestedNameSpecifier::NamespaceAlias: {
9484 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9485 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
9489 case NestedNameSpecifier::TypeSpec:
9490 case NestedNameSpecifier::TypeSpecWithTemplate: {
9491 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9495 bool Template = Record[Idx++];
9496 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9500 case NestedNameSpecifier::Global:
9501 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9502 // No associated value, and there can't be a prefix.
9505 case NestedNameSpecifier::Super: {
9506 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9507 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9516 NestedNameSpecifierLoc
9517 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9519 ASTContext &Context = getContext();
9520 unsigned N = Record[Idx++];
9521 NestedNameSpecifierLocBuilder Builder;
9522 for (unsigned I = 0; I != N; ++I) {
9523 NestedNameSpecifier::SpecifierKind Kind
9524 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9526 case NestedNameSpecifier::Identifier: {
9527 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9528 SourceRange Range = ReadSourceRange(F, Record, Idx);
9529 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9533 case NestedNameSpecifier::Namespace: {
9534 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9535 SourceRange Range = ReadSourceRange(F, Record, Idx);
9536 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9540 case NestedNameSpecifier::NamespaceAlias: {
9541 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9542 SourceRange Range = ReadSourceRange(F, Record, Idx);
9543 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9547 case NestedNameSpecifier::TypeSpec:
9548 case NestedNameSpecifier::TypeSpecWithTemplate: {
9549 bool Template = Record[Idx++];
9550 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9552 return NestedNameSpecifierLoc();
9553 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9555 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9556 Builder.Extend(Context,
9557 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9558 T->getTypeLoc(), ColonColonLoc);
9562 case NestedNameSpecifier::Global: {
9563 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9564 Builder.MakeGlobal(Context, ColonColonLoc);
9568 case NestedNameSpecifier::Super: {
9569 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9570 SourceRange Range = ReadSourceRange(F, Record, Idx);
9571 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9577 return Builder.getWithLocInContext(Context);
9581 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9583 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9584 SourceLocation end = ReadSourceLocation(F, Record, Idx);
9585 return SourceRange(beg, end);
9588 static FixedPointSemantics
9589 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
9591 unsigned Width = Record[Idx++];
9592 unsigned Scale = Record[Idx++];
9593 uint64_t Tmp = Record[Idx++];
9594 bool IsSigned = Tmp & 0x1;
9595 bool IsSaturated = Tmp & 0x2;
9596 bool HasUnsignedPadding = Tmp & 0x4;
9597 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
9598 HasUnsignedPadding);
9601 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
9602 unsigned Kind = Record[Idx++];
9606 case APValue::Indeterminate:
9607 return APValue::IndeterminateValue();
9609 return APValue(ReadAPSInt(Record, Idx));
9610 case APValue::Float: {
9611 const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
9612 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9613 return APValue(ReadAPFloat(Record, FloatSema, Idx));
9615 case APValue::FixedPoint: {
9616 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
9617 return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
9619 case APValue::ComplexInt: {
9620 llvm::APSInt First = ReadAPSInt(Record, Idx);
9621 return APValue(std::move(First), ReadAPSInt(Record, Idx));
9623 case APValue::ComplexFloat: {
9624 const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
9625 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9626 llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
9627 const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
9628 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9629 return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
9631 case APValue::LValue:
9632 case APValue::Vector:
9633 case APValue::Array:
9634 case APValue::Struct:
9635 case APValue::Union:
9636 case APValue::MemberPointer:
9637 case APValue::AddrLabelDiff:
9638 // TODO : Handle all these APValue::ValueKind.
9641 llvm_unreachable("Invalid APValue::ValueKind");
9644 /// Read an integral value
9645 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9646 unsigned BitWidth = Record[Idx++];
9647 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9648 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9653 /// Read a signed integral value
9654 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9655 bool isUnsigned = Record[Idx++];
9656 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9659 /// Read a floating-point value
9660 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9661 const llvm::fltSemantics &Sem,
9663 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9667 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9668 unsigned Len = Record[Idx++];
9669 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9674 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9676 std::string Filename = ReadString(Record, Idx);
9677 ResolveImportedPath(F, Filename);
9681 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9682 const RecordData &Record, unsigned &Idx) {
9683 std::string Filename = ReadString(Record, Idx);
9684 if (!BaseDirectory.empty())
9685 ResolveImportedPath(Filename, BaseDirectory);
9689 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9691 unsigned Major = Record[Idx++];
9692 unsigned Minor = Record[Idx++];
9693 unsigned Subminor = Record[Idx++];
9695 return VersionTuple(Major);
9697 return VersionTuple(Major, Minor - 1);
9698 return VersionTuple(Major, Minor - 1, Subminor - 1);
9701 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9702 const RecordData &Record,
9704 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9705 return CXXTemporary::Create(getContext(), Decl);
9708 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9709 return Diag(CurrentImportLoc, DiagID);
9712 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9713 return Diags.Report(Loc, DiagID);
9716 /// Retrieve the identifier table associated with the
9718 IdentifierTable &ASTReader::getIdentifierTable() {
9719 return PP.getIdentifierTable();
9722 /// Record that the given ID maps to the given switch-case
9724 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9725 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9726 "Already have a SwitchCase with this ID");
9727 (*CurrSwitchCaseStmts)[ID] = SC;
9730 /// Retrieve the switch-case statement with the given ID.
9731 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9732 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9733 return (*CurrSwitchCaseStmts)[ID];
9736 void ASTReader::ClearSwitchCaseIDs() {
9737 CurrSwitchCaseStmts->clear();
9740 void ASTReader::ReadComments() {
9741 ASTContext &Context = getContext();
9742 std::vector<RawComment *> Comments;
9743 for (SmallVectorImpl<std::pair<BitstreamCursor,
9744 serialization::ModuleFile *>>::iterator
9745 I = CommentsCursors.begin(),
9746 E = CommentsCursors.end();
9749 BitstreamCursor &Cursor = I->first;
9750 serialization::ModuleFile &F = *I->second;
9751 SavedStreamPosition SavedPosition(Cursor);
9755 Expected<llvm::BitstreamEntry> MaybeEntry =
9756 Cursor.advanceSkippingSubblocks(
9757 BitstreamCursor::AF_DontPopBlockAtEnd);
9759 Error(MaybeEntry.takeError());
9762 llvm::BitstreamEntry Entry = MaybeEntry.get();
9764 switch (Entry.Kind) {
9765 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9766 case llvm::BitstreamEntry::Error:
9767 Error("malformed block record in AST file");
9769 case llvm::BitstreamEntry::EndBlock:
9771 case llvm::BitstreamEntry::Record:
9772 // The interesting case.
9778 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9779 if (!MaybeComment) {
9780 Error(MaybeComment.takeError());
9783 switch ((CommentRecordTypes)MaybeComment.get()) {
9784 case COMMENTS_RAW_COMMENT: {
9786 SourceRange SR = ReadSourceRange(F, Record, Idx);
9787 RawComment::CommentKind Kind =
9788 (RawComment::CommentKind) Record[Idx++];
9789 bool IsTrailingComment = Record[Idx++];
9790 bool IsAlmostTrailingComment = Record[Idx++];
9791 Comments.push_back(new (Context) RawComment(
9792 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9798 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9799 FileToOffsetToComment;
9800 for (RawComment *C : Comments) {
9801 SourceLocation CommentLoc = C->getBeginLoc();
9802 if (CommentLoc.isValid()) {
9803 std::pair<FileID, unsigned> Loc =
9804 SourceMgr.getDecomposedLoc(CommentLoc);
9805 if (Loc.first.isValid())
9806 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9812 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9813 bool IncludeSystem, bool Complain,
9814 llvm::function_ref<void(const serialization::InputFile &IF,
9815 bool isSystem)> Visitor) {
9816 unsigned NumUserInputs = MF.NumUserInputFiles;
9817 unsigned NumInputs = MF.InputFilesLoaded.size();
9818 assert(NumUserInputs <= NumInputs);
9819 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9820 for (unsigned I = 0; I < N; ++I) {
9821 bool IsSystem = I >= NumUserInputs;
9822 InputFile IF = getInputFile(MF, I+1, Complain);
9823 Visitor(IF, IsSystem);
9827 void ASTReader::visitTopLevelModuleMaps(
9828 serialization::ModuleFile &MF,
9829 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9830 unsigned NumInputs = MF.InputFilesLoaded.size();
9831 for (unsigned I = 0; I < NumInputs; ++I) {
9832 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9833 if (IFI.TopLevelModuleMap)
9834 // FIXME: This unnecessarily re-reads the InputFileInfo.
9835 if (auto *FE = getInputFile(MF, I + 1).getFile())
9840 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9841 // If we know the owning module, use it.
9842 if (Module *M = D->getImportedOwningModule())
9843 return M->getFullModuleName();
9845 // Otherwise, use the name of the top-level module the decl is within.
9846 if (ModuleFile *M = getOwningModuleFile(D))
9847 return M->ModuleName;
9849 // Not from a module.
9853 void ASTReader::finishPendingActions() {
9854 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9855 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9856 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9857 !PendingUpdateRecords.empty()) {
9858 // If any identifiers with corresponding top-level declarations have
9859 // been loaded, load those declarations now.
9860 using TopLevelDeclsMap =
9861 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9862 TopLevelDeclsMap TopLevelDecls;
9864 while (!PendingIdentifierInfos.empty()) {
9865 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9866 SmallVector<uint32_t, 4> DeclIDs =
9867 std::move(PendingIdentifierInfos.back().second);
9868 PendingIdentifierInfos.pop_back();
9870 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9873 // Load each function type that we deferred loading because it was a
9874 // deduced type that might refer to a local type declared within itself.
9875 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9876 auto *FD = PendingFunctionTypes[I].first;
9877 FD->setType(GetType(PendingFunctionTypes[I].second));
9879 // If we gave a function a deduced return type, remember that we need to
9880 // propagate that along the redeclaration chain.
9881 auto *DT = FD->getReturnType()->getContainedDeducedType();
9882 if (DT && DT->isDeduced())
9883 PendingDeducedTypeUpdates.insert(
9884 {FD->getCanonicalDecl(), FD->getReturnType()});
9886 PendingFunctionTypes.clear();
9888 // For each decl chain that we wanted to complete while deserializing, mark
9889 // it as "still needs to be completed".
9890 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9891 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9893 PendingIncompleteDeclChains.clear();
9895 // Load pending declaration chains.
9896 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9897 loadPendingDeclChain(PendingDeclChains[I].first,
9898 PendingDeclChains[I].second);
9899 PendingDeclChains.clear();
9901 // Make the most recent of the top-level declarations visible.
9902 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9903 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9904 IdentifierInfo *II = TLD->first;
9905 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9906 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9910 // Load any pending macro definitions.
9911 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9912 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9913 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9914 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9915 // Initialize the macro history from chained-PCHs ahead of module imports.
9916 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9918 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9919 if (!Info.M->isModule())
9920 resolvePendingMacro(II, Info);
9922 // Handle module imports.
9923 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9925 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9926 if (Info.M->isModule())
9927 resolvePendingMacro(II, Info);
9930 PendingMacroIDs.clear();
9932 // Wire up the DeclContexts for Decls that we delayed setting until
9933 // recursive loading is completed.
9934 while (!PendingDeclContextInfos.empty()) {
9935 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9936 PendingDeclContextInfos.pop_front();
9937 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9938 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9939 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9942 // Perform any pending declaration updates.
9943 while (!PendingUpdateRecords.empty()) {
9944 auto Update = PendingUpdateRecords.pop_back_val();
9945 ReadingKindTracker ReadingKind(Read_Decl, *this);
9946 loadDeclUpdateRecords(Update);
9950 // At this point, all update records for loaded decls are in place, so any
9951 // fake class definitions should have become real.
9952 assert(PendingFakeDefinitionData.empty() &&
9953 "faked up a class definition but never saw the real one");
9955 // If we deserialized any C++ or Objective-C class definitions, any
9956 // Objective-C protocol definitions, or any redeclarable templates, make sure
9957 // that all redeclarations point to the definitions. Note that this can only
9958 // happen now, after the redeclaration chains have been fully wired.
9959 for (Decl *D : PendingDefinitions) {
9960 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9961 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9962 // Make sure that the TagType points at the definition.
9963 const_cast<TagType*>(TagT)->decl = TD;
9966 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9967 for (auto *R = getMostRecentExistingDecl(RD); R;
9968 R = R->getPreviousDecl()) {
9970 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9971 "declaration thinks it's the definition but it isn't");
9972 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9979 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9980 // Make sure that the ObjCInterfaceType points at the definition.
9981 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9984 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9985 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9990 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9991 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9992 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9997 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9998 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9999 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
10001 PendingDefinitions.clear();
10003 // Load the bodies of any functions or methods we've encountered. We do
10004 // this now (delayed) so that we can be sure that the declaration chains
10005 // have been fully wired up (hasBody relies on this).
10006 // FIXME: We shouldn't require complete redeclaration chains here.
10007 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10008 PBEnd = PendingBodies.end();
10009 PB != PBEnd; ++PB) {
10010 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
10011 // For a function defined inline within a class template, force the
10012 // canonical definition to be the one inside the canonical definition of
10013 // the template. This ensures that we instantiate from a correct view
10014 // of the template.
10016 // Sadly we can't do this more generally: we can't be sure that all
10017 // copies of an arbitrary class definition will have the same members
10018 // defined (eg, some member functions may not be instantiated, and some
10019 // special members may or may not have been implicitly defined).
10020 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
10021 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
10024 // FIXME: Check for =delete/=default?
10025 // FIXME: Complain about ODR violations here?
10026 const FunctionDecl *Defn = nullptr;
10027 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
10028 FD->setLazyBody(PB->second);
10030 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10031 mergeDefinitionVisibility(NonConstDefn, FD);
10033 if (!FD->isLateTemplateParsed() &&
10034 !NonConstDefn->isLateTemplateParsed() &&
10035 FD->getODRHash() != NonConstDefn->getODRHash()) {
10036 if (!isa<CXXMethodDecl>(FD)) {
10037 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
10038 } else if (FD->getLexicalParent()->isFileContext() &&
10039 NonConstDefn->getLexicalParent()->isFileContext()) {
10040 // Only diagnose out-of-line method definitions. If they are
10041 // in class definitions, then an error will be generated when
10042 // processing the class bodies.
10043 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
10050 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
10051 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10052 MD->setLazyBody(PB->second);
10054 PendingBodies.clear();
10056 // Do some cleanup.
10057 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10058 getContext().deduplicateMergedDefinitonsFor(ND);
10059 PendingMergedDefinitionsToDeduplicate.clear();
10062 void ASTReader::diagnoseOdrViolations() {
10063 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10064 PendingFunctionOdrMergeFailures.empty() &&
10065 PendingEnumOdrMergeFailures.empty())
10068 // Trigger the import of the full definition of each class that had any
10069 // odr-merging problems, so we can produce better diagnostics for them.
10070 // These updates may in turn find and diagnose some ODR failures, so take
10071 // ownership of the set first.
10072 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10073 PendingOdrMergeFailures.clear();
10074 for (auto &Merge : OdrMergeFailures) {
10075 Merge.first->buildLookup();
10076 Merge.first->decls_begin();
10077 Merge.first->bases_begin();
10078 Merge.first->vbases_begin();
10079 for (auto &RecordPair : Merge.second) {
10080 auto *RD = RecordPair.first;
10083 RD->vbases_begin();
10087 // Trigger the import of functions.
10088 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10089 PendingFunctionOdrMergeFailures.clear();
10090 for (auto &Merge : FunctionOdrMergeFailures) {
10091 Merge.first->buildLookup();
10092 Merge.first->decls_begin();
10093 Merge.first->getBody();
10094 for (auto &FD : Merge.second) {
10101 // Trigger the import of enums.
10102 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10103 PendingEnumOdrMergeFailures.clear();
10104 for (auto &Merge : EnumOdrMergeFailures) {
10105 Merge.first->decls_begin();
10106 for (auto &Enum : Merge.second) {
10107 Enum->decls_begin();
10111 // For each declaration from a merged context, check that the canonical
10112 // definition of that context also contains a declaration of the same
10115 // Caution: this loop does things that might invalidate iterators into
10116 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10117 while (!PendingOdrMergeChecks.empty()) {
10118 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10120 // FIXME: Skip over implicit declarations for now. This matters for things
10121 // like implicitly-declared special member functions. This isn't entirely
10122 // correct; we can end up with multiple unmerged declarations of the same
10123 // implicit entity.
10124 if (D->isImplicit())
10127 DeclContext *CanonDef = D->getDeclContext();
10129 bool Found = false;
10130 const Decl *DCanon = D->getCanonicalDecl();
10132 for (auto RI : D->redecls()) {
10133 if (RI->getLexicalDeclContext() == CanonDef) {
10141 // Quick check failed, time to do the slow thing. Note, we can't just
10142 // look up the name of D in CanonDef here, because the member that is
10143 // in CanonDef might not be found by name lookup (it might have been
10144 // replaced by a more recent declaration in the lookup table), and we
10145 // can't necessarily find it in the redeclaration chain because it might
10146 // be merely mergeable, not redeclarable.
10147 llvm::SmallVector<const NamedDecl*, 4> Candidates;
10148 for (auto *CanonMember : CanonDef->decls()) {
10149 if (CanonMember->getCanonicalDecl() == DCanon) {
10150 // This can happen if the declaration is merely mergeable and not
10151 // actually redeclarable (we looked for redeclarations earlier).
10153 // FIXME: We should be able to detect this more efficiently, without
10154 // pulling in all of the members of CanonDef.
10158 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
10159 if (ND->getDeclName() == D->getDeclName())
10160 Candidates.push_back(ND);
10164 // The AST doesn't like TagDecls becoming invalid after they've been
10165 // completed. We only really need to mark FieldDecls as invalid here.
10166 if (!isa<TagDecl>(D))
10167 D->setInvalidDecl();
10169 // Ensure we don't accidentally recursively enter deserialization while
10170 // we're producing our diagnostic.
10171 Deserializing RecursionGuard(this);
10173 std::string CanonDefModule =
10174 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
10175 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
10176 << D << getOwningModuleNameForDiagnostic(D)
10177 << CanonDef << CanonDefModule.empty() << CanonDefModule;
10179 if (Candidates.empty())
10180 Diag(cast<Decl>(CanonDef)->getLocation(),
10181 diag::note_module_odr_violation_no_possible_decls) << D;
10183 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10184 Diag(Candidates[I]->getLocation(),
10185 diag::note_module_odr_violation_possible_decl)
10189 DiagnosedOdrMergeFailures.insert(CanonDef);
10193 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
10194 EnumOdrMergeFailures.empty())
10197 // Ensure we don't accidentally recursively enter deserialization while
10198 // we're producing our diagnostics.
10199 Deserializing RecursionGuard(this);
10201 // Common code for hashing helpers.
10203 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
10205 Hash.AddQualType(Ty);
10206 return Hash.CalculateHash();
10209 auto ComputeODRHash = [&Hash](const Stmt *S) {
10213 return Hash.CalculateHash();
10216 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
10219 Hash.AddSubDecl(D);
10220 return Hash.CalculateHash();
10223 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
10225 Hash.AddTemplateArgument(TA);
10226 return Hash.CalculateHash();
10229 auto ComputeTemplateParameterListODRHash =
10230 [&Hash](const TemplateParameterList *TPL) {
10233 Hash.AddTemplateParameterList(TPL);
10234 return Hash.CalculateHash();
10237 // Issue any pending ODR-failure diagnostics.
10238 for (auto &Merge : OdrMergeFailures) {
10239 // If we've already pointed out a specific problem with this class, don't
10240 // bother issuing a general "something's different" diagnostic.
10241 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10244 bool Diagnosed = false;
10245 CXXRecordDecl *FirstRecord = Merge.first;
10246 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10247 for (auto &RecordPair : Merge.second) {
10248 CXXRecordDecl *SecondRecord = RecordPair.first;
10249 // Multiple different declarations got merged together; tell the user
10250 // where they came from.
10251 if (FirstRecord == SecondRecord)
10254 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10256 auto *FirstDD = FirstRecord->DefinitionData;
10257 auto *SecondDD = RecordPair.second;
10259 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10261 // Diagnostics from DefinitionData are emitted here.
10262 if (FirstDD != SecondDD) {
10263 enum ODRDefinitionDataDifference {
10270 auto ODRDiagError = [FirstRecord, &FirstModule,
10271 this](SourceLocation Loc, SourceRange Range,
10272 ODRDefinitionDataDifference DiffType) {
10273 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10274 << FirstRecord << FirstModule.empty() << FirstModule << Range
10277 auto ODRDiagNote = [&SecondModule,
10278 this](SourceLocation Loc, SourceRange Range,
10279 ODRDefinitionDataDifference DiffType) {
10280 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10281 << SecondModule << Range << DiffType;
10284 unsigned FirstNumBases = FirstDD->NumBases;
10285 unsigned FirstNumVBases = FirstDD->NumVBases;
10286 unsigned SecondNumBases = SecondDD->NumBases;
10287 unsigned SecondNumVBases = SecondDD->NumVBases;
10289 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10290 unsigned NumBases = DD->NumBases;
10291 if (NumBases == 0) return SourceRange();
10292 auto bases = DD->bases();
10293 return SourceRange(bases[0].getBeginLoc(),
10294 bases[NumBases - 1].getEndLoc());
10297 if (FirstNumBases != SecondNumBases) {
10298 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10301 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10308 if (FirstNumVBases != SecondNumVBases) {
10309 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10312 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10314 << SecondNumVBases;
10319 auto FirstBases = FirstDD->bases();
10320 auto SecondBases = SecondDD->bases();
10322 for (i = 0; i < FirstNumBases; ++i) {
10323 auto FirstBase = FirstBases[i];
10324 auto SecondBase = SecondBases[i];
10325 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10326 ComputeQualTypeODRHash(SecondBase.getType())) {
10327 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10329 << (i + 1) << FirstBase.getType();
10330 ODRDiagNote(SecondRecord->getLocation(),
10331 SecondBase.getSourceRange(), BaseType)
10332 << (i + 1) << SecondBase.getType();
10336 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10337 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10339 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10340 ODRDiagNote(SecondRecord->getLocation(),
10341 SecondBase.getSourceRange(), BaseVirtual)
10342 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10346 if (FirstBase.getAccessSpecifierAsWritten() !=
10347 SecondBase.getAccessSpecifierAsWritten()) {
10348 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10350 << (i + 1) << FirstBase.getType()
10351 << (int)FirstBase.getAccessSpecifierAsWritten();
10352 ODRDiagNote(SecondRecord->getLocation(),
10353 SecondBase.getSourceRange(), BaseAccess)
10354 << (i + 1) << SecondBase.getType()
10355 << (int)SecondBase.getAccessSpecifierAsWritten();
10360 if (i != FirstNumBases) {
10366 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10368 const ClassTemplateDecl *FirstTemplate =
10369 FirstRecord->getDescribedClassTemplate();
10370 const ClassTemplateDecl *SecondTemplate =
10371 SecondRecord->getDescribedClassTemplate();
10373 assert(!FirstTemplate == !SecondTemplate &&
10374 "Both pointers should be null or non-null");
10376 enum ODRTemplateDifference {
10379 ParamSingleDefaultArgument,
10380 ParamDifferentDefaultArgument,
10383 if (FirstTemplate && SecondTemplate) {
10384 DeclHashes FirstTemplateHashes;
10385 DeclHashes SecondTemplateHashes;
10387 auto PopulateTemplateParameterHashs =
10388 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10389 const ClassTemplateDecl *TD) {
10390 for (auto *D : TD->getTemplateParameters()->asArray()) {
10391 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10395 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10396 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10398 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10399 "Number of template parameters should be equal.");
10401 auto FirstIt = FirstTemplateHashes.begin();
10402 auto FirstEnd = FirstTemplateHashes.end();
10403 auto SecondIt = SecondTemplateHashes.begin();
10404 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10405 if (FirstIt->second == SecondIt->second)
10408 auto ODRDiagError = [FirstRecord, &FirstModule,
10409 this](SourceLocation Loc, SourceRange Range,
10410 ODRTemplateDifference DiffType) {
10411 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10412 << FirstRecord << FirstModule.empty() << FirstModule << Range
10415 auto ODRDiagNote = [&SecondModule,
10416 this](SourceLocation Loc, SourceRange Range,
10417 ODRTemplateDifference DiffType) {
10418 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10419 << SecondModule << Range << DiffType;
10422 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10423 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10425 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10426 "Parameter Decl's should be the same kind.");
10428 DeclarationName FirstName = FirstDecl->getDeclName();
10429 DeclarationName SecondName = SecondDecl->getDeclName();
10431 if (FirstName != SecondName) {
10432 const bool FirstNameEmpty =
10433 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10434 const bool SecondNameEmpty =
10435 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10436 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10437 "Both template parameters cannot be unnamed.");
10438 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10439 FirstNameEmpty ? ParamEmptyName : ParamName)
10441 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10442 SecondNameEmpty ? ParamEmptyName : ParamName)
10447 switch (FirstDecl->getKind()) {
10449 llvm_unreachable("Invalid template parameter type.");
10450 case Decl::TemplateTypeParm: {
10451 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10452 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10453 const bool HasFirstDefaultArgument =
10454 FirstParam->hasDefaultArgument() &&
10455 !FirstParam->defaultArgumentWasInherited();
10456 const bool HasSecondDefaultArgument =
10457 SecondParam->hasDefaultArgument() &&
10458 !SecondParam->defaultArgumentWasInherited();
10460 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10461 ODRDiagError(FirstDecl->getLocation(),
10462 FirstDecl->getSourceRange(),
10463 ParamSingleDefaultArgument)
10464 << HasFirstDefaultArgument;
10465 ODRDiagNote(SecondDecl->getLocation(),
10466 SecondDecl->getSourceRange(),
10467 ParamSingleDefaultArgument)
10468 << HasSecondDefaultArgument;
10472 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10473 "Expecting default arguments.");
10475 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10476 ParamDifferentDefaultArgument);
10477 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10478 ParamDifferentDefaultArgument);
10482 case Decl::NonTypeTemplateParm: {
10483 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10484 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10485 const bool HasFirstDefaultArgument =
10486 FirstParam->hasDefaultArgument() &&
10487 !FirstParam->defaultArgumentWasInherited();
10488 const bool HasSecondDefaultArgument =
10489 SecondParam->hasDefaultArgument() &&
10490 !SecondParam->defaultArgumentWasInherited();
10492 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10493 ODRDiagError(FirstDecl->getLocation(),
10494 FirstDecl->getSourceRange(),
10495 ParamSingleDefaultArgument)
10496 << HasFirstDefaultArgument;
10497 ODRDiagNote(SecondDecl->getLocation(),
10498 SecondDecl->getSourceRange(),
10499 ParamSingleDefaultArgument)
10500 << HasSecondDefaultArgument;
10504 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10505 "Expecting default arguments.");
10507 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10508 ParamDifferentDefaultArgument);
10509 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10510 ParamDifferentDefaultArgument);
10514 case Decl::TemplateTemplateParm: {
10515 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10516 const auto *SecondParam =
10517 cast<TemplateTemplateParmDecl>(SecondDecl);
10518 const bool HasFirstDefaultArgument =
10519 FirstParam->hasDefaultArgument() &&
10520 !FirstParam->defaultArgumentWasInherited();
10521 const bool HasSecondDefaultArgument =
10522 SecondParam->hasDefaultArgument() &&
10523 !SecondParam->defaultArgumentWasInherited();
10525 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10526 ODRDiagError(FirstDecl->getLocation(),
10527 FirstDecl->getSourceRange(),
10528 ParamSingleDefaultArgument)
10529 << HasFirstDefaultArgument;
10530 ODRDiagNote(SecondDecl->getLocation(),
10531 SecondDecl->getSourceRange(),
10532 ParamSingleDefaultArgument)
10533 << HasSecondDefaultArgument;
10537 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10538 "Expecting default arguments.");
10540 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10541 ParamDifferentDefaultArgument);
10542 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10543 ParamDifferentDefaultArgument);
10552 if (FirstIt != FirstEnd) {
10558 DeclHashes FirstHashes;
10559 DeclHashes SecondHashes;
10561 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10562 DeclHashes &Hashes, CXXRecordDecl *Record) {
10563 for (auto *D : Record->decls()) {
10564 // Due to decl merging, the first CXXRecordDecl is the parent of
10565 // Decls in both records.
10566 if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10568 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10571 PopulateHashes(FirstHashes, FirstRecord);
10572 PopulateHashes(SecondHashes, SecondRecord);
10574 // Used with err_module_odr_violation_mismatch_decl and
10575 // note_module_odr_violation_mismatch_decl
10576 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10591 } FirstDiffType = Other,
10592 SecondDiffType = Other;
10594 auto DifferenceSelector = [](Decl *D) {
10595 assert(D && "valid Decl required");
10596 switch (D->getKind()) {
10599 case Decl::AccessSpec:
10600 switch (D->getAccess()) {
10602 return PublicSpecifer;
10604 return PrivateSpecifer;
10606 return ProtectedSpecifer;
10610 llvm_unreachable("Invalid access specifier");
10611 case Decl::StaticAssert:
10612 return StaticAssert;
10615 case Decl::CXXMethod:
10616 case Decl::CXXConstructor:
10617 case Decl::CXXDestructor:
10619 case Decl::TypeAlias:
10621 case Decl::Typedef:
10627 case Decl::FunctionTemplate:
10628 return FunctionTemplate;
10632 Decl *FirstDecl = nullptr;
10633 Decl *SecondDecl = nullptr;
10634 auto FirstIt = FirstHashes.begin();
10635 auto SecondIt = SecondHashes.begin();
10637 // If there is a diagnoseable difference, FirstDiffType and
10638 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10639 // filled in if not EndOfClass.
10640 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10641 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10642 FirstIt->second == SecondIt->second) {
10648 FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10649 SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10651 FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10653 SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10658 if (FirstDiffType == Other || SecondDiffType == Other) {
10659 // Reaching this point means an unexpected Decl was encountered
10660 // or no difference was detected. This causes a generic error
10661 // message to be emitted.
10662 Diag(FirstRecord->getLocation(),
10663 diag::err_module_odr_violation_different_definitions)
10664 << FirstRecord << FirstModule.empty() << FirstModule;
10667 Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10668 << FirstRecord << FirstDecl->getSourceRange();
10671 Diag(SecondRecord->getLocation(),
10672 diag::note_module_odr_violation_different_definitions)
10676 Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10677 << SecondDecl->getSourceRange();
10684 if (FirstDiffType != SecondDiffType) {
10685 SourceLocation FirstLoc;
10686 SourceRange FirstRange;
10687 if (FirstDiffType == EndOfClass) {
10688 FirstLoc = FirstRecord->getBraceRange().getEnd();
10690 FirstLoc = FirstIt->first->getLocation();
10691 FirstRange = FirstIt->first->getSourceRange();
10693 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10694 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10697 SourceLocation SecondLoc;
10698 SourceRange SecondRange;
10699 if (SecondDiffType == EndOfClass) {
10700 SecondLoc = SecondRecord->getBraceRange().getEnd();
10702 SecondLoc = SecondDecl->getLocation();
10703 SecondRange = SecondDecl->getSourceRange();
10705 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10706 << SecondModule << SecondRange << SecondDiffType;
10711 assert(FirstDiffType == SecondDiffType);
10713 // Used with err_module_odr_violation_mismatch_decl_diff and
10714 // note_module_odr_violation_mismatch_decl_diff
10715 enum ODRDeclDifference {
10716 StaticAssertCondition,
10717 StaticAssertMessage,
10718 StaticAssertOnlyMessage,
10721 FieldSingleBitField,
10722 FieldDifferentWidthBitField,
10723 FieldSingleMutable,
10724 FieldSingleInitializer,
10725 FieldDifferentInitializers,
10734 MethodNumberParameters,
10735 MethodParameterType,
10736 MethodParameterName,
10737 MethodParameterSingleDefaultArgument,
10738 MethodParameterDifferentDefaultArgument,
10739 MethodNoTemplateArguments,
10740 MethodDifferentNumberTemplateArguments,
10741 MethodDifferentTemplateArgument,
10743 MethodDifferentBody,
10748 VarSingleInitializer,
10749 VarDifferentInitializer,
10751 FriendTypeFunction,
10754 FunctionTemplateDifferentNumberParameters,
10755 FunctionTemplateParameterDifferentKind,
10756 FunctionTemplateParameterName,
10757 FunctionTemplateParameterSingleDefaultArgument,
10758 FunctionTemplateParameterDifferentDefaultArgument,
10759 FunctionTemplateParameterDifferentType,
10760 FunctionTemplatePackParameter,
10763 // These lambdas have the common portions of the ODR diagnostics. This
10764 // has the same return as Diag(), so addition parameters can be passed
10765 // in with operator<<
10766 auto ODRDiagError = [FirstRecord, &FirstModule, this](
10767 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10768 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10769 << FirstRecord << FirstModule.empty() << FirstModule << Range
10772 auto ODRDiagNote = [&SecondModule, this](
10773 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10774 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10775 << SecondModule << Range << DiffType;
10778 switch (FirstDiffType) {
10781 case PublicSpecifer:
10782 case PrivateSpecifer:
10783 case ProtectedSpecifer:
10784 llvm_unreachable("Invalid diff type");
10786 case StaticAssert: {
10787 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10788 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10790 Expr *FirstExpr = FirstSA->getAssertExpr();
10791 Expr *SecondExpr = SecondSA->getAssertExpr();
10792 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10793 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10794 if (FirstODRHash != SecondODRHash) {
10795 ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10796 StaticAssertCondition);
10797 ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10798 StaticAssertCondition);
10803 StringLiteral *FirstStr = FirstSA->getMessage();
10804 StringLiteral *SecondStr = SecondSA->getMessage();
10805 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10806 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10807 SourceLocation FirstLoc, SecondLoc;
10808 SourceRange FirstRange, SecondRange;
10810 FirstLoc = FirstStr->getBeginLoc();
10811 FirstRange = FirstStr->getSourceRange();
10813 FirstLoc = FirstSA->getBeginLoc();
10814 FirstRange = FirstSA->getSourceRange();
10817 SecondLoc = SecondStr->getBeginLoc();
10818 SecondRange = SecondStr->getSourceRange();
10820 SecondLoc = SecondSA->getBeginLoc();
10821 SecondRange = SecondSA->getSourceRange();
10823 ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10824 << (FirstStr == nullptr);
10825 ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10826 << (SecondStr == nullptr);
10831 if (FirstStr && SecondStr &&
10832 FirstStr->getString() != SecondStr->getString()) {
10833 ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10834 StaticAssertMessage);
10835 ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10836 StaticAssertMessage);
10843 FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10844 FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10845 IdentifierInfo *FirstII = FirstField->getIdentifier();
10846 IdentifierInfo *SecondII = SecondField->getIdentifier();
10847 if (FirstII->getName() != SecondII->getName()) {
10848 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10851 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10859 assert(getContext().hasSameType(FirstField->getType(),
10860 SecondField->getType()));
10862 QualType FirstType = FirstField->getType();
10863 QualType SecondType = SecondField->getType();
10864 if (ComputeQualTypeODRHash(FirstType) !=
10865 ComputeQualTypeODRHash(SecondType)) {
10866 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10868 << FirstII << FirstType;
10869 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10871 << SecondII << SecondType;
10877 const bool IsFirstBitField = FirstField->isBitField();
10878 const bool IsSecondBitField = SecondField->isBitField();
10879 if (IsFirstBitField != IsSecondBitField) {
10880 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10881 FieldSingleBitField)
10882 << FirstII << IsFirstBitField;
10883 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10884 FieldSingleBitField)
10885 << SecondII << IsSecondBitField;
10890 if (IsFirstBitField && IsSecondBitField) {
10891 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10892 FieldDifferentWidthBitField)
10893 << FirstII << FirstField->getBitWidth()->getSourceRange();
10894 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10895 FieldDifferentWidthBitField)
10896 << SecondII << SecondField->getBitWidth()->getSourceRange();
10901 const bool IsFirstMutable = FirstField->isMutable();
10902 const bool IsSecondMutable = SecondField->isMutable();
10903 if (IsFirstMutable != IsSecondMutable) {
10904 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10905 FieldSingleMutable)
10906 << FirstII << IsFirstMutable;
10907 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10908 FieldSingleMutable)
10909 << SecondII << IsSecondMutable;
10914 const Expr *FirstInitializer = FirstField->getInClassInitializer();
10915 const Expr *SecondInitializer = SecondField->getInClassInitializer();
10916 if ((!FirstInitializer && SecondInitializer) ||
10917 (FirstInitializer && !SecondInitializer)) {
10918 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10919 FieldSingleInitializer)
10920 << FirstII << (FirstInitializer != nullptr);
10921 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10922 FieldSingleInitializer)
10923 << SecondII << (SecondInitializer != nullptr);
10928 if (FirstInitializer && SecondInitializer) {
10929 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10930 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10931 if (FirstInitHash != SecondInitHash) {
10932 ODRDiagError(FirstField->getLocation(),
10933 FirstField->getSourceRange(),
10934 FieldDifferentInitializers)
10935 << FirstII << FirstInitializer->getSourceRange();
10936 ODRDiagNote(SecondField->getLocation(),
10937 SecondField->getSourceRange(),
10938 FieldDifferentInitializers)
10939 << SecondII << SecondInitializer->getSourceRange();
10954 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10955 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10956 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10959 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10960 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10961 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10962 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10963 auto FirstName = FirstMethod->getDeclName();
10964 auto SecondName = SecondMethod->getDeclName();
10965 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10966 ODRDiagError(FirstMethod->getLocation(),
10967 FirstMethod->getSourceRange(), MethodName)
10968 << FirstMethodType << FirstName;
10969 ODRDiagNote(SecondMethod->getLocation(),
10970 SecondMethod->getSourceRange(), MethodName)
10971 << SecondMethodType << SecondName;
10977 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10978 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10979 if (FirstDeleted != SecondDeleted) {
10980 ODRDiagError(FirstMethod->getLocation(),
10981 FirstMethod->getSourceRange(), MethodDeleted)
10982 << FirstMethodType << FirstName << FirstDeleted;
10984 ODRDiagNote(SecondMethod->getLocation(),
10985 SecondMethod->getSourceRange(), MethodDeleted)
10986 << SecondMethodType << SecondName << SecondDeleted;
10991 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10992 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10993 if (FirstDefaulted != SecondDefaulted) {
10994 ODRDiagError(FirstMethod->getLocation(),
10995 FirstMethod->getSourceRange(), MethodDefaulted)
10996 << FirstMethodType << FirstName << FirstDefaulted;
10998 ODRDiagNote(SecondMethod->getLocation(),
10999 SecondMethod->getSourceRange(), MethodDefaulted)
11000 << SecondMethodType << SecondName << SecondDefaulted;
11005 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
11006 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
11007 const bool FirstPure = FirstMethod->isPure();
11008 const bool SecondPure = SecondMethod->isPure();
11009 if ((FirstVirtual || SecondVirtual) &&
11010 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
11011 ODRDiagError(FirstMethod->getLocation(),
11012 FirstMethod->getSourceRange(), MethodVirtual)
11013 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
11014 ODRDiagNote(SecondMethod->getLocation(),
11015 SecondMethod->getSourceRange(), MethodVirtual)
11016 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
11021 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
11022 // FirstDecl is the canonical Decl of SecondDecl, so the storage
11023 // class needs to be checked instead.
11024 const auto FirstStorage = FirstMethod->getStorageClass();
11025 const auto SecondStorage = SecondMethod->getStorageClass();
11026 const bool FirstStatic = FirstStorage == SC_Static;
11027 const bool SecondStatic = SecondStorage == SC_Static;
11028 if (FirstStatic != SecondStatic) {
11029 ODRDiagError(FirstMethod->getLocation(),
11030 FirstMethod->getSourceRange(), MethodStatic)
11031 << FirstMethodType << FirstName << FirstStatic;
11032 ODRDiagNote(SecondMethod->getLocation(),
11033 SecondMethod->getSourceRange(), MethodStatic)
11034 << SecondMethodType << SecondName << SecondStatic;
11039 const bool FirstVolatile = FirstMethod->isVolatile();
11040 const bool SecondVolatile = SecondMethod->isVolatile();
11041 if (FirstVolatile != SecondVolatile) {
11042 ODRDiagError(FirstMethod->getLocation(),
11043 FirstMethod->getSourceRange(), MethodVolatile)
11044 << FirstMethodType << FirstName << FirstVolatile;
11045 ODRDiagNote(SecondMethod->getLocation(),
11046 SecondMethod->getSourceRange(), MethodVolatile)
11047 << SecondMethodType << SecondName << SecondVolatile;
11052 const bool FirstConst = FirstMethod->isConst();
11053 const bool SecondConst = SecondMethod->isConst();
11054 if (FirstConst != SecondConst) {
11055 ODRDiagError(FirstMethod->getLocation(),
11056 FirstMethod->getSourceRange(), MethodConst)
11057 << FirstMethodType << FirstName << FirstConst;
11058 ODRDiagNote(SecondMethod->getLocation(),
11059 SecondMethod->getSourceRange(), MethodConst)
11060 << SecondMethodType << SecondName << SecondConst;
11065 const bool FirstInline = FirstMethod->isInlineSpecified();
11066 const bool SecondInline = SecondMethod->isInlineSpecified();
11067 if (FirstInline != SecondInline) {
11068 ODRDiagError(FirstMethod->getLocation(),
11069 FirstMethod->getSourceRange(), MethodInline)
11070 << FirstMethodType << FirstName << FirstInline;
11071 ODRDiagNote(SecondMethod->getLocation(),
11072 SecondMethod->getSourceRange(), MethodInline)
11073 << SecondMethodType << SecondName << SecondInline;
11078 const unsigned FirstNumParameters = FirstMethod->param_size();
11079 const unsigned SecondNumParameters = SecondMethod->param_size();
11080 if (FirstNumParameters != SecondNumParameters) {
11081 ODRDiagError(FirstMethod->getLocation(),
11082 FirstMethod->getSourceRange(), MethodNumberParameters)
11083 << FirstMethodType << FirstName << FirstNumParameters;
11084 ODRDiagNote(SecondMethod->getLocation(),
11085 SecondMethod->getSourceRange(), MethodNumberParameters)
11086 << SecondMethodType << SecondName << SecondNumParameters;
11091 // Need this status boolean to know when break out of the switch.
11092 bool ParameterMismatch = false;
11093 for (unsigned I = 0; I < FirstNumParameters; ++I) {
11094 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
11095 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
11097 QualType FirstParamType = FirstParam->getType();
11098 QualType SecondParamType = SecondParam->getType();
11099 if (FirstParamType != SecondParamType &&
11100 ComputeQualTypeODRHash(FirstParamType) !=
11101 ComputeQualTypeODRHash(SecondParamType)) {
11102 if (const DecayedType *ParamDecayedType =
11103 FirstParamType->getAs<DecayedType>()) {
11104 ODRDiagError(FirstMethod->getLocation(),
11105 FirstMethod->getSourceRange(), MethodParameterType)
11106 << FirstMethodType << FirstName << (I + 1) << FirstParamType
11107 << true << ParamDecayedType->getOriginalType();
11109 ODRDiagError(FirstMethod->getLocation(),
11110 FirstMethod->getSourceRange(), MethodParameterType)
11111 << FirstMethodType << FirstName << (I + 1) << FirstParamType
11115 if (const DecayedType *ParamDecayedType =
11116 SecondParamType->getAs<DecayedType>()) {
11117 ODRDiagNote(SecondMethod->getLocation(),
11118 SecondMethod->getSourceRange(), MethodParameterType)
11119 << SecondMethodType << SecondName << (I + 1)
11120 << SecondParamType << true
11121 << ParamDecayedType->getOriginalType();
11123 ODRDiagNote(SecondMethod->getLocation(),
11124 SecondMethod->getSourceRange(), MethodParameterType)
11125 << SecondMethodType << SecondName << (I + 1)
11126 << SecondParamType << false;
11128 ParameterMismatch = true;
11132 DeclarationName FirstParamName = FirstParam->getDeclName();
11133 DeclarationName SecondParamName = SecondParam->getDeclName();
11134 if (FirstParamName != SecondParamName) {
11135 ODRDiagError(FirstMethod->getLocation(),
11136 FirstMethod->getSourceRange(), MethodParameterName)
11137 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
11138 ODRDiagNote(SecondMethod->getLocation(),
11139 SecondMethod->getSourceRange(), MethodParameterName)
11140 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
11141 ParameterMismatch = true;
11145 const Expr *FirstInit = FirstParam->getInit();
11146 const Expr *SecondInit = SecondParam->getInit();
11147 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11148 ODRDiagError(FirstMethod->getLocation(),
11149 FirstMethod->getSourceRange(),
11150 MethodParameterSingleDefaultArgument)
11151 << FirstMethodType << FirstName << (I + 1)
11152 << (FirstInit == nullptr)
11153 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11154 ODRDiagNote(SecondMethod->getLocation(),
11155 SecondMethod->getSourceRange(),
11156 MethodParameterSingleDefaultArgument)
11157 << SecondMethodType << SecondName << (I + 1)
11158 << (SecondInit == nullptr)
11159 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11160 ParameterMismatch = true;
11164 if (FirstInit && SecondInit &&
11165 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11166 ODRDiagError(FirstMethod->getLocation(),
11167 FirstMethod->getSourceRange(),
11168 MethodParameterDifferentDefaultArgument)
11169 << FirstMethodType << FirstName << (I + 1)
11170 << FirstInit->getSourceRange();
11171 ODRDiagNote(SecondMethod->getLocation(),
11172 SecondMethod->getSourceRange(),
11173 MethodParameterDifferentDefaultArgument)
11174 << SecondMethodType << SecondName << (I + 1)
11175 << SecondInit->getSourceRange();
11176 ParameterMismatch = true;
11182 if (ParameterMismatch) {
11187 const auto *FirstTemplateArgs =
11188 FirstMethod->getTemplateSpecializationArgs();
11189 const auto *SecondTemplateArgs =
11190 SecondMethod->getTemplateSpecializationArgs();
11192 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
11193 (!FirstTemplateArgs && SecondTemplateArgs)) {
11194 ODRDiagError(FirstMethod->getLocation(),
11195 FirstMethod->getSourceRange(), MethodNoTemplateArguments)
11196 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
11197 ODRDiagNote(SecondMethod->getLocation(),
11198 SecondMethod->getSourceRange(), MethodNoTemplateArguments)
11199 << SecondMethodType << SecondName
11200 << (SecondTemplateArgs != nullptr);
11206 if (FirstTemplateArgs && SecondTemplateArgs) {
11207 // Remove pack expansions from argument list.
11208 auto ExpandTemplateArgumentList =
11209 [](const TemplateArgumentList *TAL) {
11210 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
11211 for (const TemplateArgument &TA : TAL->asArray()) {
11212 if (TA.getKind() != TemplateArgument::Pack) {
11213 ExpandedList.push_back(&TA);
11216 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
11217 ExpandedList.push_back(&PackTA);
11220 return ExpandedList;
11222 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
11223 ExpandTemplateArgumentList(FirstTemplateArgs);
11224 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
11225 ExpandTemplateArgumentList(SecondTemplateArgs);
11227 if (FirstExpandedList.size() != SecondExpandedList.size()) {
11228 ODRDiagError(FirstMethod->getLocation(),
11229 FirstMethod->getSourceRange(),
11230 MethodDifferentNumberTemplateArguments)
11231 << FirstMethodType << FirstName
11232 << (unsigned)FirstExpandedList.size();
11233 ODRDiagNote(SecondMethod->getLocation(),
11234 SecondMethod->getSourceRange(),
11235 MethodDifferentNumberTemplateArguments)
11236 << SecondMethodType << SecondName
11237 << (unsigned)SecondExpandedList.size();
11243 bool TemplateArgumentMismatch = false;
11244 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
11245 const TemplateArgument &FirstTA = *FirstExpandedList[i],
11246 &SecondTA = *SecondExpandedList[i];
11247 if (ComputeTemplateArgumentODRHash(FirstTA) ==
11248 ComputeTemplateArgumentODRHash(SecondTA)) {
11252 ODRDiagError(FirstMethod->getLocation(),
11253 FirstMethod->getSourceRange(),
11254 MethodDifferentTemplateArgument)
11255 << FirstMethodType << FirstName << FirstTA << i + 1;
11256 ODRDiagNote(SecondMethod->getLocation(),
11257 SecondMethod->getSourceRange(),
11258 MethodDifferentTemplateArgument)
11259 << SecondMethodType << SecondName << SecondTA << i + 1;
11261 TemplateArgumentMismatch = true;
11265 if (TemplateArgumentMismatch) {
11271 // Compute the hash of the method as if it has no body.
11272 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
11274 Hash.AddFunctionDecl(D, true /*SkipBody*/);
11275 return Hash.CalculateHash();
11278 // Compare the hash generated to the hash stored. A difference means
11279 // that a body was present in the original source. Due to merging,
11280 // the stardard way of detecting a body will not work.
11281 const bool HasFirstBody =
11282 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
11283 const bool HasSecondBody =
11284 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
11286 if (HasFirstBody != HasSecondBody) {
11287 ODRDiagError(FirstMethod->getLocation(),
11288 FirstMethod->getSourceRange(), MethodSingleBody)
11289 << FirstMethodType << FirstName << HasFirstBody;
11290 ODRDiagNote(SecondMethod->getLocation(),
11291 SecondMethod->getSourceRange(), MethodSingleBody)
11292 << SecondMethodType << SecondName << HasSecondBody;
11297 if (HasFirstBody && HasSecondBody) {
11298 ODRDiagError(FirstMethod->getLocation(),
11299 FirstMethod->getSourceRange(), MethodDifferentBody)
11300 << FirstMethodType << FirstName;
11301 ODRDiagNote(SecondMethod->getLocation(),
11302 SecondMethod->getSourceRange(), MethodDifferentBody)
11303 << SecondMethodType << SecondName;
11312 TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
11313 TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
11314 auto FirstName = FirstTD->getDeclName();
11315 auto SecondName = SecondTD->getDeclName();
11316 if (FirstName != SecondName) {
11317 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11319 << (FirstDiffType == TypeAlias) << FirstName;
11320 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11322 << (FirstDiffType == TypeAlias) << SecondName;
11327 QualType FirstType = FirstTD->getUnderlyingType();
11328 QualType SecondType = SecondTD->getUnderlyingType();
11329 if (ComputeQualTypeODRHash(FirstType) !=
11330 ComputeQualTypeODRHash(SecondType)) {
11331 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11333 << (FirstDiffType == TypeAlias) << FirstName << FirstType;
11334 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11336 << (FirstDiffType == TypeAlias) << SecondName << SecondType;
11343 VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
11344 VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
11345 auto FirstName = FirstVD->getDeclName();
11346 auto SecondName = SecondVD->getDeclName();
11347 if (FirstName != SecondName) {
11348 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11351 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11358 QualType FirstType = FirstVD->getType();
11359 QualType SecondType = SecondVD->getType();
11360 if (ComputeQualTypeODRHash(FirstType) !=
11361 ComputeQualTypeODRHash(SecondType)) {
11362 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11364 << FirstName << FirstType;
11365 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11367 << SecondName << SecondType;
11372 const Expr *FirstInit = FirstVD->getInit();
11373 const Expr *SecondInit = SecondVD->getInit();
11374 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11375 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11376 VarSingleInitializer)
11377 << FirstName << (FirstInit == nullptr)
11378 << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
11379 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11380 VarSingleInitializer)
11381 << SecondName << (SecondInit == nullptr)
11382 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11387 if (FirstInit && SecondInit &&
11388 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11389 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11390 VarDifferentInitializer)
11391 << FirstName << FirstInit->getSourceRange();
11392 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11393 VarDifferentInitializer)
11394 << SecondName << SecondInit->getSourceRange();
11399 const bool FirstIsConstexpr = FirstVD->isConstexpr();
11400 const bool SecondIsConstexpr = SecondVD->isConstexpr();
11401 if (FirstIsConstexpr != SecondIsConstexpr) {
11402 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11404 << FirstName << FirstIsConstexpr;
11405 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11407 << SecondName << SecondIsConstexpr;
11414 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
11415 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
11417 NamedDecl *FirstND = FirstFriend->getFriendDecl();
11418 NamedDecl *SecondND = SecondFriend->getFriendDecl();
11420 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
11421 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
11423 if (FirstND && SecondND) {
11424 ODRDiagError(FirstFriend->getFriendLoc(),
11425 FirstFriend->getSourceRange(), FriendFunction)
11427 ODRDiagNote(SecondFriend->getFriendLoc(),
11428 SecondFriend->getSourceRange(), FriendFunction)
11435 if (FirstTSI && SecondTSI) {
11436 QualType FirstFriendType = FirstTSI->getType();
11437 QualType SecondFriendType = SecondTSI->getType();
11438 assert(ComputeQualTypeODRHash(FirstFriendType) !=
11439 ComputeQualTypeODRHash(SecondFriendType));
11440 ODRDiagError(FirstFriend->getFriendLoc(),
11441 FirstFriend->getSourceRange(), FriendType)
11442 << FirstFriendType;
11443 ODRDiagNote(SecondFriend->getFriendLoc(),
11444 SecondFriend->getSourceRange(), FriendType)
11445 << SecondFriendType;
11450 ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
11451 FriendTypeFunction)
11452 << (FirstTSI == nullptr);
11453 ODRDiagNote(SecondFriend->getFriendLoc(),
11454 SecondFriend->getSourceRange(), FriendTypeFunction)
11455 << (SecondTSI == nullptr);
11460 case FunctionTemplate: {
11461 FunctionTemplateDecl *FirstTemplate =
11462 cast<FunctionTemplateDecl>(FirstDecl);
11463 FunctionTemplateDecl *SecondTemplate =
11464 cast<FunctionTemplateDecl>(SecondDecl);
11466 TemplateParameterList *FirstTPL =
11467 FirstTemplate->getTemplateParameters();
11468 TemplateParameterList *SecondTPL =
11469 SecondTemplate->getTemplateParameters();
11471 if (FirstTPL->size() != SecondTPL->size()) {
11472 ODRDiagError(FirstTemplate->getLocation(),
11473 FirstTemplate->getSourceRange(),
11474 FunctionTemplateDifferentNumberParameters)
11475 << FirstTemplate << FirstTPL->size();
11476 ODRDiagNote(SecondTemplate->getLocation(),
11477 SecondTemplate->getSourceRange(),
11478 FunctionTemplateDifferentNumberParameters)
11479 << SecondTemplate << SecondTPL->size();
11485 bool ParameterMismatch = false;
11486 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11487 NamedDecl *FirstParam = FirstTPL->getParam(i);
11488 NamedDecl *SecondParam = SecondTPL->getParam(i);
11490 if (FirstParam->getKind() != SecondParam->getKind()) {
11492 TemplateTypeParameter,
11493 NonTypeTemplateParameter,
11494 TemplateTemplateParameter,
11496 auto GetParamType = [](NamedDecl *D) {
11497 switch (D->getKind()) {
11499 llvm_unreachable("Unexpected template parameter type");
11500 case Decl::TemplateTypeParm:
11501 return TemplateTypeParameter;
11502 case Decl::NonTypeTemplateParm:
11503 return NonTypeTemplateParameter;
11504 case Decl::TemplateTemplateParm:
11505 return TemplateTemplateParameter;
11509 ODRDiagError(FirstTemplate->getLocation(),
11510 FirstTemplate->getSourceRange(),
11511 FunctionTemplateParameterDifferentKind)
11512 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11513 ODRDiagNote(SecondTemplate->getLocation(),
11514 SecondTemplate->getSourceRange(),
11515 FunctionTemplateParameterDifferentKind)
11516 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11518 ParameterMismatch = true;
11522 if (FirstParam->getName() != SecondParam->getName()) {
11523 ODRDiagError(FirstTemplate->getLocation(),
11524 FirstTemplate->getSourceRange(),
11525 FunctionTemplateParameterName)
11526 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11528 ODRDiagNote(SecondTemplate->getLocation(),
11529 SecondTemplate->getSourceRange(),
11530 FunctionTemplateParameterName)
11531 << SecondTemplate << (i + 1)
11532 << (bool)SecondParam->getIdentifier() << SecondParam;
11533 ParameterMismatch = true;
11537 if (isa<TemplateTypeParmDecl>(FirstParam) &&
11538 isa<TemplateTypeParmDecl>(SecondParam)) {
11539 TemplateTypeParmDecl *FirstTTPD =
11540 cast<TemplateTypeParmDecl>(FirstParam);
11541 TemplateTypeParmDecl *SecondTTPD =
11542 cast<TemplateTypeParmDecl>(SecondParam);
11543 bool HasFirstDefaultArgument =
11544 FirstTTPD->hasDefaultArgument() &&
11545 !FirstTTPD->defaultArgumentWasInherited();
11546 bool HasSecondDefaultArgument =
11547 SecondTTPD->hasDefaultArgument() &&
11548 !SecondTTPD->defaultArgumentWasInherited();
11549 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11550 ODRDiagError(FirstTemplate->getLocation(),
11551 FirstTemplate->getSourceRange(),
11552 FunctionTemplateParameterSingleDefaultArgument)
11553 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11554 ODRDiagNote(SecondTemplate->getLocation(),
11555 SecondTemplate->getSourceRange(),
11556 FunctionTemplateParameterSingleDefaultArgument)
11557 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11558 ParameterMismatch = true;
11562 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11563 QualType FirstType = FirstTTPD->getDefaultArgument();
11564 QualType SecondType = SecondTTPD->getDefaultArgument();
11565 if (ComputeQualTypeODRHash(FirstType) !=
11566 ComputeQualTypeODRHash(SecondType)) {
11567 ODRDiagError(FirstTemplate->getLocation(),
11568 FirstTemplate->getSourceRange(),
11569 FunctionTemplateParameterDifferentDefaultArgument)
11570 << FirstTemplate << (i + 1) << FirstType;
11571 ODRDiagNote(SecondTemplate->getLocation(),
11572 SecondTemplate->getSourceRange(),
11573 FunctionTemplateParameterDifferentDefaultArgument)
11574 << SecondTemplate << (i + 1) << SecondType;
11575 ParameterMismatch = true;
11580 if (FirstTTPD->isParameterPack() !=
11581 SecondTTPD->isParameterPack()) {
11582 ODRDiagError(FirstTemplate->getLocation(),
11583 FirstTemplate->getSourceRange(),
11584 FunctionTemplatePackParameter)
11585 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11586 ODRDiagNote(SecondTemplate->getLocation(),
11587 SecondTemplate->getSourceRange(),
11588 FunctionTemplatePackParameter)
11589 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11590 ParameterMismatch = true;
11595 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11596 isa<TemplateTemplateParmDecl>(SecondParam)) {
11597 TemplateTemplateParmDecl *FirstTTPD =
11598 cast<TemplateTemplateParmDecl>(FirstParam);
11599 TemplateTemplateParmDecl *SecondTTPD =
11600 cast<TemplateTemplateParmDecl>(SecondParam);
11602 TemplateParameterList *FirstTPL =
11603 FirstTTPD->getTemplateParameters();
11604 TemplateParameterList *SecondTPL =
11605 SecondTTPD->getTemplateParameters();
11607 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11608 ComputeTemplateParameterListODRHash(SecondTPL)) {
11609 ODRDiagError(FirstTemplate->getLocation(),
11610 FirstTemplate->getSourceRange(),
11611 FunctionTemplateParameterDifferentType)
11612 << FirstTemplate << (i + 1);
11613 ODRDiagNote(SecondTemplate->getLocation(),
11614 SecondTemplate->getSourceRange(),
11615 FunctionTemplateParameterDifferentType)
11616 << SecondTemplate << (i + 1);
11617 ParameterMismatch = true;
11621 bool HasFirstDefaultArgument =
11622 FirstTTPD->hasDefaultArgument() &&
11623 !FirstTTPD->defaultArgumentWasInherited();
11624 bool HasSecondDefaultArgument =
11625 SecondTTPD->hasDefaultArgument() &&
11626 !SecondTTPD->defaultArgumentWasInherited();
11627 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11628 ODRDiagError(FirstTemplate->getLocation(),
11629 FirstTemplate->getSourceRange(),
11630 FunctionTemplateParameterSingleDefaultArgument)
11631 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11632 ODRDiagNote(SecondTemplate->getLocation(),
11633 SecondTemplate->getSourceRange(),
11634 FunctionTemplateParameterSingleDefaultArgument)
11635 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11636 ParameterMismatch = true;
11640 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11641 TemplateArgument FirstTA =
11642 FirstTTPD->getDefaultArgument().getArgument();
11643 TemplateArgument SecondTA =
11644 SecondTTPD->getDefaultArgument().getArgument();
11645 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11646 ComputeTemplateArgumentODRHash(SecondTA)) {
11647 ODRDiagError(FirstTemplate->getLocation(),
11648 FirstTemplate->getSourceRange(),
11649 FunctionTemplateParameterDifferentDefaultArgument)
11650 << FirstTemplate << (i + 1) << FirstTA;
11651 ODRDiagNote(SecondTemplate->getLocation(),
11652 SecondTemplate->getSourceRange(),
11653 FunctionTemplateParameterDifferentDefaultArgument)
11654 << SecondTemplate << (i + 1) << SecondTA;
11655 ParameterMismatch = true;
11660 if (FirstTTPD->isParameterPack() !=
11661 SecondTTPD->isParameterPack()) {
11662 ODRDiagError(FirstTemplate->getLocation(),
11663 FirstTemplate->getSourceRange(),
11664 FunctionTemplatePackParameter)
11665 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11666 ODRDiagNote(SecondTemplate->getLocation(),
11667 SecondTemplate->getSourceRange(),
11668 FunctionTemplatePackParameter)
11669 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11670 ParameterMismatch = true;
11675 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11676 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11677 NonTypeTemplateParmDecl *FirstNTTPD =
11678 cast<NonTypeTemplateParmDecl>(FirstParam);
11679 NonTypeTemplateParmDecl *SecondNTTPD =
11680 cast<NonTypeTemplateParmDecl>(SecondParam);
11682 QualType FirstType = FirstNTTPD->getType();
11683 QualType SecondType = SecondNTTPD->getType();
11684 if (ComputeQualTypeODRHash(FirstType) !=
11685 ComputeQualTypeODRHash(SecondType)) {
11686 ODRDiagError(FirstTemplate->getLocation(),
11687 FirstTemplate->getSourceRange(),
11688 FunctionTemplateParameterDifferentType)
11689 << FirstTemplate << (i + 1);
11690 ODRDiagNote(SecondTemplate->getLocation(),
11691 SecondTemplate->getSourceRange(),
11692 FunctionTemplateParameterDifferentType)
11693 << SecondTemplate << (i + 1);
11694 ParameterMismatch = true;
11698 bool HasFirstDefaultArgument =
11699 FirstNTTPD->hasDefaultArgument() &&
11700 !FirstNTTPD->defaultArgumentWasInherited();
11701 bool HasSecondDefaultArgument =
11702 SecondNTTPD->hasDefaultArgument() &&
11703 !SecondNTTPD->defaultArgumentWasInherited();
11704 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11705 ODRDiagError(FirstTemplate->getLocation(),
11706 FirstTemplate->getSourceRange(),
11707 FunctionTemplateParameterSingleDefaultArgument)
11708 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11709 ODRDiagNote(SecondTemplate->getLocation(),
11710 SecondTemplate->getSourceRange(),
11711 FunctionTemplateParameterSingleDefaultArgument)
11712 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11713 ParameterMismatch = true;
11717 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11718 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11719 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11720 if (ComputeODRHash(FirstDefaultArgument) !=
11721 ComputeODRHash(SecondDefaultArgument)) {
11722 ODRDiagError(FirstTemplate->getLocation(),
11723 FirstTemplate->getSourceRange(),
11724 FunctionTemplateParameterDifferentDefaultArgument)
11725 << FirstTemplate << (i + 1) << FirstDefaultArgument;
11726 ODRDiagNote(SecondTemplate->getLocation(),
11727 SecondTemplate->getSourceRange(),
11728 FunctionTemplateParameterDifferentDefaultArgument)
11729 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11730 ParameterMismatch = true;
11735 if (FirstNTTPD->isParameterPack() !=
11736 SecondNTTPD->isParameterPack()) {
11737 ODRDiagError(FirstTemplate->getLocation(),
11738 FirstTemplate->getSourceRange(),
11739 FunctionTemplatePackParameter)
11740 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11741 ODRDiagNote(SecondTemplate->getLocation(),
11742 SecondTemplate->getSourceRange(),
11743 FunctionTemplatePackParameter)
11744 << SecondTemplate << (i + 1)
11745 << SecondNTTPD->isParameterPack();
11746 ParameterMismatch = true;
11752 if (ParameterMismatch) {
11764 Diag(FirstDecl->getLocation(),
11765 diag::err_module_odr_violation_mismatch_decl_unknown)
11766 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11767 << FirstDecl->getSourceRange();
11768 Diag(SecondDecl->getLocation(),
11769 diag::note_module_odr_violation_mismatch_decl_unknown)
11770 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11775 // All definitions are updates to the same declaration. This happens if a
11776 // module instantiates the declaration of a class template specialization
11777 // and two or more other modules instantiate its definition.
11779 // FIXME: Indicate which modules had instantiations of this definition.
11780 // FIXME: How can this even happen?
11781 Diag(Merge.first->getLocation(),
11782 diag::err_module_odr_violation_different_instantiations)
11787 // Issue ODR failures diagnostics for functions.
11788 for (auto &Merge : FunctionOdrMergeFailures) {
11789 enum ODRFunctionDifference {
11793 ParameterSingleDefaultArgument,
11794 ParameterDifferentDefaultArgument,
11798 FunctionDecl *FirstFunction = Merge.first;
11799 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11801 bool Diagnosed = false;
11802 for (auto &SecondFunction : Merge.second) {
11804 if (FirstFunction == SecondFunction)
11807 std::string SecondModule =
11808 getOwningModuleNameForDiagnostic(SecondFunction);
11810 auto ODRDiagError = [FirstFunction, &FirstModule,
11811 this](SourceLocation Loc, SourceRange Range,
11812 ODRFunctionDifference DiffType) {
11813 return Diag(Loc, diag::err_module_odr_violation_function)
11814 << FirstFunction << FirstModule.empty() << FirstModule << Range
11817 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11819 ODRFunctionDifference DiffType) {
11820 return Diag(Loc, diag::note_module_odr_violation_function)
11821 << SecondModule << Range << DiffType;
11824 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11825 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11826 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11827 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11828 << FirstFunction->getReturnType();
11829 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11830 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11831 << SecondFunction->getReturnType();
11836 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11837 "Merged functions with different number of parameters");
11839 auto ParamSize = FirstFunction->param_size();
11840 bool ParameterMismatch = false;
11841 for (unsigned I = 0; I < ParamSize; ++I) {
11842 auto *FirstParam = FirstFunction->getParamDecl(I);
11843 auto *SecondParam = SecondFunction->getParamDecl(I);
11845 assert(getContext().hasSameType(FirstParam->getType(),
11846 SecondParam->getType()) &&
11847 "Merged function has different parameter types.");
11849 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11850 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11852 << I + 1 << FirstParam->getDeclName();
11853 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11855 << I + 1 << SecondParam->getDeclName();
11856 ParameterMismatch = true;
11860 QualType FirstParamType = FirstParam->getType();
11861 QualType SecondParamType = SecondParam->getType();
11862 if (FirstParamType != SecondParamType &&
11863 ComputeQualTypeODRHash(FirstParamType) !=
11864 ComputeQualTypeODRHash(SecondParamType)) {
11865 if (const DecayedType *ParamDecayedType =
11866 FirstParamType->getAs<DecayedType>()) {
11867 ODRDiagError(FirstParam->getLocation(),
11868 FirstParam->getSourceRange(), ParameterType)
11869 << (I + 1) << FirstParamType << true
11870 << ParamDecayedType->getOriginalType();
11872 ODRDiagError(FirstParam->getLocation(),
11873 FirstParam->getSourceRange(), ParameterType)
11874 << (I + 1) << FirstParamType << false;
11877 if (const DecayedType *ParamDecayedType =
11878 SecondParamType->getAs<DecayedType>()) {
11879 ODRDiagNote(SecondParam->getLocation(),
11880 SecondParam->getSourceRange(), ParameterType)
11881 << (I + 1) << SecondParamType << true
11882 << ParamDecayedType->getOriginalType();
11884 ODRDiagNote(SecondParam->getLocation(),
11885 SecondParam->getSourceRange(), ParameterType)
11886 << (I + 1) << SecondParamType << false;
11888 ParameterMismatch = true;
11892 const Expr *FirstInit = FirstParam->getInit();
11893 const Expr *SecondInit = SecondParam->getInit();
11894 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11895 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11896 ParameterSingleDefaultArgument)
11897 << (I + 1) << (FirstInit == nullptr)
11898 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11899 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11900 ParameterSingleDefaultArgument)
11901 << (I + 1) << (SecondInit == nullptr)
11902 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11903 ParameterMismatch = true;
11907 if (FirstInit && SecondInit &&
11908 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11909 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11910 ParameterDifferentDefaultArgument)
11911 << (I + 1) << FirstInit->getSourceRange();
11912 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11913 ParameterDifferentDefaultArgument)
11914 << (I + 1) << SecondInit->getSourceRange();
11915 ParameterMismatch = true;
11919 assert(ComputeSubDeclODRHash(FirstParam) ==
11920 ComputeSubDeclODRHash(SecondParam) &&
11921 "Undiagnosed parameter difference.");
11924 if (ParameterMismatch) {
11929 // If no error has been generated before now, assume the problem is in
11930 // the body and generate a message.
11931 ODRDiagError(FirstFunction->getLocation(),
11932 FirstFunction->getSourceRange(), FunctionBody);
11933 ODRDiagNote(SecondFunction->getLocation(),
11934 SecondFunction->getSourceRange(), FunctionBody);
11939 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11942 // Issue ODR failures diagnostics for enums.
11943 for (auto &Merge : EnumOdrMergeFailures) {
11944 enum ODREnumDifference {
11946 EnumTagKeywordMismatch,
11947 SingleSpecifiedType,
11948 DifferentSpecifiedTypes,
11949 DifferentNumberEnumConstants,
11951 EnumConstantSingleInitilizer,
11952 EnumConstantDifferentInitilizer,
11955 // If we've already pointed out a specific problem with this enum, don't
11956 // bother issuing a general "something's different" diagnostic.
11957 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11960 EnumDecl *FirstEnum = Merge.first;
11961 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11964 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11965 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11966 DeclHashes &Hashes, EnumDecl *Enum) {
11967 for (auto *D : Enum->decls()) {
11968 // Due to decl merging, the first EnumDecl is the parent of
11969 // Decls in both records.
11970 if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11972 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11973 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11974 ComputeSubDeclODRHash(D));
11977 DeclHashes FirstHashes;
11978 PopulateHashes(FirstHashes, FirstEnum);
11979 bool Diagnosed = false;
11980 for (auto &SecondEnum : Merge.second) {
11982 if (FirstEnum == SecondEnum)
11985 std::string SecondModule =
11986 getOwningModuleNameForDiagnostic(SecondEnum);
11988 auto ODRDiagError = [FirstEnum, &FirstModule,
11989 this](SourceLocation Loc, SourceRange Range,
11990 ODREnumDifference DiffType) {
11991 return Diag(Loc, diag::err_module_odr_violation_enum)
11992 << FirstEnum << FirstModule.empty() << FirstModule << Range
11995 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11997 ODREnumDifference DiffType) {
11998 return Diag(Loc, diag::note_module_odr_violation_enum)
11999 << SecondModule << Range << DiffType;
12002 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
12003 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12005 << FirstEnum->isScoped();
12006 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12008 << SecondEnum->isScoped();
12013 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
12014 if (FirstEnum->isScopedUsingClassTag() !=
12015 SecondEnum->isScopedUsingClassTag()) {
12016 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12017 EnumTagKeywordMismatch)
12018 << FirstEnum->isScopedUsingClassTag();
12019 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12020 EnumTagKeywordMismatch)
12021 << SecondEnum->isScopedUsingClassTag();
12027 QualType FirstUnderlyingType =
12028 FirstEnum->getIntegerTypeSourceInfo()
12029 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
12031 QualType SecondUnderlyingType =
12032 SecondEnum->getIntegerTypeSourceInfo()
12033 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
12035 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
12036 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12037 SingleSpecifiedType)
12038 << !FirstUnderlyingType.isNull();
12039 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12040 SingleSpecifiedType)
12041 << !SecondUnderlyingType.isNull();
12046 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
12047 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
12048 ComputeQualTypeODRHash(SecondUnderlyingType)) {
12049 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12050 DifferentSpecifiedTypes)
12051 << FirstUnderlyingType;
12052 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12053 DifferentSpecifiedTypes)
12054 << SecondUnderlyingType;
12060 DeclHashes SecondHashes;
12061 PopulateHashes(SecondHashes, SecondEnum);
12063 if (FirstHashes.size() != SecondHashes.size()) {
12064 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12065 DifferentNumberEnumConstants)
12066 << (int)FirstHashes.size();
12067 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12068 DifferentNumberEnumConstants)
12069 << (int)SecondHashes.size();
12074 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
12075 if (FirstHashes[I].second == SecondHashes[I].second)
12077 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
12078 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
12080 if (FirstEnumConstant->getDeclName() !=
12081 SecondEnumConstant->getDeclName()) {
12083 ODRDiagError(FirstEnumConstant->getLocation(),
12084 FirstEnumConstant->getSourceRange(), EnumConstantName)
12085 << I + 1 << FirstEnumConstant;
12086 ODRDiagNote(SecondEnumConstant->getLocation(),
12087 SecondEnumConstant->getSourceRange(), EnumConstantName)
12088 << I + 1 << SecondEnumConstant;
12093 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
12094 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
12095 if (!FirstInit && !SecondInit)
12098 if (!FirstInit || !SecondInit) {
12099 ODRDiagError(FirstEnumConstant->getLocation(),
12100 FirstEnumConstant->getSourceRange(),
12101 EnumConstantSingleInitilizer)
12102 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
12103 ODRDiagNote(SecondEnumConstant->getLocation(),
12104 SecondEnumConstant->getSourceRange(),
12105 EnumConstantSingleInitilizer)
12106 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
12111 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
12112 ODRDiagError(FirstEnumConstant->getLocation(),
12113 FirstEnumConstant->getSourceRange(),
12114 EnumConstantDifferentInitilizer)
12115 << I + 1 << FirstEnumConstant;
12116 ODRDiagNote(SecondEnumConstant->getLocation(),
12117 SecondEnumConstant->getSourceRange(),
12118 EnumConstantDifferentInitilizer)
12119 << I + 1 << SecondEnumConstant;
12127 assert(Diagnosed && "Unable to emit ODR diagnostic.");
12131 void ASTReader::StartedDeserializing() {
12132 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
12133 ReadTimer->startTimer();
12136 void ASTReader::FinishedDeserializing() {
12137 assert(NumCurrentElementsDeserializing &&
12138 "FinishedDeserializing not paired with StartedDeserializing");
12139 if (NumCurrentElementsDeserializing == 1) {
12140 // We decrease NumCurrentElementsDeserializing only after pending actions
12141 // are finished, to avoid recursively re-calling finishPendingActions().
12142 finishPendingActions();
12144 --NumCurrentElementsDeserializing;
12146 if (NumCurrentElementsDeserializing == 0) {
12147 // Propagate exception specification and deduced type updates along
12148 // redeclaration chains.
12150 // We do this now rather than in finishPendingActions because we want to
12151 // be able to walk the complete redeclaration chains of the updated decls.
12152 while (!PendingExceptionSpecUpdates.empty() ||
12153 !PendingDeducedTypeUpdates.empty()) {
12154 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
12155 PendingExceptionSpecUpdates.clear();
12156 for (auto Update : ESUpdates) {
12157 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12158 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
12159 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
12160 if (auto *Listener = getContext().getASTMutationListener())
12161 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
12162 for (auto *Redecl : Update.second->redecls())
12163 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
12166 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
12167 PendingDeducedTypeUpdates.clear();
12168 for (auto Update : DTUpdates) {
12169 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12170 // FIXME: If the return type is already deduced, check that it matches.
12171 getContext().adjustDeducedFunctionResultType(Update.first,
12177 ReadTimer->stopTimer();
12179 diagnoseOdrViolations();
12181 // We are not in recursive loading, so it's safe to pass the "interesting"
12182 // decls to the consumer.
12184 PassInterestingDeclsToConsumer();
12188 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
12189 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
12190 // Remove any fake results before adding any real ones.
12191 auto It = PendingFakeLookupResults.find(II);
12192 if (It != PendingFakeLookupResults.end()) {
12193 for (auto *ND : It->second)
12194 SemaObj->IdResolver.RemoveDecl(ND);
12195 // FIXME: this works around module+PCH performance issue.
12196 // Rather than erase the result from the map, which is O(n), just clear
12197 // the vector of NamedDecls.
12198 It->second.clear();
12202 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
12203 SemaObj->TUScope->AddDecl(D);
12204 } else if (SemaObj->TUScope) {
12205 // Adding the decl to IdResolver may have failed because it was already in
12206 // (even though it was not added in scope). If it is already in, make sure
12207 // it gets in the scope as well.
12208 if (std::find(SemaObj->IdResolver.begin(Name),
12209 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
12210 SemaObj->TUScope->AddDecl(D);
12214 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
12215 ASTContext *Context,
12216 const PCHContainerReader &PCHContainerRdr,
12217 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
12218 StringRef isysroot, bool DisableValidation,
12219 bool AllowASTWithCompilerErrors,
12220 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
12221 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
12222 std::unique_ptr<llvm::Timer> ReadTimer)
12223 : Listener(DisableValidation
12224 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
12225 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
12226 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
12227 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
12228 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
12229 PCHContainerRdr, PP.getHeaderSearchInfo()),
12230 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
12231 DisableValidation(DisableValidation),
12232 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
12233 AllowConfigurationMismatch(AllowConfigurationMismatch),
12234 ValidateSystemInputs(ValidateSystemInputs),
12235 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
12236 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
12237 SourceMgr.setExternalSLocEntrySource(this);
12239 for (const auto &Ext : Extensions) {
12240 auto BlockName = Ext->getExtensionMetadata().BlockName;
12241 auto Known = ModuleFileExtensions.find(BlockName);
12242 if (Known != ModuleFileExtensions.end()) {
12243 Diags.Report(diag::warn_duplicate_module_file_extension)
12248 ModuleFileExtensions.insert({BlockName, Ext});
12252 ASTReader::~ASTReader() {
12253 if (OwnsDeserializationListener)
12254 delete DeserializationListener;
12257 IdentifierResolver &ASTReader::getIdResolver() {
12258 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
12261 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
12262 unsigned AbbrevID) {
12265 return Cursor.readRecord(AbbrevID, Record);
12267 //===----------------------------------------------------------------------===//
12268 //// OMPClauseReader implementation
12269 ////===----------------------------------------------------------------------===//
12271 OMPClause *OMPClauseReader::readClause() {
12272 OMPClause *C = nullptr;
12273 switch (Record.readInt()) {
12275 C = new (Context) OMPIfClause();
12278 C = new (Context) OMPFinalClause();
12280 case OMPC_num_threads:
12281 C = new (Context) OMPNumThreadsClause();
12284 C = new (Context) OMPSafelenClause();
12287 C = new (Context) OMPSimdlenClause();
12289 case OMPC_allocator:
12290 C = new (Context) OMPAllocatorClause();
12292 case OMPC_collapse:
12293 C = new (Context) OMPCollapseClause();
12296 C = new (Context) OMPDefaultClause();
12298 case OMPC_proc_bind:
12299 C = new (Context) OMPProcBindClause();
12301 case OMPC_schedule:
12302 C = new (Context) OMPScheduleClause();
12305 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
12308 C = new (Context) OMPNowaitClause();
12311 C = new (Context) OMPUntiedClause();
12313 case OMPC_mergeable:
12314 C = new (Context) OMPMergeableClause();
12317 C = new (Context) OMPReadClause();
12320 C = new (Context) OMPWriteClause();
12323 C = new (Context) OMPUpdateClause();
12326 C = new (Context) OMPCaptureClause();
12329 C = new (Context) OMPSeqCstClause();
12332 C = new (Context) OMPThreadsClause();
12335 C = new (Context) OMPSIMDClause();
12338 C = new (Context) OMPNogroupClause();
12340 case OMPC_unified_address:
12341 C = new (Context) OMPUnifiedAddressClause();
12343 case OMPC_unified_shared_memory:
12344 C = new (Context) OMPUnifiedSharedMemoryClause();
12346 case OMPC_reverse_offload:
12347 C = new (Context) OMPReverseOffloadClause();
12349 case OMPC_dynamic_allocators:
12350 C = new (Context) OMPDynamicAllocatorsClause();
12352 case OMPC_atomic_default_mem_order:
12353 C = new (Context) OMPAtomicDefaultMemOrderClause();
12356 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
12358 case OMPC_firstprivate:
12359 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
12361 case OMPC_lastprivate:
12362 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
12365 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
12367 case OMPC_reduction:
12368 C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
12370 case OMPC_task_reduction:
12371 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
12373 case OMPC_in_reduction:
12374 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
12377 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
12380 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
12383 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
12385 case OMPC_copyprivate:
12386 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
12389 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
12391 case OMPC_depend: {
12392 unsigned NumVars = Record.readInt();
12393 unsigned NumLoops = Record.readInt();
12394 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
12398 C = new (Context) OMPDeviceClause();
12401 OMPMappableExprListSizeTy Sizes;
12402 Sizes.NumVars = Record.readInt();
12403 Sizes.NumUniqueDeclarations = Record.readInt();
12404 Sizes.NumComponentLists = Record.readInt();
12405 Sizes.NumComponents = Record.readInt();
12406 C = OMPMapClause::CreateEmpty(Context, Sizes);
12409 case OMPC_num_teams:
12410 C = new (Context) OMPNumTeamsClause();
12412 case OMPC_thread_limit:
12413 C = new (Context) OMPThreadLimitClause();
12415 case OMPC_priority:
12416 C = new (Context) OMPPriorityClause();
12418 case OMPC_grainsize:
12419 C = new (Context) OMPGrainsizeClause();
12421 case OMPC_num_tasks:
12422 C = new (Context) OMPNumTasksClause();
12425 C = new (Context) OMPHintClause();
12427 case OMPC_dist_schedule:
12428 C = new (Context) OMPDistScheduleClause();
12430 case OMPC_defaultmap:
12431 C = new (Context) OMPDefaultmapClause();
12434 OMPMappableExprListSizeTy Sizes;
12435 Sizes.NumVars = Record.readInt();
12436 Sizes.NumUniqueDeclarations = Record.readInt();
12437 Sizes.NumComponentLists = Record.readInt();
12438 Sizes.NumComponents = Record.readInt();
12439 C = OMPToClause::CreateEmpty(Context, Sizes);
12443 OMPMappableExprListSizeTy Sizes;
12444 Sizes.NumVars = Record.readInt();
12445 Sizes.NumUniqueDeclarations = Record.readInt();
12446 Sizes.NumComponentLists = Record.readInt();
12447 Sizes.NumComponents = Record.readInt();
12448 C = OMPFromClause::CreateEmpty(Context, Sizes);
12451 case OMPC_use_device_ptr: {
12452 OMPMappableExprListSizeTy Sizes;
12453 Sizes.NumVars = Record.readInt();
12454 Sizes.NumUniqueDeclarations = Record.readInt();
12455 Sizes.NumComponentLists = Record.readInt();
12456 Sizes.NumComponents = Record.readInt();
12457 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12460 case OMPC_is_device_ptr: {
12461 OMPMappableExprListSizeTy Sizes;
12462 Sizes.NumVars = Record.readInt();
12463 Sizes.NumUniqueDeclarations = Record.readInt();
12464 Sizes.NumComponentLists = Record.readInt();
12465 Sizes.NumComponents = Record.readInt();
12466 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12469 case OMPC_allocate:
12470 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12473 assert(C && "Unknown OMPClause type");
12476 C->setLocStart(Record.readSourceLocation());
12477 C->setLocEnd(Record.readSourceLocation());
12482 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12483 C->setPreInitStmt(Record.readSubStmt(),
12484 static_cast<OpenMPDirectiveKind>(Record.readInt()));
12487 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12488 VisitOMPClauseWithPreInit(C);
12489 C->setPostUpdateExpr(Record.readSubExpr());
12492 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12493 VisitOMPClauseWithPreInit(C);
12494 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12495 C->setNameModifierLoc(Record.readSourceLocation());
12496 C->setColonLoc(Record.readSourceLocation());
12497 C->setCondition(Record.readSubExpr());
12498 C->setLParenLoc(Record.readSourceLocation());
12501 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12502 VisitOMPClauseWithPreInit(C);
12503 C->setCondition(Record.readSubExpr());
12504 C->setLParenLoc(Record.readSourceLocation());
12507 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12508 VisitOMPClauseWithPreInit(C);
12509 C->setNumThreads(Record.readSubExpr());
12510 C->setLParenLoc(Record.readSourceLocation());
12513 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12514 C->setSafelen(Record.readSubExpr());
12515 C->setLParenLoc(Record.readSourceLocation());
12518 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12519 C->setSimdlen(Record.readSubExpr());
12520 C->setLParenLoc(Record.readSourceLocation());
12523 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12524 C->setAllocator(Record.readExpr());
12525 C->setLParenLoc(Record.readSourceLocation());
12528 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12529 C->setNumForLoops(Record.readSubExpr());
12530 C->setLParenLoc(Record.readSourceLocation());
12533 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12535 static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
12536 C->setLParenLoc(Record.readSourceLocation());
12537 C->setDefaultKindKwLoc(Record.readSourceLocation());
12540 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12541 C->setProcBindKind(
12542 static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
12543 C->setLParenLoc(Record.readSourceLocation());
12544 C->setProcBindKindKwLoc(Record.readSourceLocation());
12547 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12548 VisitOMPClauseWithPreInit(C);
12549 C->setScheduleKind(
12550 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12551 C->setFirstScheduleModifier(
12552 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12553 C->setSecondScheduleModifier(
12554 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12555 C->setChunkSize(Record.readSubExpr());
12556 C->setLParenLoc(Record.readSourceLocation());
12557 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12558 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12559 C->setScheduleKindLoc(Record.readSourceLocation());
12560 C->setCommaLoc(Record.readSourceLocation());
12563 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12564 C->setNumForLoops(Record.readSubExpr());
12565 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12566 C->setLoopNumIterations(I, Record.readSubExpr());
12567 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12568 C->setLoopCounter(I, Record.readSubExpr());
12569 C->setLParenLoc(Record.readSourceLocation());
12572 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12574 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12576 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12578 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12580 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12582 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12584 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12586 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12588 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12590 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12592 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12594 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12596 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12597 OMPUnifiedSharedMemoryClause *) {}
12599 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12602 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12605 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12606 OMPAtomicDefaultMemOrderClause *C) {
12607 C->setAtomicDefaultMemOrderKind(
12608 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12609 C->setLParenLoc(Record.readSourceLocation());
12610 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12613 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12614 C->setLParenLoc(Record.readSourceLocation());
12615 unsigned NumVars = C->varlist_size();
12616 SmallVector<Expr *, 16> Vars;
12617 Vars.reserve(NumVars);
12618 for (unsigned i = 0; i != NumVars; ++i)
12619 Vars.push_back(Record.readSubExpr());
12620 C->setVarRefs(Vars);
12622 for (unsigned i = 0; i != NumVars; ++i)
12623 Vars.push_back(Record.readSubExpr());
12624 C->setPrivateCopies(Vars);
12627 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12628 VisitOMPClauseWithPreInit(C);
12629 C->setLParenLoc(Record.readSourceLocation());
12630 unsigned NumVars = C->varlist_size();
12631 SmallVector<Expr *, 16> Vars;
12632 Vars.reserve(NumVars);
12633 for (unsigned i = 0; i != NumVars; ++i)
12634 Vars.push_back(Record.readSubExpr());
12635 C->setVarRefs(Vars);
12637 for (unsigned i = 0; i != NumVars; ++i)
12638 Vars.push_back(Record.readSubExpr());
12639 C->setPrivateCopies(Vars);
12641 for (unsigned i = 0; i != NumVars; ++i)
12642 Vars.push_back(Record.readSubExpr());
12646 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12647 VisitOMPClauseWithPostUpdate(C);
12648 C->setLParenLoc(Record.readSourceLocation());
12649 unsigned NumVars = C->varlist_size();
12650 SmallVector<Expr *, 16> Vars;
12651 Vars.reserve(NumVars);
12652 for (unsigned i = 0; i != NumVars; ++i)
12653 Vars.push_back(Record.readSubExpr());
12654 C->setVarRefs(Vars);
12656 for (unsigned i = 0; i != NumVars; ++i)
12657 Vars.push_back(Record.readSubExpr());
12658 C->setPrivateCopies(Vars);
12660 for (unsigned i = 0; i != NumVars; ++i)
12661 Vars.push_back(Record.readSubExpr());
12662 C->setSourceExprs(Vars);
12664 for (unsigned i = 0; i != NumVars; ++i)
12665 Vars.push_back(Record.readSubExpr());
12666 C->setDestinationExprs(Vars);
12668 for (unsigned i = 0; i != NumVars; ++i)
12669 Vars.push_back(Record.readSubExpr());
12670 C->setAssignmentOps(Vars);
12673 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12674 C->setLParenLoc(Record.readSourceLocation());
12675 unsigned NumVars = C->varlist_size();
12676 SmallVector<Expr *, 16> Vars;
12677 Vars.reserve(NumVars);
12678 for (unsigned i = 0; i != NumVars; ++i)
12679 Vars.push_back(Record.readSubExpr());
12680 C->setVarRefs(Vars);
12683 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12684 VisitOMPClauseWithPostUpdate(C);
12685 C->setLParenLoc(Record.readSourceLocation());
12686 C->setColonLoc(Record.readSourceLocation());
12687 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12688 DeclarationNameInfo DNI;
12689 Record.readDeclarationNameInfo(DNI);
12690 C->setQualifierLoc(NNSL);
12691 C->setNameInfo(DNI);
12693 unsigned NumVars = C->varlist_size();
12694 SmallVector<Expr *, 16> Vars;
12695 Vars.reserve(NumVars);
12696 for (unsigned i = 0; i != NumVars; ++i)
12697 Vars.push_back(Record.readSubExpr());
12698 C->setVarRefs(Vars);
12700 for (unsigned i = 0; i != NumVars; ++i)
12701 Vars.push_back(Record.readSubExpr());
12702 C->setPrivates(Vars);
12704 for (unsigned i = 0; i != NumVars; ++i)
12705 Vars.push_back(Record.readSubExpr());
12706 C->setLHSExprs(Vars);
12708 for (unsigned i = 0; i != NumVars; ++i)
12709 Vars.push_back(Record.readSubExpr());
12710 C->setRHSExprs(Vars);
12712 for (unsigned i = 0; i != NumVars; ++i)
12713 Vars.push_back(Record.readSubExpr());
12714 C->setReductionOps(Vars);
12717 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12718 VisitOMPClauseWithPostUpdate(C);
12719 C->setLParenLoc(Record.readSourceLocation());
12720 C->setColonLoc(Record.readSourceLocation());
12721 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12722 DeclarationNameInfo DNI;
12723 Record.readDeclarationNameInfo(DNI);
12724 C->setQualifierLoc(NNSL);
12725 C->setNameInfo(DNI);
12727 unsigned NumVars = C->varlist_size();
12728 SmallVector<Expr *, 16> Vars;
12729 Vars.reserve(NumVars);
12730 for (unsigned I = 0; I != NumVars; ++I)
12731 Vars.push_back(Record.readSubExpr());
12732 C->setVarRefs(Vars);
12734 for (unsigned I = 0; I != NumVars; ++I)
12735 Vars.push_back(Record.readSubExpr());
12736 C->setPrivates(Vars);
12738 for (unsigned I = 0; I != NumVars; ++I)
12739 Vars.push_back(Record.readSubExpr());
12740 C->setLHSExprs(Vars);
12742 for (unsigned I = 0; I != NumVars; ++I)
12743 Vars.push_back(Record.readSubExpr());
12744 C->setRHSExprs(Vars);
12746 for (unsigned I = 0; I != NumVars; ++I)
12747 Vars.push_back(Record.readSubExpr());
12748 C->setReductionOps(Vars);
12751 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12752 VisitOMPClauseWithPostUpdate(C);
12753 C->setLParenLoc(Record.readSourceLocation());
12754 C->setColonLoc(Record.readSourceLocation());
12755 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12756 DeclarationNameInfo DNI;
12757 Record.readDeclarationNameInfo(DNI);
12758 C->setQualifierLoc(NNSL);
12759 C->setNameInfo(DNI);
12761 unsigned NumVars = C->varlist_size();
12762 SmallVector<Expr *, 16> Vars;
12763 Vars.reserve(NumVars);
12764 for (unsigned I = 0; I != NumVars; ++I)
12765 Vars.push_back(Record.readSubExpr());
12766 C->setVarRefs(Vars);
12768 for (unsigned I = 0; I != NumVars; ++I)
12769 Vars.push_back(Record.readSubExpr());
12770 C->setPrivates(Vars);
12772 for (unsigned I = 0; I != NumVars; ++I)
12773 Vars.push_back(Record.readSubExpr());
12774 C->setLHSExprs(Vars);
12776 for (unsigned I = 0; I != NumVars; ++I)
12777 Vars.push_back(Record.readSubExpr());
12778 C->setRHSExprs(Vars);
12780 for (unsigned I = 0; I != NumVars; ++I)
12781 Vars.push_back(Record.readSubExpr());
12782 C->setReductionOps(Vars);
12784 for (unsigned I = 0; I != NumVars; ++I)
12785 Vars.push_back(Record.readSubExpr());
12786 C->setTaskgroupDescriptors(Vars);
12789 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12790 VisitOMPClauseWithPostUpdate(C);
12791 C->setLParenLoc(Record.readSourceLocation());
12792 C->setColonLoc(Record.readSourceLocation());
12793 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12794 C->setModifierLoc(Record.readSourceLocation());
12795 unsigned NumVars = C->varlist_size();
12796 SmallVector<Expr *, 16> Vars;
12797 Vars.reserve(NumVars);
12798 for (unsigned i = 0; i != NumVars; ++i)
12799 Vars.push_back(Record.readSubExpr());
12800 C->setVarRefs(Vars);
12802 for (unsigned i = 0; i != NumVars; ++i)
12803 Vars.push_back(Record.readSubExpr());
12804 C->setPrivates(Vars);
12806 for (unsigned i = 0; i != NumVars; ++i)
12807 Vars.push_back(Record.readSubExpr());
12810 for (unsigned i = 0; i != NumVars; ++i)
12811 Vars.push_back(Record.readSubExpr());
12812 C->setUpdates(Vars);
12814 for (unsigned i = 0; i != NumVars; ++i)
12815 Vars.push_back(Record.readSubExpr());
12816 C->setFinals(Vars);
12817 C->setStep(Record.readSubExpr());
12818 C->setCalcStep(Record.readSubExpr());
12820 for (unsigned I = 0; I != NumVars + 1; ++I)
12821 Vars.push_back(Record.readSubExpr());
12822 C->setUsedExprs(Vars);
12825 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12826 C->setLParenLoc(Record.readSourceLocation());
12827 C->setColonLoc(Record.readSourceLocation());
12828 unsigned NumVars = C->varlist_size();
12829 SmallVector<Expr *, 16> Vars;
12830 Vars.reserve(NumVars);
12831 for (unsigned i = 0; i != NumVars; ++i)
12832 Vars.push_back(Record.readSubExpr());
12833 C->setVarRefs(Vars);
12834 C->setAlignment(Record.readSubExpr());
12837 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12838 C->setLParenLoc(Record.readSourceLocation());
12839 unsigned NumVars = C->varlist_size();
12840 SmallVector<Expr *, 16> Exprs;
12841 Exprs.reserve(NumVars);
12842 for (unsigned i = 0; i != NumVars; ++i)
12843 Exprs.push_back(Record.readSubExpr());
12844 C->setVarRefs(Exprs);
12846 for (unsigned i = 0; i != NumVars; ++i)
12847 Exprs.push_back(Record.readSubExpr());
12848 C->setSourceExprs(Exprs);
12850 for (unsigned i = 0; i != NumVars; ++i)
12851 Exprs.push_back(Record.readSubExpr());
12852 C->setDestinationExprs(Exprs);
12854 for (unsigned i = 0; i != NumVars; ++i)
12855 Exprs.push_back(Record.readSubExpr());
12856 C->setAssignmentOps(Exprs);
12859 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12860 C->setLParenLoc(Record.readSourceLocation());
12861 unsigned NumVars = C->varlist_size();
12862 SmallVector<Expr *, 16> Exprs;
12863 Exprs.reserve(NumVars);
12864 for (unsigned i = 0; i != NumVars; ++i)
12865 Exprs.push_back(Record.readSubExpr());
12866 C->setVarRefs(Exprs);
12868 for (unsigned i = 0; i != NumVars; ++i)
12869 Exprs.push_back(Record.readSubExpr());
12870 C->setSourceExprs(Exprs);
12872 for (unsigned i = 0; i != NumVars; ++i)
12873 Exprs.push_back(Record.readSubExpr());
12874 C->setDestinationExprs(Exprs);
12876 for (unsigned i = 0; i != NumVars; ++i)
12877 Exprs.push_back(Record.readSubExpr());
12878 C->setAssignmentOps(Exprs);
12881 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12882 C->setLParenLoc(Record.readSourceLocation());
12883 unsigned NumVars = C->varlist_size();
12884 SmallVector<Expr *, 16> Vars;
12885 Vars.reserve(NumVars);
12886 for (unsigned i = 0; i != NumVars; ++i)
12887 Vars.push_back(Record.readSubExpr());
12888 C->setVarRefs(Vars);
12891 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12892 C->setLParenLoc(Record.readSourceLocation());
12893 C->setDependencyKind(
12894 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12895 C->setDependencyLoc(Record.readSourceLocation());
12896 C->setColonLoc(Record.readSourceLocation());
12897 unsigned NumVars = C->varlist_size();
12898 SmallVector<Expr *, 16> Vars;
12899 Vars.reserve(NumVars);
12900 for (unsigned I = 0; I != NumVars; ++I)
12901 Vars.push_back(Record.readSubExpr());
12902 C->setVarRefs(Vars);
12903 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12904 C->setLoopData(I, Record.readSubExpr());
12907 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12908 VisitOMPClauseWithPreInit(C);
12909 C->setDevice(Record.readSubExpr());
12910 C->setLParenLoc(Record.readSourceLocation());
12913 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12914 C->setLParenLoc(Record.readSourceLocation());
12915 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12916 C->setMapTypeModifier(
12917 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12918 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12920 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12921 DeclarationNameInfo DNI;
12922 Record.readDeclarationNameInfo(DNI);
12923 C->setMapperIdInfo(DNI);
12925 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12926 C->setMapLoc(Record.readSourceLocation());
12927 C->setColonLoc(Record.readSourceLocation());
12928 auto NumVars = C->varlist_size();
12929 auto UniqueDecls = C->getUniqueDeclarationsNum();
12930 auto TotalLists = C->getTotalComponentListNum();
12931 auto TotalComponents = C->getTotalComponentsNum();
12933 SmallVector<Expr *, 16> Vars;
12934 Vars.reserve(NumVars);
12935 for (unsigned i = 0; i != NumVars; ++i)
12936 Vars.push_back(Record.readExpr());
12937 C->setVarRefs(Vars);
12939 SmallVector<Expr *, 16> UDMappers;
12940 UDMappers.reserve(NumVars);
12941 for (unsigned I = 0; I < NumVars; ++I)
12942 UDMappers.push_back(Record.readExpr());
12943 C->setUDMapperRefs(UDMappers);
12945 SmallVector<ValueDecl *, 16> Decls;
12946 Decls.reserve(UniqueDecls);
12947 for (unsigned i = 0; i < UniqueDecls; ++i)
12948 Decls.push_back(Record.readDeclAs<ValueDecl>());
12949 C->setUniqueDecls(Decls);
12951 SmallVector<unsigned, 16> ListsPerDecl;
12952 ListsPerDecl.reserve(UniqueDecls);
12953 for (unsigned i = 0; i < UniqueDecls; ++i)
12954 ListsPerDecl.push_back(Record.readInt());
12955 C->setDeclNumLists(ListsPerDecl);
12957 SmallVector<unsigned, 32> ListSizes;
12958 ListSizes.reserve(TotalLists);
12959 for (unsigned i = 0; i < TotalLists; ++i)
12960 ListSizes.push_back(Record.readInt());
12961 C->setComponentListSizes(ListSizes);
12963 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12964 Components.reserve(TotalComponents);
12965 for (unsigned i = 0; i < TotalComponents; ++i) {
12966 Expr *AssociatedExpr = Record.readExpr();
12967 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12968 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12969 AssociatedExpr, AssociatedDecl));
12971 C->setComponents(Components, ListSizes);
12974 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12975 C->setLParenLoc(Record.readSourceLocation());
12976 C->setColonLoc(Record.readSourceLocation());
12977 C->setAllocator(Record.readSubExpr());
12978 unsigned NumVars = C->varlist_size();
12979 SmallVector<Expr *, 16> Vars;
12980 Vars.reserve(NumVars);
12981 for (unsigned i = 0; i != NumVars; ++i)
12982 Vars.push_back(Record.readSubExpr());
12983 C->setVarRefs(Vars);
12986 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12987 VisitOMPClauseWithPreInit(C);
12988 C->setNumTeams(Record.readSubExpr());
12989 C->setLParenLoc(Record.readSourceLocation());
12992 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12993 VisitOMPClauseWithPreInit(C);
12994 C->setThreadLimit(Record.readSubExpr());
12995 C->setLParenLoc(Record.readSourceLocation());
12998 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12999 VisitOMPClauseWithPreInit(C);
13000 C->setPriority(Record.readSubExpr());
13001 C->setLParenLoc(Record.readSourceLocation());
13004 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
13005 VisitOMPClauseWithPreInit(C);
13006 C->setGrainsize(Record.readSubExpr());
13007 C->setLParenLoc(Record.readSourceLocation());
13010 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
13011 VisitOMPClauseWithPreInit(C);
13012 C->setNumTasks(Record.readSubExpr());
13013 C->setLParenLoc(Record.readSourceLocation());
13016 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
13017 C->setHint(Record.readSubExpr());
13018 C->setLParenLoc(Record.readSourceLocation());
13021 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
13022 VisitOMPClauseWithPreInit(C);
13023 C->setDistScheduleKind(
13024 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
13025 C->setChunkSize(Record.readSubExpr());
13026 C->setLParenLoc(Record.readSourceLocation());
13027 C->setDistScheduleKindLoc(Record.readSourceLocation());
13028 C->setCommaLoc(Record.readSourceLocation());
13031 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
13032 C->setDefaultmapKind(
13033 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
13034 C->setDefaultmapModifier(
13035 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
13036 C->setLParenLoc(Record.readSourceLocation());
13037 C->setDefaultmapModifierLoc(Record.readSourceLocation());
13038 C->setDefaultmapKindLoc(Record.readSourceLocation());
13041 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
13042 C->setLParenLoc(Record.readSourceLocation());
13043 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13044 DeclarationNameInfo DNI;
13045 Record.readDeclarationNameInfo(DNI);
13046 C->setMapperIdInfo(DNI);
13047 auto NumVars = C->varlist_size();
13048 auto UniqueDecls = C->getUniqueDeclarationsNum();
13049 auto TotalLists = C->getTotalComponentListNum();
13050 auto TotalComponents = C->getTotalComponentsNum();
13052 SmallVector<Expr *, 16> Vars;
13053 Vars.reserve(NumVars);
13054 for (unsigned i = 0; i != NumVars; ++i)
13055 Vars.push_back(Record.readSubExpr());
13056 C->setVarRefs(Vars);
13058 SmallVector<Expr *, 16> UDMappers;
13059 UDMappers.reserve(NumVars);
13060 for (unsigned I = 0; I < NumVars; ++I)
13061 UDMappers.push_back(Record.readSubExpr());
13062 C->setUDMapperRefs(UDMappers);
13064 SmallVector<ValueDecl *, 16> Decls;
13065 Decls.reserve(UniqueDecls);
13066 for (unsigned i = 0; i < UniqueDecls; ++i)
13067 Decls.push_back(Record.readDeclAs<ValueDecl>());
13068 C->setUniqueDecls(Decls);
13070 SmallVector<unsigned, 16> ListsPerDecl;
13071 ListsPerDecl.reserve(UniqueDecls);
13072 for (unsigned i = 0; i < UniqueDecls; ++i)
13073 ListsPerDecl.push_back(Record.readInt());
13074 C->setDeclNumLists(ListsPerDecl);
13076 SmallVector<unsigned, 32> ListSizes;
13077 ListSizes.reserve(TotalLists);
13078 for (unsigned i = 0; i < TotalLists; ++i)
13079 ListSizes.push_back(Record.readInt());
13080 C->setComponentListSizes(ListSizes);
13082 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13083 Components.reserve(TotalComponents);
13084 for (unsigned i = 0; i < TotalComponents; ++i) {
13085 Expr *AssociatedExpr = Record.readSubExpr();
13086 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13087 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13088 AssociatedExpr, AssociatedDecl));
13090 C->setComponents(Components, ListSizes);
13093 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
13094 C->setLParenLoc(Record.readSourceLocation());
13095 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13096 DeclarationNameInfo DNI;
13097 Record.readDeclarationNameInfo(DNI);
13098 C->setMapperIdInfo(DNI);
13099 auto NumVars = C->varlist_size();
13100 auto UniqueDecls = C->getUniqueDeclarationsNum();
13101 auto TotalLists = C->getTotalComponentListNum();
13102 auto TotalComponents = C->getTotalComponentsNum();
13104 SmallVector<Expr *, 16> Vars;
13105 Vars.reserve(NumVars);
13106 for (unsigned i = 0; i != NumVars; ++i)
13107 Vars.push_back(Record.readSubExpr());
13108 C->setVarRefs(Vars);
13110 SmallVector<Expr *, 16> UDMappers;
13111 UDMappers.reserve(NumVars);
13112 for (unsigned I = 0; I < NumVars; ++I)
13113 UDMappers.push_back(Record.readSubExpr());
13114 C->setUDMapperRefs(UDMappers);
13116 SmallVector<ValueDecl *, 16> Decls;
13117 Decls.reserve(UniqueDecls);
13118 for (unsigned i = 0; i < UniqueDecls; ++i)
13119 Decls.push_back(Record.readDeclAs<ValueDecl>());
13120 C->setUniqueDecls(Decls);
13122 SmallVector<unsigned, 16> ListsPerDecl;
13123 ListsPerDecl.reserve(UniqueDecls);
13124 for (unsigned i = 0; i < UniqueDecls; ++i)
13125 ListsPerDecl.push_back(Record.readInt());
13126 C->setDeclNumLists(ListsPerDecl);
13128 SmallVector<unsigned, 32> ListSizes;
13129 ListSizes.reserve(TotalLists);
13130 for (unsigned i = 0; i < TotalLists; ++i)
13131 ListSizes.push_back(Record.readInt());
13132 C->setComponentListSizes(ListSizes);
13134 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13135 Components.reserve(TotalComponents);
13136 for (unsigned i = 0; i < TotalComponents; ++i) {
13137 Expr *AssociatedExpr = Record.readSubExpr();
13138 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13139 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13140 AssociatedExpr, AssociatedDecl));
13142 C->setComponents(Components, ListSizes);
13145 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13146 C->setLParenLoc(Record.readSourceLocation());
13147 auto NumVars = C->varlist_size();
13148 auto UniqueDecls = C->getUniqueDeclarationsNum();
13149 auto TotalLists = C->getTotalComponentListNum();
13150 auto TotalComponents = C->getTotalComponentsNum();
13152 SmallVector<Expr *, 16> Vars;
13153 Vars.reserve(NumVars);
13154 for (unsigned i = 0; i != NumVars; ++i)
13155 Vars.push_back(Record.readSubExpr());
13156 C->setVarRefs(Vars);
13158 for (unsigned i = 0; i != NumVars; ++i)
13159 Vars.push_back(Record.readSubExpr());
13160 C->setPrivateCopies(Vars);
13162 for (unsigned i = 0; i != NumVars; ++i)
13163 Vars.push_back(Record.readSubExpr());
13166 SmallVector<ValueDecl *, 16> Decls;
13167 Decls.reserve(UniqueDecls);
13168 for (unsigned i = 0; i < UniqueDecls; ++i)
13169 Decls.push_back(Record.readDeclAs<ValueDecl>());
13170 C->setUniqueDecls(Decls);
13172 SmallVector<unsigned, 16> ListsPerDecl;
13173 ListsPerDecl.reserve(UniqueDecls);
13174 for (unsigned i = 0; i < UniqueDecls; ++i)
13175 ListsPerDecl.push_back(Record.readInt());
13176 C->setDeclNumLists(ListsPerDecl);
13178 SmallVector<unsigned, 32> ListSizes;
13179 ListSizes.reserve(TotalLists);
13180 for (unsigned i = 0; i < TotalLists; ++i)
13181 ListSizes.push_back(Record.readInt());
13182 C->setComponentListSizes(ListSizes);
13184 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13185 Components.reserve(TotalComponents);
13186 for (unsigned i = 0; i < TotalComponents; ++i) {
13187 Expr *AssociatedExpr = Record.readSubExpr();
13188 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13189 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13190 AssociatedExpr, AssociatedDecl));
13192 C->setComponents(Components, ListSizes);
13195 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13196 C->setLParenLoc(Record.readSourceLocation());
13197 auto NumVars = C->varlist_size();
13198 auto UniqueDecls = C->getUniqueDeclarationsNum();
13199 auto TotalLists = C->getTotalComponentListNum();
13200 auto TotalComponents = C->getTotalComponentsNum();
13202 SmallVector<Expr *, 16> Vars;
13203 Vars.reserve(NumVars);
13204 for (unsigned i = 0; i != NumVars; ++i)
13205 Vars.push_back(Record.readSubExpr());
13206 C->setVarRefs(Vars);
13209 SmallVector<ValueDecl *, 16> Decls;
13210 Decls.reserve(UniqueDecls);
13211 for (unsigned i = 0; i < UniqueDecls; ++i)
13212 Decls.push_back(Record.readDeclAs<ValueDecl>());
13213 C->setUniqueDecls(Decls);
13215 SmallVector<unsigned, 16> ListsPerDecl;
13216 ListsPerDecl.reserve(UniqueDecls);
13217 for (unsigned i = 0; i < UniqueDecls; ++i)
13218 ListsPerDecl.push_back(Record.readInt());
13219 C->setDeclNumLists(ListsPerDecl);
13221 SmallVector<unsigned, 32> ListSizes;
13222 ListSizes.reserve(TotalLists);
13223 for (unsigned i = 0; i < TotalLists; ++i)
13224 ListSizes.push_back(Record.readInt());
13225 C->setComponentListSizes(ListSizes);
13227 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13228 Components.reserve(TotalComponents);
13229 for (unsigned i = 0; i < TotalComponents; ++i) {
13230 Expr *AssociatedExpr = Record.readSubExpr();
13231 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13232 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13233 AssociatedExpr, AssociatedDecl));
13235 C->setComponents(Components, ListSizes);