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/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/MemoryBuffer.h"
118 #include "llvm/Support/Path.h"
119 #include "llvm/Support/SaveAndRestore.h"
120 #include "llvm/Support/Timer.h"
121 #include "llvm/Support/VersionTuple.h"
122 #include "llvm/Support/raw_ostream.h"
134 #include <system_error>
139 using namespace clang;
140 using namespace clang::serialization;
141 using namespace clang::serialization::reader;
142 using llvm::BitstreamCursor;
143 using llvm::RoundingMode;
145 //===----------------------------------------------------------------------===//
146 // ChainedASTReaderListener implementation
147 //===----------------------------------------------------------------------===//
150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151 return First->ReadFullVersionInformation(FullVersion) ||
152 Second->ReadFullVersionInformation(FullVersion);
155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156 First->ReadModuleName(ModuleName);
157 Second->ReadModuleName(ModuleName);
160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161 First->ReadModuleMapFile(ModuleMapPath);
162 Second->ReadModuleMapFile(ModuleMapPath);
166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168 bool AllowCompatibleDifferences) {
169 return First->ReadLanguageOptions(LangOpts, Complain,
170 AllowCompatibleDifferences) ||
171 Second->ReadLanguageOptions(LangOpts, Complain,
172 AllowCompatibleDifferences);
175 bool ChainedASTReaderListener::ReadTargetOptions(
176 const TargetOptions &TargetOpts, bool Complain,
177 bool AllowCompatibleDifferences) {
178 return First->ReadTargetOptions(TargetOpts, Complain,
179 AllowCompatibleDifferences) ||
180 Second->ReadTargetOptions(TargetOpts, Complain,
181 AllowCompatibleDifferences);
184 bool ChainedASTReaderListener::ReadDiagnosticOptions(
185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187 Second->ReadDiagnosticOptions(DiagOpts, Complain);
191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193 return First->ReadFileSystemOptions(FSOpts, Complain) ||
194 Second->ReadFileSystemOptions(FSOpts, Complain);
197 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
206 bool ChainedASTReaderListener::ReadPreprocessorOptions(
207 const PreprocessorOptions &PPOpts, bool Complain,
208 std::string &SuggestedPredefines) {
209 return First->ReadPreprocessorOptions(PPOpts, Complain,
210 SuggestedPredefines) ||
211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216 First->ReadCounter(M, Value);
217 Second->ReadCounter(M, Value);
220 bool ChainedASTReaderListener::needsInputFileVisitation() {
221 return First->needsInputFileVisitation() ||
222 Second->needsInputFileVisitation();
225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226 return First->needsSystemInputFileVisitation() ||
227 Second->needsSystemInputFileVisitation();
230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232 First->visitModuleFile(Filename, Kind);
233 Second->visitModuleFile(Filename, Kind);
236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
239 bool isExplicitModule) {
240 bool Continue = false;
241 if (First->needsInputFileVisitation() &&
242 (!isSystem || First->needsSystemInputFileVisitation()))
243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245 if (Second->needsInputFileVisitation() &&
246 (!isSystem || Second->needsSystemInputFileVisitation()))
247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
252 void ChainedASTReaderListener::readModuleFileExtension(
253 const ModuleFileExtensionMetadata &Metadata) {
254 First->readModuleFileExtension(Metadata);
255 Second->readModuleFileExtension(Metadata);
258 //===----------------------------------------------------------------------===//
259 // PCH validator implementation
260 //===----------------------------------------------------------------------===//
262 ASTReaderListener::~ASTReaderListener() = default;
264 /// Compare the given set of language options against an existing set of
265 /// language options.
267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268 /// \param AllowCompatibleDifferences If true, differences between compatible
269 /// language options will be permitted.
271 /// \returns true if the languagae options mis-match, false otherwise.
272 static bool checkLanguageOptions(const LangOptions &LangOpts,
273 const LangOptions &ExistingLangOpts,
274 DiagnosticsEngine *Diags,
275 bool AllowCompatibleDifferences = true) {
276 #define LANGOPT(Name, Bits, Default, Description) \
277 if (ExistingLangOpts.Name != LangOpts.Name) { \
279 Diags->Report(diag::err_pch_langopt_mismatch) \
280 << Description << LangOpts.Name << ExistingLangOpts.Name; \
284 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
285 if (ExistingLangOpts.Name != LangOpts.Name) { \
287 Diags->Report(diag::err_pch_langopt_value_mismatch) \
292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
295 Diags->Report(diag::err_pch_langopt_value_mismatch) \
300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
301 if (!AllowCompatibleDifferences) \
302 LANGOPT(Name, Bits, Default, Description)
304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
305 if (!AllowCompatibleDifferences) \
306 ENUM_LANGOPT(Name, Bits, Default, Description)
308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309 if (!AllowCompatibleDifferences) \
310 VALUE_LANGOPT(Name, Bits, Default, Description)
312 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315 #include "clang/Basic/LangOptions.def"
317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325 Diags->Report(diag::err_pch_langopt_value_mismatch)
326 << "target Objective-C runtime";
330 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331 LangOpts.CommentOpts.BlockCommandNames) {
333 Diags->Report(diag::err_pch_langopt_value_mismatch)
334 << "block command names";
338 // Sanitizer feature mismatches are treated as compatible differences. If
339 // compatible differences aren't allowed, we still only want to check for
340 // mismatches of non-modular sanitizers (the only ones which can affect AST
342 if (!AllowCompatibleDifferences) {
343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346 ExistingSanitizers.clear(ModularSanitizers);
347 ImportedSanitizers.clear(ModularSanitizers);
348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349 const std::string Flag = "-fsanitize=";
351 #define SANITIZER(NAME, ID) \
353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
355 if (InExistingModule != InImportedModule) \
356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
357 << InExistingModule << (Flag + NAME); \
359 #include "clang/Basic/Sanitizers.def"
368 /// Compare the given set of target options against an existing set of
371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 /// \returns true if the target options mis-match, false otherwise.
374 static bool checkTargetOptions(const TargetOptions &TargetOpts,
375 const TargetOptions &ExistingTargetOpts,
376 DiagnosticsEngine *Diags,
377 bool AllowCompatibleDifferences = true) {
378 #define CHECK_TARGET_OPT(Field, Name) \
379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
381 Diags->Report(diag::err_pch_targetopt_mismatch) \
382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
386 // The triple and ABI must match exactly.
387 CHECK_TARGET_OPT(Triple, "target");
388 CHECK_TARGET_OPT(ABI, "target ABI");
390 // We can tolerate different CPUs in many cases, notably when one CPU
391 // supports a strict superset of another. When allowing compatible
392 // differences skip this check.
393 if (!AllowCompatibleDifferences)
394 CHECK_TARGET_OPT(CPU, "target CPU");
396 #undef CHECK_TARGET_OPT
398 // Compare feature sets.
399 SmallVector<StringRef, 4> ExistingFeatures(
400 ExistingTargetOpts.FeaturesAsWritten.begin(),
401 ExistingTargetOpts.FeaturesAsWritten.end());
402 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
403 TargetOpts.FeaturesAsWritten.end());
404 llvm::sort(ExistingFeatures);
405 llvm::sort(ReadFeatures);
407 // We compute the set difference in both directions explicitly so that we can
408 // diagnose the differences differently.
409 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
411 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
412 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
413 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
414 ExistingFeatures.begin(), ExistingFeatures.end(),
415 std::back_inserter(UnmatchedReadFeatures));
417 // If we are allowing compatible differences and the read feature set is
418 // a strict subset of the existing feature set, there is nothing to diagnose.
419 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423 for (StringRef Feature : UnmatchedReadFeatures)
424 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
425 << /* is-existing-feature */ false << Feature;
426 for (StringRef Feature : UnmatchedExistingFeatures)
427 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428 << /* is-existing-feature */ true << Feature;
431 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
437 bool AllowCompatibleDifferences) {
438 const LangOptions &ExistingLangOpts = PP.getLangOpts();
439 return checkLanguageOptions(LangOpts, ExistingLangOpts,
440 Complain ? &Reader.Diags : nullptr,
441 AllowCompatibleDifferences);
444 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
446 bool AllowCompatibleDifferences) {
447 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
448 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
449 Complain ? &Reader.Diags : nullptr,
450 AllowCompatibleDifferences);
455 using MacroDefinitionsMap =
456 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
457 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
462 DiagnosticsEngine &Diags,
464 using Level = DiagnosticsEngine::Level;
466 // Check current mappings for new -Werror mappings, and the stored mappings
467 // for cases that were explicitly mapped to *not* be errors that are now
468 // errors because of options like -Werror.
469 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
471 for (DiagnosticsEngine *MappingSource : MappingSources) {
472 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
473 diag::kind DiagID = DiagIDMappingPair.first;
474 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
475 if (CurLevel < DiagnosticsEngine::Error)
476 continue; // not significant
478 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
479 if (StoredLevel < DiagnosticsEngine::Error) {
481 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
482 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
491 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
492 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
493 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
495 return Ext >= diag::Severity::Error;
498 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
499 DiagnosticsEngine &Diags,
500 bool IsSystem, bool Complain) {
503 if (Diags.getSuppressSystemWarnings())
505 // If -Wsystem-headers was not enabled before, be conservative
506 if (StoredDiags.getSuppressSystemWarnings()) {
508 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
513 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
515 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
520 !StoredDiags.getEnableAllWarnings()) {
522 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526 if (isExtHandlingFromDiagsError(Diags) &&
527 !isExtHandlingFromDiagsError(StoredDiags)) {
529 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
536 /// Return the top import module if it is implicit, nullptr otherwise.
537 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
539 // If the original import came from a file explicitly generated by the user,
540 // don't check the diagnostic mappings.
541 // FIXME: currently this is approximated by checking whether this is not a
542 // module import of an implicitly-loaded module file.
543 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
544 // the transitive closure of its imports, since unrelated modules cannot be
545 // imported until after this module finishes validation.
546 ModuleFile *TopImport = &*ModuleMgr.rbegin();
547 while (!TopImport->ImportedBy.empty())
548 TopImport = TopImport->ImportedBy[0];
549 if (TopImport->Kind != MK_ImplicitModule)
552 StringRef ModuleName = TopImport->ModuleName;
553 assert(!ModuleName.empty() && "diagnostic options read before module name");
555 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
556 assert(M && "missing module");
560 bool PCHValidator::ReadDiagnosticOptions(
561 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
562 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
563 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
564 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
565 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
566 // This should never fail, because we would have processed these options
567 // before writing them to an ASTFile.
568 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
570 ModuleManager &ModuleMgr = Reader.getModuleManager();
571 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
573 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
577 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
578 // contains the union of their flags.
579 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
583 /// Collect the macro definitions provided by the given preprocessor
586 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
587 MacroDefinitionsMap &Macros,
588 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
589 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
590 StringRef Macro = PPOpts.Macros[I].first;
591 bool IsUndef = PPOpts.Macros[I].second;
593 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
594 StringRef MacroName = MacroPair.first;
595 StringRef MacroBody = MacroPair.second;
597 // For an #undef'd macro, we only care about the name.
599 if (MacroNames && !Macros.count(MacroName))
600 MacroNames->push_back(MacroName);
602 Macros[MacroName] = std::make_pair("", true);
606 // For a #define'd macro, figure out the actual definition.
607 if (MacroName.size() == Macro.size())
610 // Note: GCC drops anything following an end-of-line character.
611 StringRef::size_type End = MacroBody.find_first_of("\n\r");
612 MacroBody = MacroBody.substr(0, End);
615 if (MacroNames && !Macros.count(MacroName))
616 MacroNames->push_back(MacroName);
617 Macros[MacroName] = std::make_pair(MacroBody, false);
621 /// Check the preprocessor options deserialized from the control block
622 /// against the preprocessor options in an existing preprocessor.
624 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
625 /// \param Validate If true, validate preprocessor options. If false, allow
626 /// macros defined by \p ExistingPPOpts to override those defined by
627 /// \p PPOpts in SuggestedPredefines.
628 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
629 const PreprocessorOptions &ExistingPPOpts,
630 DiagnosticsEngine *Diags,
631 FileManager &FileMgr,
632 std::string &SuggestedPredefines,
633 const LangOptions &LangOpts,
634 bool Validate = true) {
635 // Check macro definitions.
636 MacroDefinitionsMap ASTFileMacros;
637 collectMacroDefinitions(PPOpts, ASTFileMacros);
638 MacroDefinitionsMap ExistingMacros;
639 SmallVector<StringRef, 4> ExistingMacroNames;
640 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
642 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
643 // Dig out the macro definition in the existing preprocessor options.
644 StringRef MacroName = ExistingMacroNames[I];
645 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
647 // Check whether we know anything about this macro name or not.
648 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
649 ASTFileMacros.find(MacroName);
650 if (!Validate || Known == ASTFileMacros.end()) {
651 // FIXME: Check whether this identifier was referenced anywhere in the
652 // AST file. If so, we should reject the AST file. Unfortunately, this
653 // information isn't in the control block. What shall we do about it?
655 if (Existing.second) {
656 SuggestedPredefines += "#undef ";
657 SuggestedPredefines += MacroName.str();
658 SuggestedPredefines += '\n';
660 SuggestedPredefines += "#define ";
661 SuggestedPredefines += MacroName.str();
662 SuggestedPredefines += ' ';
663 SuggestedPredefines += Existing.first.str();
664 SuggestedPredefines += '\n';
669 // If the macro was defined in one but undef'd in the other, we have a
671 if (Existing.second != Known->second.second) {
673 Diags->Report(diag::err_pch_macro_def_undef)
674 << MacroName << Known->second.second;
679 // If the macro was #undef'd in both, or if the macro bodies are identical,
681 if (Existing.second || Existing.first == Known->second.first)
684 // The macro bodies differ; complain.
686 Diags->Report(diag::err_pch_macro_def_conflict)
687 << MacroName << Known->second.first << Existing.first;
692 // Check whether we're using predefines.
693 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
695 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
700 // Detailed record is important since it is used for the module cache hash.
701 if (LangOpts.Modules &&
702 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
704 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
709 // Compute the #include and #include_macros lines we need.
710 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
711 StringRef File = ExistingPPOpts.Includes[I];
713 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
714 !ExistingPPOpts.PCHThroughHeader.empty()) {
715 // In case the through header is an include, we must add all the includes
716 // to the predefines so the start point can be determined.
717 SuggestedPredefines += "#include \"";
718 SuggestedPredefines += File;
719 SuggestedPredefines += "\"\n";
723 if (File == ExistingPPOpts.ImplicitPCHInclude)
726 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
727 != PPOpts.Includes.end())
730 SuggestedPredefines += "#include \"";
731 SuggestedPredefines += File;
732 SuggestedPredefines += "\"\n";
735 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
736 StringRef File = ExistingPPOpts.MacroIncludes[I];
737 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
739 != PPOpts.MacroIncludes.end())
742 SuggestedPredefines += "#__include_macros \"";
743 SuggestedPredefines += File;
744 SuggestedPredefines += "\"\n##\n";
750 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
752 std::string &SuggestedPredefines) {
753 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
755 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
756 Complain? &Reader.Diags : nullptr,
762 bool SimpleASTReaderListener::ReadPreprocessorOptions(
763 const PreprocessorOptions &PPOpts,
765 std::string &SuggestedPredefines) {
766 return checkPreprocessorOptions(PPOpts,
767 PP.getPreprocessorOpts(),
775 /// Check the header search options deserialized from the control block
776 /// against the header search options in an existing preprocessor.
778 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
779 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
780 StringRef SpecificModuleCachePath,
781 StringRef ExistingModuleCachePath,
782 DiagnosticsEngine *Diags,
783 const LangOptions &LangOpts) {
784 if (LangOpts.Modules) {
785 if (SpecificModuleCachePath != ExistingModuleCachePath) {
787 Diags->Report(diag::err_pch_modulecache_mismatch)
788 << SpecificModuleCachePath << ExistingModuleCachePath;
796 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
797 StringRef SpecificModuleCachePath,
799 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
800 PP.getHeaderSearchInfo().getModuleCachePath(),
801 Complain ? &Reader.Diags : nullptr,
805 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
806 PP.setCounterValue(Value);
809 //===----------------------------------------------------------------------===//
810 // AST reader implementation
811 //===----------------------------------------------------------------------===//
813 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
814 bool TakeOwnership) {
815 DeserializationListener = Listener;
816 OwnsDeserializationListener = TakeOwnership;
819 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
820 return serialization::ComputeHash(Sel);
823 std::pair<unsigned, unsigned>
824 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
825 using namespace llvm::support;
827 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
828 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
829 return std::make_pair(KeyLen, DataLen);
832 ASTSelectorLookupTrait::internal_key_type
833 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
834 using namespace llvm::support;
836 SelectorTable &SelTable = Reader.getContext().Selectors;
837 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
838 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
839 F, endian::readNext<uint32_t, little, unaligned>(d));
841 return SelTable.getNullarySelector(FirstII);
843 return SelTable.getUnarySelector(FirstII);
845 SmallVector<IdentifierInfo *, 16> Args;
846 Args.push_back(FirstII);
847 for (unsigned I = 1; I != N; ++I)
848 Args.push_back(Reader.getLocalIdentifier(
849 F, endian::readNext<uint32_t, little, unaligned>(d)));
851 return SelTable.getSelector(N, Args.data());
854 ASTSelectorLookupTrait::data_type
855 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
857 using namespace llvm::support;
861 Result.ID = Reader.getGlobalSelectorID(
862 F, endian::readNext<uint32_t, little, unaligned>(d));
863 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
864 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
865 Result.InstanceBits = FullInstanceBits & 0x3;
866 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
867 Result.FactoryBits = FullFactoryBits & 0x3;
868 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
869 unsigned NumInstanceMethods = FullInstanceBits >> 3;
870 unsigned NumFactoryMethods = FullFactoryBits >> 3;
872 // Load instance methods
873 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
874 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
875 F, endian::readNext<uint32_t, little, unaligned>(d)))
876 Result.Instance.push_back(Method);
879 // Load factory methods
880 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
881 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
882 F, endian::readNext<uint32_t, little, unaligned>(d)))
883 Result.Factory.push_back(Method);
889 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
890 return llvm::djbHash(a);
893 std::pair<unsigned, unsigned>
894 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
895 using namespace llvm::support;
897 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
899 return std::make_pair(KeyLen, DataLen);
902 ASTIdentifierLookupTraitBase::internal_key_type
903 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
904 assert(n >= 2 && d[n-1] == '\0');
905 return StringRef((const char*) d, n-1);
908 /// Whether the given identifier is "interesting".
909 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
911 return II.hadMacroDefinition() ||
913 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
914 II.hasRevertedTokenIDToIdentifier() ||
915 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
916 II.getFETokenInfo());
919 static bool readBit(unsigned &Bits) {
920 bool Value = Bits & 0x1;
925 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
926 using namespace llvm::support;
928 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
929 return Reader.getGlobalIdentifierID(F, RawID >> 1);
932 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
933 if (!II.isFromAST()) {
935 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
936 if (isInterestingIdentifier(Reader, II, IsModule))
937 II.setChangedSinceDeserialization();
941 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
942 const unsigned char* d,
944 using namespace llvm::support;
946 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
947 bool IsInteresting = RawID & 0x01;
949 // Wipe out the "is interesting" bit.
952 // Build the IdentifierInfo and link the identifier ID with it.
953 IdentifierInfo *II = KnownII;
955 II = &Reader.getIdentifierTable().getOwn(k);
958 markIdentifierFromAST(Reader, *II);
959 Reader.markIdentifierUpToDate(II);
961 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
962 if (!IsInteresting) {
963 // For uninteresting identifiers, there's nothing else to do. Just notify
964 // the reader that we've finished loading this identifier.
965 Reader.SetIdentifierInfo(ID, II);
969 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
970 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
971 bool CPlusPlusOperatorKeyword = readBit(Bits);
972 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
973 bool HasRevertedBuiltin = readBit(Bits);
974 bool Poisoned = readBit(Bits);
975 bool ExtensionToken = readBit(Bits);
976 bool HadMacroDefinition = readBit(Bits);
978 assert(Bits == 0 && "Extra bits in the identifier?");
981 // Set or check the various bits in the IdentifierInfo structure.
982 // Token IDs are read-only.
983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
984 II->revertTokenIDToIdentifier();
986 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
987 else if (HasRevertedBuiltin && II->getBuiltinID()) {
989 assert((II->hasRevertedBuiltin() ||
990 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
991 "Incorrect ObjC keyword or builtin ID");
993 assert(II->isExtensionToken() == ExtensionToken &&
994 "Incorrect extension token flag");
995 (void)ExtensionToken;
997 II->setIsPoisoned(true);
998 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
999 "Incorrect C++ operator keyword flag");
1000 (void)CPlusPlusOperatorKeyword;
1002 // If this identifier is a macro, deserialize the macro
1004 if (HadMacroDefinition) {
1005 uint32_t MacroDirectivesOffset =
1006 endian::readNext<uint32_t, little, unaligned>(d);
1009 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1012 Reader.SetIdentifierInfo(ID, II);
1014 // Read all of the declarations visible at global scope with this
1017 SmallVector<uint32_t, 4> DeclIDs;
1018 for (; DataLen > 0; DataLen -= 4)
1019 DeclIDs.push_back(Reader.getGlobalDeclID(
1020 F, endian::readNext<uint32_t, little, unaligned>(d)));
1021 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1027 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1028 : Kind(Name.getNameKind()) {
1030 case DeclarationName::Identifier:
1031 Data = (uint64_t)Name.getAsIdentifierInfo();
1033 case DeclarationName::ObjCZeroArgSelector:
1034 case DeclarationName::ObjCOneArgSelector:
1035 case DeclarationName::ObjCMultiArgSelector:
1036 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1038 case DeclarationName::CXXOperatorName:
1039 Data = Name.getCXXOverloadedOperator();
1041 case DeclarationName::CXXLiteralOperatorName:
1042 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1044 case DeclarationName::CXXDeductionGuideName:
1045 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1046 ->getDeclName().getAsIdentifierInfo();
1048 case DeclarationName::CXXConstructorName:
1049 case DeclarationName::CXXDestructorName:
1050 case DeclarationName::CXXConversionFunctionName:
1051 case DeclarationName::CXXUsingDirective:
1057 unsigned DeclarationNameKey::getHash() const {
1058 llvm::FoldingSetNodeID ID;
1059 ID.AddInteger(Kind);
1062 case DeclarationName::Identifier:
1063 case DeclarationName::CXXLiteralOperatorName:
1064 case DeclarationName::CXXDeductionGuideName:
1065 ID.AddString(((IdentifierInfo*)Data)->getName());
1067 case DeclarationName::ObjCZeroArgSelector:
1068 case DeclarationName::ObjCOneArgSelector:
1069 case DeclarationName::ObjCMultiArgSelector:
1070 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1072 case DeclarationName::CXXOperatorName:
1073 ID.AddInteger((OverloadedOperatorKind)Data);
1075 case DeclarationName::CXXConstructorName:
1076 case DeclarationName::CXXDestructorName:
1077 case DeclarationName::CXXConversionFunctionName:
1078 case DeclarationName::CXXUsingDirective:
1082 return ID.ComputeHash();
1086 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1087 using namespace llvm::support;
1089 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1090 return Reader.getLocalModuleFile(F, ModuleFileID);
1093 std::pair<unsigned, unsigned>
1094 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1095 using namespace llvm::support;
1097 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1098 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1099 return std::make_pair(KeyLen, DataLen);
1102 ASTDeclContextNameLookupTrait::internal_key_type
1103 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1104 using namespace llvm::support;
1106 auto Kind = (DeclarationName::NameKind)*d++;
1109 case DeclarationName::Identifier:
1110 case DeclarationName::CXXLiteralOperatorName:
1111 case DeclarationName::CXXDeductionGuideName:
1112 Data = (uint64_t)Reader.getLocalIdentifier(
1113 F, endian::readNext<uint32_t, little, unaligned>(d));
1115 case DeclarationName::ObjCZeroArgSelector:
1116 case DeclarationName::ObjCOneArgSelector:
1117 case DeclarationName::ObjCMultiArgSelector:
1119 (uint64_t)Reader.getLocalSelector(
1120 F, endian::readNext<uint32_t, little, unaligned>(
1121 d)).getAsOpaquePtr();
1123 case DeclarationName::CXXOperatorName:
1124 Data = *d++; // OverloadedOperatorKind
1126 case DeclarationName::CXXConstructorName:
1127 case DeclarationName::CXXDestructorName:
1128 case DeclarationName::CXXConversionFunctionName:
1129 case DeclarationName::CXXUsingDirective:
1134 return DeclarationNameKey(Kind, Data);
1137 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1138 const unsigned char *d,
1140 data_type_builder &Val) {
1141 using namespace llvm::support;
1143 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1144 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1145 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1149 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1150 BitstreamCursor &Cursor,
1153 assert(Offset != 0);
1155 SavedStreamPosition SavedPosition(Cursor);
1156 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1157 Error(std::move(Err));
1163 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1165 Error(MaybeCode.takeError());
1168 unsigned Code = MaybeCode.get();
1170 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1171 if (!MaybeRecCode) {
1172 Error(MaybeRecCode.takeError());
1175 unsigned RecCode = MaybeRecCode.get();
1176 if (RecCode != DECL_CONTEXT_LEXICAL) {
1177 Error("Expected lexical block");
1181 assert(!isa<TranslationUnitDecl>(DC) &&
1182 "expected a TU_UPDATE_LEXICAL record for TU");
1183 // If we are handling a C++ class template instantiation, we can see multiple
1184 // lexical updates for the same record. It's important that we select only one
1185 // of them, so that field numbering works properly. Just pick the first one we
1187 auto &Lex = LexicalDecls[DC];
1189 Lex = std::make_pair(
1190 &M, llvm::makeArrayRef(
1191 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1195 DC->setHasExternalLexicalStorage(true);
1199 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1200 BitstreamCursor &Cursor,
1203 assert(Offset != 0);
1205 SavedStreamPosition SavedPosition(Cursor);
1206 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1207 Error(std::move(Err));
1213 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1215 Error(MaybeCode.takeError());
1218 unsigned Code = MaybeCode.get();
1220 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1221 if (!MaybeRecCode) {
1222 Error(MaybeRecCode.takeError());
1225 unsigned RecCode = MaybeRecCode.get();
1226 if (RecCode != DECL_CONTEXT_VISIBLE) {
1227 Error("Expected visible lookup table block");
1231 // We can't safely determine the primary context yet, so delay attaching the
1232 // lookup table until we're done with recursive deserialization.
1233 auto *Data = (const unsigned char*)Blob.data();
1234 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1238 void ASTReader::Error(StringRef Msg) const {
1239 Error(diag::err_fe_pch_malformed, Msg);
1240 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1241 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1242 Diag(diag::note_module_cache_path)
1243 << PP.getHeaderSearchInfo().getModuleCachePath();
1247 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1248 StringRef Arg3) const {
1249 if (Diags.isDiagnosticInFlight())
1250 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1252 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256 unsigned Select) const {
1257 if (!Diags.isDiagnosticInFlight())
1258 Diag(DiagID) << Arg1 << Arg2 << Select;
1261 void ASTReader::Error(llvm::Error &&Err) const {
1262 Error(toString(std::move(Err)));
1265 //===----------------------------------------------------------------------===//
1266 // Source Manager Deserialization
1267 //===----------------------------------------------------------------------===//
1269 /// Read the line table in the source manager block.
1270 /// \returns true if there was an error.
1271 bool ASTReader::ParseLineTable(ModuleFile &F,
1272 const RecordData &Record) {
1274 LineTableInfo &LineTable = SourceMgr.getLineTable();
1276 // Parse the file names
1277 std::map<int, int> FileIDs;
1278 FileIDs[-1] = -1; // For unspecified filenames.
1279 for (unsigned I = 0; Record[Idx]; ++I) {
1280 // Extract the file name
1281 auto Filename = ReadPath(F, Record, Idx);
1282 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1286 // Parse the line entries
1287 std::vector<LineEntry> Entries;
1288 while (Idx < Record.size()) {
1289 int FID = Record[Idx++];
1290 assert(FID >= 0 && "Serialized line entries for non-local file.");
1291 // Remap FileID from 1-based old view.
1292 FID += F.SLocEntryBaseID - 1;
1294 // Extract the line entries
1295 unsigned NumEntries = Record[Idx++];
1296 assert(NumEntries && "no line entries for file ID");
1298 Entries.reserve(NumEntries);
1299 for (unsigned I = 0; I != NumEntries; ++I) {
1300 unsigned FileOffset = Record[Idx++];
1301 unsigned LineNo = Record[Idx++];
1302 int FilenameID = FileIDs[Record[Idx++]];
1303 SrcMgr::CharacteristicKind FileKind
1304 = (SrcMgr::CharacteristicKind)Record[Idx++];
1305 unsigned IncludeOffset = Record[Idx++];
1306 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1307 FileKind, IncludeOffset));
1309 LineTable.AddEntry(FileID::get(FID), Entries);
1315 /// Read a source manager block
1316 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1317 using namespace SrcMgr;
1319 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1321 // Set the source-location entry cursor to the current position in
1322 // the stream. This cursor will be used to read the contents of the
1323 // source manager block initially, and then lazily read
1324 // source-location entries as needed.
1325 SLocEntryCursor = F.Stream;
1327 // The stream itself is going to skip over the source manager block.
1328 if (llvm::Error Err = F.Stream.SkipBlock()) {
1329 Error(std::move(Err));
1333 // Enter the source manager block.
1334 if (llvm::Error Err =
1335 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1336 Error(std::move(Err));
1339 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1343 Expected<llvm::BitstreamEntry> MaybeE =
1344 SLocEntryCursor.advanceSkippingSubblocks();
1346 Error(MaybeE.takeError());
1349 llvm::BitstreamEntry E = MaybeE.get();
1352 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1353 case llvm::BitstreamEntry::Error:
1354 Error("malformed block record in AST file");
1356 case llvm::BitstreamEntry::EndBlock:
1358 case llvm::BitstreamEntry::Record:
1359 // The interesting case.
1366 Expected<unsigned> MaybeRecord =
1367 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1369 Error(MaybeRecord.takeError());
1372 switch (MaybeRecord.get()) {
1373 default: // Default behavior: ignore.
1376 case SM_SLOC_FILE_ENTRY:
1377 case SM_SLOC_BUFFER_ENTRY:
1378 case SM_SLOC_EXPANSION_ENTRY:
1379 // Once we hit one of the source location entries, we're done.
1385 /// If a header file is not found at the path that we expect it to be
1386 /// and the PCH file was moved from its original location, try to resolve the
1387 /// file by assuming that header+PCH were moved together and the header is in
1388 /// the same place relative to the PCH.
1390 resolveFileRelativeToOriginalDir(const std::string &Filename,
1391 const std::string &OriginalDir,
1392 const std::string &CurrDir) {
1393 assert(OriginalDir != CurrDir &&
1394 "No point trying to resolve the file if the PCH dir didn't change");
1396 using namespace llvm::sys;
1398 SmallString<128> filePath(Filename);
1399 fs::make_absolute(filePath);
1400 assert(path::is_absolute(OriginalDir));
1401 SmallString<128> currPCHPath(CurrDir);
1403 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1404 fileDirE = path::end(path::parent_path(filePath));
1405 path::const_iterator origDirI = path::begin(OriginalDir),
1406 origDirE = path::end(OriginalDir);
1407 // Skip the common path components from filePath and OriginalDir.
1408 while (fileDirI != fileDirE && origDirI != origDirE &&
1409 *fileDirI == *origDirI) {
1413 for (; origDirI != origDirE; ++origDirI)
1414 path::append(currPCHPath, "..");
1415 path::append(currPCHPath, fileDirI, fileDirE);
1416 path::append(currPCHPath, path::filename(Filename));
1417 return std::string(currPCHPath.str());
1420 bool ASTReader::ReadSLocEntry(int ID) {
1424 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1425 Error("source location entry ID out-of-range for AST file");
1429 // Local helper to read the (possibly-compressed) buffer data following the
1431 auto ReadBuffer = [this](
1432 BitstreamCursor &SLocEntryCursor,
1433 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1436 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1438 Error(MaybeCode.takeError());
1441 unsigned Code = MaybeCode.get();
1443 Expected<unsigned> MaybeRecCode =
1444 SLocEntryCursor.readRecord(Code, Record, &Blob);
1445 if (!MaybeRecCode) {
1446 Error(MaybeRecCode.takeError());
1449 unsigned RecCode = MaybeRecCode.get();
1451 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1452 if (!llvm::zlib::isAvailable()) {
1453 Error("zlib is not available");
1456 SmallString<0> Uncompressed;
1458 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1459 Error("could not decompress embedded file contents: " +
1460 llvm::toString(std::move(E)));
1463 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1464 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1465 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1467 Error("AST record has invalid code");
1472 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1473 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1474 F->SLocEntryOffsetsBase +
1475 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1476 Error(std::move(Err));
1480 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1481 unsigned BaseOffset = F->SLocEntryBaseOffset;
1483 ++NumSLocEntriesRead;
1484 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1486 Error(MaybeEntry.takeError());
1489 llvm::BitstreamEntry Entry = MaybeEntry.get();
1491 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1492 Error("incorrectly-formatted source location entry in AST file");
1498 Expected<unsigned> MaybeSLOC =
1499 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1501 Error(MaybeSLOC.takeError());
1504 switch (MaybeSLOC.get()) {
1506 Error("incorrectly-formatted source location entry in AST file");
1509 case SM_SLOC_FILE_ENTRY: {
1510 // We will detect whether a file changed and return 'Failure' for it, but
1511 // we will also try to fail gracefully by setting up the SLocEntry.
1512 unsigned InputID = Record[4];
1513 InputFile IF = getInputFile(*F, InputID);
1514 const FileEntry *File = IF.getFile();
1515 bool OverriddenBuffer = IF.isOverridden();
1517 // Note that we only check if a File was returned. If it was out-of-date
1518 // we have complained but we will continue creating a FileID to recover
1523 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1524 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1525 // This is the module's main file.
1526 IncludeLoc = getImportLocation(F);
1528 SrcMgr::CharacteristicKind
1529 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1530 // FIXME: The FileID should be created from the FileEntryRef.
1531 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1532 ID, BaseOffset + Record[0]);
1533 SrcMgr::FileInfo &FileInfo =
1534 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1535 FileInfo.NumCreatedFIDs = Record[5];
1537 FileInfo.setHasLineDirectives();
1539 unsigned NumFileDecls = Record[7];
1540 if (NumFileDecls && ContextObj) {
1541 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1542 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1543 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1547 const SrcMgr::ContentCache *ContentCache
1548 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1549 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1550 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1551 !ContentCache->getRawBuffer()) {
1552 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1555 SourceMgr.overrideFileContents(File, std::move(Buffer));
1561 case SM_SLOC_BUFFER_ENTRY: {
1562 const char *Name = Blob.data();
1563 unsigned Offset = Record[0];
1564 SrcMgr::CharacteristicKind
1565 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1566 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1567 if (IncludeLoc.isInvalid() && F->isModule()) {
1568 IncludeLoc = getImportLocation(F);
1571 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1574 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1575 BaseOffset + Offset, IncludeLoc);
1579 case SM_SLOC_EXPANSION_ENTRY: {
1580 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1581 SourceMgr.createExpansionLoc(SpellingLoc,
1582 ReadSourceLocation(*F, Record[2]),
1583 ReadSourceLocation(*F, Record[3]),
1587 BaseOffset + Record[0]);
1595 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1597 return std::make_pair(SourceLocation(), "");
1599 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1600 Error("source location entry ID out-of-range for AST file");
1601 return std::make_pair(SourceLocation(), "");
1604 // Find which module file this entry lands in.
1605 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1607 return std::make_pair(SourceLocation(), "");
1609 // FIXME: Can we map this down to a particular submodule? That would be
1611 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1614 /// Find the location where the module F is imported.
1615 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1616 if (F->ImportLoc.isValid())
1617 return F->ImportLoc;
1619 // Otherwise we have a PCH. It's considered to be "imported" at the first
1620 // location of its includer.
1621 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1622 // Main file is the importer.
1623 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1624 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1626 return F->ImportedBy[0]->FirstLoc;
1629 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1630 /// the abbreviations that are at the top of the block and then leave the cursor
1631 /// pointing into the block.
1632 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1633 uint64_t *StartOfBlockOffset) {
1634 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1635 // FIXME this drops errors on the floor.
1636 consumeError(std::move(Err));
1640 if (StartOfBlockOffset)
1641 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1644 uint64_t Offset = Cursor.GetCurrentBitNo();
1645 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1647 // FIXME this drops errors on the floor.
1648 consumeError(MaybeCode.takeError());
1651 unsigned Code = MaybeCode.get();
1653 // We expect all abbrevs to be at the start of the block.
1654 if (Code != llvm::bitc::DEFINE_ABBREV) {
1655 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1656 // FIXME this drops errors on the floor.
1657 consumeError(std::move(Err));
1662 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1663 // FIXME this drops errors on the floor.
1664 consumeError(std::move(Err));
1670 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1674 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1675 Tok.setLength(Record[Idx++]);
1676 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1677 Tok.setIdentifierInfo(II);
1678 Tok.setKind((tok::TokenKind)Record[Idx++]);
1679 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1683 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1684 BitstreamCursor &Stream = F.MacroCursor;
1686 // Keep track of where we are in the stream, then jump back there
1687 // after reading this macro.
1688 SavedStreamPosition SavedPosition(Stream);
1690 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1691 // FIXME this drops errors on the floor.
1692 consumeError(std::move(Err));
1696 SmallVector<IdentifierInfo*, 16> MacroParams;
1697 MacroInfo *Macro = nullptr;
1700 // Advance to the next record, but if we get to the end of the block, don't
1701 // pop it (removing all the abbreviations from the cursor) since we want to
1702 // be able to reseek within the block and read entries.
1703 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1704 Expected<llvm::BitstreamEntry> MaybeEntry =
1705 Stream.advanceSkippingSubblocks(Flags);
1707 Error(MaybeEntry.takeError());
1710 llvm::BitstreamEntry Entry = MaybeEntry.get();
1712 switch (Entry.Kind) {
1713 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1714 case llvm::BitstreamEntry::Error:
1715 Error("malformed block record in AST file");
1717 case llvm::BitstreamEntry::EndBlock:
1719 case llvm::BitstreamEntry::Record:
1720 // The interesting case.
1726 PreprocessorRecordTypes RecType;
1727 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1728 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1730 Error(MaybeRecType.takeError());
1734 case PP_MODULE_MACRO:
1735 case PP_MACRO_DIRECTIVE_HISTORY:
1738 case PP_MACRO_OBJECT_LIKE:
1739 case PP_MACRO_FUNCTION_LIKE: {
1740 // If we already have a macro, that means that we've hit the end
1741 // of the definition of the macro we were looking for. We're
1746 unsigned NextIndex = 1; // Skip identifier ID.
1747 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1748 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1749 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1750 MI->setIsUsed(Record[NextIndex++]);
1751 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1753 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1754 // Decode function-like macro info.
1755 bool isC99VarArgs = Record[NextIndex++];
1756 bool isGNUVarArgs = Record[NextIndex++];
1757 bool hasCommaPasting = Record[NextIndex++];
1758 MacroParams.clear();
1759 unsigned NumArgs = Record[NextIndex++];
1760 for (unsigned i = 0; i != NumArgs; ++i)
1761 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1763 // Install function-like macro info.
1764 MI->setIsFunctionLike();
1765 if (isC99VarArgs) MI->setIsC99Varargs();
1766 if (isGNUVarArgs) MI->setIsGNUVarargs();
1767 if (hasCommaPasting) MI->setHasCommaPasting();
1768 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1771 // Remember that we saw this macro last so that we add the tokens that
1772 // form its body to it.
1775 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1776 Record[NextIndex]) {
1777 // We have a macro definition. Register the association
1778 PreprocessedEntityID
1779 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1780 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1781 PreprocessingRecord::PPEntityID PPID =
1782 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1783 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1784 PPRec.getPreprocessedEntity(PPID));
1786 PPRec.RegisterMacroDefinition(Macro, PPDef);
1794 // If we see a TOKEN before a PP_MACRO_*, then the file is
1795 // erroneous, just pretend we didn't see this.
1799 Token Tok = ReadToken(F, Record, Idx);
1800 Macro->AddTokenToBody(Tok);
1807 PreprocessedEntityID
1808 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1809 unsigned LocalID) const {
1810 if (!M.ModuleOffsetMap.empty())
1811 ReadModuleOffsetMap(M);
1813 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1814 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1815 assert(I != M.PreprocessedEntityRemap.end()
1816 && "Invalid index into preprocessed entity index remap");
1818 return LocalID + I->second;
1821 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1822 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1825 HeaderFileInfoTrait::internal_key_type
1826 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1827 internal_key_type ikey = {FE->getSize(),
1828 M.HasTimestamps ? FE->getModificationTime() : 0,
1829 FE->getName(), /*Imported*/ false};
1833 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1834 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1837 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1840 // Determine whether the actual files are equivalent.
1841 FileManager &FileMgr = Reader.getFileManager();
1842 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1843 if (!Key.Imported) {
1844 if (auto File = FileMgr.getFile(Key.Filename))
1849 std::string Resolved = std::string(Key.Filename);
1850 Reader.ResolveImportedPath(M, Resolved);
1851 if (auto File = FileMgr.getFile(Resolved))
1856 const FileEntry *FEA = GetFile(a);
1857 const FileEntry *FEB = GetFile(b);
1858 return FEA && FEA == FEB;
1861 std::pair<unsigned, unsigned>
1862 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1863 using namespace llvm::support;
1865 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1866 unsigned DataLen = (unsigned) *d++;
1867 return std::make_pair(KeyLen, DataLen);
1870 HeaderFileInfoTrait::internal_key_type
1871 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1872 using namespace llvm::support;
1874 internal_key_type ikey;
1875 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1876 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1877 ikey.Filename = (const char *)d;
1878 ikey.Imported = true;
1882 HeaderFileInfoTrait::data_type
1883 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1885 using namespace llvm::support;
1887 const unsigned char *End = d + DataLen;
1889 unsigned Flags = *d++;
1890 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1891 HFI.isImport |= (Flags >> 5) & 0x01;
1892 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1893 HFI.DirInfo = (Flags >> 1) & 0x07;
1894 HFI.IndexHeaderMapHeader = Flags & 0x01;
1895 // FIXME: Find a better way to handle this. Maybe just store a
1896 // "has been included" flag?
1897 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1899 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1900 M, endian::readNext<uint32_t, little, unaligned>(d));
1901 if (unsigned FrameworkOffset =
1902 endian::readNext<uint32_t, little, unaligned>(d)) {
1903 // The framework offset is 1 greater than the actual offset,
1904 // since 0 is used as an indicator for "no framework name".
1905 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1906 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1909 assert((End - d) % 4 == 0 &&
1910 "Wrong data length in HeaderFileInfo deserialization");
1912 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1913 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1916 // This header is part of a module. Associate it with the module to enable
1917 // implicit module import.
1918 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1919 Module *Mod = Reader.getSubmodule(GlobalSMID);
1920 FileManager &FileMgr = Reader.getFileManager();
1922 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1924 std::string Filename = std::string(key.Filename);
1926 Reader.ResolveImportedPath(M, Filename);
1927 // FIXME: This is not always the right filename-as-written, but we're not
1928 // going to use this information to rebuild the module, so it doesn't make
1929 // a lot of difference.
1930 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1931 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1932 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1935 // This HeaderFileInfo was externally loaded.
1936 HFI.External = true;
1941 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1942 uint32_t MacroDirectivesOffset) {
1943 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1944 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1947 void ASTReader::ReadDefinedMacros() {
1948 // Note that we are loading defined macros.
1949 Deserializing Macros(this);
1951 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1952 BitstreamCursor &MacroCursor = I.MacroCursor;
1954 // If there was no preprocessor block, skip this file.
1955 if (MacroCursor.getBitcodeBytes().empty())
1958 BitstreamCursor Cursor = MacroCursor;
1959 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1960 Error(std::move(Err));
1966 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1968 Error(MaybeE.takeError());
1971 llvm::BitstreamEntry E = MaybeE.get();
1974 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1975 case llvm::BitstreamEntry::Error:
1976 Error("malformed block record in AST file");
1978 case llvm::BitstreamEntry::EndBlock:
1981 case llvm::BitstreamEntry::Record: {
1983 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1985 Error(MaybeRecord.takeError());
1988 switch (MaybeRecord.get()) {
1989 default: // Default behavior: ignore.
1992 case PP_MACRO_OBJECT_LIKE:
1993 case PP_MACRO_FUNCTION_LIKE: {
1994 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1995 if (II->isOutOfDate())
1996 updateOutOfDateIdentifier(*II);
2014 /// Visitor class used to look up identifirs in an AST file.
2015 class IdentifierLookupVisitor {
2018 unsigned PriorGeneration;
2019 unsigned &NumIdentifierLookups;
2020 unsigned &NumIdentifierLookupHits;
2021 IdentifierInfo *Found = nullptr;
2024 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2025 unsigned &NumIdentifierLookups,
2026 unsigned &NumIdentifierLookupHits)
2027 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2028 PriorGeneration(PriorGeneration),
2029 NumIdentifierLookups(NumIdentifierLookups),
2030 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2032 bool operator()(ModuleFile &M) {
2033 // If we've already searched this module file, skip it now.
2034 if (M.Generation <= PriorGeneration)
2037 ASTIdentifierLookupTable *IdTable
2038 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2042 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2044 ++NumIdentifierLookups;
2045 ASTIdentifierLookupTable::iterator Pos =
2046 IdTable->find_hashed(Name, NameHash, &Trait);
2047 if (Pos == IdTable->end())
2050 // Dereferencing the iterator has the effect of building the
2051 // IdentifierInfo node and populating it with the various
2052 // declarations it needs.
2053 ++NumIdentifierLookupHits;
2058 // Retrieve the identifier info found within the module
2060 IdentifierInfo *getIdentifierInfo() const { return Found; }
2065 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2066 // Note that we are loading an identifier.
2067 Deserializing AnIdentifier(this);
2069 unsigned PriorGeneration = 0;
2070 if (getContext().getLangOpts().Modules)
2071 PriorGeneration = IdentifierGeneration[&II];
2073 // If there is a global index, look there first to determine which modules
2074 // provably do not have any results for this identifier.
2075 GlobalModuleIndex::HitSet Hits;
2076 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2077 if (!loadGlobalIndex()) {
2078 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2083 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2084 NumIdentifierLookups,
2085 NumIdentifierLookupHits);
2086 ModuleMgr.visit(Visitor, HitsPtr);
2087 markIdentifierUpToDate(&II);
2090 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2094 II->setOutOfDate(false);
2096 // Update the generation for this identifier.
2097 if (getContext().getLangOpts().Modules)
2098 IdentifierGeneration[II] = getGeneration();
2101 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2102 const PendingMacroInfo &PMInfo) {
2103 ModuleFile &M = *PMInfo.M;
2105 BitstreamCursor &Cursor = M.MacroCursor;
2106 SavedStreamPosition SavedPosition(Cursor);
2107 if (llvm::Error Err =
2108 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2109 Error(std::move(Err));
2113 struct ModuleMacroRecord {
2114 SubmoduleID SubModID;
2116 SmallVector<SubmoduleID, 8> Overrides;
2118 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2120 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2121 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2125 Expected<llvm::BitstreamEntry> MaybeEntry =
2126 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2128 Error(MaybeEntry.takeError());
2131 llvm::BitstreamEntry Entry = MaybeEntry.get();
2133 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2134 Error("malformed block record in AST file");
2139 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2141 Error(MaybePP.takeError());
2144 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2145 case PP_MACRO_DIRECTIVE_HISTORY:
2148 case PP_MODULE_MACRO: {
2149 ModuleMacros.push_back(ModuleMacroRecord());
2150 auto &Info = ModuleMacros.back();
2151 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2152 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2153 for (int I = 2, N = Record.size(); I != N; ++I)
2154 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2159 Error("malformed block record in AST file");
2163 // We found the macro directive history; that's the last record
2168 // Module macros are listed in reverse dependency order.
2170 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2171 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2172 for (auto &MMR : ModuleMacros) {
2174 for (unsigned ModID : MMR.Overrides) {
2175 Module *Mod = getSubmodule(ModID);
2176 auto *Macro = PP.getModuleMacro(Mod, II);
2177 assert(Macro && "missing definition for overridden macro");
2178 Overrides.push_back(Macro);
2181 bool Inserted = false;
2182 Module *Owner = getSubmodule(MMR.SubModID);
2183 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2187 // Don't read the directive history for a module; we don't have anywhere
2192 // Deserialize the macro directives history in reverse source-order.
2193 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2194 unsigned Idx = 0, N = Record.size();
2196 MacroDirective *MD = nullptr;
2197 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2198 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2200 case MacroDirective::MD_Define: {
2201 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2202 MD = PP.AllocateDefMacroDirective(MI, Loc);
2205 case MacroDirective::MD_Undefine:
2206 MD = PP.AllocateUndefMacroDirective(Loc);
2208 case MacroDirective::MD_Visibility:
2209 bool isPublic = Record[Idx++];
2210 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2217 Earliest->setPrevious(MD);
2222 PP.setLoadedMacroDirective(II, Earliest, Latest);
2225 ASTReader::InputFileInfo
2226 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2227 // Go find this input file.
2228 BitstreamCursor &Cursor = F.InputFilesCursor;
2229 SavedStreamPosition SavedPosition(Cursor);
2230 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2231 // FIXME this drops errors on the floor.
2232 consumeError(std::move(Err));
2235 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2237 // FIXME this drops errors on the floor.
2238 consumeError(MaybeCode.takeError());
2240 unsigned Code = MaybeCode.get();
2244 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2245 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2246 "invalid record type for input file");
2248 // FIXME this drops errors on the floor.
2249 consumeError(Maybe.takeError());
2252 assert(Record[0] == ID && "Bogus stored ID or offset");
2254 R.StoredSize = static_cast<off_t>(Record[1]);
2255 R.StoredTime = static_cast<time_t>(Record[2]);
2256 R.Overridden = static_cast<bool>(Record[3]);
2257 R.Transient = static_cast<bool>(Record[4]);
2258 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2259 R.Filename = std::string(Blob);
2260 ResolveImportedPath(F, R.Filename);
2262 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2263 if (!MaybeEntry) // FIXME this drops errors on the floor.
2264 consumeError(MaybeEntry.takeError());
2265 llvm::BitstreamEntry Entry = MaybeEntry.get();
2266 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2267 "expected record type for input file hash");
2270 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2271 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2272 "invalid record type for input file hash");
2274 // FIXME this drops errors on the floor.
2275 consumeError(Maybe.takeError());
2277 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2278 static_cast<uint64_t>(Record[0]);
2282 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2283 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2284 // If this ID is bogus, just return an empty input file.
2285 if (ID == 0 || ID > F.InputFilesLoaded.size())
2288 // If we've already loaded this input file, return it.
2289 if (F.InputFilesLoaded[ID-1].getFile())
2290 return F.InputFilesLoaded[ID-1];
2292 if (F.InputFilesLoaded[ID-1].isNotFound())
2295 // Go find this input file.
2296 BitstreamCursor &Cursor = F.InputFilesCursor;
2297 SavedStreamPosition SavedPosition(Cursor);
2298 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2299 // FIXME this drops errors on the floor.
2300 consumeError(std::move(Err));
2303 InputFileInfo FI = readInputFileInfo(F, ID);
2304 off_t StoredSize = FI.StoredSize;
2305 time_t StoredTime = FI.StoredTime;
2306 bool Overridden = FI.Overridden;
2307 bool Transient = FI.Transient;
2308 StringRef Filename = FI.Filename;
2309 uint64_t StoredContentHash = FI.ContentHash;
2311 const FileEntry *File = nullptr;
2312 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2315 // If we didn't find the file, resolve it relative to the
2316 // original directory from which this AST file was created.
2317 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2318 F.OriginalDir != F.BaseDirectory) {
2319 std::string Resolved = resolveFileRelativeToOriginalDir(
2320 std::string(Filename), F.OriginalDir, F.BaseDirectory);
2321 if (!Resolved.empty())
2322 if (auto FE = FileMgr.getFile(Resolved))
2326 // For an overridden file, create a virtual file with the stored
2328 if ((Overridden || Transient) && File == nullptr)
2329 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2331 if (File == nullptr) {
2333 std::string ErrorStr = "could not find file '";
2334 ErrorStr += Filename;
2335 ErrorStr += "' referenced by AST file '";
2336 ErrorStr += F.FileName;
2340 // Record that we didn't find the file.
2341 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2345 // Check if there was a request to override the contents of the file
2346 // that was part of the precompiled header. Overriding such a file
2347 // can lead to problems when lexing using the source locations from the
2349 SourceManager &SM = getSourceManager();
2350 // FIXME: Reject if the overrides are different.
2351 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2353 Error(diag::err_fe_pch_file_overridden, Filename);
2355 // After emitting the diagnostic, bypass the overriding file to recover
2356 // (this creates a separate FileEntry).
2357 File = SM.bypassFileContentsOverride(*File);
2359 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2364 enum ModificationType {
2370 auto HasInputFileChanged = [&]() {
2371 if (StoredSize != File->getSize())
2372 return ModificationType::Size;
2373 if (!DisableValidation && StoredTime &&
2374 StoredTime != File->getModificationTime()) {
2375 // In case the modification time changes but not the content,
2376 // accept the cached file as legit.
2377 if (ValidateASTInputFilesContent &&
2378 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2379 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2380 if (!MemBuffOrError) {
2382 return ModificationType::ModTime;
2383 std::string ErrorStr = "could not get buffer for file '";
2384 ErrorStr += File->getName();
2387 return ModificationType::ModTime;
2390 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2391 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2392 return ModificationType::None;
2393 return ModificationType::Content;
2395 return ModificationType::ModTime;
2397 return ModificationType::None;
2400 bool IsOutOfDate = false;
2401 auto FileChange = HasInputFileChanged();
2402 // For an overridden file, there is nothing to validate.
2403 if (!Overridden && FileChange != ModificationType::None) {
2405 // Build a list of the PCH imports that got us here (in reverse).
2406 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2407 while (!ImportStack.back()->ImportedBy.empty())
2408 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2410 // The top-level PCH is stale.
2411 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2412 unsigned DiagnosticKind =
2413 moduleKindForDiagnostic(ImportStack.back()->Kind);
2414 if (DiagnosticKind == 0)
2415 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2416 (unsigned)FileChange);
2417 else if (DiagnosticKind == 1)
2418 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2419 (unsigned)FileChange);
2421 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2422 (unsigned)FileChange);
2424 // Print the import stack.
2425 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2426 Diag(diag::note_pch_required_by)
2427 << Filename << ImportStack[0]->FileName;
2428 for (unsigned I = 1; I < ImportStack.size(); ++I)
2429 Diag(diag::note_pch_required_by)
2430 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2433 if (!Diags.isDiagnosticInFlight())
2434 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2439 // FIXME: If the file is overridden and we've already opened it,
2440 // issue an error (or split it into a separate FileEntry).
2442 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2444 // Note that we've loaded this input file.
2445 F.InputFilesLoaded[ID-1] = IF;
2449 /// If we are loading a relocatable PCH or module file, and the filename
2450 /// is not an absolute path, add the system or module root to the beginning of
2452 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2453 // Resolve relative to the base directory, if we have one.
2454 if (!M.BaseDirectory.empty())
2455 return ResolveImportedPath(Filename, M.BaseDirectory);
2458 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2459 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2462 SmallString<128> Buffer;
2463 llvm::sys::path::append(Buffer, Prefix, Filename);
2464 Filename.assign(Buffer.begin(), Buffer.end());
2467 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2469 case ASTReader::Failure: return true;
2470 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2471 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2472 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2473 case ASTReader::ConfigurationMismatch:
2474 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2475 case ASTReader::HadErrors: return true;
2476 case ASTReader::Success: return false;
2479 llvm_unreachable("unknown ASTReadResult");
2482 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2483 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2484 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2485 std::string &SuggestedPredefines) {
2486 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2487 // FIXME this drops errors on the floor.
2488 consumeError(std::move(Err));
2492 // Read all of the records in the options block.
2494 ASTReadResult Result = Success;
2496 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2498 // FIXME this drops errors on the floor.
2499 consumeError(MaybeEntry.takeError());
2502 llvm::BitstreamEntry Entry = MaybeEntry.get();
2504 switch (Entry.Kind) {
2505 case llvm::BitstreamEntry::Error:
2506 case llvm::BitstreamEntry::SubBlock:
2509 case llvm::BitstreamEntry::EndBlock:
2512 case llvm::BitstreamEntry::Record:
2513 // The interesting case.
2517 // Read and process a record.
2519 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2520 if (!MaybeRecordType) {
2521 // FIXME this drops errors on the floor.
2522 consumeError(MaybeRecordType.takeError());
2525 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2526 case LANGUAGE_OPTIONS: {
2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528 if (ParseLanguageOptions(Record, Complain, Listener,
2529 AllowCompatibleConfigurationMismatch))
2530 Result = ConfigurationMismatch;
2534 case TARGET_OPTIONS: {
2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536 if (ParseTargetOptions(Record, Complain, Listener,
2537 AllowCompatibleConfigurationMismatch))
2538 Result = ConfigurationMismatch;
2542 case FILE_SYSTEM_OPTIONS: {
2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544 if (!AllowCompatibleConfigurationMismatch &&
2545 ParseFileSystemOptions(Record, Complain, Listener))
2546 Result = ConfigurationMismatch;
2550 case HEADER_SEARCH_OPTIONS: {
2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552 if (!AllowCompatibleConfigurationMismatch &&
2553 ParseHeaderSearchOptions(Record, Complain, Listener))
2554 Result = ConfigurationMismatch;
2558 case PREPROCESSOR_OPTIONS:
2559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2560 if (!AllowCompatibleConfigurationMismatch &&
2561 ParsePreprocessorOptions(Record, Complain, Listener,
2562 SuggestedPredefines))
2563 Result = ConfigurationMismatch;
2569 ASTReader::ASTReadResult
2570 ASTReader::ReadControlBlock(ModuleFile &F,
2571 SmallVectorImpl<ImportedModule> &Loaded,
2572 const ModuleFile *ImportedBy,
2573 unsigned ClientLoadCapabilities) {
2574 BitstreamCursor &Stream = F.Stream;
2576 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2577 Error(std::move(Err));
2581 // Lambda to read the unhashed control block the first time it's called.
2583 // For PCM files, the unhashed control block cannot be read until after the
2584 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2585 // need to look ahead before reading the IMPORTS record. For consistency,
2586 // this block is always read somehow (see BitstreamEntry::EndBlock).
2587 bool HasReadUnhashedControlBlock = false;
2588 auto readUnhashedControlBlockOnce = [&]() {
2589 if (!HasReadUnhashedControlBlock) {
2590 HasReadUnhashedControlBlock = true;
2591 if (ASTReadResult Result =
2592 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2598 // Read all of the records and blocks in the control block.
2600 unsigned NumInputs = 0;
2601 unsigned NumUserInputs = 0;
2602 StringRef BaseDirectoryAsWritten;
2604 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2606 Error(MaybeEntry.takeError());
2609 llvm::BitstreamEntry Entry = MaybeEntry.get();
2611 switch (Entry.Kind) {
2612 case llvm::BitstreamEntry::Error:
2613 Error("malformed block record in AST file");
2615 case llvm::BitstreamEntry::EndBlock: {
2616 // Validate the module before returning. This call catches an AST with
2617 // no module name and no imports.
2618 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2621 // Validate input files.
2622 const HeaderSearchOptions &HSOpts =
2623 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2625 // All user input files reside at the index range [0, NumUserInputs), and
2626 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2627 // loaded module files, ignore missing inputs.
2628 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2629 F.Kind != MK_PrebuiltModule) {
2630 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2632 // If we are reading a module, we will create a verification timestamp,
2633 // so we verify all input files. Otherwise, verify only user input
2636 unsigned N = NumUserInputs;
2637 if (ValidateSystemInputs ||
2638 (HSOpts.ModulesValidateOncePerBuildSession &&
2639 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2640 F.Kind == MK_ImplicitModule))
2643 for (unsigned I = 0; I < N; ++I) {
2644 InputFile IF = getInputFile(F, I+1, Complain);
2645 if (!IF.getFile() || IF.isOutOfDate())
2651 Listener->visitModuleFile(F.FileName, F.Kind);
2653 if (Listener && Listener->needsInputFileVisitation()) {
2654 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2656 for (unsigned I = 0; I < N; ++I) {
2657 bool IsSystem = I >= NumUserInputs;
2658 InputFileInfo FI = readInputFileInfo(F, I+1);
2659 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2660 F.Kind == MK_ExplicitModule ||
2661 F.Kind == MK_PrebuiltModule);
2668 case llvm::BitstreamEntry::SubBlock:
2670 case INPUT_FILES_BLOCK_ID:
2671 F.InputFilesCursor = Stream;
2672 if (llvm::Error Err = Stream.SkipBlock()) {
2673 Error(std::move(Err));
2676 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2677 Error("malformed block record in AST file");
2682 case OPTIONS_BLOCK_ID:
2683 // If we're reading the first module for this group, check its options
2684 // are compatible with ours. For modules it imports, no further checking
2685 // is required, because we checked them when we built it.
2686 if (Listener && !ImportedBy) {
2687 // Should we allow the configuration of the module file to differ from
2688 // the configuration of the current translation unit in a compatible
2691 // FIXME: Allow this for files explicitly specified with -include-pch.
2692 bool AllowCompatibleConfigurationMismatch =
2693 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2695 ASTReadResult Result =
2696 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2697 AllowCompatibleConfigurationMismatch, *Listener,
2698 SuggestedPredefines);
2699 if (Result == Failure) {
2700 Error("malformed block record in AST file");
2704 if (DisableValidation ||
2705 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2708 // If we can't load the module, exit early since we likely
2709 // will rebuild the module anyway. The stream may be in the
2710 // middle of a block.
2711 if (Result != Success)
2713 } else if (llvm::Error Err = Stream.SkipBlock()) {
2714 Error(std::move(Err));
2720 if (llvm::Error Err = Stream.SkipBlock()) {
2721 Error(std::move(Err));
2727 case llvm::BitstreamEntry::Record:
2728 // The interesting case.
2732 // Read and process a record.
2735 Expected<unsigned> MaybeRecordType =
2736 Stream.readRecord(Entry.ID, Record, &Blob);
2737 if (!MaybeRecordType) {
2738 Error(MaybeRecordType.takeError());
2741 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2743 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2744 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2745 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2746 : diag::err_pch_version_too_new);
2747 return VersionMismatch;
2750 bool hasErrors = Record[7];
2751 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2752 Diag(diag::err_pch_with_compiler_errors);
2756 Diags.ErrorOccurred = true;
2757 Diags.UncompilableErrorOccurred = true;
2758 Diags.UnrecoverableErrorOccurred = true;
2761 F.RelocatablePCH = Record[4];
2762 // Relative paths in a relocatable PCH are relative to our sysroot.
2763 if (F.RelocatablePCH)
2764 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2766 F.HasTimestamps = Record[5];
2768 F.PCHHasObjectFile = Record[6];
2770 const std::string &CurBranch = getClangFullRepositoryVersion();
2771 StringRef ASTBranch = Blob;
2772 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2773 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2774 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2775 return VersionMismatch;
2781 // Validate the AST before processing any imports (otherwise, untangling
2782 // them can be error-prone and expensive). A module will have a name and
2783 // will already have been validated, but this catches the PCH case.
2784 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2787 // Load each of the imported PCH files.
2788 unsigned Idx = 0, N = Record.size();
2790 // Read information about the AST file.
2791 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2792 // The import location will be the local one for now; we will adjust
2793 // all import locations of module imports after the global source
2794 // location info are setup, in ReadAST.
2795 SourceLocation ImportLoc =
2796 ReadUntranslatedSourceLocation(Record[Idx++]);
2797 off_t StoredSize = (off_t)Record[Idx++];
2798 time_t StoredModTime = (time_t)Record[Idx++];
2799 auto FirstSignatureByte = Record.begin() + Idx;
2800 ASTFileSignature StoredSignature = ASTFileSignature::create(
2801 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2802 Idx += ASTFileSignature::size;
2804 std::string ImportedName = ReadString(Record, Idx);
2805 std::string ImportedFile;
2807 // For prebuilt and explicit modules first consult the file map for
2808 // an override. Note that here we don't search prebuilt module
2809 // directories, only the explicit name to file mappings. Also, we will
2810 // still verify the size/signature making sure it is essentially the
2811 // same file but perhaps in a different location.
2812 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2813 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2814 ImportedName, /*FileMapOnly*/ true);
2816 if (ImportedFile.empty())
2817 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2818 // ModuleCache as when writing.
2819 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2821 SkipPath(Record, Idx);
2823 // If our client can't cope with us being out of date, we can't cope with
2824 // our dependency being missing.
2825 unsigned Capabilities = ClientLoadCapabilities;
2826 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2827 Capabilities &= ~ARR_Missing;
2829 // Load the AST file.
2830 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2831 Loaded, StoredSize, StoredModTime,
2832 StoredSignature, Capabilities);
2834 // If we diagnosed a problem, produce a backtrace.
2835 if (isDiagnosedResult(Result, Capabilities))
2836 Diag(diag::note_module_file_imported_by)
2837 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2840 case Failure: return Failure;
2841 // If we have to ignore the dependency, we'll have to ignore this too.
2843 case OutOfDate: return OutOfDate;
2844 case VersionMismatch: return VersionMismatch;
2845 case ConfigurationMismatch: return ConfigurationMismatch;
2846 case HadErrors: return HadErrors;
2847 case Success: break;
2854 F.OriginalSourceFileID = FileID::get(Record[0]);
2855 F.ActualOriginalSourceFileName = std::string(Blob);
2856 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2857 ResolveImportedPath(F, F.OriginalSourceFileName);
2860 case ORIGINAL_FILE_ID:
2861 F.OriginalSourceFileID = FileID::get(Record[0]);
2864 case ORIGINAL_PCH_DIR:
2865 F.OriginalDir = std::string(Blob);
2869 F.ModuleName = std::string(Blob);
2870 Diag(diag::remark_module_import)
2871 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2872 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2874 Listener->ReadModuleName(F.ModuleName);
2876 // Validate the AST as soon as we have a name so we can exit early on
2878 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2883 case MODULE_DIRECTORY: {
2884 // Save the BaseDirectory as written in the PCM for computing the module
2885 // filename for the ModuleCache.
2886 BaseDirectoryAsWritten = Blob;
2887 assert(!F.ModuleName.empty() &&
2888 "MODULE_DIRECTORY found before MODULE_NAME");
2889 // If we've already loaded a module map file covering this module, we may
2890 // have a better path for it (relative to the current build).
2891 Module *M = PP.getHeaderSearchInfo().lookupModule(
2892 F.ModuleName, /*AllowSearch*/ true,
2893 /*AllowExtraModuleMapSearch*/ true);
2894 if (M && M->Directory) {
2895 // If we're implicitly loading a module, the base directory can't
2896 // change between the build and use.
2897 // Don't emit module relocation error if we have -fno-validate-pch
2898 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2899 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2900 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2901 if (!BuildDir || *BuildDir != M->Directory) {
2902 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2903 Diag(diag::err_imported_module_relocated)
2904 << F.ModuleName << Blob << M->Directory->getName();
2908 F.BaseDirectory = std::string(M->Directory->getName());
2910 F.BaseDirectory = std::string(Blob);
2915 case MODULE_MAP_FILE:
2916 if (ASTReadResult Result =
2917 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2921 case INPUT_FILE_OFFSETS:
2922 NumInputs = Record[0];
2923 NumUserInputs = Record[1];
2924 F.InputFileOffsets =
2925 (const llvm::support::unaligned_uint64_t *)Blob.data();
2926 F.InputFilesLoaded.resize(NumInputs);
2927 F.NumUserInputFiles = NumUserInputs;
2933 ASTReader::ASTReadResult
2934 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2935 BitstreamCursor &Stream = F.Stream;
2937 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2938 Error(std::move(Err));
2941 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2943 // Read all of the records and blocks for the AST file.
2946 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2948 Error(MaybeEntry.takeError());
2951 llvm::BitstreamEntry Entry = MaybeEntry.get();
2953 switch (Entry.Kind) {
2954 case llvm::BitstreamEntry::Error:
2955 Error("error at end of module block in AST file");
2957 case llvm::BitstreamEntry::EndBlock:
2958 // Outside of C++, we do not store a lookup map for the translation unit.
2959 // Instead, mark it as needing a lookup map to be built if this module
2960 // contains any declarations lexically within it (which it always does!).
2961 // This usually has no cost, since we very rarely need the lookup map for
2962 // the translation unit outside C++.
2963 if (ASTContext *Ctx = ContextObj) {
2964 DeclContext *DC = Ctx->getTranslationUnitDecl();
2965 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2966 DC->setMustBuildLookupTable();
2970 case llvm::BitstreamEntry::SubBlock:
2972 case DECLTYPES_BLOCK_ID:
2973 // We lazily load the decls block, but we want to set up the
2974 // DeclsCursor cursor to point into it. Clone our current bitcode
2975 // cursor to it, enter the block and read the abbrevs in that block.
2976 // With the main cursor, we just skip over it.
2977 F.DeclsCursor = Stream;
2978 if (llvm::Error Err = Stream.SkipBlock()) {
2979 Error(std::move(Err));
2982 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2983 &F.DeclsBlockStartOffset)) {
2984 Error("malformed block record in AST file");
2989 case PREPROCESSOR_BLOCK_ID:
2990 F.MacroCursor = Stream;
2991 if (!PP.getExternalSource())
2992 PP.setExternalSource(this);
2994 if (llvm::Error Err = Stream.SkipBlock()) {
2995 Error(std::move(Err));
2998 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2999 Error("malformed block record in AST file");
3002 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3005 case PREPROCESSOR_DETAIL_BLOCK_ID:
3006 F.PreprocessorDetailCursor = Stream;
3008 if (llvm::Error Err = Stream.SkipBlock()) {
3009 Error(std::move(Err));
3012 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3013 PREPROCESSOR_DETAIL_BLOCK_ID)) {
3014 Error("malformed preprocessor detail record in AST file");
3017 F.PreprocessorDetailStartOffset
3018 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3020 if (!PP.getPreprocessingRecord())
3021 PP.createPreprocessingRecord();
3022 if (!PP.getPreprocessingRecord()->getExternalSource())
3023 PP.getPreprocessingRecord()->SetExternalSource(*this);
3026 case SOURCE_MANAGER_BLOCK_ID:
3027 if (ReadSourceManagerBlock(F))
3031 case SUBMODULE_BLOCK_ID:
3032 if (ASTReadResult Result =
3033 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3037 case COMMENTS_BLOCK_ID: {
3038 BitstreamCursor C = Stream;
3040 if (llvm::Error Err = Stream.SkipBlock()) {
3041 Error(std::move(Err));
3044 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3045 Error("malformed comments block in AST file");
3048 CommentsCursors.push_back(std::make_pair(C, &F));
3053 if (llvm::Error Err = Stream.SkipBlock()) {
3054 Error(std::move(Err));
3061 case llvm::BitstreamEntry::Record:
3062 // The interesting case.
3066 // Read and process a record.
3069 Expected<unsigned> MaybeRecordType =
3070 Stream.readRecord(Entry.ID, Record, &Blob);
3071 if (!MaybeRecordType) {
3072 Error(MaybeRecordType.takeError());
3075 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3077 // If we're not loading an AST context, we don't care about most records.
3079 switch (RecordType) {
3080 case IDENTIFIER_TABLE:
3081 case IDENTIFIER_OFFSET:
3082 case INTERESTING_IDENTIFIERS:
3084 case PP_CONDITIONAL_STACK:
3085 case PP_COUNTER_VALUE:
3086 case SOURCE_LOCATION_OFFSETS:
3087 case MODULE_OFFSET_MAP:
3088 case SOURCE_MANAGER_LINE_TABLE:
3089 case SOURCE_LOCATION_PRELOADS:
3090 case PPD_ENTITIES_OFFSETS:
3091 case HEADER_SEARCH_TABLE:
3092 case IMPORTED_MODULES:
3100 switch (RecordType) {
3101 default: // Default behavior: ignore.
3105 if (F.LocalNumTypes != 0) {
3106 Error("duplicate TYPE_OFFSET record in AST file");
3109 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3110 F.LocalNumTypes = Record[0];
3111 unsigned LocalBaseTypeIndex = Record[1];
3112 F.BaseTypeIndex = getTotalNumTypes();
3114 if (F.LocalNumTypes > 0) {
3115 // Introduce the global -> local mapping for types within this module.
3116 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3118 // Introduce the local -> global mapping for types within this module.
3119 F.TypeRemap.insertOrReplace(
3120 std::make_pair(LocalBaseTypeIndex,
3121 F.BaseTypeIndex - LocalBaseTypeIndex));
3123 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3129 if (F.LocalNumDecls != 0) {
3130 Error("duplicate DECL_OFFSET record in AST file");
3133 F.DeclOffsets = (const DeclOffset *)Blob.data();
3134 F.LocalNumDecls = Record[0];
3135 unsigned LocalBaseDeclID = Record[1];
3136 F.BaseDeclID = getTotalNumDecls();
3138 if (F.LocalNumDecls > 0) {
3139 // Introduce the global -> local mapping for declarations within this
3141 GlobalDeclMap.insert(
3142 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3144 // Introduce the local -> global mapping for declarations within this
3146 F.DeclRemap.insertOrReplace(
3147 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3149 // Introduce the global -> local mapping for declarations within this
3151 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3153 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3158 case TU_UPDATE_LEXICAL: {
3159 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3160 LexicalContents Contents(
3161 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3163 static_cast<unsigned int>(Blob.size() / 4));
3164 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3165 TU->setHasExternalLexicalStorage(true);
3169 case UPDATE_VISIBLE: {
3171 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3172 auto *Data = (const unsigned char*)Blob.data();
3173 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3174 // If we've already loaded the decl, perform the updates when we finish
3175 // loading this block.
3176 if (Decl *D = GetExistingDecl(ID))
3177 PendingUpdateRecords.push_back(
3178 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3182 case IDENTIFIER_TABLE:
3183 F.IdentifierTableData = Blob.data();
3185 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3186 (const unsigned char *)F.IdentifierTableData + Record[0],
3187 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3188 (const unsigned char *)F.IdentifierTableData,
3189 ASTIdentifierLookupTrait(*this, F));
3191 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3195 case IDENTIFIER_OFFSET: {
3196 if (F.LocalNumIdentifiers != 0) {
3197 Error("duplicate IDENTIFIER_OFFSET record in AST file");
3200 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3201 F.LocalNumIdentifiers = Record[0];
3202 unsigned LocalBaseIdentifierID = Record[1];
3203 F.BaseIdentifierID = getTotalNumIdentifiers();
3205 if (F.LocalNumIdentifiers > 0) {
3206 // Introduce the global -> local mapping for identifiers within this
3208 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3211 // Introduce the local -> global mapping for identifiers within this
3213 F.IdentifierRemap.insertOrReplace(
3214 std::make_pair(LocalBaseIdentifierID,
3215 F.BaseIdentifierID - LocalBaseIdentifierID));
3217 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3218 + F.LocalNumIdentifiers);
3223 case INTERESTING_IDENTIFIERS:
3224 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3227 case EAGERLY_DESERIALIZED_DECLS:
3228 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3229 // about "interesting" decls (for instance, if we're building a module).
3230 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3231 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3234 case MODULAR_CODEGEN_DECLS:
3235 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3236 // them (ie: if we're not codegenerating this module).
3237 if (F.Kind == MK_MainFile ||
3238 getContext().getLangOpts().BuildingPCHWithObjectFile)
3239 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3240 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3244 if (SpecialTypes.empty()) {
3245 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3246 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3250 if (SpecialTypes.size() != Record.size()) {
3251 Error("invalid special-types record");
3255 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3256 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3257 if (!SpecialTypes[I])
3258 SpecialTypes[I] = ID;
3259 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3265 TotalNumStatements += Record[0];
3266 TotalNumMacros += Record[1];
3267 TotalLexicalDeclContexts += Record[2];
3268 TotalVisibleDeclContexts += Record[3];
3271 case UNUSED_FILESCOPED_DECLS:
3272 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3273 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3276 case DELEGATING_CTORS:
3277 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3278 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3281 case WEAK_UNDECLARED_IDENTIFIERS:
3282 if (Record.size() % 4 != 0) {
3283 Error("invalid weak identifiers record");
3287 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3288 // files. This isn't the way to do it :)
3289 WeakUndeclaredIdentifiers.clear();
3291 // Translate the weak, undeclared identifiers into global IDs.
3292 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3293 WeakUndeclaredIdentifiers.push_back(
3294 getGlobalIdentifierID(F, Record[I++]));
3295 WeakUndeclaredIdentifiers.push_back(
3296 getGlobalIdentifierID(F, Record[I++]));
3297 WeakUndeclaredIdentifiers.push_back(
3298 ReadSourceLocation(F, Record, I).getRawEncoding());
3299 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3303 case SELECTOR_OFFSETS: {
3304 F.SelectorOffsets = (const uint32_t *)Blob.data();
3305 F.LocalNumSelectors = Record[0];
3306 unsigned LocalBaseSelectorID = Record[1];
3307 F.BaseSelectorID = getTotalNumSelectors();
3309 if (F.LocalNumSelectors > 0) {
3310 // Introduce the global -> local mapping for selectors within this
3312 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3314 // Introduce the local -> global mapping for selectors within this
3316 F.SelectorRemap.insertOrReplace(
3317 std::make_pair(LocalBaseSelectorID,
3318 F.BaseSelectorID - LocalBaseSelectorID));
3320 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3326 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3328 F.SelectorLookupTable
3329 = ASTSelectorLookupTable::Create(
3330 F.SelectorLookupTableData + Record[0],
3331 F.SelectorLookupTableData,
3332 ASTSelectorLookupTrait(*this, F));
3333 TotalNumMethodPoolEntries += Record[1];
3336 case REFERENCED_SELECTOR_POOL:
3337 if (!Record.empty()) {
3338 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3339 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3341 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3347 case PP_CONDITIONAL_STACK:
3348 if (!Record.empty()) {
3349 unsigned Idx = 0, End = Record.size() - 1;
3350 bool ReachedEOFWhileSkipping = Record[Idx++];
3351 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3352 if (ReachedEOFWhileSkipping) {
3353 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3354 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3355 bool FoundNonSkipPortion = Record[Idx++];
3356 bool FoundElse = Record[Idx++];
3357 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3358 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3359 FoundElse, ElseLoc);
3361 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3363 auto Loc = ReadSourceLocation(F, Record, Idx);
3364 bool WasSkipping = Record[Idx++];
3365 bool FoundNonSkip = Record[Idx++];
3366 bool FoundElse = Record[Idx++];
3367 ConditionalStack.push_back(
3368 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3370 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3374 case PP_COUNTER_VALUE:
3375 if (!Record.empty() && Listener)
3376 Listener->ReadCounter(F, Record[0]);
3379 case FILE_SORTED_DECLS:
3380 F.FileSortedDecls = (const DeclID *)Blob.data();
3381 F.NumFileSortedDecls = Record[0];
3384 case SOURCE_LOCATION_OFFSETS: {
3385 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3386 F.LocalNumSLocEntries = Record[0];
3387 unsigned SLocSpaceSize = Record[1];
3388 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3389 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3390 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3392 if (!F.SLocEntryBaseID) {
3393 Error("ran out of source locations");
3396 // Make our entry in the range map. BaseID is negative and growing, so
3397 // we invert it. Because we invert it, though, we need the other end of
3399 unsigned RangeStart =
3400 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3401 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3402 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3404 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3405 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3406 GlobalSLocOffsetMap.insert(
3407 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3408 - SLocSpaceSize,&F));
3410 // Initialize the remapping table.
3411 // Invalid stays invalid.
3412 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3413 // This module. Base was 2 when being compiled.
3414 F.SLocRemap.insertOrReplace(std::make_pair(2U,
3415 static_cast<int>(F.SLocEntryBaseOffset - 2)));
3417 TotalNumSLocEntries += F.LocalNumSLocEntries;
3421 case MODULE_OFFSET_MAP:
3422 F.ModuleOffsetMap = Blob;
3425 case SOURCE_MANAGER_LINE_TABLE:
3426 if (ParseLineTable(F, Record)) {
3427 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3432 case SOURCE_LOCATION_PRELOADS: {
3433 // Need to transform from the local view (1-based IDs) to the global view,
3434 // which is based off F.SLocEntryBaseID.
3435 if (!F.PreloadSLocEntries.empty()) {
3436 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3440 F.PreloadSLocEntries.swap(Record);
3444 case EXT_VECTOR_DECLS:
3445 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3446 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3450 if (Record.size() % 3 != 0) {
3451 Error("Invalid VTABLE_USES record");
3455 // Later tables overwrite earlier ones.
3456 // FIXME: Modules will have some trouble with this. This is clearly not
3457 // the right way to do this.
3460 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3461 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3462 VTableUses.push_back(
3463 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3464 VTableUses.push_back(Record[Idx++]);
3468 case PENDING_IMPLICIT_INSTANTIATIONS:
3469 if (PendingInstantiations.size() % 2 != 0) {
3470 Error("Invalid existing PendingInstantiations");
3474 if (Record.size() % 2 != 0) {
3475 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3479 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3480 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3481 PendingInstantiations.push_back(
3482 ReadSourceLocation(F, Record, I).getRawEncoding());
3486 case SEMA_DECL_REFS:
3487 if (Record.size() != 3) {
3488 Error("Invalid SEMA_DECL_REFS block");
3491 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3492 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3495 case PPD_ENTITIES_OFFSETS: {
3496 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3497 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3498 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3500 unsigned LocalBasePreprocessedEntityID = Record[0];
3502 unsigned StartingID;
3503 if (!PP.getPreprocessingRecord())
3504 PP.createPreprocessingRecord();
3505 if (!PP.getPreprocessingRecord()->getExternalSource())
3506 PP.getPreprocessingRecord()->SetExternalSource(*this);
3508 = PP.getPreprocessingRecord()
3509 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3510 F.BasePreprocessedEntityID = StartingID;
3512 if (F.NumPreprocessedEntities > 0) {
3513 // Introduce the global -> local mapping for preprocessed entities in
3515 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3517 // Introduce the local -> global mapping for preprocessed entities in
3519 F.PreprocessedEntityRemap.insertOrReplace(
3520 std::make_pair(LocalBasePreprocessedEntityID,
3521 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3527 case PPD_SKIPPED_RANGES: {
3528 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3529 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3530 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3532 if (!PP.getPreprocessingRecord())
3533 PP.createPreprocessingRecord();
3534 if (!PP.getPreprocessingRecord()->getExternalSource())
3535 PP.getPreprocessingRecord()->SetExternalSource(*this);
3536 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3537 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3539 if (F.NumPreprocessedSkippedRanges > 0)
3540 GlobalSkippedRangeMap.insert(
3541 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3545 case DECL_UPDATE_OFFSETS:
3546 if (Record.size() % 2 != 0) {
3547 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3550 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3551 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3552 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3554 // If we've already loaded the decl, perform the updates when we finish
3555 // loading this block.
3556 if (Decl *D = GetExistingDecl(ID))
3557 PendingUpdateRecords.push_back(
3558 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3562 case OBJC_CATEGORIES_MAP:
3563 if (F.LocalNumObjCCategoriesInMap != 0) {
3564 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3568 F.LocalNumObjCCategoriesInMap = Record[0];
3569 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3572 case OBJC_CATEGORIES:
3573 F.ObjCCategories.swap(Record);
3576 case CUDA_SPECIAL_DECL_REFS:
3577 // Later tables overwrite earlier ones.
3578 // FIXME: Modules will have trouble with this.
3579 CUDASpecialDeclRefs.clear();
3580 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3581 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3584 case HEADER_SEARCH_TABLE:
3585 F.HeaderFileInfoTableData = Blob.data();
3586 F.LocalNumHeaderFileInfos = Record[1];
3588 F.HeaderFileInfoTable
3589 = HeaderFileInfoLookupTable::Create(
3590 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3591 (const unsigned char *)F.HeaderFileInfoTableData,
3592 HeaderFileInfoTrait(*this, F,
3593 &PP.getHeaderSearchInfo(),
3594 Blob.data() + Record[2]));
3596 PP.getHeaderSearchInfo().SetExternalSource(this);
3597 if (!PP.getHeaderSearchInfo().getExternalLookup())
3598 PP.getHeaderSearchInfo().SetExternalLookup(this);
3602 case FP_PRAGMA_OPTIONS:
3603 // Later tables overwrite earlier ones.
3604 FPPragmaOptions.swap(Record);
3607 case OPENCL_EXTENSIONS:
3608 for (unsigned I = 0, E = Record.size(); I != E; ) {
3609 auto Name = ReadString(Record, I);
3610 auto &Opt = OpenCLExtensions.OptMap[Name];
3611 Opt.Supported = Record[I++] != 0;
3612 Opt.Enabled = Record[I++] != 0;
3613 Opt.Avail = Record[I++];
3614 Opt.Core = Record[I++];
3618 case OPENCL_EXTENSION_TYPES:
3619 for (unsigned I = 0, E = Record.size(); I != E;) {
3620 auto TypeID = static_cast<::TypeID>(Record[I++]);
3621 auto *Type = GetType(TypeID).getTypePtr();
3622 auto NumExt = static_cast<unsigned>(Record[I++]);
3623 for (unsigned II = 0; II != NumExt; ++II) {
3624 auto Ext = ReadString(Record, I);
3625 OpenCLTypeExtMap[Type].insert(Ext);
3630 case OPENCL_EXTENSION_DECLS:
3631 for (unsigned I = 0, E = Record.size(); I != E;) {
3632 auto DeclID = static_cast<::DeclID>(Record[I++]);
3633 auto *Decl = GetDecl(DeclID);
3634 auto NumExt = static_cast<unsigned>(Record[I++]);
3635 for (unsigned II = 0; II != NumExt; ++II) {
3636 auto Ext = ReadString(Record, I);
3637 OpenCLDeclExtMap[Decl].insert(Ext);
3642 case TENTATIVE_DEFINITIONS:
3643 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3644 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3647 case KNOWN_NAMESPACES:
3648 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3649 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3652 case UNDEFINED_BUT_USED:
3653 if (UndefinedButUsed.size() % 2 != 0) {
3654 Error("Invalid existing UndefinedButUsed");
3658 if (Record.size() % 2 != 0) {
3659 Error("invalid undefined-but-used record");
3662 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3663 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3664 UndefinedButUsed.push_back(
3665 ReadSourceLocation(F, Record, I).getRawEncoding());
3669 case DELETE_EXPRS_TO_ANALYZE:
3670 for (unsigned I = 0, N = Record.size(); I != N;) {
3671 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3672 const uint64_t Count = Record[I++];
3673 DelayedDeleteExprs.push_back(Count);
3674 for (uint64_t C = 0; C < Count; ++C) {
3675 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3676 bool IsArrayForm = Record[I++] == 1;
3677 DelayedDeleteExprs.push_back(IsArrayForm);
3682 case IMPORTED_MODULES:
3683 if (!F.isModule()) {
3684 // If we aren't loading a module (which has its own exports), make
3685 // all of the imported modules visible.
3686 // FIXME: Deal with macros-only imports.
3687 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3688 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3689 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3691 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3692 if (DeserializationListener)
3693 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3699 case MACRO_OFFSET: {
3700 if (F.LocalNumMacros != 0) {
3701 Error("duplicate MACRO_OFFSET record in AST file");
3704 F.MacroOffsets = (const uint32_t *)Blob.data();
3705 F.LocalNumMacros = Record[0];
3706 unsigned LocalBaseMacroID = Record[1];
3707 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3708 F.BaseMacroID = getTotalNumMacros();
3710 if (F.LocalNumMacros > 0) {
3711 // Introduce the global -> local mapping for macros within this module.
3712 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3714 // Introduce the local -> global mapping for macros within this module.
3715 F.MacroRemap.insertOrReplace(
3716 std::make_pair(LocalBaseMacroID,
3717 F.BaseMacroID - LocalBaseMacroID));
3719 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3724 case LATE_PARSED_TEMPLATE:
3725 LateParsedTemplates.append(Record.begin(), Record.end());
3728 case OPTIMIZE_PRAGMA_OPTIONS:
3729 if (Record.size() != 1) {
3730 Error("invalid pragma optimize record");
3733 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3736 case MSSTRUCT_PRAGMA_OPTIONS:
3737 if (Record.size() != 1) {
3738 Error("invalid pragma ms_struct record");
3741 PragmaMSStructState = Record[0];
3744 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3745 if (Record.size() != 2) {
3746 Error("invalid pragma ms_struct record");
3749 PragmaMSPointersToMembersState = Record[0];
3750 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3753 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3754 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3755 UnusedLocalTypedefNameCandidates.push_back(
3756 getGlobalDeclID(F, Record[I]));
3759 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3760 if (Record.size() != 1) {
3761 Error("invalid cuda pragma options record");
3764 ForceCUDAHostDeviceDepth = Record[0];
3767 case PACK_PRAGMA_OPTIONS: {
3768 if (Record.size() < 3) {
3769 Error("invalid pragma pack record");
3772 PragmaPackCurrentValue = Record[0];
3773 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3774 unsigned NumStackEntries = Record[2];
3776 // Reset the stack when importing a new module.
3777 PragmaPackStack.clear();
3778 for (unsigned I = 0; I < NumStackEntries; ++I) {
3779 PragmaPackStackEntry Entry;
3780 Entry.Value = Record[Idx++];
3781 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3782 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3783 PragmaPackStrings.push_back(ReadString(Record, Idx));
3784 Entry.SlotLabel = PragmaPackStrings.back();
3785 PragmaPackStack.push_back(Entry);
3790 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3791 if (Record.size() < 3) {
3792 Error("invalid pragma pack record");
3795 FpPragmaCurrentValue = Record[0];
3796 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3797 unsigned NumStackEntries = Record[2];
3799 // Reset the stack when importing a new module.
3800 FpPragmaStack.clear();
3801 for (unsigned I = 0; I < NumStackEntries; ++I) {
3802 FpPragmaStackEntry Entry;
3803 Entry.Value = Record[Idx++];
3804 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3805 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3806 FpPragmaStrings.push_back(ReadString(Record, Idx));
3807 Entry.SlotLabel = FpPragmaStrings.back();
3808 FpPragmaStack.push_back(Entry);
3813 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3814 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3815 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3821 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3822 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3824 // Additional remapping information.
3825 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3826 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3827 F.ModuleOffsetMap = StringRef();
3829 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3830 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3831 F.SLocRemap.insert(std::make_pair(0U, 0));
3832 F.SLocRemap.insert(std::make_pair(2U, 1));
3835 // Continuous range maps we may be updating in our module.
3836 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3837 RemapBuilder SLocRemap(F.SLocRemap);
3838 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3839 RemapBuilder MacroRemap(F.MacroRemap);
3840 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3841 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3842 RemapBuilder SelectorRemap(F.SelectorRemap);
3843 RemapBuilder DeclRemap(F.DeclRemap);
3844 RemapBuilder TypeRemap(F.TypeRemap);
3846 while (Data < DataEnd) {
3847 // FIXME: Looking up dependency modules by filename is horrible. Let's
3848 // start fixing this with prebuilt, explicit and implicit modules and see
3850 using namespace llvm::support;
3851 ModuleKind Kind = static_cast<ModuleKind>(
3852 endian::readNext<uint8_t, little, unaligned>(Data));
3853 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3854 StringRef Name = StringRef((const char*)Data, Len);
3856 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3857 Kind == MK_ImplicitModule
3858 ? ModuleMgr.lookupByModuleName(Name)
3859 : ModuleMgr.lookupByFileName(Name));
3862 "SourceLocation remap refers to unknown module, cannot find ";
3863 Msg.append(std::string(Name));
3868 uint32_t SLocOffset =
3869 endian::readNext<uint32_t, little, unaligned>(Data);
3870 uint32_t IdentifierIDOffset =
3871 endian::readNext<uint32_t, little, unaligned>(Data);
3872 uint32_t MacroIDOffset =
3873 endian::readNext<uint32_t, little, unaligned>(Data);
3874 uint32_t PreprocessedEntityIDOffset =
3875 endian::readNext<uint32_t, little, unaligned>(Data);
3876 uint32_t SubmoduleIDOffset =
3877 endian::readNext<uint32_t, little, unaligned>(Data);
3878 uint32_t SelectorIDOffset =
3879 endian::readNext<uint32_t, little, unaligned>(Data);
3880 uint32_t DeclIDOffset =
3881 endian::readNext<uint32_t, little, unaligned>(Data);
3882 uint32_t TypeIndexOffset =
3883 endian::readNext<uint32_t, little, unaligned>(Data);
3885 uint32_t None = std::numeric_limits<uint32_t>::max();
3887 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3888 RemapBuilder &Remap) {
3890 Remap.insert(std::make_pair(Offset,
3891 static_cast<int>(BaseOffset - Offset)));
3893 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3894 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3895 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3896 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3897 PreprocessedEntityRemap);
3898 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3899 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3900 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3901 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3903 // Global -> local mappings.
3904 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3908 ASTReader::ASTReadResult
3909 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3910 const ModuleFile *ImportedBy,
3911 unsigned ClientLoadCapabilities) {
3913 F.ModuleMapPath = ReadPath(F, Record, Idx);
3915 // Try to resolve ModuleName in the current header search context and
3916 // verify that it is found in the same module map file as we saved. If the
3917 // top-level AST file is a main file, skip this check because there is no
3918 // usable header search context.
3919 assert(!F.ModuleName.empty() &&
3920 "MODULE_NAME should come before MODULE_MAP_FILE");
3921 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3922 // An implicitly-loaded module file should have its module listed in some
3923 // module map file that we've already loaded.
3924 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3925 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3926 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3927 // Don't emit module relocation error if we have -fno-validate-pch
3928 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3929 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3930 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3931 // This module was defined by an imported (explicit) module.
3932 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3933 << ASTFE->getName();
3935 // This module was built with a different module map.
3936 Diag(diag::err_imported_module_not_found)
3937 << F.ModuleName << F.FileName
3938 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3940 // In case it was imported by a PCH, there's a chance the user is
3941 // just missing to include the search path to the directory containing
3943 if (ImportedBy && ImportedBy->Kind == MK_PCH)
3944 Diag(diag::note_imported_by_pch_module_not_found)
3945 << llvm::sys::path::parent_path(F.ModuleMapPath);
3951 assert(M->Name == F.ModuleName && "found module with different name");
3953 // Check the primary module map file.
3954 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3955 if (!StoredModMap || *StoredModMap != ModMap) {
3956 assert(ModMap && "found module is missing module map file");
3957 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3958 "top-level import should be verified");
3959 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3960 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3961 Diag(diag::err_imported_module_modmap_changed)
3962 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3963 << ModMap->getName() << F.ModuleMapPath << NotImported;
3967 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3968 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3969 // FIXME: we should use input files rather than storing names.
3970 std::string Filename = ReadPath(F, Record, Idx);
3971 auto F = FileMgr.getFile(Filename, false, false);
3973 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3974 Error("could not find file '" + Filename +"' referenced by AST file");
3977 AdditionalStoredMaps.insert(*F);
3980 // Check any additional module map files (e.g. module.private.modulemap)
3981 // that are not in the pcm.
3982 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3983 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3984 // Remove files that match
3985 // Note: SmallPtrSet::erase is really remove
3986 if (!AdditionalStoredMaps.erase(ModMap)) {
3987 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3988 Diag(diag::err_module_different_modmap)
3989 << F.ModuleName << /*new*/0 << ModMap->getName();
3995 // Check any additional module map files that are in the pcm, but not
3996 // found in header search. Cases that match are already removed.
3997 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3998 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3999 Diag(diag::err_module_different_modmap)
4000 << F.ModuleName << /*not new*/1 << ModMap->getName();
4006 Listener->ReadModuleMapFile(F.ModuleMapPath);
4010 /// Move the given method to the back of the global list of methods.
4011 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4012 // Find the entry for this selector in the method pool.
4013 Sema::GlobalMethodPool::iterator Known
4014 = S.MethodPool.find(Method->getSelector());
4015 if (Known == S.MethodPool.end())
4018 // Retrieve the appropriate method list.
4019 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4020 : Known->second.second;
4022 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4024 if (List->getMethod() == Method) {
4032 if (List->getNext())
4033 List->setMethod(List->getNext()->getMethod());
4035 List->setMethod(Method);
4039 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4040 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4041 for (Decl *D : Names) {
4042 bool wasHidden = !D->isUnconditionallyVisible();
4043 D->setVisibleDespiteOwningModule();
4045 if (wasHidden && SemaObj) {
4046 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4047 moveMethodToBackOfGlobalList(*SemaObj, Method);
4053 void ASTReader::makeModuleVisible(Module *Mod,
4054 Module::NameVisibilityKind NameVisibility,
4055 SourceLocation ImportLoc) {
4056 llvm::SmallPtrSet<Module *, 4> Visited;
4057 SmallVector<Module *, 4> Stack;
4058 Stack.push_back(Mod);
4059 while (!Stack.empty()) {
4060 Mod = Stack.pop_back_val();
4062 if (NameVisibility <= Mod->NameVisibility) {
4063 // This module already has this level of visibility (or greater), so
4064 // there is nothing more to do.
4068 if (Mod->isUnimportable()) {
4069 // Modules that aren't importable cannot be made visible.
4073 // Update the module's name visibility.
4074 Mod->NameVisibility = NameVisibility;
4076 // If we've already deserialized any names from this module,
4077 // mark them as visible.
4078 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4079 if (Hidden != HiddenNamesMap.end()) {
4080 auto HiddenNames = std::move(*Hidden);
4081 HiddenNamesMap.erase(Hidden);
4082 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4083 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4084 "making names visible added hidden names");
4087 // Push any exported modules onto the stack to be marked as visible.
4088 SmallVector<Module *, 16> Exports;
4089 Mod->getExportedModules(Exports);
4090 for (SmallVectorImpl<Module *>::iterator
4091 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4092 Module *Exported = *I;
4093 if (Visited.insert(Exported).second)
4094 Stack.push_back(Exported);
4099 /// We've merged the definition \p MergedDef into the existing definition
4100 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4102 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4103 NamedDecl *MergedDef) {
4104 if (!Def->isUnconditionallyVisible()) {
4105 // If MergedDef is visible or becomes visible, make the definition visible.
4106 if (MergedDef->isUnconditionallyVisible())
4107 Def->setVisibleDespiteOwningModule();
4109 getContext().mergeDefinitionIntoModule(
4110 Def, MergedDef->getImportedOwningModule(),
4111 /*NotifyListeners*/ false);
4112 PendingMergedDefinitionsToDeduplicate.insert(Def);
4117 bool ASTReader::loadGlobalIndex() {
4121 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4122 !PP.getLangOpts().Modules)
4125 // Try to load the global index.
4126 TriedLoadingGlobalIndex = true;
4127 StringRef ModuleCachePath
4128 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4129 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4130 GlobalModuleIndex::readIndex(ModuleCachePath);
4131 if (llvm::Error Err = std::move(Result.second)) {
4132 assert(!Result.first);
4133 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4137 GlobalIndex.reset(Result.first);
4138 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4142 bool ASTReader::isGlobalIndexUnavailable() const {
4143 return PP.getLangOpts().Modules && UseGlobalIndex &&
4144 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4147 static void updateModuleTimestamp(ModuleFile &MF) {
4148 // Overwrite the timestamp file contents so that file's mtime changes.
4149 std::string TimestampFilename = MF.getTimestampFilename();
4151 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4154 OS << "Timestamp file\n";
4156 OS.clear_error(); // Avoid triggering a fatal error.
4159 /// Given a cursor at the start of an AST file, scan ahead and drop the
4160 /// cursor into the start of the given block ID, returning false on success and
4161 /// true on failure.
4162 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4164 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4166 // FIXME this drops errors on the floor.
4167 consumeError(MaybeEntry.takeError());
4170 llvm::BitstreamEntry Entry = MaybeEntry.get();
4172 switch (Entry.Kind) {
4173 case llvm::BitstreamEntry::Error:
4174 case llvm::BitstreamEntry::EndBlock:
4177 case llvm::BitstreamEntry::Record:
4178 // Ignore top-level records.
4179 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4182 // FIXME this drops errors on the floor.
4183 consumeError(Skipped.takeError());
4187 case llvm::BitstreamEntry::SubBlock:
4188 if (Entry.ID == BlockID) {
4189 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4190 // FIXME this drops the error on the floor.
4191 consumeError(std::move(Err));
4198 if (llvm::Error Err = Cursor.SkipBlock()) {
4199 // FIXME this drops the error on the floor.
4200 consumeError(std::move(Err));
4207 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4209 SourceLocation ImportLoc,
4210 unsigned ClientLoadCapabilities,
4211 SmallVectorImpl<ImportedSubmodule> *Imported) {
4212 llvm::SaveAndRestore<SourceLocation>
4213 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4215 // Defer any pending actions until we get to the end of reading the AST file.
4216 Deserializing AnASTFile(this);
4218 // Bump the generation number.
4219 unsigned PreviousGeneration = 0;
4221 PreviousGeneration = incrementGeneration(*ContextObj);
4223 unsigned NumModules = ModuleMgr.size();
4224 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4225 assert(ReadResult && "expected to return error");
4226 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4227 PP.getLangOpts().Modules
4228 ? &PP.getHeaderSearchInfo().getModuleMap()
4231 // If we find that any modules are unusable, the global index is going
4232 // to be out-of-date. Just remove it.
4233 GlobalIndex.reset();
4234 ModuleMgr.setGlobalIndex(nullptr);
4238 SmallVector<ImportedModule, 4> Loaded;
4239 switch (ASTReadResult ReadResult =
4240 ReadASTCore(FileName, Type, ImportLoc,
4241 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4242 ASTFileSignature(), ClientLoadCapabilities)) {
4246 case VersionMismatch:
4247 case ConfigurationMismatch:
4249 return removeModulesAndReturn(ReadResult);
4254 // Here comes stuff that we only do once the entire chain is loaded.
4256 // Load the AST blocks of all of the modules that we loaded. We can still
4257 // hit errors parsing the ASTs at this point.
4258 for (ImportedModule &M : Loaded) {
4259 ModuleFile &F = *M.Mod;
4261 // Read the AST block.
4262 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4263 return removeModulesAndReturn(Result);
4265 // The AST block should always have a definition for the main module.
4266 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4267 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4268 return removeModulesAndReturn(Failure);
4271 // Read the extension blocks.
4272 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4273 if (ASTReadResult Result = ReadExtensionBlock(F))
4274 return removeModulesAndReturn(Result);
4277 // Once read, set the ModuleFile bit base offset and update the size in
4278 // bits of all files we've seen.
4279 F.GlobalBitOffset = TotalModulesSizeInBits;
4280 TotalModulesSizeInBits += F.SizeInBits;
4281 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4284 // Preload source locations and interesting indentifiers.
4285 for (ImportedModule &M : Loaded) {
4286 ModuleFile &F = *M.Mod;
4288 // Preload SLocEntries.
4289 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4290 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4291 // Load it through the SourceManager and don't call ReadSLocEntry()
4292 // directly because the entry may have already been loaded in which case
4293 // calling ReadSLocEntry() directly would trigger an assertion in
4295 SourceMgr.getLoadedSLocEntryByID(Index);
4298 // Map the original source file ID into the ID space of the current
4300 if (F.OriginalSourceFileID.isValid()) {
4301 F.OriginalSourceFileID = FileID::get(
4302 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4305 // Preload all the pending interesting identifiers by marking them out of
4307 for (auto Offset : F.PreloadIdentifierOffsets) {
4308 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4309 F.IdentifierTableData + Offset);
4311 ASTIdentifierLookupTrait Trait(*this, F);
4312 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4313 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4314 auto &II = PP.getIdentifierTable().getOwn(Key);
4315 II.setOutOfDate(true);
4317 // Mark this identifier as being from an AST file so that we can track
4318 // whether we need to serialize it.
4319 markIdentifierFromAST(*this, II);
4321 // Associate the ID with the identifier so that the writer can reuse it.
4322 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4323 SetIdentifierInfo(ID, &II);
4327 // Setup the import locations and notify the module manager that we've
4328 // committed to these module files.
4329 for (ImportedModule &M : Loaded) {
4330 ModuleFile &F = *M.Mod;
4332 ModuleMgr.moduleFileAccepted(&F);
4334 // Set the import location.
4335 F.DirectImportLoc = ImportLoc;
4336 // FIXME: We assume that locations from PCH / preamble do not need
4339 F.ImportLoc = M.ImportLoc;
4341 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4344 if (!PP.getLangOpts().CPlusPlus ||
4345 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4346 Type != MK_PrebuiltModule)) {
4347 // Mark all of the identifiers in the identifier table as being out of date,
4348 // so that various accessors know to check the loaded modules when the
4349 // identifier is used.
4351 // For C++ modules, we don't need information on many identifiers (just
4352 // those that provide macros or are poisoned), so we mark all of
4353 // the interesting ones via PreloadIdentifierOffsets.
4354 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4355 IdEnd = PP.getIdentifierTable().end();
4357 Id->second->setOutOfDate(true);
4359 // Mark selectors as out of date.
4360 for (auto Sel : SelectorGeneration)
4361 SelectorOutOfDate[Sel.first] = true;
4363 // Resolve any unresolved module exports.
4364 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4365 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4366 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4367 Module *ResolvedMod = getSubmodule(GlobalID);
4369 switch (Unresolved.Kind) {
4370 case UnresolvedModuleRef::Conflict:
4372 Module::Conflict Conflict;
4373 Conflict.Other = ResolvedMod;
4374 Conflict.Message = Unresolved.String.str();
4375 Unresolved.Mod->Conflicts.push_back(Conflict);
4379 case UnresolvedModuleRef::Import:
4381 Unresolved.Mod->Imports.insert(ResolvedMod);
4384 case UnresolvedModuleRef::Export:
4385 if (ResolvedMod || Unresolved.IsWildcard)
4386 Unresolved.Mod->Exports.push_back(
4387 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4391 UnresolvedModuleRefs.clear();
4394 Imported->append(ImportedModules.begin(),
4395 ImportedModules.end());
4397 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4398 // Might be unnecessary as use declarations are only used to build the
4402 InitializeContext();
4407 if (DeserializationListener)
4408 DeserializationListener->ReaderInitialized(this);
4410 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4411 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4412 // If this AST file is a precompiled preamble, then set the
4413 // preamble file ID of the source manager to the file source file
4414 // from which the preamble was built.
4415 if (Type == MK_Preamble) {
4416 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4417 } else if (Type == MK_MainFile) {
4418 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4422 // For any Objective-C class definitions we have already loaded, make sure
4423 // that we load any additional categories.
4425 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4426 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4427 ObjCClassesLoaded[I],
4428 PreviousGeneration);
4432 if (PP.getHeaderSearchInfo()
4433 .getHeaderSearchOpts()
4434 .ModulesValidateOncePerBuildSession) {
4435 // Now we are certain that the module and all modules it depends on are
4436 // up to date. Create or update timestamp files for modules that are
4437 // located in the module cache (not for PCH files that could be anywhere
4438 // in the filesystem).
4439 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4440 ImportedModule &M = Loaded[I];
4441 if (M.Mod->Kind == MK_ImplicitModule) {
4442 updateModuleTimestamp(*M.Mod);
4450 static ASTFileSignature readASTFileSignature(StringRef PCH);
4452 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4453 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4454 // FIXME checking magic headers is done in other places such as
4455 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4456 // always done the same. Unify it all with a helper.
4457 if (!Stream.canSkipToPos(4))
4458 return llvm::createStringError(std::errc::illegal_byte_sequence,
4459 "file too small to contain AST file magic");
4460 for (unsigned C : {'C', 'P', 'C', 'H'})
4461 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4463 return llvm::createStringError(
4464 std::errc::illegal_byte_sequence,
4465 "file doesn't start with AST file magic");
4467 return Res.takeError();
4468 return llvm::Error::success();
4471 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4475 case MK_ImplicitModule:
4476 case MK_ExplicitModule:
4477 case MK_PrebuiltModule:
4481 return 2; // main source file
4483 llvm_unreachable("unknown module kind");
4486 ASTReader::ASTReadResult
4487 ASTReader::ReadASTCore(StringRef FileName,
4489 SourceLocation ImportLoc,
4490 ModuleFile *ImportedBy,
4491 SmallVectorImpl<ImportedModule> &Loaded,
4492 off_t ExpectedSize, time_t ExpectedModTime,
4493 ASTFileSignature ExpectedSignature,
4494 unsigned ClientLoadCapabilities) {
4496 std::string ErrorStr;
4497 ModuleManager::AddModuleResult AddResult
4498 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4499 getGeneration(), ExpectedSize, ExpectedModTime,
4500 ExpectedSignature, readASTFileSignature,
4503 switch (AddResult) {
4504 case ModuleManager::AlreadyLoaded:
4505 Diag(diag::remark_module_import)
4506 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4507 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4510 case ModuleManager::NewlyLoaded:
4511 // Load module file below.
4514 case ModuleManager::Missing:
4515 // The module file was missing; if the client can handle that, return
4517 if (ClientLoadCapabilities & ARR_Missing)
4520 // Otherwise, return an error.
4521 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4522 << FileName << !ErrorStr.empty()
4526 case ModuleManager::OutOfDate:
4527 // We couldn't load the module file because it is out-of-date. If the
4528 // client can handle out-of-date, return it.
4529 if (ClientLoadCapabilities & ARR_OutOfDate)
4532 // Otherwise, return an error.
4533 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4534 << FileName << !ErrorStr.empty()
4539 assert(M && "Missing module file");
4541 bool ShouldFinalizePCM = false;
4542 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4543 auto &MC = getModuleManager().getModuleCache();
4544 if (ShouldFinalizePCM)
4545 MC.finalizePCM(FileName);
4547 MC.tryToDropPCM(FileName);
4550 BitstreamCursor &Stream = F.Stream;
4551 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4552 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4554 // Sniff for the signature.
4555 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4556 Diag(diag::err_module_file_invalid)
4557 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4561 // This is used for compatibility with older PCH formats.
4562 bool HaveReadControlBlock = false;
4564 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4566 Error(MaybeEntry.takeError());
4569 llvm::BitstreamEntry Entry = MaybeEntry.get();
4571 switch (Entry.Kind) {
4572 case llvm::BitstreamEntry::Error:
4573 case llvm::BitstreamEntry::Record:
4574 case llvm::BitstreamEntry::EndBlock:
4575 Error("invalid record at top-level of AST file");
4578 case llvm::BitstreamEntry::SubBlock:
4583 case CONTROL_BLOCK_ID:
4584 HaveReadControlBlock = true;
4585 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4587 // Check that we didn't try to load a non-module AST file as a module.
4589 // FIXME: Should we also perform the converse check? Loading a module as
4590 // a PCH file sort of works, but it's a bit wonky.
4591 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4592 Type == MK_PrebuiltModule) &&
4593 F.ModuleName.empty()) {
4594 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4595 if (Result != OutOfDate ||
4596 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4597 Diag(diag::err_module_file_not_module) << FileName;
4602 case Failure: return Failure;
4603 case Missing: return Missing;
4604 case OutOfDate: return OutOfDate;
4605 case VersionMismatch: return VersionMismatch;
4606 case ConfigurationMismatch: return ConfigurationMismatch;
4607 case HadErrors: return HadErrors;
4612 if (!HaveReadControlBlock) {
4613 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4614 Diag(diag::err_pch_version_too_old);
4615 return VersionMismatch;
4618 // Record that we've loaded this module.
4619 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4620 ShouldFinalizePCM = true;
4623 case UNHASHED_CONTROL_BLOCK_ID:
4624 // This block is handled using look-ahead during ReadControlBlock. We
4625 // shouldn't get here!
4626 Error("malformed block record in AST file");
4630 if (llvm::Error Err = Stream.SkipBlock()) {
4631 Error(std::move(Err));
4638 llvm_unreachable("unexpected break; expected return");
4641 ASTReader::ASTReadResult
4642 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4643 unsigned ClientLoadCapabilities) {
4644 const HeaderSearchOptions &HSOpts =
4645 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4646 bool AllowCompatibleConfigurationMismatch =
4647 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4649 ASTReadResult Result = readUnhashedControlBlockImpl(
4650 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4652 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4654 // If F was directly imported by another module, it's implicitly validated by
4655 // the importing module.
4656 if (DisableValidation || WasImportedBy ||
4657 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4660 if (Result == Failure) {
4661 Error("malformed block record in AST file");
4665 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4666 // If this module has already been finalized in the ModuleCache, we're stuck
4667 // with it; we can only load a single version of each module.
4669 // This can happen when a module is imported in two contexts: in one, as a
4670 // user module; in another, as a system module (due to an import from
4671 // another module marked with the [system] flag). It usually indicates a
4672 // bug in the module map: this module should also be marked with [system].
4674 // If -Wno-system-headers (the default), and the first import is as a
4675 // system module, then validation will fail during the as-user import,
4676 // since -Werror flags won't have been validated. However, it's reasonable
4677 // to treat this consistently as a system module.
4679 // If -Wsystem-headers, the PCM on disk was built with
4680 // -Wno-system-headers, and the first import is as a user module, then
4681 // validation will fail during the as-system import since the PCM on disk
4682 // doesn't guarantee that -Werror was respected. However, the -Werror
4683 // flags were checked during the initial as-user import.
4684 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4685 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4693 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4694 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4695 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4696 bool ValidateDiagnosticOptions) {
4697 // Initialize a stream.
4698 BitstreamCursor Stream(StreamData);
4700 // Sniff for the signature.
4701 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4702 // FIXME this drops the error on the floor.
4703 consumeError(std::move(Err));
4707 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4708 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4711 // Read all of the records in the options block.
4713 ASTReadResult Result = Success;
4715 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4717 // FIXME this drops the error on the floor.
4718 consumeError(MaybeEntry.takeError());
4721 llvm::BitstreamEntry Entry = MaybeEntry.get();
4723 switch (Entry.Kind) {
4724 case llvm::BitstreamEntry::Error:
4725 case llvm::BitstreamEntry::SubBlock:
4728 case llvm::BitstreamEntry::EndBlock:
4731 case llvm::BitstreamEntry::Record:
4732 // The interesting case.
4736 // Read and process a record.
4738 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4739 if (!MaybeRecordType) {
4740 // FIXME this drops the error.
4743 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4746 F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4748 case AST_BLOCK_HASH:
4751 ASTFileSignature::create(Record.begin(), Record.end());
4753 case DIAGNOSTIC_OPTIONS: {
4754 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4755 if (Listener && ValidateDiagnosticOptions &&
4756 !AllowCompatibleConfigurationMismatch &&
4757 ParseDiagnosticOptions(Record, Complain, *Listener))
4758 Result = OutOfDate; // Don't return early. Read the signature.
4761 case DIAG_PRAGMA_MAPPINGS:
4764 if (F->PragmaDiagMappings.empty())
4765 F->PragmaDiagMappings.swap(Record);
4767 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4768 Record.begin(), Record.end());
4774 /// Parse a record and blob containing module file extension metadata.
4775 static bool parseModuleFileExtensionMetadata(
4776 const SmallVectorImpl<uint64_t> &Record,
4778 ModuleFileExtensionMetadata &Metadata) {
4779 if (Record.size() < 4) return true;
4781 Metadata.MajorVersion = Record[0];
4782 Metadata.MinorVersion = Record[1];
4784 unsigned BlockNameLen = Record[2];
4785 unsigned UserInfoLen = Record[3];
4787 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4789 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4790 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4791 Blob.data() + BlockNameLen + UserInfoLen);
4795 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4796 BitstreamCursor &Stream = F.Stream;
4800 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4802 Error(MaybeEntry.takeError());
4805 llvm::BitstreamEntry Entry = MaybeEntry.get();
4807 switch (Entry.Kind) {
4808 case llvm::BitstreamEntry::SubBlock:
4809 if (llvm::Error Err = Stream.SkipBlock()) {
4810 Error(std::move(Err));
4815 case llvm::BitstreamEntry::EndBlock:
4818 case llvm::BitstreamEntry::Error:
4821 case llvm::BitstreamEntry::Record:
4827 Expected<unsigned> MaybeRecCode =
4828 Stream.readRecord(Entry.ID, Record, &Blob);
4829 if (!MaybeRecCode) {
4830 Error(MaybeRecCode.takeError());
4833 switch (MaybeRecCode.get()) {
4834 case EXTENSION_METADATA: {
4835 ModuleFileExtensionMetadata Metadata;
4836 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4837 Error("malformed EXTENSION_METADATA in AST file");
4841 // Find a module file extension with this block name.
4842 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4843 if (Known == ModuleFileExtensions.end()) break;
4846 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4848 F.ExtensionReaders.push_back(std::move(Reader));
4859 void ASTReader::InitializeContext() {
4860 assert(ContextObj && "no context to initialize");
4861 ASTContext &Context = *ContextObj;
4863 // If there's a listener, notify them that we "read" the translation unit.
4864 if (DeserializationListener)
4865 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4866 Context.getTranslationUnitDecl());
4868 // FIXME: Find a better way to deal with collisions between these
4869 // built-in types. Right now, we just ignore the problem.
4871 // Load the special types.
4872 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4873 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4874 if (!Context.CFConstantStringTypeDecl)
4875 Context.setCFConstantStringType(GetType(String));
4878 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4879 QualType FileType = GetType(File);
4880 if (FileType.isNull()) {
4881 Error("FILE type is NULL");
4885 if (!Context.FILEDecl) {
4886 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4887 Context.setFILEDecl(Typedef->getDecl());
4889 const TagType *Tag = FileType->getAs<TagType>();
4891 Error("Invalid FILE type in AST file");
4894 Context.setFILEDecl(Tag->getDecl());
4899 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4900 QualType Jmp_bufType = GetType(Jmp_buf);
4901 if (Jmp_bufType.isNull()) {
4902 Error("jmp_buf type is NULL");
4906 if (!Context.jmp_bufDecl) {
4907 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4908 Context.setjmp_bufDecl(Typedef->getDecl());
4910 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4912 Error("Invalid jmp_buf type in AST file");
4915 Context.setjmp_bufDecl(Tag->getDecl());
4920 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4921 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4922 if (Sigjmp_bufType.isNull()) {
4923 Error("sigjmp_buf type is NULL");
4927 if (!Context.sigjmp_bufDecl) {
4928 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4929 Context.setsigjmp_bufDecl(Typedef->getDecl());
4931 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4932 assert(Tag && "Invalid sigjmp_buf type in AST file");
4933 Context.setsigjmp_bufDecl(Tag->getDecl());
4938 if (unsigned ObjCIdRedef
4939 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4940 if (Context.ObjCIdRedefinitionType.isNull())
4941 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4944 if (unsigned ObjCClassRedef
4945 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4946 if (Context.ObjCClassRedefinitionType.isNull())
4947 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4950 if (unsigned ObjCSelRedef
4951 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4952 if (Context.ObjCSelRedefinitionType.isNull())
4953 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4956 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4957 QualType Ucontext_tType = GetType(Ucontext_t);
4958 if (Ucontext_tType.isNull()) {
4959 Error("ucontext_t type is NULL");
4963 if (!Context.ucontext_tDecl) {
4964 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4965 Context.setucontext_tDecl(Typedef->getDecl());
4967 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4968 assert(Tag && "Invalid ucontext_t type in AST file");
4969 Context.setucontext_tDecl(Tag->getDecl());
4975 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4977 // If there were any CUDA special declarations, deserialize them.
4978 if (!CUDASpecialDeclRefs.empty()) {
4979 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4980 Context.setcudaConfigureCallDecl(
4981 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4984 // Re-export any modules that were imported by a non-module AST file.
4985 // FIXME: This does not make macro-only imports visible again.
4986 for (auto &Import : ImportedModules) {
4987 if (Module *Imported = getSubmodule(Import.ID)) {
4988 makeModuleVisible(Imported, Module::AllVisible,
4989 /*ImportLoc=*/Import.ImportLoc);
4990 if (Import.ImportLoc.isValid())
4991 PP.makeModuleVisible(Imported, Import.ImportLoc);
4992 // FIXME: should we tell Sema to make the module visible too?
4995 ImportedModules.clear();
4998 void ASTReader::finalizeForWriting() {
4999 // Nothing to do for now.
5002 /// Reads and return the signature record from \p PCH's control block, or
5004 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5005 BitstreamCursor Stream(PCH);
5006 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5007 // FIXME this drops the error on the floor.
5008 consumeError(std::move(Err));
5009 return ASTFileSignature();
5012 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5013 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5014 return ASTFileSignature();
5016 // Scan for SIGNATURE inside the diagnostic options block.
5017 ASTReader::RecordData Record;
5019 Expected<llvm::BitstreamEntry> MaybeEntry =
5020 Stream.advanceSkippingSubblocks();
5022 // FIXME this drops the error on the floor.
5023 consumeError(MaybeEntry.takeError());
5024 return ASTFileSignature();
5026 llvm::BitstreamEntry Entry = MaybeEntry.get();
5028 if (Entry.Kind != llvm::BitstreamEntry::Record)
5029 return ASTFileSignature();
5033 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5035 // FIXME this drops the error on the floor.
5036 consumeError(MaybeRecord.takeError());
5037 return ASTFileSignature();
5039 if (SIGNATURE == MaybeRecord.get())
5040 return ASTFileSignature::create(Record.begin(),
5041 Record.begin() + ASTFileSignature::size);
5045 /// Retrieve the name of the original source file name
5046 /// directly from the AST file, without actually loading the AST
5048 std::string ASTReader::getOriginalSourceFile(
5049 const std::string &ASTFileName, FileManager &FileMgr,
5050 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5051 // Open the AST file.
5052 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5054 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5055 << ASTFileName << Buffer.getError().message();
5056 return std::string();
5059 // Initialize the stream
5060 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5062 // Sniff for the signature.
5063 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5064 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5065 return std::string();
5068 // Scan for the CONTROL_BLOCK_ID block.
5069 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5070 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5071 return std::string();
5074 // Scan for ORIGINAL_FILE inside the control block.
5077 Expected<llvm::BitstreamEntry> MaybeEntry =
5078 Stream.advanceSkippingSubblocks();
5080 // FIXME this drops errors on the floor.
5081 consumeError(MaybeEntry.takeError());
5082 return std::string();
5084 llvm::BitstreamEntry Entry = MaybeEntry.get();
5086 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5087 return std::string();
5089 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5090 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5091 return std::string();
5096 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5098 // FIXME this drops the errors on the floor.
5099 consumeError(MaybeRecord.takeError());
5100 return std::string();
5102 if (ORIGINAL_FILE == MaybeRecord.get())
5109 class SimplePCHValidator : public ASTReaderListener {
5110 const LangOptions &ExistingLangOpts;
5111 const TargetOptions &ExistingTargetOpts;
5112 const PreprocessorOptions &ExistingPPOpts;
5113 std::string ExistingModuleCachePath;
5114 FileManager &FileMgr;
5117 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5118 const TargetOptions &ExistingTargetOpts,
5119 const PreprocessorOptions &ExistingPPOpts,
5120 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5121 : ExistingLangOpts(ExistingLangOpts),
5122 ExistingTargetOpts(ExistingTargetOpts),
5123 ExistingPPOpts(ExistingPPOpts),
5124 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5126 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5127 bool AllowCompatibleDifferences) override {
5128 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5129 AllowCompatibleDifferences);
5132 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5133 bool AllowCompatibleDifferences) override {
5134 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5135 AllowCompatibleDifferences);
5138 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5139 StringRef SpecificModuleCachePath,
5140 bool Complain) override {
5141 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5142 ExistingModuleCachePath,
5143 nullptr, ExistingLangOpts);
5146 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5148 std::string &SuggestedPredefines) override {
5149 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5150 SuggestedPredefines, ExistingLangOpts);
5156 bool ASTReader::readASTFileControlBlock(
5157 StringRef Filename, FileManager &FileMgr,
5158 const PCHContainerReader &PCHContainerRdr,
5159 bool FindModuleFileExtensions,
5160 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5161 // Open the AST file.
5162 // FIXME: This allows use of the VFS; we do not allow use of the
5163 // VFS when actually loading a module.
5164 auto Buffer = FileMgr.getBufferForFile(Filename);
5169 // Initialize the stream
5170 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5171 BitstreamCursor Stream(Bytes);
5173 // Sniff for the signature.
5174 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5175 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5179 // Scan for the CONTROL_BLOCK_ID block.
5180 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5183 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5184 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5185 bool NeedsImports = Listener.needsImportVisitation();
5186 BitstreamCursor InputFilesCursor;
5189 std::string ModuleDir;
5190 bool DoneWithControlBlock = false;
5191 while (!DoneWithControlBlock) {
5192 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5194 // FIXME this drops the error on the floor.
5195 consumeError(MaybeEntry.takeError());
5198 llvm::BitstreamEntry Entry = MaybeEntry.get();
5200 switch (Entry.Kind) {
5201 case llvm::BitstreamEntry::SubBlock: {
5203 case OPTIONS_BLOCK_ID: {
5204 std::string IgnoredSuggestedPredefines;
5205 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5206 /*AllowCompatibleConfigurationMismatch*/ false,
5207 Listener, IgnoredSuggestedPredefines) != Success)
5212 case INPUT_FILES_BLOCK_ID:
5213 InputFilesCursor = Stream;
5214 if (llvm::Error Err = Stream.SkipBlock()) {
5215 // FIXME this drops the error on the floor.
5216 consumeError(std::move(Err));
5219 if (NeedsInputFiles &&
5220 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5225 if (llvm::Error Err = Stream.SkipBlock()) {
5226 // FIXME this drops the error on the floor.
5227 consumeError(std::move(Err));
5236 case llvm::BitstreamEntry::EndBlock:
5237 DoneWithControlBlock = true;
5240 case llvm::BitstreamEntry::Error:
5243 case llvm::BitstreamEntry::Record:
5247 if (DoneWithControlBlock) break;
5251 Expected<unsigned> MaybeRecCode =
5252 Stream.readRecord(Entry.ID, Record, &Blob);
5253 if (!MaybeRecCode) {
5254 // FIXME this drops the error.
5257 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5259 if (Record[0] != VERSION_MAJOR)
5261 if (Listener.ReadFullVersionInformation(Blob))
5265 Listener.ReadModuleName(Blob);
5267 case MODULE_DIRECTORY:
5268 ModuleDir = std::string(Blob);
5270 case MODULE_MAP_FILE: {
5272 auto Path = ReadString(Record, Idx);
5273 ResolveImportedPath(Path, ModuleDir);
5274 Listener.ReadModuleMapFile(Path);
5277 case INPUT_FILE_OFFSETS: {
5278 if (!NeedsInputFiles)
5281 unsigned NumInputFiles = Record[0];
5282 unsigned NumUserFiles = Record[1];
5283 const llvm::support::unaligned_uint64_t *InputFileOffs =
5284 (const llvm::support::unaligned_uint64_t *)Blob.data();
5285 for (unsigned I = 0; I != NumInputFiles; ++I) {
5286 // Go find this input file.
5287 bool isSystemFile = I >= NumUserFiles;
5289 if (isSystemFile && !NeedsSystemInputFiles)
5290 break; // the rest are system input files
5292 BitstreamCursor &Cursor = InputFilesCursor;
5293 SavedStreamPosition SavedPosition(Cursor);
5294 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5295 // FIXME this drops errors on the floor.
5296 consumeError(std::move(Err));
5299 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5301 // FIXME this drops errors on the floor.
5302 consumeError(MaybeCode.takeError());
5304 unsigned Code = MaybeCode.get();
5308 bool shouldContinue = false;
5309 Expected<unsigned> MaybeRecordType =
5310 Cursor.readRecord(Code, Record, &Blob);
5311 if (!MaybeRecordType) {
5312 // FIXME this drops errors on the floor.
5313 consumeError(MaybeRecordType.takeError());
5315 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5316 case INPUT_FILE_HASH:
5319 bool Overridden = static_cast<bool>(Record[3]);
5320 std::string Filename = std::string(Blob);
5321 ResolveImportedPath(Filename, ModuleDir);
5322 shouldContinue = Listener.visitInputFile(
5323 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5326 if (!shouldContinue)
5336 unsigned Idx = 0, N = Record.size();
5338 // Read information about the AST file.
5341 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5342 std::string ModuleName = ReadString(Record, Idx);
5343 std::string Filename = ReadString(Record, Idx);
5344 ResolveImportedPath(Filename, ModuleDir);
5345 Listener.visitImport(ModuleName, Filename);
5351 // No other validation to perform.
5356 // Look for module file extension blocks, if requested.
5357 if (FindModuleFileExtensions) {
5358 BitstreamCursor SavedStream = Stream;
5359 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5360 bool DoneWithExtensionBlock = false;
5361 while (!DoneWithExtensionBlock) {
5362 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5364 // FIXME this drops the error.
5367 llvm::BitstreamEntry Entry = MaybeEntry.get();
5369 switch (Entry.Kind) {
5370 case llvm::BitstreamEntry::SubBlock:
5371 if (llvm::Error Err = Stream.SkipBlock()) {
5372 // FIXME this drops the error on the floor.
5373 consumeError(std::move(Err));
5378 case llvm::BitstreamEntry::EndBlock:
5379 DoneWithExtensionBlock = true;
5382 case llvm::BitstreamEntry::Error:
5385 case llvm::BitstreamEntry::Record:
5391 Expected<unsigned> MaybeRecCode =
5392 Stream.readRecord(Entry.ID, Record, &Blob);
5393 if (!MaybeRecCode) {
5394 // FIXME this drops the error.
5397 switch (MaybeRecCode.get()) {
5398 case EXTENSION_METADATA: {
5399 ModuleFileExtensionMetadata Metadata;
5400 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5403 Listener.readModuleFileExtension(Metadata);
5409 Stream = SavedStream;
5412 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5413 if (readUnhashedControlBlockImpl(
5414 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5415 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5416 ValidateDiagnosticOptions) != Success)
5422 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5423 const PCHContainerReader &PCHContainerRdr,
5424 const LangOptions &LangOpts,
5425 const TargetOptions &TargetOpts,
5426 const PreprocessorOptions &PPOpts,
5427 StringRef ExistingModuleCachePath) {
5428 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5429 ExistingModuleCachePath, FileMgr);
5430 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5431 /*FindModuleFileExtensions=*/false,
5433 /*ValidateDiagnosticOptions=*/true);
5436 ASTReader::ASTReadResult
5437 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5438 // Enter the submodule block.
5439 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5440 Error(std::move(Err));
5444 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5446 Module *CurrentModule = nullptr;
5449 Expected<llvm::BitstreamEntry> MaybeEntry =
5450 F.Stream.advanceSkippingSubblocks();
5452 Error(MaybeEntry.takeError());
5455 llvm::BitstreamEntry Entry = MaybeEntry.get();
5457 switch (Entry.Kind) {
5458 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5459 case llvm::BitstreamEntry::Error:
5460 Error("malformed block record in AST file");
5462 case llvm::BitstreamEntry::EndBlock:
5464 case llvm::BitstreamEntry::Record:
5465 // The interesting case.
5472 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5474 Error(MaybeKind.takeError());
5477 unsigned Kind = MaybeKind.get();
5479 if ((Kind == SUBMODULE_METADATA) != First) {
5480 Error("submodule metadata record should be at beginning of block");
5485 // Submodule information is only valid if we have a current module.
5486 // FIXME: Should we error on these cases?
5487 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5488 Kind != SUBMODULE_DEFINITION)
5492 default: // Default behavior: ignore.
5495 case SUBMODULE_DEFINITION: {
5496 if (Record.size() < 12) {
5497 Error("malformed module definition");
5501 StringRef Name = Blob;
5503 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5504 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5505 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5506 bool IsFramework = Record[Idx++];
5507 bool IsExplicit = Record[Idx++];
5508 bool IsSystem = Record[Idx++];
5509 bool IsExternC = Record[Idx++];
5510 bool InferSubmodules = Record[Idx++];
5511 bool InferExplicitSubmodules = Record[Idx++];
5512 bool InferExportWildcard = Record[Idx++];
5513 bool ConfigMacrosExhaustive = Record[Idx++];
5514 bool ModuleMapIsPrivate = Record[Idx++];
5516 Module *ParentModule = nullptr;
5518 ParentModule = getSubmodule(Parent);
5520 // Retrieve this (sub)module from the module map, creating it if
5523 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5526 // FIXME: set the definition loc for CurrentModule, or call
5527 // ModMap.setInferredModuleAllowedBy()
5529 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5530 if (GlobalIndex >= SubmodulesLoaded.size() ||
5531 SubmodulesLoaded[GlobalIndex]) {
5532 Error("too many submodules");
5536 if (!ParentModule) {
5537 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5538 // Don't emit module relocation error if we have -fno-validate-pch
5539 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5540 CurFile != F.File) {
5541 Error(diag::err_module_file_conflict,
5542 CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5548 F.DidReadTopLevelSubmodule = true;
5549 CurrentModule->setASTFile(F.File);
5550 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5553 CurrentModule->Kind = Kind;
5554 CurrentModule->Signature = F.Signature;
5555 CurrentModule->IsFromModuleFile = true;
5556 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5557 CurrentModule->IsExternC = IsExternC;
5558 CurrentModule->InferSubmodules = InferSubmodules;
5559 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5560 CurrentModule->InferExportWildcard = InferExportWildcard;
5561 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5562 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5563 if (DeserializationListener)
5564 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5566 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5568 // Clear out data that will be replaced by what is in the module file.
5569 CurrentModule->LinkLibraries.clear();
5570 CurrentModule->ConfigMacros.clear();
5571 CurrentModule->UnresolvedConflicts.clear();
5572 CurrentModule->Conflicts.clear();
5574 // The module is available unless it's missing a requirement; relevant
5575 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5576 // Missing headers that were present when the module was built do not
5577 // make it unavailable -- if we got this far, this must be an explicitly
5578 // imported module file.
5579 CurrentModule->Requirements.clear();
5580 CurrentModule->MissingHeaders.clear();
5581 CurrentModule->IsUnimportable =
5582 ParentModule && ParentModule->IsUnimportable;
5583 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5587 case SUBMODULE_UMBRELLA_HEADER: {
5588 std::string Filename = std::string(Blob);
5589 ResolveImportedPath(F, Filename);
5590 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5591 if (!CurrentModule->getUmbrellaHeader())
5592 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5593 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5594 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5595 Error("mismatched umbrella headers in submodule");
5602 case SUBMODULE_HEADER:
5603 case SUBMODULE_EXCLUDED_HEADER:
5604 case SUBMODULE_PRIVATE_HEADER:
5605 // We lazily associate headers with their modules via the HeaderInfo table.
5606 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5607 // of complete filenames or remove it entirely.
5610 case SUBMODULE_TEXTUAL_HEADER:
5611 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5612 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5616 case SUBMODULE_TOPHEADER:
5617 CurrentModule->addTopHeaderFilename(Blob);
5620 case SUBMODULE_UMBRELLA_DIR: {
5621 std::string Dirname = std::string(Blob);
5622 ResolveImportedPath(F, Dirname);
5623 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5624 if (!CurrentModule->getUmbrellaDir())
5625 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5626 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5627 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5628 Error("mismatched umbrella directories in submodule");
5635 case SUBMODULE_METADATA: {
5636 F.BaseSubmoduleID = getTotalNumSubmodules();
5637 F.LocalNumSubmodules = Record[0];
5638 unsigned LocalBaseSubmoduleID = Record[1];
5639 if (F.LocalNumSubmodules > 0) {
5640 // Introduce the global -> local mapping for submodules within this
5642 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5644 // Introduce the local -> global mapping for submodules within this
5646 F.SubmoduleRemap.insertOrReplace(
5647 std::make_pair(LocalBaseSubmoduleID,
5648 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5650 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5655 case SUBMODULE_IMPORTS:
5656 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5657 UnresolvedModuleRef Unresolved;
5658 Unresolved.File = &F;
5659 Unresolved.Mod = CurrentModule;
5660 Unresolved.ID = Record[Idx];
5661 Unresolved.Kind = UnresolvedModuleRef::Import;
5662 Unresolved.IsWildcard = false;
5663 UnresolvedModuleRefs.push_back(Unresolved);
5667 case SUBMODULE_EXPORTS:
5668 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5669 UnresolvedModuleRef Unresolved;
5670 Unresolved.File = &F;
5671 Unresolved.Mod = CurrentModule;
5672 Unresolved.ID = Record[Idx];
5673 Unresolved.Kind = UnresolvedModuleRef::Export;
5674 Unresolved.IsWildcard = Record[Idx + 1];
5675 UnresolvedModuleRefs.push_back(Unresolved);
5678 // Once we've loaded the set of exports, there's no reason to keep
5679 // the parsed, unresolved exports around.
5680 CurrentModule->UnresolvedExports.clear();
5683 case SUBMODULE_REQUIRES:
5684 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5685 PP.getTargetInfo());
5688 case SUBMODULE_LINK_LIBRARY:
5689 ModMap.resolveLinkAsDependencies(CurrentModule);
5690 CurrentModule->LinkLibraries.push_back(
5691 Module::LinkLibrary(std::string(Blob), Record[0]));
5694 case SUBMODULE_CONFIG_MACRO:
5695 CurrentModule->ConfigMacros.push_back(Blob.str());
5698 case SUBMODULE_CONFLICT: {
5699 UnresolvedModuleRef Unresolved;
5700 Unresolved.File = &F;
5701 Unresolved.Mod = CurrentModule;
5702 Unresolved.ID = Record[0];
5703 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5704 Unresolved.IsWildcard = false;
5705 Unresolved.String = Blob;
5706 UnresolvedModuleRefs.push_back(Unresolved);
5710 case SUBMODULE_INITIALIZERS: {
5713 SmallVector<uint32_t, 16> Inits;
5714 for (auto &ID : Record)
5715 Inits.push_back(getGlobalDeclID(F, ID));
5716 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5720 case SUBMODULE_EXPORT_AS:
5721 CurrentModule->ExportAsModule = Blob.str();
5722 ModMap.addLinkAsDependency(CurrentModule);
5728 /// Parse the record that corresponds to a LangOptions data
5731 /// This routine parses the language options from the AST file and then gives
5732 /// them to the AST listener if one is set.
5734 /// \returns true if the listener deems the file unacceptable, false otherwise.
5735 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5737 ASTReaderListener &Listener,
5738 bool AllowCompatibleDifferences) {
5739 LangOptions LangOpts;
5741 #define LANGOPT(Name, Bits, Default, Description) \
5742 LangOpts.Name = Record[Idx++];
5743 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5744 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5745 #include "clang/Basic/LangOptions.def"
5746 #define SANITIZER(NAME, ID) \
5747 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5748 #include "clang/Basic/Sanitizers.def"
5750 for (unsigned N = Record[Idx++]; N; --N)
5751 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5753 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5754 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5755 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5757 LangOpts.CurrentModule = ReadString(Record, Idx);
5760 for (unsigned N = Record[Idx++]; N; --N) {
5761 LangOpts.CommentOpts.BlockCommandNames.push_back(
5762 ReadString(Record, Idx));
5764 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5766 // OpenMP offloading options.
5767 for (unsigned N = Record[Idx++]; N; --N) {
5768 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5771 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5773 return Listener.ReadLanguageOptions(LangOpts, Complain,
5774 AllowCompatibleDifferences);
5777 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5778 ASTReaderListener &Listener,
5779 bool AllowCompatibleDifferences) {
5781 TargetOptions TargetOpts;
5782 TargetOpts.Triple = ReadString(Record, Idx);
5783 TargetOpts.CPU = ReadString(Record, Idx);
5784 TargetOpts.ABI = ReadString(Record, Idx);
5785 for (unsigned N = Record[Idx++]; N; --N) {
5786 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5788 for (unsigned N = Record[Idx++]; N; --N) {
5789 TargetOpts.Features.push_back(ReadString(Record, Idx));
5792 return Listener.ReadTargetOptions(TargetOpts, Complain,
5793 AllowCompatibleDifferences);
5796 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5797 ASTReaderListener &Listener) {
5798 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5800 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5801 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5802 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5803 #include "clang/Basic/DiagnosticOptions.def"
5805 for (unsigned N = Record[Idx++]; N; --N)
5806 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5807 for (unsigned N = Record[Idx++]; N; --N)
5808 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5810 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5813 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5814 ASTReaderListener &Listener) {
5815 FileSystemOptions FSOpts;
5817 FSOpts.WorkingDir = ReadString(Record, Idx);
5818 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5821 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5823 ASTReaderListener &Listener) {
5824 HeaderSearchOptions HSOpts;
5826 HSOpts.Sysroot = ReadString(Record, Idx);
5829 for (unsigned N = Record[Idx++]; N; --N) {
5830 std::string Path = ReadString(Record, Idx);
5831 frontend::IncludeDirGroup Group
5832 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5833 bool IsFramework = Record[Idx++];
5834 bool IgnoreSysRoot = Record[Idx++];
5835 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5839 // System header prefixes.
5840 for (unsigned N = Record[Idx++]; N; --N) {
5841 std::string Prefix = ReadString(Record, Idx);
5842 bool IsSystemHeader = Record[Idx++];
5843 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5846 HSOpts.ResourceDir = ReadString(Record, Idx);
5847 HSOpts.ModuleCachePath = ReadString(Record, Idx);
5848 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5849 HSOpts.DisableModuleHash = Record[Idx++];
5850 HSOpts.ImplicitModuleMaps = Record[Idx++];
5851 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5852 HSOpts.UseBuiltinIncludes = Record[Idx++];
5853 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5854 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5855 HSOpts.UseLibcxx = Record[Idx++];
5856 std::string SpecificModuleCachePath = ReadString(Record, Idx);
5858 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5862 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5864 ASTReaderListener &Listener,
5865 std::string &SuggestedPredefines) {
5866 PreprocessorOptions PPOpts;
5869 // Macro definitions/undefs
5870 for (unsigned N = Record[Idx++]; N; --N) {
5871 std::string Macro = ReadString(Record, Idx);
5872 bool IsUndef = Record[Idx++];
5873 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5877 for (unsigned N = Record[Idx++]; N; --N) {
5878 PPOpts.Includes.push_back(ReadString(Record, Idx));
5882 for (unsigned N = Record[Idx++]; N; --N) {
5883 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5886 PPOpts.UsePredefines = Record[Idx++];
5887 PPOpts.DetailedRecord = Record[Idx++];
5888 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5889 PPOpts.ObjCXXARCStandardLibrary =
5890 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5891 SuggestedPredefines.clear();
5892 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5893 SuggestedPredefines);
5896 std::pair<ModuleFile *, unsigned>
5897 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5898 GlobalPreprocessedEntityMapType::iterator
5899 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5900 assert(I != GlobalPreprocessedEntityMap.end() &&
5901 "Corrupted global preprocessed entity map");
5902 ModuleFile *M = I->second;
5903 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5904 return std::make_pair(M, LocalIndex);
5907 llvm::iterator_range<PreprocessingRecord::iterator>
5908 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5909 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5910 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5911 Mod.NumPreprocessedEntities);
5913 return llvm::make_range(PreprocessingRecord::iterator(),
5914 PreprocessingRecord::iterator());
5917 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5918 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5919 return llvm::make_range(
5920 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5921 ModuleDeclIterator(this, &Mod,
5922 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5925 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5926 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5927 assert(I != GlobalSkippedRangeMap.end() &&
5928 "Corrupted global skipped range map");
5929 ModuleFile *M = I->second;
5930 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5931 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5932 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5933 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5934 TranslateSourceLocation(*M, RawRange.getEnd()));
5935 assert(Range.isValid());
5939 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5940 PreprocessedEntityID PPID = Index+1;
5941 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5942 ModuleFile &M = *PPInfo.first;
5943 unsigned LocalIndex = PPInfo.second;
5944 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5946 if (!PP.getPreprocessingRecord()) {
5947 Error("no preprocessing record");
5951 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5952 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5953 M.MacroOffsetsBase + PPOffs.BitOffset)) {
5954 Error(std::move(Err));
5958 Expected<llvm::BitstreamEntry> MaybeEntry =
5959 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5961 Error(MaybeEntry.takeError());
5964 llvm::BitstreamEntry Entry = MaybeEntry.get();
5966 if (Entry.Kind != llvm::BitstreamEntry::Record)
5970 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5971 TranslateSourceLocation(M, PPOffs.getEnd()));
5972 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5975 Expected<unsigned> MaybeRecType =
5976 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5977 if (!MaybeRecType) {
5978 Error(MaybeRecType.takeError());
5981 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5982 case PPD_MACRO_EXPANSION: {
5983 bool isBuiltin = Record[0];
5984 IdentifierInfo *Name = nullptr;
5985 MacroDefinitionRecord *Def = nullptr;
5987 Name = getLocalIdentifier(M, Record[1]);
5989 PreprocessedEntityID GlobalID =
5990 getGlobalPreprocessedEntityID(M, Record[1]);
5991 Def = cast<MacroDefinitionRecord>(
5992 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5997 ME = new (PPRec) MacroExpansion(Name, Range);
5999 ME = new (PPRec) MacroExpansion(Def, Range);
6004 case PPD_MACRO_DEFINITION: {
6005 // Decode the identifier info and then check again; if the macro is
6006 // still defined and associated with the identifier,
6007 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6008 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6010 if (DeserializationListener)
6011 DeserializationListener->MacroDefinitionRead(PPID, MD);
6016 case PPD_INCLUSION_DIRECTIVE: {
6017 const char *FullFileNameStart = Blob.data() + Record[0];
6018 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6019 const FileEntry *File = nullptr;
6020 if (!FullFileName.empty())
6021 if (auto FE = PP.getFileManager().getFile(FullFileName))
6024 // FIXME: Stable encoding
6025 InclusionDirective::InclusionKind Kind
6026 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6027 InclusionDirective *ID
6028 = new (PPRec) InclusionDirective(PPRec, Kind,
6029 StringRef(Blob.data(), Record[0]),
6030 Record[1], Record[3],
6037 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6040 /// Find the next module that contains entities and return the ID
6041 /// of the first entry.
6043 /// \param SLocMapI points at a chunk of a module that contains no
6044 /// preprocessed entities or the entities it contains are not the ones we are
6046 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6047 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6049 for (GlobalSLocOffsetMapType::const_iterator
6050 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6051 ModuleFile &M = *SLocMapI->second;
6052 if (M.NumPreprocessedEntities)
6053 return M.BasePreprocessedEntityID;
6056 return getTotalNumPreprocessedEntities();
6061 struct PPEntityComp {
6062 const ASTReader &Reader;
6065 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6067 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6068 SourceLocation LHS = getLoc(L);
6069 SourceLocation RHS = getLoc(R);
6070 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6073 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6074 SourceLocation LHS = getLoc(L);
6075 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6078 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6079 SourceLocation RHS = getLoc(R);
6080 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6083 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6084 return Reader.TranslateSourceLocation(M, PPE.getBegin());
6090 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6091 bool EndsAfter) const {
6092 if (SourceMgr.isLocalSourceLocation(Loc))
6093 return getTotalNumPreprocessedEntities();
6095 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6096 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6097 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6098 "Corrupted global sloc offset map");
6100 if (SLocMapI->second->NumPreprocessedEntities == 0)
6101 return findNextPreprocessedEntity(SLocMapI);
6103 ModuleFile &M = *SLocMapI->second;
6105 using pp_iterator = const PPEntityOffset *;
6107 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6108 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6110 size_t Count = M.NumPreprocessedEntities;
6112 pp_iterator First = pp_begin;
6116 PPI = std::upper_bound(pp_begin, pp_end, Loc,
6117 PPEntityComp(*this, M));
6119 // Do a binary search manually instead of using std::lower_bound because
6120 // The end locations of entities may be unordered (when a macro expansion
6121 // is inside another macro argument), but for this case it is not important
6122 // whether we get the first macro expansion or its containing macro.
6126 std::advance(PPI, Half);
6127 if (SourceMgr.isBeforeInTranslationUnit(
6128 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6131 Count = Count - Half - 1;
6138 return findNextPreprocessedEntity(SLocMapI);
6140 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6143 /// Returns a pair of [Begin, End) indices of preallocated
6144 /// preprocessed entities that \arg Range encompasses.
6145 std::pair<unsigned, unsigned>
6146 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6147 if (Range.isInvalid())
6148 return std::make_pair(0,0);
6149 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6151 PreprocessedEntityID BeginID =
6152 findPreprocessedEntity(Range.getBegin(), false);
6153 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6154 return std::make_pair(BeginID, EndID);
6157 /// Optionally returns true or false if the preallocated preprocessed
6158 /// entity with index \arg Index came from file \arg FID.
6159 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6161 if (FID.isInvalid())
6164 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6165 ModuleFile &M = *PPInfo.first;
6166 unsigned LocalIndex = PPInfo.second;
6167 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6169 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6170 if (Loc.isInvalid())
6173 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6181 /// Visitor used to search for information about a header file.
6182 class HeaderFileInfoVisitor {
6183 const FileEntry *FE;
6184 Optional<HeaderFileInfo> HFI;
6187 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6189 bool operator()(ModuleFile &M) {
6190 HeaderFileInfoLookupTable *Table
6191 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6195 // Look in the on-disk hash table for an entry for this file name.
6196 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6197 if (Pos == Table->end())
6204 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6209 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6210 HeaderFileInfoVisitor Visitor(FE);
6211 ModuleMgr.visit(Visitor);
6212 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6215 return HeaderFileInfo();
6218 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6219 using DiagState = DiagnosticsEngine::DiagState;
6220 SmallVector<DiagState *, 32> DiagStates;
6222 for (ModuleFile &F : ModuleMgr) {
6224 auto &Record = F.PragmaDiagMappings;
6230 auto ReadDiagState =
6231 [&](const DiagState &BasedOn, SourceLocation Loc,
6232 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6233 unsigned BackrefID = Record[Idx++];
6235 return DiagStates[BackrefID - 1];
6237 // A new DiagState was created here.
6238 Diag.DiagStates.push_back(BasedOn);
6239 DiagState *NewState = &Diag.DiagStates.back();
6240 DiagStates.push_back(NewState);
6241 unsigned Size = Record[Idx++];
6242 assert(Idx + Size * 2 <= Record.size() &&
6243 "Invalid data, not enough diag/map pairs");
6245 unsigned DiagID = Record[Idx++];
6246 DiagnosticMapping NewMapping =
6247 DiagnosticMapping::deserialize(Record[Idx++]);
6248 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6251 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6253 // If this mapping was specified as a warning but the severity was
6254 // upgraded due to diagnostic settings, simulate the current diagnostic
6255 // settings (and use a warning).
6256 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6257 NewMapping.setSeverity(diag::Severity::Warning);
6258 NewMapping.setUpgradedFromWarning(false);
6261 Mapping = NewMapping;
6266 // Read the first state.
6267 DiagState *FirstState;
6268 if (F.Kind == MK_ImplicitModule) {
6269 // Implicitly-built modules are reused with different diagnostic
6270 // settings. Use the initial diagnostic state from Diag to simulate this
6271 // compilation's diagnostic settings.
6272 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6273 DiagStates.push_back(FirstState);
6275 // Skip the initial diagnostic state from the serialized module.
6276 assert(Record[1] == 0 &&
6277 "Invalid data, unexpected backref in initial state");
6278 Idx = 3 + Record[2] * 2;
6279 assert(Idx < Record.size() &&
6280 "Invalid data, not enough state change pairs in initial state");
6281 } else if (F.isModule()) {
6282 // For an explicit module, preserve the flags from the module build
6283 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6285 unsigned Flags = Record[Idx++];
6287 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6288 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6289 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6290 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6291 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6292 Initial.ExtBehavior = (diag::Severity)Flags;
6293 FirstState = ReadDiagState(Initial, SourceLocation(), true);
6295 assert(F.OriginalSourceFileID.isValid());
6297 // Set up the root buffer of the module to start with the initial
6298 // diagnostic state of the module itself, to cover files that contain no
6299 // explicit transitions (for which we did not serialize anything).
6300 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6301 .StateTransitions.push_back({FirstState, 0});
6303 // For prefix ASTs, start with whatever the user configured on the
6305 Idx++; // Skip flags.
6306 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6307 SourceLocation(), false);
6310 // Read the state transitions.
6311 unsigned NumLocations = Record[Idx++];
6312 while (NumLocations--) {
6313 assert(Idx < Record.size() &&
6314 "Invalid data, missing pragma diagnostic states");
6315 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6316 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6317 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6318 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6319 unsigned Transitions = Record[Idx++];
6321 // Note that we don't need to set up Parent/ParentOffset here, because
6322 // we won't be changing the diagnostic state within imported FileIDs
6323 // (other than perhaps appending to the main source file, which has no
6325 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6326 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6327 for (unsigned I = 0; I != Transitions; ++I) {
6328 unsigned Offset = Record[Idx++];
6330 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6331 F.StateTransitions.push_back({State, Offset});
6335 // Read the final state.
6336 assert(Idx < Record.size() &&
6337 "Invalid data, missing final pragma diagnostic state");
6338 SourceLocation CurStateLoc =
6339 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6340 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6342 if (!F.isModule()) {
6343 Diag.DiagStatesByLoc.CurDiagState = CurState;
6344 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6346 // Preserve the property that the imaginary root file describes the
6349 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6351 T.push_back({CurState, 0});
6353 T[0].State = CurState;
6356 // Don't try to read these mappings again.
6361 /// Get the correct cursor and offset for loading a type.
6362 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6363 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6364 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6365 ModuleFile *M = I->second;
6366 return RecordLocation(
6367 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6368 M->DeclsBlockStartOffset);
6371 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6373 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6374 case TYPE_##CODE_ID: return Type::CLASS_ID;
6375 #include "clang/Serialization/TypeBitCodes.def"
6376 default: return llvm::None;
6380 /// Read and return the type with the given index..
6382 /// The index is the type ID, shifted and minus the number of predefs. This
6383 /// routine actually reads the record corresponding to the type at the given
6384 /// location. It is a helper routine for GetType, which deals with reading type
6386 QualType ASTReader::readTypeRecord(unsigned Index) {
6387 assert(ContextObj && "reading type with no AST context");
6388 ASTContext &Context = *ContextObj;
6389 RecordLocation Loc = TypeCursorForIndex(Index);
6390 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6392 // Keep track of where we are in the stream, then jump back there
6393 // after reading this type.
6394 SavedStreamPosition SavedPosition(DeclsCursor);
6396 ReadingKindTracker ReadingKind(Read_Type, *this);
6398 // Note that we are loading a type record.
6399 Deserializing AType(this);
6401 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6402 Error(std::move(Err));
6405 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6407 Error(RawCode.takeError());
6411 ASTRecordReader Record(*this, *Loc.F);
6412 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6414 Error(Code.takeError());
6417 if (Code.get() == TYPE_EXT_QUAL) {
6418 QualType baseType = Record.readQualType();
6419 Qualifiers quals = Record.readQualifiers();
6420 return Context.getQualifiedType(baseType, quals);
6423 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6425 Error("Unexpected code for type");
6429 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6430 return TypeReader.read(*maybeClass);
6435 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6436 ASTRecordReader &Reader;
6438 SourceLocation readSourceLocation() {
6439 return Reader.readSourceLocation();
6442 TypeSourceInfo *GetTypeSourceInfo() {
6443 return Reader.readTypeSourceInfo();
6446 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6447 return Reader.readNestedNameSpecifierLoc();
6451 return Reader.readAttr();
6455 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6457 // We want compile-time assurance that we've enumerated all of
6458 // these, so unfortunately we have to declare them first, then
6459 // define them out-of-line.
6460 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6461 #define TYPELOC(CLASS, PARENT) \
6462 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6463 #include "clang/AST/TypeLocNodes.def"
6465 void VisitFunctionTypeLoc(FunctionTypeLoc);
6466 void VisitArrayTypeLoc(ArrayTypeLoc);
6469 } // namespace clang
6471 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6475 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6476 TL.setBuiltinLoc(readSourceLocation());
6477 if (TL.needsExtraLocalData()) {
6478 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6479 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6480 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6481 TL.setModeAttr(Reader.readInt());
6485 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6486 TL.setNameLoc(readSourceLocation());
6489 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6490 TL.setStarLoc(readSourceLocation());
6493 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6497 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6501 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6502 TL.setExpansionLoc(readSourceLocation());
6505 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6506 TL.setCaretLoc(readSourceLocation());
6509 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6510 TL.setAmpLoc(readSourceLocation());
6513 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6514 TL.setAmpAmpLoc(readSourceLocation());
6517 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6518 TL.setStarLoc(readSourceLocation());
6519 TL.setClassTInfo(GetTypeSourceInfo());
6522 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6523 TL.setLBracketLoc(readSourceLocation());
6524 TL.setRBracketLoc(readSourceLocation());
6525 if (Reader.readBool())
6526 TL.setSizeExpr(Reader.readExpr());
6528 TL.setSizeExpr(nullptr);
6531 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6532 VisitArrayTypeLoc(TL);
6535 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6536 VisitArrayTypeLoc(TL);
6539 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6540 VisitArrayTypeLoc(TL);
6543 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6544 DependentSizedArrayTypeLoc TL) {
6545 VisitArrayTypeLoc(TL);
6548 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6549 DependentAddressSpaceTypeLoc TL) {
6551 TL.setAttrNameLoc(readSourceLocation());
6552 TL.setAttrOperandParensRange(Reader.readSourceRange());
6553 TL.setAttrExprOperand(Reader.readExpr());
6556 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6557 DependentSizedExtVectorTypeLoc TL) {
6558 TL.setNameLoc(readSourceLocation());
6561 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6562 TL.setNameLoc(readSourceLocation());
6565 void TypeLocReader::VisitDependentVectorTypeLoc(
6566 DependentVectorTypeLoc TL) {
6567 TL.setNameLoc(readSourceLocation());
6570 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6571 TL.setNameLoc(readSourceLocation());
6574 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6575 TL.setAttrNameLoc(readSourceLocation());
6576 TL.setAttrOperandParensRange(Reader.readSourceRange());
6577 TL.setAttrRowOperand(Reader.readExpr());
6578 TL.setAttrColumnOperand(Reader.readExpr());
6581 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6582 DependentSizedMatrixTypeLoc TL) {
6583 TL.setAttrNameLoc(readSourceLocation());
6584 TL.setAttrOperandParensRange(Reader.readSourceRange());
6585 TL.setAttrRowOperand(Reader.readExpr());
6586 TL.setAttrColumnOperand(Reader.readExpr());
6589 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6590 TL.setLocalRangeBegin(readSourceLocation());
6591 TL.setLParenLoc(readSourceLocation());
6592 TL.setRParenLoc(readSourceLocation());
6593 TL.setExceptionSpecRange(Reader.readSourceRange());
6594 TL.setLocalRangeEnd(readSourceLocation());
6595 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6596 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6600 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6601 VisitFunctionTypeLoc(TL);
6604 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6605 VisitFunctionTypeLoc(TL);
6608 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6609 TL.setNameLoc(readSourceLocation());
6612 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6613 TL.setNameLoc(readSourceLocation());
6616 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6617 TL.setTypeofLoc(readSourceLocation());
6618 TL.setLParenLoc(readSourceLocation());
6619 TL.setRParenLoc(readSourceLocation());
6622 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6623 TL.setTypeofLoc(readSourceLocation());
6624 TL.setLParenLoc(readSourceLocation());
6625 TL.setRParenLoc(readSourceLocation());
6626 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6629 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6630 TL.setNameLoc(readSourceLocation());
6633 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6634 TL.setKWLoc(readSourceLocation());
6635 TL.setLParenLoc(readSourceLocation());
6636 TL.setRParenLoc(readSourceLocation());
6637 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6640 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6641 TL.setNameLoc(readSourceLocation());
6642 if (Reader.readBool()) {
6643 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6644 TL.setTemplateKWLoc(readSourceLocation());
6645 TL.setConceptNameLoc(readSourceLocation());
6646 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6647 TL.setLAngleLoc(readSourceLocation());
6648 TL.setRAngleLoc(readSourceLocation());
6649 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6650 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6651 TL.getTypePtr()->getArg(i).getKind()));
6655 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6656 DeducedTemplateSpecializationTypeLoc TL) {
6657 TL.setTemplateNameLoc(readSourceLocation());
6660 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6661 TL.setNameLoc(readSourceLocation());
6664 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6665 TL.setNameLoc(readSourceLocation());
6668 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6669 TL.setAttr(ReadAttr());
6672 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6673 TL.setNameLoc(readSourceLocation());
6676 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6677 SubstTemplateTypeParmTypeLoc TL) {
6678 TL.setNameLoc(readSourceLocation());
6681 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6682 SubstTemplateTypeParmPackTypeLoc TL) {
6683 TL.setNameLoc(readSourceLocation());
6686 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6687 TemplateSpecializationTypeLoc TL) {
6688 TL.setTemplateKeywordLoc(readSourceLocation());
6689 TL.setTemplateNameLoc(readSourceLocation());
6690 TL.setLAngleLoc(readSourceLocation());
6691 TL.setRAngleLoc(readSourceLocation());
6692 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6695 Reader.readTemplateArgumentLocInfo(
6696 TL.getTypePtr()->getArg(i).getKind()));
6699 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6700 TL.setLParenLoc(readSourceLocation());
6701 TL.setRParenLoc(readSourceLocation());
6704 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6705 TL.setElaboratedKeywordLoc(readSourceLocation());
6706 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6709 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6710 TL.setNameLoc(readSourceLocation());
6713 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6714 TL.setElaboratedKeywordLoc(readSourceLocation());
6715 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6716 TL.setNameLoc(readSourceLocation());
6719 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6720 DependentTemplateSpecializationTypeLoc TL) {
6721 TL.setElaboratedKeywordLoc(readSourceLocation());
6722 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6723 TL.setTemplateKeywordLoc(readSourceLocation());
6724 TL.setTemplateNameLoc(readSourceLocation());
6725 TL.setLAngleLoc(readSourceLocation());
6726 TL.setRAngleLoc(readSourceLocation());
6727 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6730 Reader.readTemplateArgumentLocInfo(
6731 TL.getTypePtr()->getArg(I).getKind()));
6734 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6735 TL.setEllipsisLoc(readSourceLocation());
6738 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6739 TL.setNameLoc(readSourceLocation());
6742 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6743 if (TL.getNumProtocols()) {
6744 TL.setProtocolLAngleLoc(readSourceLocation());
6745 TL.setProtocolRAngleLoc(readSourceLocation());
6747 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6748 TL.setProtocolLoc(i, readSourceLocation());
6751 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6752 TL.setHasBaseTypeAsWritten(Reader.readBool());
6753 TL.setTypeArgsLAngleLoc(readSourceLocation());
6754 TL.setTypeArgsRAngleLoc(readSourceLocation());
6755 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6756 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6757 TL.setProtocolLAngleLoc(readSourceLocation());
6758 TL.setProtocolRAngleLoc(readSourceLocation());
6759 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6760 TL.setProtocolLoc(i, readSourceLocation());
6763 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6764 TL.setStarLoc(readSourceLocation());
6767 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6768 TL.setKWLoc(readSourceLocation());
6769 TL.setLParenLoc(readSourceLocation());
6770 TL.setRParenLoc(readSourceLocation());
6773 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6774 TL.setKWLoc(readSourceLocation());
6777 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6778 TL.setNameLoc(readSourceLocation());
6780 void TypeLocReader::VisitDependentExtIntTypeLoc(
6781 clang::DependentExtIntTypeLoc TL) {
6782 TL.setNameLoc(readSourceLocation());
6786 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6787 TypeLocReader TLR(*this);
6788 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6792 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6793 QualType InfoTy = readType();
6794 if (InfoTy.isNull())
6797 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6798 readTypeLoc(TInfo->getTypeLoc());
6802 QualType ASTReader::GetType(TypeID ID) {
6803 assert(ContextObj && "reading type with no AST context");
6804 ASTContext &Context = *ContextObj;
6806 unsigned FastQuals = ID & Qualifiers::FastMask;
6807 unsigned Index = ID >> Qualifiers::FastWidth;
6809 if (Index < NUM_PREDEF_TYPE_IDS) {
6811 switch ((PredefinedTypeIDs)Index) {
6812 case PREDEF_TYPE_NULL_ID:
6814 case PREDEF_TYPE_VOID_ID:
6817 case PREDEF_TYPE_BOOL_ID:
6820 case PREDEF_TYPE_CHAR_U_ID:
6821 case PREDEF_TYPE_CHAR_S_ID:
6822 // FIXME: Check that the signedness of CharTy is correct!
6825 case PREDEF_TYPE_UCHAR_ID:
6826 T = Context.UnsignedCharTy;
6828 case PREDEF_TYPE_USHORT_ID:
6829 T = Context.UnsignedShortTy;
6831 case PREDEF_TYPE_UINT_ID:
6832 T = Context.UnsignedIntTy;
6834 case PREDEF_TYPE_ULONG_ID:
6835 T = Context.UnsignedLongTy;
6837 case PREDEF_TYPE_ULONGLONG_ID:
6838 T = Context.UnsignedLongLongTy;
6840 case PREDEF_TYPE_UINT128_ID:
6841 T = Context.UnsignedInt128Ty;
6843 case PREDEF_TYPE_SCHAR_ID:
6844 T = Context.SignedCharTy;
6846 case PREDEF_TYPE_WCHAR_ID:
6847 T = Context.WCharTy;
6849 case PREDEF_TYPE_SHORT_ID:
6850 T = Context.ShortTy;
6852 case PREDEF_TYPE_INT_ID:
6855 case PREDEF_TYPE_LONG_ID:
6858 case PREDEF_TYPE_LONGLONG_ID:
6859 T = Context.LongLongTy;
6861 case PREDEF_TYPE_INT128_ID:
6862 T = Context.Int128Ty;
6864 case PREDEF_TYPE_BFLOAT16_ID:
6865 T = Context.BFloat16Ty;
6867 case PREDEF_TYPE_HALF_ID:
6870 case PREDEF_TYPE_FLOAT_ID:
6871 T = Context.FloatTy;
6873 case PREDEF_TYPE_DOUBLE_ID:
6874 T = Context.DoubleTy;
6876 case PREDEF_TYPE_LONGDOUBLE_ID:
6877 T = Context.LongDoubleTy;
6879 case PREDEF_TYPE_SHORT_ACCUM_ID:
6880 T = Context.ShortAccumTy;
6882 case PREDEF_TYPE_ACCUM_ID:
6883 T = Context.AccumTy;
6885 case PREDEF_TYPE_LONG_ACCUM_ID:
6886 T = Context.LongAccumTy;
6888 case PREDEF_TYPE_USHORT_ACCUM_ID:
6889 T = Context.UnsignedShortAccumTy;
6891 case PREDEF_TYPE_UACCUM_ID:
6892 T = Context.UnsignedAccumTy;
6894 case PREDEF_TYPE_ULONG_ACCUM_ID:
6895 T = Context.UnsignedLongAccumTy;
6897 case PREDEF_TYPE_SHORT_FRACT_ID:
6898 T = Context.ShortFractTy;
6900 case PREDEF_TYPE_FRACT_ID:
6901 T = Context.FractTy;
6903 case PREDEF_TYPE_LONG_FRACT_ID:
6904 T = Context.LongFractTy;
6906 case PREDEF_TYPE_USHORT_FRACT_ID:
6907 T = Context.UnsignedShortFractTy;
6909 case PREDEF_TYPE_UFRACT_ID:
6910 T = Context.UnsignedFractTy;
6912 case PREDEF_TYPE_ULONG_FRACT_ID:
6913 T = Context.UnsignedLongFractTy;
6915 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6916 T = Context.SatShortAccumTy;
6918 case PREDEF_TYPE_SAT_ACCUM_ID:
6919 T = Context.SatAccumTy;
6921 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6922 T = Context.SatLongAccumTy;
6924 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6925 T = Context.SatUnsignedShortAccumTy;
6927 case PREDEF_TYPE_SAT_UACCUM_ID:
6928 T = Context.SatUnsignedAccumTy;
6930 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6931 T = Context.SatUnsignedLongAccumTy;
6933 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6934 T = Context.SatShortFractTy;
6936 case PREDEF_TYPE_SAT_FRACT_ID:
6937 T = Context.SatFractTy;
6939 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6940 T = Context.SatLongFractTy;
6942 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6943 T = Context.SatUnsignedShortFractTy;
6945 case PREDEF_TYPE_SAT_UFRACT_ID:
6946 T = Context.SatUnsignedFractTy;
6948 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6949 T = Context.SatUnsignedLongFractTy;
6951 case PREDEF_TYPE_FLOAT16_ID:
6952 T = Context.Float16Ty;
6954 case PREDEF_TYPE_FLOAT128_ID:
6955 T = Context.Float128Ty;
6957 case PREDEF_TYPE_OVERLOAD_ID:
6958 T = Context.OverloadTy;
6960 case PREDEF_TYPE_BOUND_MEMBER:
6961 T = Context.BoundMemberTy;
6963 case PREDEF_TYPE_PSEUDO_OBJECT:
6964 T = Context.PseudoObjectTy;
6966 case PREDEF_TYPE_DEPENDENT_ID:
6967 T = Context.DependentTy;
6969 case PREDEF_TYPE_UNKNOWN_ANY:
6970 T = Context.UnknownAnyTy;
6972 case PREDEF_TYPE_NULLPTR_ID:
6973 T = Context.NullPtrTy;
6975 case PREDEF_TYPE_CHAR8_ID:
6976 T = Context.Char8Ty;
6978 case PREDEF_TYPE_CHAR16_ID:
6979 T = Context.Char16Ty;
6981 case PREDEF_TYPE_CHAR32_ID:
6982 T = Context.Char32Ty;
6984 case PREDEF_TYPE_OBJC_ID:
6985 T = Context.ObjCBuiltinIdTy;
6987 case PREDEF_TYPE_OBJC_CLASS:
6988 T = Context.ObjCBuiltinClassTy;
6990 case PREDEF_TYPE_OBJC_SEL:
6991 T = Context.ObjCBuiltinSelTy;
6993 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6994 case PREDEF_TYPE_##Id##_ID: \
6995 T = Context.SingletonId; \
6997 #include "clang/Basic/OpenCLImageTypes.def"
6998 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6999 case PREDEF_TYPE_##Id##_ID: \
7000 T = Context.Id##Ty; \
7002 #include "clang/Basic/OpenCLExtensionTypes.def"
7003 case PREDEF_TYPE_SAMPLER_ID:
7004 T = Context.OCLSamplerTy;
7006 case PREDEF_TYPE_EVENT_ID:
7007 T = Context.OCLEventTy;
7009 case PREDEF_TYPE_CLK_EVENT_ID:
7010 T = Context.OCLClkEventTy;
7012 case PREDEF_TYPE_QUEUE_ID:
7013 T = Context.OCLQueueTy;
7015 case PREDEF_TYPE_RESERVE_ID_ID:
7016 T = Context.OCLReserveIDTy;
7018 case PREDEF_TYPE_AUTO_DEDUCT:
7019 T = Context.getAutoDeductType();
7021 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7022 T = Context.getAutoRRefDeductType();
7024 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7025 T = Context.ARCUnbridgedCastTy;
7027 case PREDEF_TYPE_BUILTIN_FN:
7028 T = Context.BuiltinFnTy;
7030 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7031 T = Context.IncompleteMatrixIdxTy;
7033 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7034 T = Context.OMPArraySectionTy;
7036 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7037 T = Context.OMPArraySectionTy;
7039 case PREDEF_TYPE_OMP_ITERATOR:
7040 T = Context.OMPIteratorTy;
7042 #define SVE_TYPE(Name, Id, SingletonId) \
7043 case PREDEF_TYPE_##Id##_ID: \
7044 T = Context.SingletonId; \
7046 #include "clang/Basic/AArch64SVEACLETypes.def"
7049 assert(!T.isNull() && "Unknown predefined type");
7050 return T.withFastQualifiers(FastQuals);
7053 Index -= NUM_PREDEF_TYPE_IDS;
7054 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7055 if (TypesLoaded[Index].isNull()) {
7056 TypesLoaded[Index] = readTypeRecord(Index);
7057 if (TypesLoaded[Index].isNull())
7060 TypesLoaded[Index]->setFromAST();
7061 if (DeserializationListener)
7062 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7063 TypesLoaded[Index]);
7066 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7069 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7070 return GetType(getGlobalTypeID(F, LocalID));
7073 serialization::TypeID
7074 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7075 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7076 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7078 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7081 if (!F.ModuleOffsetMap.empty())
7082 ReadModuleOffsetMap(F);
7084 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7085 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7086 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7088 unsigned GlobalIndex = LocalIndex + I->second;
7089 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7092 TemplateArgumentLocInfo
7093 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7095 case TemplateArgument::Expression:
7097 case TemplateArgument::Type:
7098 return readTypeSourceInfo();
7099 case TemplateArgument::Template: {
7100 NestedNameSpecifierLoc QualifierLoc =
7101 readNestedNameSpecifierLoc();
7102 SourceLocation TemplateNameLoc = readSourceLocation();
7103 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7106 case TemplateArgument::TemplateExpansion: {
7107 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7108 SourceLocation TemplateNameLoc = readSourceLocation();
7109 SourceLocation EllipsisLoc = readSourceLocation();
7110 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7113 case TemplateArgument::Null:
7114 case TemplateArgument::Integral:
7115 case TemplateArgument::Declaration:
7116 case TemplateArgument::NullPtr:
7117 case TemplateArgument::Pack:
7118 // FIXME: Is this right?
7119 return TemplateArgumentLocInfo();
7121 llvm_unreachable("unexpected template argument loc");
7124 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7125 TemplateArgument Arg = readTemplateArgument();
7127 if (Arg.getKind() == TemplateArgument::Expression) {
7128 if (readBool()) // bool InfoHasSameExpr.
7129 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7131 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7134 const ASTTemplateArgumentListInfo *
7135 ASTRecordReader::readASTTemplateArgumentListInfo() {
7136 SourceLocation LAngleLoc = readSourceLocation();
7137 SourceLocation RAngleLoc = readSourceLocation();
7138 unsigned NumArgsAsWritten = readInt();
7139 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7140 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7141 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7142 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7145 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7149 void ASTReader::CompleteRedeclChain(const Decl *D) {
7150 if (NumCurrentElementsDeserializing) {
7151 // We arrange to not care about the complete redeclaration chain while we're
7152 // deserializing. Just remember that the AST has marked this one as complete
7153 // but that it's not actually complete yet, so we know we still need to
7154 // complete it later.
7155 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7159 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7161 // If this is a named declaration, complete it by looking it up
7162 // within its context.
7164 // FIXME: Merging a function definition should merge
7165 // all mergeable entities within it.
7166 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7167 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7168 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7169 if (!getContext().getLangOpts().CPlusPlus &&
7170 isa<TranslationUnitDecl>(DC)) {
7171 // Outside of C++, we don't have a lookup table for the TU, so update
7172 // the identifier instead. (For C++ modules, we don't store decls
7173 // in the serialized identifier table, so we do the lookup in the TU.)
7174 auto *II = Name.getAsIdentifierInfo();
7175 assert(II && "non-identifier name in C?");
7176 if (II->isOutOfDate())
7177 updateOutOfDateIdentifier(*II);
7180 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7181 // Find all declarations of this kind from the relevant context.
7182 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7183 auto *DC = cast<DeclContext>(DCDecl);
7184 SmallVector<Decl*, 8> Decls;
7185 FindExternalLexicalDecls(
7186 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7191 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7192 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7193 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7194 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7195 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7196 if (auto *Template = FD->getPrimaryTemplate())
7197 Template->LoadLazySpecializations();
7201 CXXCtorInitializer **
7202 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7203 RecordLocation Loc = getLocalBitOffset(Offset);
7204 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7205 SavedStreamPosition SavedPosition(Cursor);
7206 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7207 Error(std::move(Err));
7210 ReadingKindTracker ReadingKind(Read_Decl, *this);
7212 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7214 Error(MaybeCode.takeError());
7217 unsigned Code = MaybeCode.get();
7219 ASTRecordReader Record(*this, *Loc.F);
7220 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7221 if (!MaybeRecCode) {
7222 Error(MaybeRecCode.takeError());
7225 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7226 Error("malformed AST file: missing C++ ctor initializers");
7230 return Record.readCXXCtorInitializers();
7233 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7234 assert(ContextObj && "reading base specifiers with no AST context");
7235 ASTContext &Context = *ContextObj;
7237 RecordLocation Loc = getLocalBitOffset(Offset);
7238 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7239 SavedStreamPosition SavedPosition(Cursor);
7240 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7241 Error(std::move(Err));
7244 ReadingKindTracker ReadingKind(Read_Decl, *this);
7246 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7248 Error(MaybeCode.takeError());
7251 unsigned Code = MaybeCode.get();
7253 ASTRecordReader Record(*this, *Loc.F);
7254 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7255 if (!MaybeRecCode) {
7256 Error(MaybeCode.takeError());
7259 unsigned RecCode = MaybeRecCode.get();
7261 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7262 Error("malformed AST file: missing C++ base specifiers");
7266 unsigned NumBases = Record.readInt();
7267 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7268 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7269 for (unsigned I = 0; I != NumBases; ++I)
7270 Bases[I] = Record.readCXXBaseSpecifier();
7274 serialization::DeclID
7275 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7276 if (LocalID < NUM_PREDEF_DECL_IDS)
7279 if (!F.ModuleOffsetMap.empty())
7280 ReadModuleOffsetMap(F);
7282 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7283 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7284 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7286 return LocalID + I->second;
7289 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7290 ModuleFile &M) const {
7291 // Predefined decls aren't from any module.
7292 if (ID < NUM_PREDEF_DECL_IDS)
7295 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7296 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7299 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7300 if (!D->isFromASTFile())
7302 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7303 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7307 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7308 if (ID < NUM_PREDEF_DECL_IDS)
7309 return SourceLocation();
7311 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7313 if (Index > DeclsLoaded.size()) {
7314 Error("declaration ID out-of-range for AST file");
7315 return SourceLocation();
7318 if (Decl *D = DeclsLoaded[Index])
7319 return D->getLocation();
7322 DeclCursorForID(ID, Loc);
7326 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7328 case PREDEF_DECL_NULL_ID:
7331 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7332 return Context.getTranslationUnitDecl();
7334 case PREDEF_DECL_OBJC_ID_ID:
7335 return Context.getObjCIdDecl();
7337 case PREDEF_DECL_OBJC_SEL_ID:
7338 return Context.getObjCSelDecl();
7340 case PREDEF_DECL_OBJC_CLASS_ID:
7341 return Context.getObjCClassDecl();
7343 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7344 return Context.getObjCProtocolDecl();
7346 case PREDEF_DECL_INT_128_ID:
7347 return Context.getInt128Decl();
7349 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7350 return Context.getUInt128Decl();
7352 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7353 return Context.getObjCInstanceTypeDecl();
7355 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7356 return Context.getBuiltinVaListDecl();
7358 case PREDEF_DECL_VA_LIST_TAG:
7359 return Context.getVaListTagDecl();
7361 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7362 return Context.getBuiltinMSVaListDecl();
7364 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7365 return Context.getMSGuidTagDecl();
7367 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7368 return Context.getExternCContextDecl();
7370 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7371 return Context.getMakeIntegerSeqDecl();
7373 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7374 return Context.getCFConstantStringDecl();
7376 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7377 return Context.getCFConstantStringTagDecl();
7379 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7380 return Context.getTypePackElementDecl();
7382 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7385 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7386 assert(ContextObj && "reading decl with no AST context");
7387 if (ID < NUM_PREDEF_DECL_IDS) {
7388 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7390 // Track that we have merged the declaration with ID \p ID into the
7391 // pre-existing predefined declaration \p D.
7392 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7394 Merged.push_back(ID);
7399 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7401 if (Index >= DeclsLoaded.size()) {
7402 assert(0 && "declaration ID out-of-range for AST file");
7403 Error("declaration ID out-of-range for AST file");
7407 return DeclsLoaded[Index];
7410 Decl *ASTReader::GetDecl(DeclID ID) {
7411 if (ID < NUM_PREDEF_DECL_IDS)
7412 return GetExistingDecl(ID);
7414 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7416 if (Index >= DeclsLoaded.size()) {
7417 assert(0 && "declaration ID out-of-range for AST file");
7418 Error("declaration ID out-of-range for AST file");
7422 if (!DeclsLoaded[Index]) {
7424 if (DeserializationListener)
7425 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7428 return DeclsLoaded[Index];
7431 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7433 if (GlobalID < NUM_PREDEF_DECL_IDS)
7436 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7437 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7438 ModuleFile *Owner = I->second;
7440 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7441 = M.GlobalToLocalDeclIDs.find(Owner);
7442 if (Pos == M.GlobalToLocalDeclIDs.end())
7445 return GlobalID - Owner->BaseDeclID + Pos->second;
7448 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7449 const RecordData &Record,
7451 if (Idx >= Record.size()) {
7452 Error("Corrupted AST file");
7456 return getGlobalDeclID(F, Record[Idx++]);
7459 /// Resolve the offset of a statement into a statement.
7461 /// This operation will read a new statement from the external
7462 /// source each time it is called, and is meant to be used via a
7463 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7464 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7465 // Switch case IDs are per Decl.
7466 ClearSwitchCaseIDs();
7468 // Offset here is a global offset across the entire chain.
7469 RecordLocation Loc = getLocalBitOffset(Offset);
7470 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7471 Error(std::move(Err));
7474 assert(NumCurrentElementsDeserializing == 0 &&
7475 "should not be called while already deserializing");
7476 Deserializing D(this);
7477 return ReadStmtFromStream(*Loc.F);
7480 void ASTReader::FindExternalLexicalDecls(
7481 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7482 SmallVectorImpl<Decl *> &Decls) {
7483 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7485 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7486 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7487 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7488 auto K = (Decl::Kind)+LexicalDecls[I];
7489 if (!IsKindWeWant(K))
7492 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7494 // Don't add predefined declarations to the lexical context more
7496 if (ID < NUM_PREDEF_DECL_IDS) {
7497 if (PredefsVisited[ID])
7500 PredefsVisited[ID] = true;
7503 if (Decl *D = GetLocalDecl(*M, ID)) {
7504 assert(D->getKind() == K && "wrong kind for lexical decl");
7505 if (!DC->isDeclInLexicalTraversal(D))
7511 if (isa<TranslationUnitDecl>(DC)) {
7512 for (auto Lexical : TULexicalDecls)
7513 Visit(Lexical.first, Lexical.second);
7515 auto I = LexicalDecls.find(DC);
7516 if (I != LexicalDecls.end())
7517 Visit(I->second.first, I->second.second);
7520 ++NumLexicalDeclContextsRead;
7530 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7532 bool operator()(LocalDeclID L, LocalDeclID R) const {
7533 SourceLocation LHS = getLocation(L);
7534 SourceLocation RHS = getLocation(R);
7535 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7538 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7539 SourceLocation RHS = getLocation(R);
7540 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7543 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7544 SourceLocation LHS = getLocation(L);
7545 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7548 SourceLocation getLocation(LocalDeclID ID) const {
7549 return Reader.getSourceManager().getFileLoc(
7550 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7556 void ASTReader::FindFileRegionDecls(FileID File,
7557 unsigned Offset, unsigned Length,
7558 SmallVectorImpl<Decl *> &Decls) {
7559 SourceManager &SM = getSourceManager();
7561 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7562 if (I == FileDeclIDs.end())
7565 FileDeclsInfo &DInfo = I->second;
7566 if (DInfo.Decls.empty())
7570 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7571 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7573 DeclIDComp DIDComp(*this, *DInfo.Mod);
7574 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7575 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7576 if (BeginIt != DInfo.Decls.begin())
7579 // If we are pointing at a top-level decl inside an objc container, we need
7580 // to backtrack until we find it otherwise we will fail to report that the
7581 // region overlaps with an objc container.
7582 while (BeginIt != DInfo.Decls.begin() &&
7583 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7584 ->isTopLevelDeclInObjCContainer())
7587 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7588 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7589 if (EndIt != DInfo.Decls.end())
7592 for (ArrayRef<serialization::LocalDeclID>::iterator
7593 DIt = BeginIt; DIt != EndIt; ++DIt)
7594 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7598 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7599 DeclarationName Name) {
7600 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7601 "DeclContext has no visible decls in storage");
7605 auto It = Lookups.find(DC);
7606 if (It == Lookups.end())
7609 Deserializing LookupResults(this);
7611 // Load the list of declarations.
7612 SmallVector<NamedDecl *, 64> Decls;
7613 for (DeclID ID : It->second.Table.find(Name)) {
7614 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7615 if (ND->getDeclName() == Name)
7616 Decls.push_back(ND);
7619 ++NumVisibleDeclContextsRead;
7620 SetExternalVisibleDeclsForName(DC, Name, Decls);
7621 return !Decls.empty();
7624 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7625 if (!DC->hasExternalVisibleStorage())
7628 auto It = Lookups.find(DC);
7629 assert(It != Lookups.end() &&
7630 "have external visible storage but no lookup tables");
7634 for (DeclID ID : It->second.Table.findAll()) {
7635 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7636 Decls[ND->getDeclName()].push_back(ND);
7639 ++NumVisibleDeclContextsRead;
7641 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7642 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7644 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7647 const serialization::reader::DeclContextLookupTable *
7648 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7649 auto I = Lookups.find(Primary);
7650 return I == Lookups.end() ? nullptr : &I->second;
7653 /// Under non-PCH compilation the consumer receives the objc methods
7654 /// before receiving the implementation, and codegen depends on this.
7655 /// We simulate this by deserializing and passing to consumer the methods of the
7656 /// implementation before passing the deserialized implementation decl.
7657 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7658 ASTConsumer *Consumer) {
7659 assert(ImplD && Consumer);
7661 for (auto *I : ImplD->methods())
7662 Consumer->HandleInterestingDecl(DeclGroupRef(I));
7664 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7667 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7668 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7669 PassObjCImplDeclToConsumer(ImplD, Consumer);
7671 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7674 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7675 this->Consumer = Consumer;
7678 PassInterestingDeclsToConsumer();
7680 if (DeserializationListener)
7681 DeserializationListener->ReaderInitialized(this);
7684 void ASTReader::PrintStats() {
7685 std::fprintf(stderr, "*** AST File Statistics:\n");
7687 unsigned NumTypesLoaded
7688 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7690 unsigned NumDeclsLoaded
7691 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7693 unsigned NumIdentifiersLoaded
7694 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7695 IdentifiersLoaded.end(),
7696 (IdentifierInfo *)nullptr);
7697 unsigned NumMacrosLoaded
7698 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7700 (MacroInfo *)nullptr);
7701 unsigned NumSelectorsLoaded
7702 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7703 SelectorsLoaded.end(),
7706 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7707 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7708 NumSLocEntriesRead, TotalNumSLocEntries,
7709 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7710 if (!TypesLoaded.empty())
7711 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7712 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7713 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7714 if (!DeclsLoaded.empty())
7715 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7716 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7717 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7718 if (!IdentifiersLoaded.empty())
7719 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7720 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7721 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7722 if (!MacrosLoaded.empty())
7723 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7724 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7725 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7726 if (!SelectorsLoaded.empty())
7727 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7728 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7729 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7730 if (TotalNumStatements)
7731 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7732 NumStatementsRead, TotalNumStatements,
7733 ((float)NumStatementsRead/TotalNumStatements * 100));
7735 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7736 NumMacrosRead, TotalNumMacros,
7737 ((float)NumMacrosRead/TotalNumMacros * 100));
7738 if (TotalLexicalDeclContexts)
7739 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7740 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7741 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7743 if (TotalVisibleDeclContexts)
7744 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7745 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7746 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7748 if (TotalNumMethodPoolEntries)
7749 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7750 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7751 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7753 if (NumMethodPoolLookups)
7754 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7755 NumMethodPoolHits, NumMethodPoolLookups,
7756 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7757 if (NumMethodPoolTableLookups)
7758 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7759 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7760 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7762 if (NumIdentifierLookupHits)
7763 std::fprintf(stderr,
7764 " %u / %u identifier table lookups succeeded (%f%%)\n",
7765 NumIdentifierLookupHits, NumIdentifierLookups,
7766 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7769 std::fprintf(stderr, "\n");
7770 GlobalIndex->printStats();
7773 std::fprintf(stderr, "\n");
7775 std::fprintf(stderr, "\n");
7778 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7779 LLVM_DUMP_METHOD static void
7780 dumpModuleIDMap(StringRef Name,
7781 const ContinuousRangeMap<Key, ModuleFile *,
7782 InitialCapacity> &Map) {
7783 if (Map.begin() == Map.end())
7786 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7788 llvm::errs() << Name << ":\n";
7789 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7791 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7796 LLVM_DUMP_METHOD void ASTReader::dump() {
7797 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7798 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7799 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7800 dumpModuleIDMap("Global type map", GlobalTypeMap);
7801 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7802 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7803 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7804 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7805 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7806 dumpModuleIDMap("Global preprocessed entity map",
7807 GlobalPreprocessedEntityMap);
7809 llvm::errs() << "\n*** PCH/Modules Loaded:";
7810 for (ModuleFile &M : ModuleMgr)
7814 /// Return the amount of memory used by memory buffers, breaking down
7815 /// by heap-backed versus mmap'ed memory.
7816 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7817 for (ModuleFile &I : ModuleMgr) {
7818 if (llvm::MemoryBuffer *buf = I.Buffer) {
7819 size_t bytes = buf->getBufferSize();
7820 switch (buf->getBufferKind()) {
7821 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7822 sizes.malloc_bytes += bytes;
7824 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7825 sizes.mmap_bytes += bytes;
7832 void ASTReader::InitializeSema(Sema &S) {
7834 S.addExternalSource(this);
7836 // Makes sure any declarations that were deserialized "too early"
7837 // still get added to the identifier's declaration chains.
7838 for (uint64_t ID : PreloadedDeclIDs) {
7839 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7840 pushExternalDeclIntoScope(D, D->getDeclName());
7842 PreloadedDeclIDs.clear();
7844 // FIXME: What happens if these are changed by a module import?
7845 if (!FPPragmaOptions.empty()) {
7846 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7847 FPOptionsOverride NewOverrides(FPPragmaOptions[0]);
7848 SemaObj->CurFPFeatures =
7849 NewOverrides.applyOverrides(SemaObj->getLangOpts());
7852 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7853 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7854 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7859 void ASTReader::UpdateSema() {
7860 assert(SemaObj && "no Sema to update");
7862 // Load the offsets of the declarations that Sema references.
7863 // They will be lazily deserialized when needed.
7864 if (!SemaDeclRefs.empty()) {
7865 assert(SemaDeclRefs.size() % 3 == 0);
7866 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7867 if (!SemaObj->StdNamespace)
7868 SemaObj->StdNamespace = SemaDeclRefs[I];
7869 if (!SemaObj->StdBadAlloc)
7870 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7871 if (!SemaObj->StdAlignValT)
7872 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7874 SemaDeclRefs.clear();
7877 // Update the state of pragmas. Use the same API as if we had encountered the
7878 // pragma in the source.
7879 if(OptimizeOffPragmaLocation.isValid())
7880 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7881 if (PragmaMSStructState != -1)
7882 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7883 if (PointersToMembersPragmaLocation.isValid()) {
7884 SemaObj->ActOnPragmaMSPointersToMembers(
7885 (LangOptions::PragmaMSPointersToMembersKind)
7886 PragmaMSPointersToMembersState,
7887 PointersToMembersPragmaLocation);
7889 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7891 if (PragmaPackCurrentValue) {
7892 // The bottom of the stack might have a default value. It must be adjusted
7893 // to the current value to ensure that the packing state is preserved after
7894 // popping entries that were included/imported from a PCH/module.
7895 bool DropFirst = false;
7896 if (!PragmaPackStack.empty() &&
7897 PragmaPackStack.front().Location.isInvalid()) {
7898 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7899 "Expected a default alignment value");
7900 SemaObj->PackStack.Stack.emplace_back(
7901 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7902 SemaObj->PackStack.CurrentPragmaLocation,
7903 PragmaPackStack.front().PushLocation);
7906 for (const auto &Entry :
7907 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7908 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7909 Entry.Location, Entry.PushLocation);
7910 if (PragmaPackCurrentLocation.isInvalid()) {
7911 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7912 "Expected a default alignment value");
7913 // Keep the current values.
7915 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7916 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7919 if (FpPragmaCurrentValue) {
7920 // The bottom of the stack might have a default value. It must be adjusted
7921 // to the current value to ensure that fp-pragma state is preserved after
7922 // popping entries that were included/imported from a PCH/module.
7923 bool DropFirst = false;
7924 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7925 assert(FpPragmaStack.front().Value ==
7926 SemaObj->FpPragmaStack.DefaultValue &&
7927 "Expected a default pragma float_control value");
7928 SemaObj->FpPragmaStack.Stack.emplace_back(
7929 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7930 SemaObj->FpPragmaStack.CurrentPragmaLocation,
7931 FpPragmaStack.front().PushLocation);
7934 for (const auto &Entry :
7935 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7936 SemaObj->FpPragmaStack.Stack.emplace_back(
7937 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7938 if (FpPragmaCurrentLocation.isInvalid()) {
7939 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7940 "Expected a default pragma float_control value");
7941 // Keep the current values.
7943 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7944 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7949 IdentifierInfo *ASTReader::get(StringRef Name) {
7950 // Note that we are loading an identifier.
7951 Deserializing AnIdentifier(this);
7953 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7954 NumIdentifierLookups,
7955 NumIdentifierLookupHits);
7957 // We don't need to do identifier table lookups in C++ modules (we preload
7958 // all interesting declarations, and don't need to use the scope for name
7959 // lookups). Perform the lookup in PCH files, though, since we don't build
7960 // a complete initial identifier table if we're carrying on from a PCH.
7961 if (PP.getLangOpts().CPlusPlus) {
7962 for (auto F : ModuleMgr.pch_modules())
7966 // If there is a global index, look there first to determine which modules
7967 // provably do not have any results for this identifier.
7968 GlobalModuleIndex::HitSet Hits;
7969 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7970 if (!loadGlobalIndex()) {
7971 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7976 ModuleMgr.visit(Visitor, HitsPtr);
7979 IdentifierInfo *II = Visitor.getIdentifierInfo();
7980 markIdentifierUpToDate(II);
7986 /// An identifier-lookup iterator that enumerates all of the
7987 /// identifiers stored within a set of AST files.
7988 class ASTIdentifierIterator : public IdentifierIterator {
7989 /// The AST reader whose identifiers are being enumerated.
7990 const ASTReader &Reader;
7992 /// The current index into the chain of AST files stored in
7996 /// The current position within the identifier lookup table
7997 /// of the current AST file.
7998 ASTIdentifierLookupTable::key_iterator Current;
8000 /// The end position within the identifier lookup table of
8001 /// the current AST file.
8002 ASTIdentifierLookupTable::key_iterator End;
8004 /// Whether to skip any modules in the ASTReader.
8008 explicit ASTIdentifierIterator(const ASTReader &Reader,
8009 bool SkipModules = false);
8011 StringRef Next() override;
8014 } // namespace clang
8016 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8018 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8021 StringRef ASTIdentifierIterator::Next() {
8022 while (Current == End) {
8023 // If we have exhausted all of our AST files, we're done.
8028 ModuleFile &F = Reader.ModuleMgr[Index];
8029 if (SkipModules && F.isModule())
8032 ASTIdentifierLookupTable *IdTable =
8033 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8034 Current = IdTable->key_begin();
8035 End = IdTable->key_end();
8038 // We have any identifiers remaining in the current AST file; return
8040 StringRef Result = *Current;
8047 /// A utility for appending two IdentifierIterators.
8048 class ChainedIdentifierIterator : public IdentifierIterator {
8049 std::unique_ptr<IdentifierIterator> Current;
8050 std::unique_ptr<IdentifierIterator> Queued;
8053 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8054 std::unique_ptr<IdentifierIterator> Second)
8055 : Current(std::move(First)), Queued(std::move(Second)) {}
8057 StringRef Next() override {
8061 StringRef result = Current->Next();
8062 if (!result.empty())
8065 // Try the queued iterator, which may itself be empty.
8067 std::swap(Current, Queued);
8074 IdentifierIterator *ASTReader::getIdentifiers() {
8075 if (!loadGlobalIndex()) {
8076 std::unique_ptr<IdentifierIterator> ReaderIter(
8077 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8078 std::unique_ptr<IdentifierIterator> ModulesIter(
8079 GlobalIndex->createIdentifierIterator());
8080 return new ChainedIdentifierIterator(std::move(ReaderIter),
8081 std::move(ModulesIter));
8084 return new ASTIdentifierIterator(*this);
8088 namespace serialization {
8090 class ReadMethodPoolVisitor {
8093 unsigned PriorGeneration;
8094 unsigned InstanceBits = 0;
8095 unsigned FactoryBits = 0;
8096 bool InstanceHasMoreThanOneDecl = false;
8097 bool FactoryHasMoreThanOneDecl = false;
8098 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8099 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8102 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8103 unsigned PriorGeneration)
8104 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8106 bool operator()(ModuleFile &M) {
8107 if (!M.SelectorLookupTable)
8110 // If we've already searched this module file, skip it now.
8111 if (M.Generation <= PriorGeneration)
8114 ++Reader.NumMethodPoolTableLookups;
8115 ASTSelectorLookupTable *PoolTable
8116 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8117 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8118 if (Pos == PoolTable->end())
8121 ++Reader.NumMethodPoolTableHits;
8122 ++Reader.NumSelectorsRead;
8123 // FIXME: Not quite happy with the statistics here. We probably should
8124 // disable this tracking when called via LoadSelector.
8125 // Also, should entries without methods count as misses?
8126 ++Reader.NumMethodPoolEntriesRead;
8127 ASTSelectorLookupTrait::data_type Data = *Pos;
8128 if (Reader.DeserializationListener)
8129 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8131 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8132 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8133 InstanceBits = Data.InstanceBits;
8134 FactoryBits = Data.FactoryBits;
8135 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8136 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8140 /// Retrieve the instance methods found by this visitor.
8141 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8142 return InstanceMethods;
8145 /// Retrieve the instance methods found by this visitor.
8146 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8147 return FactoryMethods;
8150 unsigned getInstanceBits() const { return InstanceBits; }
8151 unsigned getFactoryBits() const { return FactoryBits; }
8153 bool instanceHasMoreThanOneDecl() const {
8154 return InstanceHasMoreThanOneDecl;
8157 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8160 } // namespace serialization
8161 } // namespace clang
8163 /// Add the given set of methods to the method list.
8164 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8165 ObjCMethodList &List) {
8166 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8167 S.addMethodToGlobalList(&List, Methods[I]);
8171 void ASTReader::ReadMethodPool(Selector Sel) {
8172 // Get the selector generation and update it to the current generation.
8173 unsigned &Generation = SelectorGeneration[Sel];
8174 unsigned PriorGeneration = Generation;
8175 Generation = getGeneration();
8176 SelectorOutOfDate[Sel] = false;
8178 // Search for methods defined with this selector.
8179 ++NumMethodPoolLookups;
8180 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8181 ModuleMgr.visit(Visitor);
8183 if (Visitor.getInstanceMethods().empty() &&
8184 Visitor.getFactoryMethods().empty())
8187 ++NumMethodPoolHits;
8192 Sema &S = *getSema();
8193 Sema::GlobalMethodPool::iterator Pos
8194 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8196 Pos->second.first.setBits(Visitor.getInstanceBits());
8197 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8198 Pos->second.second.setBits(Visitor.getFactoryBits());
8199 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8201 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8202 // when building a module we keep every method individually and may need to
8203 // update hasMoreThanOneDecl as we add the methods.
8204 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8205 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8208 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8209 if (SelectorOutOfDate[Sel])
8210 ReadMethodPool(Sel);
8213 void ASTReader::ReadKnownNamespaces(
8214 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8217 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8218 if (NamespaceDecl *Namespace
8219 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8220 Namespaces.push_back(Namespace);
8224 void ASTReader::ReadUndefinedButUsed(
8225 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8226 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8227 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8228 SourceLocation Loc =
8229 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8230 Undefined.insert(std::make_pair(D, Loc));
8234 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8235 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8237 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8238 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8239 uint64_t Count = DelayedDeleteExprs[Idx++];
8240 for (uint64_t C = 0; C < Count; ++C) {
8241 SourceLocation DeleteLoc =
8242 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8243 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8244 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8249 void ASTReader::ReadTentativeDefinitions(
8250 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8251 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8252 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8254 TentativeDefs.push_back(Var);
8256 TentativeDefinitions.clear();
8259 void ASTReader::ReadUnusedFileScopedDecls(
8260 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8261 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8263 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8267 UnusedFileScopedDecls.clear();
8270 void ASTReader::ReadDelegatingConstructors(
8271 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8272 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8273 CXXConstructorDecl *D
8274 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8278 DelegatingCtorDecls.clear();
8281 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8282 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8284 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8288 ExtVectorDecls.clear();
8291 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8292 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8293 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8295 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8296 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8300 UnusedLocalTypedefNameCandidates.clear();
8303 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8304 llvm::SmallVector<Decl *, 4> &Decls) {
8305 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8307 auto *D = dyn_cast_or_null<Decl>(
8308 GetDecl(DeclsToCheckForDeferredDiags[I]));
8312 DeclsToCheckForDeferredDiags.clear();
8316 void ASTReader::ReadReferencedSelectors(
8317 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8318 if (ReferencedSelectorsData.empty())
8321 // If there are @selector references added them to its pool. This is for
8322 // implementation of -Wselector.
8323 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8325 while (I < DataSize) {
8326 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8327 SourceLocation SelLoc
8328 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8329 Sels.push_back(std::make_pair(Sel, SelLoc));
8331 ReferencedSelectorsData.clear();
8334 void ASTReader::ReadWeakUndeclaredIdentifiers(
8335 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8336 if (WeakUndeclaredIdentifiers.empty())
8339 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8340 IdentifierInfo *WeakId
8341 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8342 IdentifierInfo *AliasId
8343 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8345 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8346 bool Used = WeakUndeclaredIdentifiers[I++];
8347 WeakInfo WI(AliasId, Loc);
8349 WeakIDs.push_back(std::make_pair(WeakId, WI));
8351 WeakUndeclaredIdentifiers.clear();
8354 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8355 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8356 ExternalVTableUse VT;
8357 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8358 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8359 VT.DefinitionRequired = VTableUses[Idx++];
8360 VTables.push_back(VT);
8366 void ASTReader::ReadPendingInstantiations(
8367 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8368 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8369 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8371 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8373 Pending.push_back(std::make_pair(D, Loc));
8375 PendingInstantiations.clear();
8378 void ASTReader::ReadLateParsedTemplates(
8379 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8381 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8383 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8385 auto LT = std::make_unique<LateParsedTemplate>();
8386 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8388 ModuleFile *F = getOwningModuleFile(LT->D);
8389 assert(F && "No module");
8391 unsigned TokN = LateParsedTemplates[Idx++];
8392 LT->Toks.reserve(TokN);
8393 for (unsigned T = 0; T < TokN; ++T)
8394 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8396 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8399 LateParsedTemplates.clear();
8402 void ASTReader::LoadSelector(Selector Sel) {
8403 // It would be complicated to avoid reading the methods anyway. So don't.
8404 ReadMethodPool(Sel);
8407 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8408 assert(ID && "Non-zero identifier ID required");
8409 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8410 IdentifiersLoaded[ID - 1] = II;
8411 if (DeserializationListener)
8412 DeserializationListener->IdentifierRead(ID, II);
8415 /// Set the globally-visible declarations associated with the given
8418 /// If the AST reader is currently in a state where the given declaration IDs
8419 /// cannot safely be resolved, they are queued until it is safe to resolve
8422 /// \param II an IdentifierInfo that refers to one or more globally-visible
8425 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8426 /// visible at global scope.
8428 /// \param Decls if non-null, this vector will be populated with the set of
8429 /// deserialized declarations. These declarations will not be pushed into
8432 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8433 const SmallVectorImpl<uint32_t> &DeclIDs,
8434 SmallVectorImpl<Decl *> *Decls) {
8435 if (NumCurrentElementsDeserializing && !Decls) {
8436 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8440 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8442 // Queue this declaration so that it will be added to the
8443 // translation unit scope and identifier's declaration chain
8444 // once a Sema object is known.
8445 PreloadedDeclIDs.push_back(DeclIDs[I]);
8449 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8451 // If we're simply supposed to record the declarations, do so now.
8453 Decls->push_back(D);
8457 // Introduce this declaration into the translation-unit scope
8458 // and add it to the declaration chain for this identifier, so
8459 // that (unqualified) name lookup will find it.
8460 pushExternalDeclIntoScope(D, II);
8464 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8468 if (IdentifiersLoaded.empty()) {
8469 Error("no identifier table in AST file");
8474 if (!IdentifiersLoaded[ID]) {
8475 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8476 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8477 ModuleFile *M = I->second;
8478 unsigned Index = ID - M->BaseIdentifierID;
8479 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8481 // All of the strings in the AST file are preceded by a 16-bit length.
8482 // Extract that 16-bit length to avoid having to execute strlen().
8483 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8484 // unsigned integers. This is important to avoid integer overflow when
8485 // we cast them to 'unsigned'.
8486 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8487 unsigned StrLen = (((unsigned) StrLenPtr[0])
8488 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8489 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8490 IdentifiersLoaded[ID] = &II;
8491 markIdentifierFromAST(*this, II);
8492 if (DeserializationListener)
8493 DeserializationListener->IdentifierRead(ID + 1, &II);
8496 return IdentifiersLoaded[ID];
8499 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8500 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8503 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8504 if (LocalID < NUM_PREDEF_IDENT_IDS)
8507 if (!M.ModuleOffsetMap.empty())
8508 ReadModuleOffsetMap(M);
8510 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8511 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8512 assert(I != M.IdentifierRemap.end()
8513 && "Invalid index into identifier index remap");
8515 return LocalID + I->second;
8518 MacroInfo *ASTReader::getMacro(MacroID ID) {
8522 if (MacrosLoaded.empty()) {
8523 Error("no macro table in AST file");
8527 ID -= NUM_PREDEF_MACRO_IDS;
8528 if (!MacrosLoaded[ID]) {
8529 GlobalMacroMapType::iterator I
8530 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8531 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8532 ModuleFile *M = I->second;
8533 unsigned Index = ID - M->BaseMacroID;
8535 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8537 if (DeserializationListener)
8538 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8542 return MacrosLoaded[ID];
8545 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8546 if (LocalID < NUM_PREDEF_MACRO_IDS)
8549 if (!M.ModuleOffsetMap.empty())
8550 ReadModuleOffsetMap(M);
8552 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8553 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8554 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8556 return LocalID + I->second;
8559 serialization::SubmoduleID
8560 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8561 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8564 if (!M.ModuleOffsetMap.empty())
8565 ReadModuleOffsetMap(M);
8567 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8568 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8569 assert(I != M.SubmoduleRemap.end()
8570 && "Invalid index into submodule index remap");
8572 return LocalID + I->second;
8575 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8576 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8577 assert(GlobalID == 0 && "Unhandled global submodule ID");
8581 if (GlobalID > SubmodulesLoaded.size()) {
8582 Error("submodule ID out of range in AST file");
8586 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8589 Module *ASTReader::getModule(unsigned ID) {
8590 return getSubmodule(ID);
8593 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8594 ModuleFile *MF = getOwningModuleFile(D);
8595 return MF && MF->PCHHasObjectFile;
8598 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8600 // It's a module, look it up by submodule ID.
8601 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8602 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8604 // It's a prefix (preamble, PCH, ...). Look it up by index.
8605 unsigned IndexFromEnd = ID >> 1;
8606 assert(IndexFromEnd && "got reference to unknown module file");
8607 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8611 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8615 // For a file representing a module, use the submodule ID of the top-level
8616 // module as the file ID. For any other kind of file, the number of such
8617 // files loaded beforehand will be the same on reload.
8618 // FIXME: Is this true even if we have an explicit module file and a PCH?
8620 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8622 auto PCHModules = getModuleManager().pch_modules();
8623 auto I = llvm::find(PCHModules, F);
8624 assert(I != PCHModules.end() && "emitting reference to unknown file");
8625 return (I - PCHModules.end()) << 1;
8628 llvm::Optional<ASTSourceDescriptor>
8629 ASTReader::getSourceDescriptor(unsigned ID) {
8630 if (Module *M = getSubmodule(ID))
8631 return ASTSourceDescriptor(*M);
8633 // If there is only a single PCH, return it instead.
8634 // Chained PCH are not supported.
8635 const auto &PCHChain = ModuleMgr.pch_modules();
8636 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8637 ModuleFile &MF = ModuleMgr.getPrimaryModule();
8638 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8639 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8640 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8646 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8647 auto I = DefinitionSource.find(FD);
8648 if (I == DefinitionSource.end())
8649 return EK_ReplyHazy;
8650 return I->second ? EK_Never : EK_Always;
8653 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8654 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8657 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8661 if (ID > SelectorsLoaded.size()) {
8662 Error("selector ID out of range in AST file");
8666 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8667 // Load this selector from the selector table.
8668 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8669 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8670 ModuleFile &M = *I->second;
8671 ASTSelectorLookupTrait Trait(*this, M);
8672 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8673 SelectorsLoaded[ID - 1] =
8674 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8675 if (DeserializationListener)
8676 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8679 return SelectorsLoaded[ID - 1];
8682 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8683 return DecodeSelector(ID);
8686 uint32_t ASTReader::GetNumExternalSelectors() {
8687 // ID 0 (the null selector) is considered an external selector.
8688 return getTotalNumSelectors() + 1;
8691 serialization::SelectorID
8692 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8693 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8696 if (!M.ModuleOffsetMap.empty())
8697 ReadModuleOffsetMap(M);
8699 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8700 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8701 assert(I != M.SelectorRemap.end()
8702 && "Invalid index into selector index remap");
8704 return LocalID + I->second;
8708 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8709 DeclarationNameLoc DNLoc;
8710 switch (Name.getNameKind()) {
8711 case DeclarationName::CXXConstructorName:
8712 case DeclarationName::CXXDestructorName:
8713 case DeclarationName::CXXConversionFunctionName:
8714 DNLoc.NamedType.TInfo = readTypeSourceInfo();
8717 case DeclarationName::CXXOperatorName:
8718 DNLoc.CXXOperatorName.BeginOpNameLoc
8719 = readSourceLocation().getRawEncoding();
8720 DNLoc.CXXOperatorName.EndOpNameLoc
8721 = readSourceLocation().getRawEncoding();
8724 case DeclarationName::CXXLiteralOperatorName:
8725 DNLoc.CXXLiteralOperatorName.OpNameLoc
8726 = readSourceLocation().getRawEncoding();
8729 case DeclarationName::Identifier:
8730 case DeclarationName::ObjCZeroArgSelector:
8731 case DeclarationName::ObjCOneArgSelector:
8732 case DeclarationName::ObjCMultiArgSelector:
8733 case DeclarationName::CXXUsingDirective:
8734 case DeclarationName::CXXDeductionGuideName:
8740 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8741 DeclarationNameInfo NameInfo;
8742 NameInfo.setName(readDeclarationName());
8743 NameInfo.setLoc(readSourceLocation());
8744 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8748 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8749 Info.QualifierLoc = readNestedNameSpecifierLoc();
8750 unsigned NumTPLists = readInt();
8751 Info.NumTemplParamLists = NumTPLists;
8753 Info.TemplParamLists =
8754 new (getContext()) TemplateParameterList *[NumTPLists];
8755 for (unsigned i = 0; i != NumTPLists; ++i)
8756 Info.TemplParamLists[i] = readTemplateParameterList();
8760 TemplateParameterList *
8761 ASTRecordReader::readTemplateParameterList() {
8762 SourceLocation TemplateLoc = readSourceLocation();
8763 SourceLocation LAngleLoc = readSourceLocation();
8764 SourceLocation RAngleLoc = readSourceLocation();
8766 unsigned NumParams = readInt();
8767 SmallVector<NamedDecl *, 16> Params;
8768 Params.reserve(NumParams);
8770 Params.push_back(readDeclAs<NamedDecl>());
8772 bool HasRequiresClause = readBool();
8773 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8775 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8776 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8777 return TemplateParams;
8780 void ASTRecordReader::readTemplateArgumentList(
8781 SmallVectorImpl<TemplateArgument> &TemplArgs,
8782 bool Canonicalize) {
8783 unsigned NumTemplateArgs = readInt();
8784 TemplArgs.reserve(NumTemplateArgs);
8785 while (NumTemplateArgs--)
8786 TemplArgs.push_back(readTemplateArgument(Canonicalize));
8789 /// Read a UnresolvedSet structure.
8790 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8791 unsigned NumDecls = readInt();
8792 Set.reserve(getContext(), NumDecls);
8793 while (NumDecls--) {
8794 DeclID ID = readDeclID();
8795 AccessSpecifier AS = (AccessSpecifier) readInt();
8796 Set.addLazyDecl(getContext(), ID, AS);
8801 ASTRecordReader::readCXXBaseSpecifier() {
8802 bool isVirtual = readBool();
8803 bool isBaseOfClass = readBool();
8804 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8805 bool inheritConstructors = readBool();
8806 TypeSourceInfo *TInfo = readTypeSourceInfo();
8807 SourceRange Range = readSourceRange();
8808 SourceLocation EllipsisLoc = readSourceLocation();
8809 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8811 Result.setInheritConstructors(inheritConstructors);
8815 CXXCtorInitializer **
8816 ASTRecordReader::readCXXCtorInitializers() {
8817 ASTContext &Context = getContext();
8818 unsigned NumInitializers = readInt();
8819 assert(NumInitializers && "wrote ctor initializers but have no inits");
8820 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8821 for (unsigned i = 0; i != NumInitializers; ++i) {
8822 TypeSourceInfo *TInfo = nullptr;
8823 bool IsBaseVirtual = false;
8824 FieldDecl *Member = nullptr;
8825 IndirectFieldDecl *IndirectMember = nullptr;
8827 CtorInitializerType Type = (CtorInitializerType) readInt();
8829 case CTOR_INITIALIZER_BASE:
8830 TInfo = readTypeSourceInfo();
8831 IsBaseVirtual = readBool();
8834 case CTOR_INITIALIZER_DELEGATING:
8835 TInfo = readTypeSourceInfo();
8838 case CTOR_INITIALIZER_MEMBER:
8839 Member = readDeclAs<FieldDecl>();
8842 case CTOR_INITIALIZER_INDIRECT_MEMBER:
8843 IndirectMember = readDeclAs<IndirectFieldDecl>();
8847 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8848 Expr *Init = readExpr();
8849 SourceLocation LParenLoc = readSourceLocation();
8850 SourceLocation RParenLoc = readSourceLocation();
8852 CXXCtorInitializer *BOMInit;
8853 if (Type == CTOR_INITIALIZER_BASE)
8854 BOMInit = new (Context)
8855 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8856 RParenLoc, MemberOrEllipsisLoc);
8857 else if (Type == CTOR_INITIALIZER_DELEGATING)
8858 BOMInit = new (Context)
8859 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8861 BOMInit = new (Context)
8862 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8865 BOMInit = new (Context)
8866 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8867 LParenLoc, Init, RParenLoc);
8869 if (/*IsWritten*/readBool()) {
8870 unsigned SourceOrder = readInt();
8871 BOMInit->setSourceOrder(SourceOrder);
8874 CtorInitializers[i] = BOMInit;
8877 return CtorInitializers;
8880 NestedNameSpecifierLoc
8881 ASTRecordReader::readNestedNameSpecifierLoc() {
8882 ASTContext &Context = getContext();
8883 unsigned N = readInt();
8884 NestedNameSpecifierLocBuilder Builder;
8885 for (unsigned I = 0; I != N; ++I) {
8886 auto Kind = readNestedNameSpecifierKind();
8888 case NestedNameSpecifier::Identifier: {
8889 IdentifierInfo *II = readIdentifier();
8890 SourceRange Range = readSourceRange();
8891 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8895 case NestedNameSpecifier::Namespace: {
8896 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8897 SourceRange Range = readSourceRange();
8898 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8902 case NestedNameSpecifier::NamespaceAlias: {
8903 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8904 SourceRange Range = readSourceRange();
8905 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8909 case NestedNameSpecifier::TypeSpec:
8910 case NestedNameSpecifier::TypeSpecWithTemplate: {
8911 bool Template = readBool();
8912 TypeSourceInfo *T = readTypeSourceInfo();
8914 return NestedNameSpecifierLoc();
8915 SourceLocation ColonColonLoc = readSourceLocation();
8917 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8918 Builder.Extend(Context,
8919 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8920 T->getTypeLoc(), ColonColonLoc);
8924 case NestedNameSpecifier::Global: {
8925 SourceLocation ColonColonLoc = readSourceLocation();
8926 Builder.MakeGlobal(Context, ColonColonLoc);
8930 case NestedNameSpecifier::Super: {
8931 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8932 SourceRange Range = readSourceRange();
8933 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8939 return Builder.getWithLocInContext(Context);
8943 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8945 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8946 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8947 return SourceRange(beg, end);
8950 static FixedPointSemantics
8951 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8953 unsigned Width = Record[Idx++];
8954 unsigned Scale = Record[Idx++];
8955 uint64_t Tmp = Record[Idx++];
8956 bool IsSigned = Tmp & 0x1;
8957 bool IsSaturated = Tmp & 0x2;
8958 bool HasUnsignedPadding = Tmp & 0x4;
8959 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8960 HasUnsignedPadding);
8963 static const llvm::fltSemantics &
8964 readAPFloatSemantics(ASTRecordReader &reader) {
8965 return llvm::APFloatBase::EnumToSemantics(
8966 static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8969 APValue ASTRecordReader::readAPValue() {
8970 unsigned Kind = readInt();
8971 switch ((APValue::ValueKind) Kind) {
8974 case APValue::Indeterminate:
8975 return APValue::IndeterminateValue();
8977 return APValue(readAPSInt());
8978 case APValue::Float: {
8979 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8980 return APValue(readAPFloat(FloatSema));
8982 case APValue::FixedPoint: {
8983 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8984 return APValue(APFixedPoint(readAPInt(), FPSema));
8986 case APValue::ComplexInt: {
8987 llvm::APSInt First = readAPSInt();
8988 return APValue(std::move(First), readAPSInt());
8990 case APValue::ComplexFloat: {
8991 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8992 llvm::APFloat First = readAPFloat(FloatSema1);
8993 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8994 return APValue(std::move(First), readAPFloat(FloatSema2));
8996 case APValue::LValue:
8997 case APValue::Vector:
8998 case APValue::Array:
8999 case APValue::Struct:
9000 case APValue::Union:
9001 case APValue::MemberPointer:
9002 case APValue::AddrLabelDiff:
9003 // TODO : Handle all these APValue::ValueKind.
9006 llvm_unreachable("Invalid APValue::ValueKind");
9009 /// Read a floating-point value
9010 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9011 return llvm::APFloat(Sem, readAPInt());
9015 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9016 unsigned Len = Record[Idx++];
9017 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9022 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9024 std::string Filename = ReadString(Record, Idx);
9025 ResolveImportedPath(F, Filename);
9029 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9030 const RecordData &Record, unsigned &Idx) {
9031 std::string Filename = ReadString(Record, Idx);
9032 if (!BaseDirectory.empty())
9033 ResolveImportedPath(Filename, BaseDirectory);
9037 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9039 unsigned Major = Record[Idx++];
9040 unsigned Minor = Record[Idx++];
9041 unsigned Subminor = Record[Idx++];
9043 return VersionTuple(Major);
9045 return VersionTuple(Major, Minor - 1);
9046 return VersionTuple(Major, Minor - 1, Subminor - 1);
9049 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9050 const RecordData &Record,
9052 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9053 return CXXTemporary::Create(getContext(), Decl);
9056 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9057 return Diag(CurrentImportLoc, DiagID);
9060 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9061 return Diags.Report(Loc, DiagID);
9064 /// Retrieve the identifier table associated with the
9066 IdentifierTable &ASTReader::getIdentifierTable() {
9067 return PP.getIdentifierTable();
9070 /// Record that the given ID maps to the given switch-case
9072 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9073 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9074 "Already have a SwitchCase with this ID");
9075 (*CurrSwitchCaseStmts)[ID] = SC;
9078 /// Retrieve the switch-case statement with the given ID.
9079 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9080 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9081 return (*CurrSwitchCaseStmts)[ID];
9084 void ASTReader::ClearSwitchCaseIDs() {
9085 CurrSwitchCaseStmts->clear();
9088 void ASTReader::ReadComments() {
9089 ASTContext &Context = getContext();
9090 std::vector<RawComment *> Comments;
9091 for (SmallVectorImpl<std::pair<BitstreamCursor,
9092 serialization::ModuleFile *>>::iterator
9093 I = CommentsCursors.begin(),
9094 E = CommentsCursors.end();
9097 BitstreamCursor &Cursor = I->first;
9098 serialization::ModuleFile &F = *I->second;
9099 SavedStreamPosition SavedPosition(Cursor);
9103 Expected<llvm::BitstreamEntry> MaybeEntry =
9104 Cursor.advanceSkippingSubblocks(
9105 BitstreamCursor::AF_DontPopBlockAtEnd);
9107 Error(MaybeEntry.takeError());
9110 llvm::BitstreamEntry Entry = MaybeEntry.get();
9112 switch (Entry.Kind) {
9113 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9114 case llvm::BitstreamEntry::Error:
9115 Error("malformed block record in AST file");
9117 case llvm::BitstreamEntry::EndBlock:
9119 case llvm::BitstreamEntry::Record:
9120 // The interesting case.
9126 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9127 if (!MaybeComment) {
9128 Error(MaybeComment.takeError());
9131 switch ((CommentRecordTypes)MaybeComment.get()) {
9132 case COMMENTS_RAW_COMMENT: {
9134 SourceRange SR = ReadSourceRange(F, Record, Idx);
9135 RawComment::CommentKind Kind =
9136 (RawComment::CommentKind) Record[Idx++];
9137 bool IsTrailingComment = Record[Idx++];
9138 bool IsAlmostTrailingComment = Record[Idx++];
9139 Comments.push_back(new (Context) RawComment(
9140 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9146 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9147 FileToOffsetToComment;
9148 for (RawComment *C : Comments) {
9149 SourceLocation CommentLoc = C->getBeginLoc();
9150 if (CommentLoc.isValid()) {
9151 std::pair<FileID, unsigned> Loc =
9152 SourceMgr.getDecomposedLoc(CommentLoc);
9153 if (Loc.first.isValid())
9154 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9160 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9161 bool IncludeSystem, bool Complain,
9162 llvm::function_ref<void(const serialization::InputFile &IF,
9163 bool isSystem)> Visitor) {
9164 unsigned NumUserInputs = MF.NumUserInputFiles;
9165 unsigned NumInputs = MF.InputFilesLoaded.size();
9166 assert(NumUserInputs <= NumInputs);
9167 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9168 for (unsigned I = 0; I < N; ++I) {
9169 bool IsSystem = I >= NumUserInputs;
9170 InputFile IF = getInputFile(MF, I+1, Complain);
9171 Visitor(IF, IsSystem);
9175 void ASTReader::visitTopLevelModuleMaps(
9176 serialization::ModuleFile &MF,
9177 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9178 unsigned NumInputs = MF.InputFilesLoaded.size();
9179 for (unsigned I = 0; I < NumInputs; ++I) {
9180 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9181 if (IFI.TopLevelModuleMap)
9182 // FIXME: This unnecessarily re-reads the InputFileInfo.
9183 if (auto *FE = getInputFile(MF, I + 1).getFile())
9188 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9189 // If we know the owning module, use it.
9190 if (Module *M = D->getImportedOwningModule())
9191 return M->getFullModuleName();
9193 // Otherwise, use the name of the top-level module the decl is within.
9194 if (ModuleFile *M = getOwningModuleFile(D))
9195 return M->ModuleName;
9197 // Not from a module.
9201 void ASTReader::finishPendingActions() {
9202 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9203 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9204 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9205 !PendingUpdateRecords.empty()) {
9206 // If any identifiers with corresponding top-level declarations have
9207 // been loaded, load those declarations now.
9208 using TopLevelDeclsMap =
9209 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9210 TopLevelDeclsMap TopLevelDecls;
9212 while (!PendingIdentifierInfos.empty()) {
9213 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9214 SmallVector<uint32_t, 4> DeclIDs =
9215 std::move(PendingIdentifierInfos.back().second);
9216 PendingIdentifierInfos.pop_back();
9218 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9221 // Load each function type that we deferred loading because it was a
9222 // deduced type that might refer to a local type declared within itself.
9223 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9224 auto *FD = PendingFunctionTypes[I].first;
9225 FD->setType(GetType(PendingFunctionTypes[I].second));
9227 // If we gave a function a deduced return type, remember that we need to
9228 // propagate that along the redeclaration chain.
9229 auto *DT = FD->getReturnType()->getContainedDeducedType();
9230 if (DT && DT->isDeduced())
9231 PendingDeducedTypeUpdates.insert(
9232 {FD->getCanonicalDecl(), FD->getReturnType()});
9234 PendingFunctionTypes.clear();
9236 // For each decl chain that we wanted to complete while deserializing, mark
9237 // it as "still needs to be completed".
9238 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9239 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9241 PendingIncompleteDeclChains.clear();
9243 // Load pending declaration chains.
9244 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9245 loadPendingDeclChain(PendingDeclChains[I].first,
9246 PendingDeclChains[I].second);
9247 PendingDeclChains.clear();
9249 // Make the most recent of the top-level declarations visible.
9250 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9251 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9252 IdentifierInfo *II = TLD->first;
9253 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9254 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9258 // Load any pending macro definitions.
9259 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9260 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9261 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9262 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9263 // Initialize the macro history from chained-PCHs ahead of module imports.
9264 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9266 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9267 if (!Info.M->isModule())
9268 resolvePendingMacro(II, Info);
9270 // Handle module imports.
9271 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9273 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9274 if (Info.M->isModule())
9275 resolvePendingMacro(II, Info);
9278 PendingMacroIDs.clear();
9280 // Wire up the DeclContexts for Decls that we delayed setting until
9281 // recursive loading is completed.
9282 while (!PendingDeclContextInfos.empty()) {
9283 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9284 PendingDeclContextInfos.pop_front();
9285 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9286 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9287 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9290 // Perform any pending declaration updates.
9291 while (!PendingUpdateRecords.empty()) {
9292 auto Update = PendingUpdateRecords.pop_back_val();
9293 ReadingKindTracker ReadingKind(Read_Decl, *this);
9294 loadDeclUpdateRecords(Update);
9298 // At this point, all update records for loaded decls are in place, so any
9299 // fake class definitions should have become real.
9300 assert(PendingFakeDefinitionData.empty() &&
9301 "faked up a class definition but never saw the real one");
9303 // If we deserialized any C++ or Objective-C class definitions, any
9304 // Objective-C protocol definitions, or any redeclarable templates, make sure
9305 // that all redeclarations point to the definitions. Note that this can only
9306 // happen now, after the redeclaration chains have been fully wired.
9307 for (Decl *D : PendingDefinitions) {
9308 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9309 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9310 // Make sure that the TagType points at the definition.
9311 const_cast<TagType*>(TagT)->decl = TD;
9314 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9315 for (auto *R = getMostRecentExistingDecl(RD); R;
9316 R = R->getPreviousDecl()) {
9318 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9319 "declaration thinks it's the definition but it isn't");
9320 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9327 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9328 // Make sure that the ObjCInterfaceType points at the definition.
9329 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9332 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9333 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9338 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9339 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9340 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9345 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9346 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9347 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9349 PendingDefinitions.clear();
9351 // Load the bodies of any functions or methods we've encountered. We do
9352 // this now (delayed) so that we can be sure that the declaration chains
9353 // have been fully wired up (hasBody relies on this).
9354 // FIXME: We shouldn't require complete redeclaration chains here.
9355 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9356 PBEnd = PendingBodies.end();
9357 PB != PBEnd; ++PB) {
9358 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9359 // For a function defined inline within a class template, force the
9360 // canonical definition to be the one inside the canonical definition of
9361 // the template. This ensures that we instantiate from a correct view
9364 // Sadly we can't do this more generally: we can't be sure that all
9365 // copies of an arbitrary class definition will have the same members
9366 // defined (eg, some member functions may not be instantiated, and some
9367 // special members may or may not have been implicitly defined).
9368 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9369 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9372 // FIXME: Check for =delete/=default?
9373 // FIXME: Complain about ODR violations here?
9374 const FunctionDecl *Defn = nullptr;
9375 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9376 FD->setLazyBody(PB->second);
9378 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9379 mergeDefinitionVisibility(NonConstDefn, FD);
9381 if (!FD->isLateTemplateParsed() &&
9382 !NonConstDefn->isLateTemplateParsed() &&
9383 FD->getODRHash() != NonConstDefn->getODRHash()) {
9384 if (!isa<CXXMethodDecl>(FD)) {
9385 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9386 } else if (FD->getLexicalParent()->isFileContext() &&
9387 NonConstDefn->getLexicalParent()->isFileContext()) {
9388 // Only diagnose out-of-line method definitions. If they are
9389 // in class definitions, then an error will be generated when
9390 // processing the class bodies.
9391 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9398 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9399 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9400 MD->setLazyBody(PB->second);
9402 PendingBodies.clear();
9405 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9406 getContext().deduplicateMergedDefinitonsFor(ND);
9407 PendingMergedDefinitionsToDeduplicate.clear();
9410 void ASTReader::diagnoseOdrViolations() {
9411 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9412 PendingFunctionOdrMergeFailures.empty() &&
9413 PendingEnumOdrMergeFailures.empty())
9416 // Trigger the import of the full definition of each class that had any
9417 // odr-merging problems, so we can produce better diagnostics for them.
9418 // These updates may in turn find and diagnose some ODR failures, so take
9419 // ownership of the set first.
9420 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9421 PendingOdrMergeFailures.clear();
9422 for (auto &Merge : OdrMergeFailures) {
9423 Merge.first->buildLookup();
9424 Merge.first->decls_begin();
9425 Merge.first->bases_begin();
9426 Merge.first->vbases_begin();
9427 for (auto &RecordPair : Merge.second) {
9428 auto *RD = RecordPair.first;
9435 // Trigger the import of functions.
9436 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9437 PendingFunctionOdrMergeFailures.clear();
9438 for (auto &Merge : FunctionOdrMergeFailures) {
9439 Merge.first->buildLookup();
9440 Merge.first->decls_begin();
9441 Merge.first->getBody();
9442 for (auto &FD : Merge.second) {
9449 // Trigger the import of enums.
9450 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9451 PendingEnumOdrMergeFailures.clear();
9452 for (auto &Merge : EnumOdrMergeFailures) {
9453 Merge.first->decls_begin();
9454 for (auto &Enum : Merge.second) {
9455 Enum->decls_begin();
9459 // For each declaration from a merged context, check that the canonical
9460 // definition of that context also contains a declaration of the same
9463 // Caution: this loop does things that might invalidate iterators into
9464 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9465 while (!PendingOdrMergeChecks.empty()) {
9466 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9468 // FIXME: Skip over implicit declarations for now. This matters for things
9469 // like implicitly-declared special member functions. This isn't entirely
9470 // correct; we can end up with multiple unmerged declarations of the same
9472 if (D->isImplicit())
9475 DeclContext *CanonDef = D->getDeclContext();
9478 const Decl *DCanon = D->getCanonicalDecl();
9480 for (auto RI : D->redecls()) {
9481 if (RI->getLexicalDeclContext() == CanonDef) {
9489 // Quick check failed, time to do the slow thing. Note, we can't just
9490 // look up the name of D in CanonDef here, because the member that is
9491 // in CanonDef might not be found by name lookup (it might have been
9492 // replaced by a more recent declaration in the lookup table), and we
9493 // can't necessarily find it in the redeclaration chain because it might
9494 // be merely mergeable, not redeclarable.
9495 llvm::SmallVector<const NamedDecl*, 4> Candidates;
9496 for (auto *CanonMember : CanonDef->decls()) {
9497 if (CanonMember->getCanonicalDecl() == DCanon) {
9498 // This can happen if the declaration is merely mergeable and not
9499 // actually redeclarable (we looked for redeclarations earlier).
9501 // FIXME: We should be able to detect this more efficiently, without
9502 // pulling in all of the members of CanonDef.
9506 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9507 if (ND->getDeclName() == D->getDeclName())
9508 Candidates.push_back(ND);
9512 // The AST doesn't like TagDecls becoming invalid after they've been
9513 // completed. We only really need to mark FieldDecls as invalid here.
9514 if (!isa<TagDecl>(D))
9515 D->setInvalidDecl();
9517 // Ensure we don't accidentally recursively enter deserialization while
9518 // we're producing our diagnostic.
9519 Deserializing RecursionGuard(this);
9521 std::string CanonDefModule =
9522 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9523 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9524 << D << getOwningModuleNameForDiagnostic(D)
9525 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9527 if (Candidates.empty())
9528 Diag(cast<Decl>(CanonDef)->getLocation(),
9529 diag::note_module_odr_violation_no_possible_decls) << D;
9531 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9532 Diag(Candidates[I]->getLocation(),
9533 diag::note_module_odr_violation_possible_decl)
9537 DiagnosedOdrMergeFailures.insert(CanonDef);
9541 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9542 EnumOdrMergeFailures.empty())
9545 // Ensure we don't accidentally recursively enter deserialization while
9546 // we're producing our diagnostics.
9547 Deserializing RecursionGuard(this);
9549 // Common code for hashing helpers.
9551 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9553 Hash.AddQualType(Ty);
9554 return Hash.CalculateHash();
9557 auto ComputeODRHash = [&Hash](const Stmt *S) {
9561 return Hash.CalculateHash();
9564 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9568 return Hash.CalculateHash();
9571 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9573 Hash.AddTemplateArgument(TA);
9574 return Hash.CalculateHash();
9577 auto ComputeTemplateParameterListODRHash =
9578 [&Hash](const TemplateParameterList *TPL) {
9581 Hash.AddTemplateParameterList(TPL);
9582 return Hash.CalculateHash();
9585 // Used with err_module_odr_violation_mismatch_decl and
9586 // note_module_odr_violation_mismatch_decl
9587 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9588 enum ODRMismatchDecl {
9604 // Used with err_module_odr_violation_mismatch_decl_diff and
9605 // note_module_odr_violation_mismatch_decl_diff
9606 enum ODRMismatchDeclDifference {
9607 StaticAssertCondition,
9608 StaticAssertMessage,
9609 StaticAssertOnlyMessage,
9612 FieldSingleBitField,
9613 FieldDifferentWidthBitField,
9615 FieldSingleInitializer,
9616 FieldDifferentInitializers,
9625 MethodNumberParameters,
9626 MethodParameterType,
9627 MethodParameterName,
9628 MethodParameterSingleDefaultArgument,
9629 MethodParameterDifferentDefaultArgument,
9630 MethodNoTemplateArguments,
9631 MethodDifferentNumberTemplateArguments,
9632 MethodDifferentTemplateArgument,
9634 MethodDifferentBody,
9639 VarSingleInitializer,
9640 VarDifferentInitializer,
9645 FunctionTemplateDifferentNumberParameters,
9646 FunctionTemplateParameterDifferentKind,
9647 FunctionTemplateParameterName,
9648 FunctionTemplateParameterSingleDefaultArgument,
9649 FunctionTemplateParameterDifferentDefaultArgument,
9650 FunctionTemplateParameterDifferentType,
9651 FunctionTemplatePackParameter,
9654 // These lambdas have the common portions of the ODR diagnostics. This
9655 // has the same return as Diag(), so addition parameters can be passed
9656 // in with operator<<
9657 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9658 SourceLocation Loc, SourceRange Range,
9659 ODRMismatchDeclDifference DiffType) {
9660 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9661 << FirstRecord << FirstModule.empty() << FirstModule << Range
9664 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9665 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9666 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9667 << SecondModule << Range << DiffType;
9670 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9671 &ComputeQualTypeODRHash, &ComputeODRHash](
9672 NamedDecl *FirstRecord, StringRef FirstModule,
9673 StringRef SecondModule, FieldDecl *FirstField,
9674 FieldDecl *SecondField) {
9675 IdentifierInfo *FirstII = FirstField->getIdentifier();
9676 IdentifierInfo *SecondII = SecondField->getIdentifier();
9677 if (FirstII->getName() != SecondII->getName()) {
9678 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9679 FirstField->getSourceRange(), FieldName)
9681 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9682 SecondField->getSourceRange(), FieldName)
9688 assert(getContext().hasSameType(FirstField->getType(),
9689 SecondField->getType()));
9691 QualType FirstType = FirstField->getType();
9692 QualType SecondType = SecondField->getType();
9693 if (ComputeQualTypeODRHash(FirstType) !=
9694 ComputeQualTypeODRHash(SecondType)) {
9695 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9696 FirstField->getSourceRange(), FieldTypeName)
9697 << FirstII << FirstType;
9698 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9699 SecondField->getSourceRange(), FieldTypeName)
9700 << SecondII << SecondType;
9705 const bool IsFirstBitField = FirstField->isBitField();
9706 const bool IsSecondBitField = SecondField->isBitField();
9707 if (IsFirstBitField != IsSecondBitField) {
9708 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9709 FirstField->getSourceRange(), FieldSingleBitField)
9710 << FirstII << IsFirstBitField;
9711 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9712 SecondField->getSourceRange(), FieldSingleBitField)
9713 << SecondII << IsSecondBitField;
9717 if (IsFirstBitField && IsSecondBitField) {
9718 unsigned FirstBitWidthHash =
9719 ComputeODRHash(FirstField->getBitWidth());
9720 unsigned SecondBitWidthHash =
9721 ComputeODRHash(SecondField->getBitWidth());
9722 if (FirstBitWidthHash != SecondBitWidthHash) {
9723 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9724 FirstField->getSourceRange(),
9725 FieldDifferentWidthBitField)
9726 << FirstII << FirstField->getBitWidth()->getSourceRange();
9727 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9728 SecondField->getSourceRange(),
9729 FieldDifferentWidthBitField)
9730 << SecondII << SecondField->getBitWidth()->getSourceRange();
9735 if (!PP.getLangOpts().CPlusPlus)
9738 const bool IsFirstMutable = FirstField->isMutable();
9739 const bool IsSecondMutable = SecondField->isMutable();
9740 if (IsFirstMutable != IsSecondMutable) {
9741 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9742 FirstField->getSourceRange(), FieldSingleMutable)
9743 << FirstII << IsFirstMutable;
9744 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9745 SecondField->getSourceRange(), FieldSingleMutable)
9746 << SecondII << IsSecondMutable;
9750 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9751 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9752 if ((!FirstInitializer && SecondInitializer) ||
9753 (FirstInitializer && !SecondInitializer)) {
9754 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9755 FirstField->getSourceRange(), FieldSingleInitializer)
9756 << FirstII << (FirstInitializer != nullptr);
9757 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9758 SecondField->getSourceRange(), FieldSingleInitializer)
9759 << SecondII << (SecondInitializer != nullptr);
9763 if (FirstInitializer && SecondInitializer) {
9764 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9765 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9766 if (FirstInitHash != SecondInitHash) {
9767 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9768 FirstField->getSourceRange(),
9769 FieldDifferentInitializers)
9770 << FirstII << FirstInitializer->getSourceRange();
9771 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9772 SecondField->getSourceRange(),
9773 FieldDifferentInitializers)
9774 << SecondII << SecondInitializer->getSourceRange();
9782 auto ODRDiagTypeDefOrAlias =
9783 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9784 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9785 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9787 auto FirstName = FirstTD->getDeclName();
9788 auto SecondName = SecondTD->getDeclName();
9789 if (FirstName != SecondName) {
9790 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9791 FirstTD->getSourceRange(), TypedefName)
9792 << IsTypeAlias << FirstName;
9793 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9794 SecondTD->getSourceRange(), TypedefName)
9795 << IsTypeAlias << SecondName;
9799 QualType FirstType = FirstTD->getUnderlyingType();
9800 QualType SecondType = SecondTD->getUnderlyingType();
9801 if (ComputeQualTypeODRHash(FirstType) !=
9802 ComputeQualTypeODRHash(SecondType)) {
9803 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9804 FirstTD->getSourceRange(), TypedefType)
9805 << IsTypeAlias << FirstName << FirstType;
9806 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9807 SecondTD->getSourceRange(), TypedefType)
9808 << IsTypeAlias << SecondName << SecondType;
9815 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9816 &ComputeQualTypeODRHash, &ComputeODRHash,
9817 this](NamedDecl *FirstRecord, StringRef FirstModule,
9818 StringRef SecondModule, VarDecl *FirstVD,
9819 VarDecl *SecondVD) {
9820 auto FirstName = FirstVD->getDeclName();
9821 auto SecondName = SecondVD->getDeclName();
9822 if (FirstName != SecondName) {
9823 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9824 FirstVD->getSourceRange(), VarName)
9826 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9827 SecondVD->getSourceRange(), VarName)
9832 QualType FirstType = FirstVD->getType();
9833 QualType SecondType = SecondVD->getType();
9834 if (ComputeQualTypeODRHash(FirstType) !=
9835 ComputeQualTypeODRHash(SecondType)) {
9836 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9837 FirstVD->getSourceRange(), VarType)
9838 << FirstName << FirstType;
9839 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9840 SecondVD->getSourceRange(), VarType)
9841 << SecondName << SecondType;
9845 if (!PP.getLangOpts().CPlusPlus)
9848 const Expr *FirstInit = FirstVD->getInit();
9849 const Expr *SecondInit = SecondVD->getInit();
9850 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9851 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9852 FirstVD->getSourceRange(), VarSingleInitializer)
9853 << FirstName << (FirstInit == nullptr)
9854 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9855 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9856 SecondVD->getSourceRange(), VarSingleInitializer)
9857 << SecondName << (SecondInit == nullptr)
9858 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9862 if (FirstInit && SecondInit &&
9863 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9864 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9865 FirstVD->getSourceRange(), VarDifferentInitializer)
9866 << FirstName << FirstInit->getSourceRange();
9867 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9868 SecondVD->getSourceRange(), VarDifferentInitializer)
9869 << SecondName << SecondInit->getSourceRange();
9873 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9874 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9875 if (FirstIsConstexpr != SecondIsConstexpr) {
9876 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9877 FirstVD->getSourceRange(), VarConstexpr)
9878 << FirstName << FirstIsConstexpr;
9879 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9880 SecondVD->getSourceRange(), VarConstexpr)
9881 << SecondName << SecondIsConstexpr;
9887 auto DifferenceSelector = [](Decl *D) {
9888 assert(D && "valid Decl required");
9889 switch (D->getKind()) {
9892 case Decl::AccessSpec:
9893 switch (D->getAccess()) {
9895 return PublicSpecifer;
9897 return PrivateSpecifer;
9899 return ProtectedSpecifer;
9903 llvm_unreachable("Invalid access specifier");
9904 case Decl::StaticAssert:
9905 return StaticAssert;
9908 case Decl::CXXMethod:
9909 case Decl::CXXConstructor:
9910 case Decl::CXXDestructor:
9912 case Decl::TypeAlias:
9920 case Decl::FunctionTemplate:
9921 return FunctionTemplate;
9925 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9926 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9928 const DeclContext *DC) {
9929 for (auto *D : Record->decls()) {
9930 if (!ODRHash::isDeclToBeProcessed(D, DC))
9932 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9937 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9938 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9941 // If there is a diagnoseable difference, FirstDiffType and
9942 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9943 // filled in if not EndOfClass.
9944 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9945 DeclHashes &SecondHashes) {
9947 auto FirstIt = FirstHashes.begin();
9948 auto SecondIt = SecondHashes.begin();
9949 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9950 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9951 FirstIt->second == SecondIt->second) {
9957 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9959 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9962 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9964 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9970 // Use this to diagnose that an unexpected Decl was encountered
9971 // or no difference was detected. This causes a generic error
9972 // message to be emitted.
9973 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9974 StringRef FirstModule,
9975 NamedDecl *SecondRecord,
9976 StringRef SecondModule) {
9977 Diag(FirstRecord->getLocation(),
9978 diag::err_module_odr_violation_different_definitions)
9979 << FirstRecord << FirstModule.empty() << FirstModule;
9982 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9983 << FirstRecord << DR.FirstDecl->getSourceRange();
9986 Diag(SecondRecord->getLocation(),
9987 diag::note_module_odr_violation_different_definitions)
9990 if (DR.SecondDecl) {
9991 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9992 << DR.SecondDecl->getSourceRange();
9996 auto DiagnoseODRMismatch =
9997 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9998 NamedDecl *SecondRecord, StringRef SecondModule) {
9999 SourceLocation FirstLoc;
10000 SourceRange FirstRange;
10001 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10002 if (DR.FirstDiffType == EndOfClass && FirstTag) {
10003 FirstLoc = FirstTag->getBraceRange().getEnd();
10005 FirstLoc = DR.FirstDecl->getLocation();
10006 FirstRange = DR.FirstDecl->getSourceRange();
10008 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10009 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10010 << DR.FirstDiffType;
10012 SourceLocation SecondLoc;
10013 SourceRange SecondRange;
10014 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10015 if (DR.SecondDiffType == EndOfClass && SecondTag) {
10016 SecondLoc = SecondTag->getBraceRange().getEnd();
10018 SecondLoc = DR.SecondDecl->getLocation();
10019 SecondRange = DR.SecondDecl->getSourceRange();
10021 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10022 << SecondModule << SecondRange << DR.SecondDiffType;
10025 // Issue any pending ODR-failure diagnostics.
10026 for (auto &Merge : OdrMergeFailures) {
10027 // If we've already pointed out a specific problem with this class, don't
10028 // bother issuing a general "something's different" diagnostic.
10029 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10032 bool Diagnosed = false;
10033 CXXRecordDecl *FirstRecord = Merge.first;
10034 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10035 for (auto &RecordPair : Merge.second) {
10036 CXXRecordDecl *SecondRecord = RecordPair.first;
10037 // Multiple different declarations got merged together; tell the user
10038 // where they came from.
10039 if (FirstRecord == SecondRecord)
10042 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10044 auto *FirstDD = FirstRecord->DefinitionData;
10045 auto *SecondDD = RecordPair.second;
10047 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10049 // Diagnostics from DefinitionData are emitted here.
10050 if (FirstDD != SecondDD) {
10051 enum ODRDefinitionDataDifference {
10058 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10059 this](SourceLocation Loc, SourceRange Range,
10060 ODRDefinitionDataDifference DiffType) {
10061 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10062 << FirstRecord << FirstModule.empty() << FirstModule << Range
10065 auto ODRDiagBaseNote = [&SecondModule,
10066 this](SourceLocation Loc, SourceRange Range,
10067 ODRDefinitionDataDifference DiffType) {
10068 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10069 << SecondModule << Range << DiffType;
10072 unsigned FirstNumBases = FirstDD->NumBases;
10073 unsigned FirstNumVBases = FirstDD->NumVBases;
10074 unsigned SecondNumBases = SecondDD->NumBases;
10075 unsigned SecondNumVBases = SecondDD->NumVBases;
10077 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10078 unsigned NumBases = DD->NumBases;
10079 if (NumBases == 0) return SourceRange();
10080 auto bases = DD->bases();
10081 return SourceRange(bases[0].getBeginLoc(),
10082 bases[NumBases - 1].getEndLoc());
10085 if (FirstNumBases != SecondNumBases) {
10086 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10089 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10096 if (FirstNumVBases != SecondNumVBases) {
10097 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10100 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10102 << SecondNumVBases;
10107 auto FirstBases = FirstDD->bases();
10108 auto SecondBases = SecondDD->bases();
10110 for (i = 0; i < FirstNumBases; ++i) {
10111 auto FirstBase = FirstBases[i];
10112 auto SecondBase = SecondBases[i];
10113 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10114 ComputeQualTypeODRHash(SecondBase.getType())) {
10115 ODRDiagBaseError(FirstRecord->getLocation(),
10116 FirstBase.getSourceRange(), BaseType)
10117 << (i + 1) << FirstBase.getType();
10118 ODRDiagBaseNote(SecondRecord->getLocation(),
10119 SecondBase.getSourceRange(), BaseType)
10120 << (i + 1) << SecondBase.getType();
10124 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10125 ODRDiagBaseError(FirstRecord->getLocation(),
10126 FirstBase.getSourceRange(), BaseVirtual)
10127 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10128 ODRDiagBaseNote(SecondRecord->getLocation(),
10129 SecondBase.getSourceRange(), BaseVirtual)
10130 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10134 if (FirstBase.getAccessSpecifierAsWritten() !=
10135 SecondBase.getAccessSpecifierAsWritten()) {
10136 ODRDiagBaseError(FirstRecord->getLocation(),
10137 FirstBase.getSourceRange(), BaseAccess)
10138 << (i + 1) << FirstBase.getType()
10139 << (int)FirstBase.getAccessSpecifierAsWritten();
10140 ODRDiagBaseNote(SecondRecord->getLocation(),
10141 SecondBase.getSourceRange(), BaseAccess)
10142 << (i + 1) << SecondBase.getType()
10143 << (int)SecondBase.getAccessSpecifierAsWritten();
10148 if (i != FirstNumBases) {
10154 const ClassTemplateDecl *FirstTemplate =
10155 FirstRecord->getDescribedClassTemplate();
10156 const ClassTemplateDecl *SecondTemplate =
10157 SecondRecord->getDescribedClassTemplate();
10159 assert(!FirstTemplate == !SecondTemplate &&
10160 "Both pointers should be null or non-null");
10162 enum ODRTemplateDifference {
10165 ParamSingleDefaultArgument,
10166 ParamDifferentDefaultArgument,
10169 if (FirstTemplate && SecondTemplate) {
10170 DeclHashes FirstTemplateHashes;
10171 DeclHashes SecondTemplateHashes;
10173 auto PopulateTemplateParameterHashs =
10174 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10175 const ClassTemplateDecl *TD) {
10176 for (auto *D : TD->getTemplateParameters()->asArray()) {
10177 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10181 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10182 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10184 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10185 "Number of template parameters should be equal.");
10187 auto FirstIt = FirstTemplateHashes.begin();
10188 auto FirstEnd = FirstTemplateHashes.end();
10189 auto SecondIt = SecondTemplateHashes.begin();
10190 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10191 if (FirstIt->second == SecondIt->second)
10194 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10195 SourceLocation Loc, SourceRange Range,
10196 ODRTemplateDifference DiffType) {
10197 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10198 << FirstRecord << FirstModule.empty() << FirstModule << Range
10201 auto ODRDiagTemplateNote = [&SecondModule, this](
10202 SourceLocation Loc, SourceRange Range,
10203 ODRTemplateDifference DiffType) {
10204 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10205 << SecondModule << Range << DiffType;
10208 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10209 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10211 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10212 "Parameter Decl's should be the same kind.");
10214 DeclarationName FirstName = FirstDecl->getDeclName();
10215 DeclarationName SecondName = SecondDecl->getDeclName();
10217 if (FirstName != SecondName) {
10218 const bool FirstNameEmpty =
10219 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10220 const bool SecondNameEmpty =
10221 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10222 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10223 "Both template parameters cannot be unnamed.");
10224 ODRDiagTemplateError(FirstDecl->getLocation(),
10225 FirstDecl->getSourceRange(),
10226 FirstNameEmpty ? ParamEmptyName : ParamName)
10228 ODRDiagTemplateNote(SecondDecl->getLocation(),
10229 SecondDecl->getSourceRange(),
10230 SecondNameEmpty ? ParamEmptyName : ParamName)
10235 switch (FirstDecl->getKind()) {
10237 llvm_unreachable("Invalid template parameter type.");
10238 case Decl::TemplateTypeParm: {
10239 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10240 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10241 const bool HasFirstDefaultArgument =
10242 FirstParam->hasDefaultArgument() &&
10243 !FirstParam->defaultArgumentWasInherited();
10244 const bool HasSecondDefaultArgument =
10245 SecondParam->hasDefaultArgument() &&
10246 !SecondParam->defaultArgumentWasInherited();
10248 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10249 ODRDiagTemplateError(FirstDecl->getLocation(),
10250 FirstDecl->getSourceRange(),
10251 ParamSingleDefaultArgument)
10252 << HasFirstDefaultArgument;
10253 ODRDiagTemplateNote(SecondDecl->getLocation(),
10254 SecondDecl->getSourceRange(),
10255 ParamSingleDefaultArgument)
10256 << HasSecondDefaultArgument;
10260 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10261 "Expecting default arguments.");
10263 ODRDiagTemplateError(FirstDecl->getLocation(),
10264 FirstDecl->getSourceRange(),
10265 ParamDifferentDefaultArgument);
10266 ODRDiagTemplateNote(SecondDecl->getLocation(),
10267 SecondDecl->getSourceRange(),
10268 ParamDifferentDefaultArgument);
10272 case Decl::NonTypeTemplateParm: {
10273 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10274 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10275 const bool HasFirstDefaultArgument =
10276 FirstParam->hasDefaultArgument() &&
10277 !FirstParam->defaultArgumentWasInherited();
10278 const bool HasSecondDefaultArgument =
10279 SecondParam->hasDefaultArgument() &&
10280 !SecondParam->defaultArgumentWasInherited();
10282 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10283 ODRDiagTemplateError(FirstDecl->getLocation(),
10284 FirstDecl->getSourceRange(),
10285 ParamSingleDefaultArgument)
10286 << HasFirstDefaultArgument;
10287 ODRDiagTemplateNote(SecondDecl->getLocation(),
10288 SecondDecl->getSourceRange(),
10289 ParamSingleDefaultArgument)
10290 << HasSecondDefaultArgument;
10294 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10295 "Expecting default arguments.");
10297 ODRDiagTemplateError(FirstDecl->getLocation(),
10298 FirstDecl->getSourceRange(),
10299 ParamDifferentDefaultArgument);
10300 ODRDiagTemplateNote(SecondDecl->getLocation(),
10301 SecondDecl->getSourceRange(),
10302 ParamDifferentDefaultArgument);
10306 case Decl::TemplateTemplateParm: {
10307 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10308 const auto *SecondParam =
10309 cast<TemplateTemplateParmDecl>(SecondDecl);
10310 const bool HasFirstDefaultArgument =
10311 FirstParam->hasDefaultArgument() &&
10312 !FirstParam->defaultArgumentWasInherited();
10313 const bool HasSecondDefaultArgument =
10314 SecondParam->hasDefaultArgument() &&
10315 !SecondParam->defaultArgumentWasInherited();
10317 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10318 ODRDiagTemplateError(FirstDecl->getLocation(),
10319 FirstDecl->getSourceRange(),
10320 ParamSingleDefaultArgument)
10321 << HasFirstDefaultArgument;
10322 ODRDiagTemplateNote(SecondDecl->getLocation(),
10323 SecondDecl->getSourceRange(),
10324 ParamSingleDefaultArgument)
10325 << HasSecondDefaultArgument;
10329 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10330 "Expecting default arguments.");
10332 ODRDiagTemplateError(FirstDecl->getLocation(),
10333 FirstDecl->getSourceRange(),
10334 ParamDifferentDefaultArgument);
10335 ODRDiagTemplateNote(SecondDecl->getLocation(),
10336 SecondDecl->getSourceRange(),
10337 ParamDifferentDefaultArgument);
10346 if (FirstIt != FirstEnd) {
10352 DeclHashes FirstHashes;
10353 DeclHashes SecondHashes;
10354 const DeclContext *DC = FirstRecord;
10355 PopulateHashes(FirstHashes, FirstRecord, DC);
10356 PopulateHashes(SecondHashes, SecondRecord, DC);
10358 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10359 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10360 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10361 Decl *FirstDecl = DR.FirstDecl;
10362 Decl *SecondDecl = DR.SecondDecl;
10364 if (FirstDiffType == Other || SecondDiffType == Other) {
10365 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10371 if (FirstDiffType != SecondDiffType) {
10372 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10378 assert(FirstDiffType == SecondDiffType);
10380 switch (FirstDiffType) {
10383 case PublicSpecifer:
10384 case PrivateSpecifer:
10385 case ProtectedSpecifer:
10386 llvm_unreachable("Invalid diff type");
10388 case StaticAssert: {
10389 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10390 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10392 Expr *FirstExpr = FirstSA->getAssertExpr();
10393 Expr *SecondExpr = SecondSA->getAssertExpr();
10394 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10395 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10396 if (FirstODRHash != SecondODRHash) {
10397 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10398 FirstExpr->getSourceRange(), StaticAssertCondition);
10399 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10400 SecondExpr->getSourceRange(), StaticAssertCondition);
10405 StringLiteral *FirstStr = FirstSA->getMessage();
10406 StringLiteral *SecondStr = SecondSA->getMessage();
10407 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10408 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10409 SourceLocation FirstLoc, SecondLoc;
10410 SourceRange FirstRange, SecondRange;
10412 FirstLoc = FirstStr->getBeginLoc();
10413 FirstRange = FirstStr->getSourceRange();
10415 FirstLoc = FirstSA->getBeginLoc();
10416 FirstRange = FirstSA->getSourceRange();
10419 SecondLoc = SecondStr->getBeginLoc();
10420 SecondRange = SecondStr->getSourceRange();
10422 SecondLoc = SecondSA->getBeginLoc();
10423 SecondRange = SecondSA->getSourceRange();
10425 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10426 StaticAssertOnlyMessage)
10427 << (FirstStr == nullptr);
10428 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10429 StaticAssertOnlyMessage)
10430 << (SecondStr == nullptr);
10435 if (FirstStr && SecondStr &&
10436 FirstStr->getString() != SecondStr->getString()) {
10437 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10438 FirstStr->getSourceRange(), StaticAssertMessage);
10439 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10440 SecondStr->getSourceRange(), StaticAssertMessage);
10447 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10448 cast<FieldDecl>(FirstDecl),
10449 cast<FieldDecl>(SecondDecl));
10459 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10460 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10461 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10464 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10465 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10466 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10467 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10468 auto FirstName = FirstMethod->getDeclName();
10469 auto SecondName = SecondMethod->getDeclName();
10470 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10471 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10472 FirstMethod->getSourceRange(), MethodName)
10473 << FirstMethodType << FirstName;
10474 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10475 SecondMethod->getSourceRange(), MethodName)
10476 << SecondMethodType << SecondName;
10482 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10483 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10484 if (FirstDeleted != SecondDeleted) {
10485 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10486 FirstMethod->getSourceRange(), MethodDeleted)
10487 << FirstMethodType << FirstName << FirstDeleted;
10489 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10490 SecondMethod->getSourceRange(), MethodDeleted)
10491 << SecondMethodType << SecondName << SecondDeleted;
10496 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10497 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10498 if (FirstDefaulted != SecondDefaulted) {
10499 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10500 FirstMethod->getSourceRange(), MethodDefaulted)
10501 << FirstMethodType << FirstName << FirstDefaulted;
10503 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10504 SecondMethod->getSourceRange(), MethodDefaulted)
10505 << SecondMethodType << SecondName << SecondDefaulted;
10510 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10511 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10512 const bool FirstPure = FirstMethod->isPure();
10513 const bool SecondPure = SecondMethod->isPure();
10514 if ((FirstVirtual || SecondVirtual) &&
10515 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10516 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10517 FirstMethod->getSourceRange(), MethodVirtual)
10518 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10519 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10520 SecondMethod->getSourceRange(), MethodVirtual)
10521 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10526 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10527 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10528 // class needs to be checked instead.
10529 const auto FirstStorage = FirstMethod->getStorageClass();
10530 const auto SecondStorage = SecondMethod->getStorageClass();
10531 const bool FirstStatic = FirstStorage == SC_Static;
10532 const bool SecondStatic = SecondStorage == SC_Static;
10533 if (FirstStatic != SecondStatic) {
10534 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10535 FirstMethod->getSourceRange(), MethodStatic)
10536 << FirstMethodType << FirstName << FirstStatic;
10537 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10538 SecondMethod->getSourceRange(), MethodStatic)
10539 << SecondMethodType << SecondName << SecondStatic;
10544 const bool FirstVolatile = FirstMethod->isVolatile();
10545 const bool SecondVolatile = SecondMethod->isVolatile();
10546 if (FirstVolatile != SecondVolatile) {
10547 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10548 FirstMethod->getSourceRange(), MethodVolatile)
10549 << FirstMethodType << FirstName << FirstVolatile;
10550 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10551 SecondMethod->getSourceRange(), MethodVolatile)
10552 << SecondMethodType << SecondName << SecondVolatile;
10557 const bool FirstConst = FirstMethod->isConst();
10558 const bool SecondConst = SecondMethod->isConst();
10559 if (FirstConst != SecondConst) {
10560 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10561 FirstMethod->getSourceRange(), MethodConst)
10562 << FirstMethodType << FirstName << FirstConst;
10563 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10564 SecondMethod->getSourceRange(), MethodConst)
10565 << SecondMethodType << SecondName << SecondConst;
10570 const bool FirstInline = FirstMethod->isInlineSpecified();
10571 const bool SecondInline = SecondMethod->isInlineSpecified();
10572 if (FirstInline != SecondInline) {
10573 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10574 FirstMethod->getSourceRange(), MethodInline)
10575 << FirstMethodType << FirstName << FirstInline;
10576 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10577 SecondMethod->getSourceRange(), MethodInline)
10578 << SecondMethodType << SecondName << SecondInline;
10583 const unsigned FirstNumParameters = FirstMethod->param_size();
10584 const unsigned SecondNumParameters = SecondMethod->param_size();
10585 if (FirstNumParameters != SecondNumParameters) {
10586 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10587 FirstMethod->getSourceRange(),
10588 MethodNumberParameters)
10589 << FirstMethodType << FirstName << FirstNumParameters;
10590 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10591 SecondMethod->getSourceRange(),
10592 MethodNumberParameters)
10593 << SecondMethodType << SecondName << SecondNumParameters;
10598 // Need this status boolean to know when break out of the switch.
10599 bool ParameterMismatch = false;
10600 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10601 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10602 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10604 QualType FirstParamType = FirstParam->getType();
10605 QualType SecondParamType = SecondParam->getType();
10606 if (FirstParamType != SecondParamType &&
10607 ComputeQualTypeODRHash(FirstParamType) !=
10608 ComputeQualTypeODRHash(SecondParamType)) {
10609 if (const DecayedType *ParamDecayedType =
10610 FirstParamType->getAs<DecayedType>()) {
10612 FirstRecord, FirstModule, FirstMethod->getLocation(),
10613 FirstMethod->getSourceRange(), MethodParameterType)
10614 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10615 << true << ParamDecayedType->getOriginalType();
10618 FirstRecord, FirstModule, FirstMethod->getLocation(),
10619 FirstMethod->getSourceRange(), MethodParameterType)
10620 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10624 if (const DecayedType *ParamDecayedType =
10625 SecondParamType->getAs<DecayedType>()) {
10626 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10627 SecondMethod->getSourceRange(),
10628 MethodParameterType)
10629 << SecondMethodType << SecondName << (I + 1)
10630 << SecondParamType << true
10631 << ParamDecayedType->getOriginalType();
10633 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10634 SecondMethod->getSourceRange(),
10635 MethodParameterType)
10636 << SecondMethodType << SecondName << (I + 1)
10637 << SecondParamType << false;
10639 ParameterMismatch = true;
10643 DeclarationName FirstParamName = FirstParam->getDeclName();
10644 DeclarationName SecondParamName = SecondParam->getDeclName();
10645 if (FirstParamName != SecondParamName) {
10646 ODRDiagDeclError(FirstRecord, FirstModule,
10647 FirstMethod->getLocation(),
10648 FirstMethod->getSourceRange(), MethodParameterName)
10649 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10650 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10651 SecondMethod->getSourceRange(), MethodParameterName)
10652 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10653 ParameterMismatch = true;
10657 const Expr *FirstInit = FirstParam->getInit();
10658 const Expr *SecondInit = SecondParam->getInit();
10659 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10660 ODRDiagDeclError(FirstRecord, FirstModule,
10661 FirstMethod->getLocation(),
10662 FirstMethod->getSourceRange(),
10663 MethodParameterSingleDefaultArgument)
10664 << FirstMethodType << FirstName << (I + 1)
10665 << (FirstInit == nullptr)
10666 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10667 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10668 SecondMethod->getSourceRange(),
10669 MethodParameterSingleDefaultArgument)
10670 << SecondMethodType << SecondName << (I + 1)
10671 << (SecondInit == nullptr)
10672 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10673 ParameterMismatch = true;
10677 if (FirstInit && SecondInit &&
10678 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10679 ODRDiagDeclError(FirstRecord, FirstModule,
10680 FirstMethod->getLocation(),
10681 FirstMethod->getSourceRange(),
10682 MethodParameterDifferentDefaultArgument)
10683 << FirstMethodType << FirstName << (I + 1)
10684 << FirstInit->getSourceRange();
10685 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10686 SecondMethod->getSourceRange(),
10687 MethodParameterDifferentDefaultArgument)
10688 << SecondMethodType << SecondName << (I + 1)
10689 << SecondInit->getSourceRange();
10690 ParameterMismatch = true;
10696 if (ParameterMismatch) {
10701 const auto *FirstTemplateArgs =
10702 FirstMethod->getTemplateSpecializationArgs();
10703 const auto *SecondTemplateArgs =
10704 SecondMethod->getTemplateSpecializationArgs();
10706 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10707 (!FirstTemplateArgs && SecondTemplateArgs)) {
10708 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10709 FirstMethod->getSourceRange(),
10710 MethodNoTemplateArguments)
10711 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10712 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10713 SecondMethod->getSourceRange(),
10714 MethodNoTemplateArguments)
10715 << SecondMethodType << SecondName
10716 << (SecondTemplateArgs != nullptr);
10722 if (FirstTemplateArgs && SecondTemplateArgs) {
10723 // Remove pack expansions from argument list.
10724 auto ExpandTemplateArgumentList =
10725 [](const TemplateArgumentList *TAL) {
10726 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10727 for (const TemplateArgument &TA : TAL->asArray()) {
10728 if (TA.getKind() != TemplateArgument::Pack) {
10729 ExpandedList.push_back(&TA);
10732 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10733 ExpandedList.push_back(&PackTA);
10736 return ExpandedList;
10738 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10739 ExpandTemplateArgumentList(FirstTemplateArgs);
10740 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10741 ExpandTemplateArgumentList(SecondTemplateArgs);
10743 if (FirstExpandedList.size() != SecondExpandedList.size()) {
10744 ODRDiagDeclError(FirstRecord, FirstModule,
10745 FirstMethod->getLocation(),
10746 FirstMethod->getSourceRange(),
10747 MethodDifferentNumberTemplateArguments)
10748 << FirstMethodType << FirstName
10749 << (unsigned)FirstExpandedList.size();
10750 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10751 SecondMethod->getSourceRange(),
10752 MethodDifferentNumberTemplateArguments)
10753 << SecondMethodType << SecondName
10754 << (unsigned)SecondExpandedList.size();
10760 bool TemplateArgumentMismatch = false;
10761 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10762 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10763 &SecondTA = *SecondExpandedList[i];
10764 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10765 ComputeTemplateArgumentODRHash(SecondTA)) {
10770 FirstRecord, FirstModule, FirstMethod->getLocation(),
10771 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10772 << FirstMethodType << FirstName << FirstTA << i + 1;
10773 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10774 SecondMethod->getSourceRange(),
10775 MethodDifferentTemplateArgument)
10776 << SecondMethodType << SecondName << SecondTA << i + 1;
10778 TemplateArgumentMismatch = true;
10782 if (TemplateArgumentMismatch) {
10788 // Compute the hash of the method as if it has no body.
10789 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10791 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10792 return Hash.CalculateHash();
10795 // Compare the hash generated to the hash stored. A difference means
10796 // that a body was present in the original source. Due to merging,
10797 // the stardard way of detecting a body will not work.
10798 const bool HasFirstBody =
10799 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10800 const bool HasSecondBody =
10801 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10803 if (HasFirstBody != HasSecondBody) {
10804 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10805 FirstMethod->getSourceRange(), MethodSingleBody)
10806 << FirstMethodType << FirstName << HasFirstBody;
10807 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10808 SecondMethod->getSourceRange(), MethodSingleBody)
10809 << SecondMethodType << SecondName << HasSecondBody;
10814 if (HasFirstBody && HasSecondBody) {
10815 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10816 FirstMethod->getSourceRange(), MethodDifferentBody)
10817 << FirstMethodType << FirstName;
10818 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10819 SecondMethod->getSourceRange(), MethodDifferentBody)
10820 << SecondMethodType << SecondName;
10829 Diagnosed = ODRDiagTypeDefOrAlias(
10830 FirstRecord, FirstModule, SecondModule,
10831 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10832 FirstDiffType == TypeAlias);
10837 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10838 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10842 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10843 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10845 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10846 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10848 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10849 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10851 if (FirstND && SecondND) {
10852 ODRDiagDeclError(FirstRecord, FirstModule,
10853 FirstFriend->getFriendLoc(),
10854 FirstFriend->getSourceRange(), FriendFunction)
10856 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10857 SecondFriend->getSourceRange(), FriendFunction)
10864 if (FirstTSI && SecondTSI) {
10865 QualType FirstFriendType = FirstTSI->getType();
10866 QualType SecondFriendType = SecondTSI->getType();
10867 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10868 ComputeQualTypeODRHash(SecondFriendType));
10869 ODRDiagDeclError(FirstRecord, FirstModule,
10870 FirstFriend->getFriendLoc(),
10871 FirstFriend->getSourceRange(), FriendType)
10872 << FirstFriendType;
10873 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10874 SecondFriend->getSourceRange(), FriendType)
10875 << SecondFriendType;
10880 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10881 FirstFriend->getSourceRange(), FriendTypeFunction)
10882 << (FirstTSI == nullptr);
10883 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10884 SecondFriend->getSourceRange(), FriendTypeFunction)
10885 << (SecondTSI == nullptr);
10890 case FunctionTemplate: {
10891 FunctionTemplateDecl *FirstTemplate =
10892 cast<FunctionTemplateDecl>(FirstDecl);
10893 FunctionTemplateDecl *SecondTemplate =
10894 cast<FunctionTemplateDecl>(SecondDecl);
10896 TemplateParameterList *FirstTPL =
10897 FirstTemplate->getTemplateParameters();
10898 TemplateParameterList *SecondTPL =
10899 SecondTemplate->getTemplateParameters();
10901 if (FirstTPL->size() != SecondTPL->size()) {
10902 ODRDiagDeclError(FirstRecord, FirstModule,
10903 FirstTemplate->getLocation(),
10904 FirstTemplate->getSourceRange(),
10905 FunctionTemplateDifferentNumberParameters)
10906 << FirstTemplate << FirstTPL->size();
10907 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10908 SecondTemplate->getSourceRange(),
10909 FunctionTemplateDifferentNumberParameters)
10910 << SecondTemplate << SecondTPL->size();
10916 bool ParameterMismatch = false;
10917 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10918 NamedDecl *FirstParam = FirstTPL->getParam(i);
10919 NamedDecl *SecondParam = SecondTPL->getParam(i);
10921 if (FirstParam->getKind() != SecondParam->getKind()) {
10923 TemplateTypeParameter,
10924 NonTypeTemplateParameter,
10925 TemplateTemplateParameter,
10927 auto GetParamType = [](NamedDecl *D) {
10928 switch (D->getKind()) {
10930 llvm_unreachable("Unexpected template parameter type");
10931 case Decl::TemplateTypeParm:
10932 return TemplateTypeParameter;
10933 case Decl::NonTypeTemplateParm:
10934 return NonTypeTemplateParameter;
10935 case Decl::TemplateTemplateParm:
10936 return TemplateTemplateParameter;
10940 ODRDiagDeclError(FirstRecord, FirstModule,
10941 FirstTemplate->getLocation(),
10942 FirstTemplate->getSourceRange(),
10943 FunctionTemplateParameterDifferentKind)
10944 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10945 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10946 SecondTemplate->getSourceRange(),
10947 FunctionTemplateParameterDifferentKind)
10948 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10950 ParameterMismatch = true;
10954 if (FirstParam->getName() != SecondParam->getName()) {
10956 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10957 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10958 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10960 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10961 SecondTemplate->getSourceRange(),
10962 FunctionTemplateParameterName)
10963 << SecondTemplate << (i + 1)
10964 << (bool)SecondParam->getIdentifier() << SecondParam;
10965 ParameterMismatch = true;
10969 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10970 isa<TemplateTypeParmDecl>(SecondParam)) {
10971 TemplateTypeParmDecl *FirstTTPD =
10972 cast<TemplateTypeParmDecl>(FirstParam);
10973 TemplateTypeParmDecl *SecondTTPD =
10974 cast<TemplateTypeParmDecl>(SecondParam);
10975 bool HasFirstDefaultArgument =
10976 FirstTTPD->hasDefaultArgument() &&
10977 !FirstTTPD->defaultArgumentWasInherited();
10978 bool HasSecondDefaultArgument =
10979 SecondTTPD->hasDefaultArgument() &&
10980 !SecondTTPD->defaultArgumentWasInherited();
10981 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10982 ODRDiagDeclError(FirstRecord, FirstModule,
10983 FirstTemplate->getLocation(),
10984 FirstTemplate->getSourceRange(),
10985 FunctionTemplateParameterSingleDefaultArgument)
10986 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10987 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10988 SecondTemplate->getSourceRange(),
10989 FunctionTemplateParameterSingleDefaultArgument)
10990 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10991 ParameterMismatch = true;
10995 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10996 QualType FirstType = FirstTTPD->getDefaultArgument();
10997 QualType SecondType = SecondTTPD->getDefaultArgument();
10998 if (ComputeQualTypeODRHash(FirstType) !=
10999 ComputeQualTypeODRHash(SecondType)) {
11001 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11002 FirstTemplate->getSourceRange(),
11003 FunctionTemplateParameterDifferentDefaultArgument)
11004 << FirstTemplate << (i + 1) << FirstType;
11006 SecondModule, SecondTemplate->getLocation(),
11007 SecondTemplate->getSourceRange(),
11008 FunctionTemplateParameterDifferentDefaultArgument)
11009 << SecondTemplate << (i + 1) << SecondType;
11010 ParameterMismatch = true;
11015 if (FirstTTPD->isParameterPack() !=
11016 SecondTTPD->isParameterPack()) {
11017 ODRDiagDeclError(FirstRecord, FirstModule,
11018 FirstTemplate->getLocation(),
11019 FirstTemplate->getSourceRange(),
11020 FunctionTemplatePackParameter)
11021 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11022 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11023 SecondTemplate->getSourceRange(),
11024 FunctionTemplatePackParameter)
11025 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11026 ParameterMismatch = true;
11031 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11032 isa<TemplateTemplateParmDecl>(SecondParam)) {
11033 TemplateTemplateParmDecl *FirstTTPD =
11034 cast<TemplateTemplateParmDecl>(FirstParam);
11035 TemplateTemplateParmDecl *SecondTTPD =
11036 cast<TemplateTemplateParmDecl>(SecondParam);
11038 TemplateParameterList *FirstTPL =
11039 FirstTTPD->getTemplateParameters();
11040 TemplateParameterList *SecondTPL =
11041 SecondTTPD->getTemplateParameters();
11043 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11044 ComputeTemplateParameterListODRHash(SecondTPL)) {
11045 ODRDiagDeclError(FirstRecord, FirstModule,
11046 FirstTemplate->getLocation(),
11047 FirstTemplate->getSourceRange(),
11048 FunctionTemplateParameterDifferentType)
11049 << FirstTemplate << (i + 1);
11050 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11051 SecondTemplate->getSourceRange(),
11052 FunctionTemplateParameterDifferentType)
11053 << SecondTemplate << (i + 1);
11054 ParameterMismatch = true;
11058 bool HasFirstDefaultArgument =
11059 FirstTTPD->hasDefaultArgument() &&
11060 !FirstTTPD->defaultArgumentWasInherited();
11061 bool HasSecondDefaultArgument =
11062 SecondTTPD->hasDefaultArgument() &&
11063 !SecondTTPD->defaultArgumentWasInherited();
11064 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11065 ODRDiagDeclError(FirstRecord, FirstModule,
11066 FirstTemplate->getLocation(),
11067 FirstTemplate->getSourceRange(),
11068 FunctionTemplateParameterSingleDefaultArgument)
11069 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11070 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11071 SecondTemplate->getSourceRange(),
11072 FunctionTemplateParameterSingleDefaultArgument)
11073 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11074 ParameterMismatch = true;
11078 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11079 TemplateArgument FirstTA =
11080 FirstTTPD->getDefaultArgument().getArgument();
11081 TemplateArgument SecondTA =
11082 SecondTTPD->getDefaultArgument().getArgument();
11083 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11084 ComputeTemplateArgumentODRHash(SecondTA)) {
11086 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11087 FirstTemplate->getSourceRange(),
11088 FunctionTemplateParameterDifferentDefaultArgument)
11089 << FirstTemplate << (i + 1) << FirstTA;
11091 SecondModule, SecondTemplate->getLocation(),
11092 SecondTemplate->getSourceRange(),
11093 FunctionTemplateParameterDifferentDefaultArgument)
11094 << SecondTemplate << (i + 1) << SecondTA;
11095 ParameterMismatch = true;
11100 if (FirstTTPD->isParameterPack() !=
11101 SecondTTPD->isParameterPack()) {
11102 ODRDiagDeclError(FirstRecord, FirstModule,
11103 FirstTemplate->getLocation(),
11104 FirstTemplate->getSourceRange(),
11105 FunctionTemplatePackParameter)
11106 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11107 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11108 SecondTemplate->getSourceRange(),
11109 FunctionTemplatePackParameter)
11110 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11111 ParameterMismatch = true;
11116 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11117 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11118 NonTypeTemplateParmDecl *FirstNTTPD =
11119 cast<NonTypeTemplateParmDecl>(FirstParam);
11120 NonTypeTemplateParmDecl *SecondNTTPD =
11121 cast<NonTypeTemplateParmDecl>(SecondParam);
11123 QualType FirstType = FirstNTTPD->getType();
11124 QualType SecondType = SecondNTTPD->getType();
11125 if (ComputeQualTypeODRHash(FirstType) !=
11126 ComputeQualTypeODRHash(SecondType)) {
11127 ODRDiagDeclError(FirstRecord, FirstModule,
11128 FirstTemplate->getLocation(),
11129 FirstTemplate->getSourceRange(),
11130 FunctionTemplateParameterDifferentType)
11131 << FirstTemplate << (i + 1);
11132 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11133 SecondTemplate->getSourceRange(),
11134 FunctionTemplateParameterDifferentType)
11135 << SecondTemplate << (i + 1);
11136 ParameterMismatch = true;
11140 bool HasFirstDefaultArgument =
11141 FirstNTTPD->hasDefaultArgument() &&
11142 !FirstNTTPD->defaultArgumentWasInherited();
11143 bool HasSecondDefaultArgument =
11144 SecondNTTPD->hasDefaultArgument() &&
11145 !SecondNTTPD->defaultArgumentWasInherited();
11146 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11147 ODRDiagDeclError(FirstRecord, FirstModule,
11148 FirstTemplate->getLocation(),
11149 FirstTemplate->getSourceRange(),
11150 FunctionTemplateParameterSingleDefaultArgument)
11151 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11152 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11153 SecondTemplate->getSourceRange(),
11154 FunctionTemplateParameterSingleDefaultArgument)
11155 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11156 ParameterMismatch = true;
11160 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11161 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11162 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11163 if (ComputeODRHash(FirstDefaultArgument) !=
11164 ComputeODRHash(SecondDefaultArgument)) {
11166 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11167 FirstTemplate->getSourceRange(),
11168 FunctionTemplateParameterDifferentDefaultArgument)
11169 << FirstTemplate << (i + 1) << FirstDefaultArgument;
11171 SecondModule, SecondTemplate->getLocation(),
11172 SecondTemplate->getSourceRange(),
11173 FunctionTemplateParameterDifferentDefaultArgument)
11174 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11175 ParameterMismatch = true;
11180 if (FirstNTTPD->isParameterPack() !=
11181 SecondNTTPD->isParameterPack()) {
11182 ODRDiagDeclError(FirstRecord, FirstModule,
11183 FirstTemplate->getLocation(),
11184 FirstTemplate->getSourceRange(),
11185 FunctionTemplatePackParameter)
11186 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11187 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11188 SecondTemplate->getSourceRange(),
11189 FunctionTemplatePackParameter)
11190 << SecondTemplate << (i + 1)
11191 << SecondNTTPD->isParameterPack();
11192 ParameterMismatch = true;
11198 if (ParameterMismatch) {
11210 Diag(FirstDecl->getLocation(),
11211 diag::err_module_odr_violation_mismatch_decl_unknown)
11212 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11213 << FirstDecl->getSourceRange();
11214 Diag(SecondDecl->getLocation(),
11215 diag::note_module_odr_violation_mismatch_decl_unknown)
11216 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11221 // All definitions are updates to the same declaration. This happens if a
11222 // module instantiates the declaration of a class template specialization
11223 // and two or more other modules instantiate its definition.
11225 // FIXME: Indicate which modules had instantiations of this definition.
11226 // FIXME: How can this even happen?
11227 Diag(Merge.first->getLocation(),
11228 diag::err_module_odr_violation_different_instantiations)
11233 // Issue ODR failures diagnostics for functions.
11234 for (auto &Merge : FunctionOdrMergeFailures) {
11235 enum ODRFunctionDifference {
11239 ParameterSingleDefaultArgument,
11240 ParameterDifferentDefaultArgument,
11244 FunctionDecl *FirstFunction = Merge.first;
11245 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11247 bool Diagnosed = false;
11248 for (auto &SecondFunction : Merge.second) {
11250 if (FirstFunction == SecondFunction)
11253 std::string SecondModule =
11254 getOwningModuleNameForDiagnostic(SecondFunction);
11256 auto ODRDiagError = [FirstFunction, &FirstModule,
11257 this](SourceLocation Loc, SourceRange Range,
11258 ODRFunctionDifference DiffType) {
11259 return Diag(Loc, diag::err_module_odr_violation_function)
11260 << FirstFunction << FirstModule.empty() << FirstModule << Range
11263 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11265 ODRFunctionDifference DiffType) {
11266 return Diag(Loc, diag::note_module_odr_violation_function)
11267 << SecondModule << Range << DiffType;
11270 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11271 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11272 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11273 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11274 << FirstFunction->getReturnType();
11275 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11276 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11277 << SecondFunction->getReturnType();
11282 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11283 "Merged functions with different number of parameters");
11285 auto ParamSize = FirstFunction->param_size();
11286 bool ParameterMismatch = false;
11287 for (unsigned I = 0; I < ParamSize; ++I) {
11288 auto *FirstParam = FirstFunction->getParamDecl(I);
11289 auto *SecondParam = SecondFunction->getParamDecl(I);
11291 assert(getContext().hasSameType(FirstParam->getType(),
11292 SecondParam->getType()) &&
11293 "Merged function has different parameter types.");
11295 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11296 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11298 << I + 1 << FirstParam->getDeclName();
11299 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11301 << I + 1 << SecondParam->getDeclName();
11302 ParameterMismatch = true;
11306 QualType FirstParamType = FirstParam->getType();
11307 QualType SecondParamType = SecondParam->getType();
11308 if (FirstParamType != SecondParamType &&
11309 ComputeQualTypeODRHash(FirstParamType) !=
11310 ComputeQualTypeODRHash(SecondParamType)) {
11311 if (const DecayedType *ParamDecayedType =
11312 FirstParamType->getAs<DecayedType>()) {
11313 ODRDiagError(FirstParam->getLocation(),
11314 FirstParam->getSourceRange(), ParameterType)
11315 << (I + 1) << FirstParamType << true
11316 << ParamDecayedType->getOriginalType();
11318 ODRDiagError(FirstParam->getLocation(),
11319 FirstParam->getSourceRange(), ParameterType)
11320 << (I + 1) << FirstParamType << false;
11323 if (const DecayedType *ParamDecayedType =
11324 SecondParamType->getAs<DecayedType>()) {
11325 ODRDiagNote(SecondParam->getLocation(),
11326 SecondParam->getSourceRange(), ParameterType)
11327 << (I + 1) << SecondParamType << true
11328 << ParamDecayedType->getOriginalType();
11330 ODRDiagNote(SecondParam->getLocation(),
11331 SecondParam->getSourceRange(), ParameterType)
11332 << (I + 1) << SecondParamType << false;
11334 ParameterMismatch = true;
11338 const Expr *FirstInit = FirstParam->getInit();
11339 const Expr *SecondInit = SecondParam->getInit();
11340 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11341 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11342 ParameterSingleDefaultArgument)
11343 << (I + 1) << (FirstInit == nullptr)
11344 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11345 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11346 ParameterSingleDefaultArgument)
11347 << (I + 1) << (SecondInit == nullptr)
11348 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11349 ParameterMismatch = true;
11353 if (FirstInit && SecondInit &&
11354 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11355 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11356 ParameterDifferentDefaultArgument)
11357 << (I + 1) << FirstInit->getSourceRange();
11358 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11359 ParameterDifferentDefaultArgument)
11360 << (I + 1) << SecondInit->getSourceRange();
11361 ParameterMismatch = true;
11365 assert(ComputeSubDeclODRHash(FirstParam) ==
11366 ComputeSubDeclODRHash(SecondParam) &&
11367 "Undiagnosed parameter difference.");
11370 if (ParameterMismatch) {
11375 // If no error has been generated before now, assume the problem is in
11376 // the body and generate a message.
11377 ODRDiagError(FirstFunction->getLocation(),
11378 FirstFunction->getSourceRange(), FunctionBody);
11379 ODRDiagNote(SecondFunction->getLocation(),
11380 SecondFunction->getSourceRange(), FunctionBody);
11385 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11388 // Issue ODR failures diagnostics for enums.
11389 for (auto &Merge : EnumOdrMergeFailures) {
11390 enum ODREnumDifference {
11392 EnumTagKeywordMismatch,
11393 SingleSpecifiedType,
11394 DifferentSpecifiedTypes,
11395 DifferentNumberEnumConstants,
11397 EnumConstantSingleInitilizer,
11398 EnumConstantDifferentInitilizer,
11401 // If we've already pointed out a specific problem with this enum, don't
11402 // bother issuing a general "something's different" diagnostic.
11403 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11406 EnumDecl *FirstEnum = Merge.first;
11407 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11410 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11411 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11412 DeclHashes &Hashes, EnumDecl *Enum) {
11413 for (auto *D : Enum->decls()) {
11414 // Due to decl merging, the first EnumDecl is the parent of
11415 // Decls in both records.
11416 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11418 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11419 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11420 ComputeSubDeclODRHash(D));
11423 DeclHashes FirstHashes;
11424 PopulateHashes(FirstHashes, FirstEnum);
11425 bool Diagnosed = false;
11426 for (auto &SecondEnum : Merge.second) {
11428 if (FirstEnum == SecondEnum)
11431 std::string SecondModule =
11432 getOwningModuleNameForDiagnostic(SecondEnum);
11434 auto ODRDiagError = [FirstEnum, &FirstModule,
11435 this](SourceLocation Loc, SourceRange Range,
11436 ODREnumDifference DiffType) {
11437 return Diag(Loc, diag::err_module_odr_violation_enum)
11438 << FirstEnum << FirstModule.empty() << FirstModule << Range
11441 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11443 ODREnumDifference DiffType) {
11444 return Diag(Loc, diag::note_module_odr_violation_enum)
11445 << SecondModule << Range << DiffType;
11448 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11449 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11451 << FirstEnum->isScoped();
11452 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11454 << SecondEnum->isScoped();
11459 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11460 if (FirstEnum->isScopedUsingClassTag() !=
11461 SecondEnum->isScopedUsingClassTag()) {
11462 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11463 EnumTagKeywordMismatch)
11464 << FirstEnum->isScopedUsingClassTag();
11465 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11466 EnumTagKeywordMismatch)
11467 << SecondEnum->isScopedUsingClassTag();
11473 QualType FirstUnderlyingType =
11474 FirstEnum->getIntegerTypeSourceInfo()
11475 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11477 QualType SecondUnderlyingType =
11478 SecondEnum->getIntegerTypeSourceInfo()
11479 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11481 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11482 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11483 SingleSpecifiedType)
11484 << !FirstUnderlyingType.isNull();
11485 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11486 SingleSpecifiedType)
11487 << !SecondUnderlyingType.isNull();
11492 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11493 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11494 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11495 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11496 DifferentSpecifiedTypes)
11497 << FirstUnderlyingType;
11498 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11499 DifferentSpecifiedTypes)
11500 << SecondUnderlyingType;
11506 DeclHashes SecondHashes;
11507 PopulateHashes(SecondHashes, SecondEnum);
11509 if (FirstHashes.size() != SecondHashes.size()) {
11510 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11511 DifferentNumberEnumConstants)
11512 << (int)FirstHashes.size();
11513 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11514 DifferentNumberEnumConstants)
11515 << (int)SecondHashes.size();
11520 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11521 if (FirstHashes[I].second == SecondHashes[I].second)
11523 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11524 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11526 if (FirstEnumConstant->getDeclName() !=
11527 SecondEnumConstant->getDeclName()) {
11529 ODRDiagError(FirstEnumConstant->getLocation(),
11530 FirstEnumConstant->getSourceRange(), EnumConstantName)
11531 << I + 1 << FirstEnumConstant;
11532 ODRDiagNote(SecondEnumConstant->getLocation(),
11533 SecondEnumConstant->getSourceRange(), EnumConstantName)
11534 << I + 1 << SecondEnumConstant;
11539 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11540 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11541 if (!FirstInit && !SecondInit)
11544 if (!FirstInit || !SecondInit) {
11545 ODRDiagError(FirstEnumConstant->getLocation(),
11546 FirstEnumConstant->getSourceRange(),
11547 EnumConstantSingleInitilizer)
11548 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11549 ODRDiagNote(SecondEnumConstant->getLocation(),
11550 SecondEnumConstant->getSourceRange(),
11551 EnumConstantSingleInitilizer)
11552 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11557 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11558 ODRDiagError(FirstEnumConstant->getLocation(),
11559 FirstEnumConstant->getSourceRange(),
11560 EnumConstantDifferentInitilizer)
11561 << I + 1 << FirstEnumConstant;
11562 ODRDiagNote(SecondEnumConstant->getLocation(),
11563 SecondEnumConstant->getSourceRange(),
11564 EnumConstantDifferentInitilizer)
11565 << I + 1 << SecondEnumConstant;
11573 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11577 void ASTReader::StartedDeserializing() {
11578 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11579 ReadTimer->startTimer();
11582 void ASTReader::FinishedDeserializing() {
11583 assert(NumCurrentElementsDeserializing &&
11584 "FinishedDeserializing not paired with StartedDeserializing");
11585 if (NumCurrentElementsDeserializing == 1) {
11586 // We decrease NumCurrentElementsDeserializing only after pending actions
11587 // are finished, to avoid recursively re-calling finishPendingActions().
11588 finishPendingActions();
11590 --NumCurrentElementsDeserializing;
11592 if (NumCurrentElementsDeserializing == 0) {
11593 // Propagate exception specification and deduced type updates along
11594 // redeclaration chains.
11596 // We do this now rather than in finishPendingActions because we want to
11597 // be able to walk the complete redeclaration chains of the updated decls.
11598 while (!PendingExceptionSpecUpdates.empty() ||
11599 !PendingDeducedTypeUpdates.empty()) {
11600 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11601 PendingExceptionSpecUpdates.clear();
11602 for (auto Update : ESUpdates) {
11603 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11604 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11605 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11606 if (auto *Listener = getContext().getASTMutationListener())
11607 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11608 for (auto *Redecl : Update.second->redecls())
11609 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11612 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11613 PendingDeducedTypeUpdates.clear();
11614 for (auto Update : DTUpdates) {
11615 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11616 // FIXME: If the return type is already deduced, check that it matches.
11617 getContext().adjustDeducedFunctionResultType(Update.first,
11623 ReadTimer->stopTimer();
11625 diagnoseOdrViolations();
11627 // We are not in recursive loading, so it's safe to pass the "interesting"
11628 // decls to the consumer.
11630 PassInterestingDeclsToConsumer();
11634 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11635 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11636 // Remove any fake results before adding any real ones.
11637 auto It = PendingFakeLookupResults.find(II);
11638 if (It != PendingFakeLookupResults.end()) {
11639 for (auto *ND : It->second)
11640 SemaObj->IdResolver.RemoveDecl(ND);
11641 // FIXME: this works around module+PCH performance issue.
11642 // Rather than erase the result from the map, which is O(n), just clear
11643 // the vector of NamedDecls.
11644 It->second.clear();
11648 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11649 SemaObj->TUScope->AddDecl(D);
11650 } else if (SemaObj->TUScope) {
11651 // Adding the decl to IdResolver may have failed because it was already in
11652 // (even though it was not added in scope). If it is already in, make sure
11653 // it gets in the scope as well.
11654 if (std::find(SemaObj->IdResolver.begin(Name),
11655 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11656 SemaObj->TUScope->AddDecl(D);
11660 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11661 ASTContext *Context,
11662 const PCHContainerReader &PCHContainerRdr,
11663 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11664 StringRef isysroot, bool DisableValidation,
11665 bool AllowASTWithCompilerErrors,
11666 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11667 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11668 std::unique_ptr<llvm::Timer> ReadTimer)
11669 : Listener(DisableValidation
11670 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11671 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11672 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11673 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11674 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11675 PCHContainerRdr, PP.getHeaderSearchInfo()),
11676 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11677 DisableValidation(DisableValidation),
11678 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11679 AllowConfigurationMismatch(AllowConfigurationMismatch),
11680 ValidateSystemInputs(ValidateSystemInputs),
11681 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11682 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11683 SourceMgr.setExternalSLocEntrySource(this);
11685 for (const auto &Ext : Extensions) {
11686 auto BlockName = Ext->getExtensionMetadata().BlockName;
11687 auto Known = ModuleFileExtensions.find(BlockName);
11688 if (Known != ModuleFileExtensions.end()) {
11689 Diags.Report(diag::warn_duplicate_module_file_extension)
11694 ModuleFileExtensions.insert({BlockName, Ext});
11698 ASTReader::~ASTReader() {
11699 if (OwnsDeserializationListener)
11700 delete DeserializationListener;
11703 IdentifierResolver &ASTReader::getIdResolver() {
11704 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11707 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11708 unsigned AbbrevID) {
11711 return Cursor.readRecord(AbbrevID, Record);
11713 //===----------------------------------------------------------------------===//
11714 //// OMPClauseReader implementation
11715 ////===----------------------------------------------------------------------===//
11717 // This has to be in namespace clang because it's friended by all
11718 // of the OMP clauses.
11721 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11722 ASTRecordReader &Record;
11723 ASTContext &Context;
11726 OMPClauseReader(ASTRecordReader &Record)
11727 : Record(Record), Context(Record.getContext()) {}
11729 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11730 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11731 OMPClause *readClause();
11732 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11733 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11736 } // end namespace clang
11738 OMPClause *ASTRecordReader::readOMPClause() {
11739 return OMPClauseReader(*this).readClause();
11742 OMPClause *OMPClauseReader::readClause() {
11743 OMPClause *C = nullptr;
11744 switch (llvm::omp::Clause(Record.readInt())) {
11745 case llvm::omp::OMPC_if:
11746 C = new (Context) OMPIfClause();
11748 case llvm::omp::OMPC_final:
11749 C = new (Context) OMPFinalClause();
11751 case llvm::omp::OMPC_num_threads:
11752 C = new (Context) OMPNumThreadsClause();
11754 case llvm::omp::OMPC_safelen:
11755 C = new (Context) OMPSafelenClause();
11757 case llvm::omp::OMPC_simdlen:
11758 C = new (Context) OMPSimdlenClause();
11760 case llvm::omp::OMPC_allocator:
11761 C = new (Context) OMPAllocatorClause();
11763 case llvm::omp::OMPC_collapse:
11764 C = new (Context) OMPCollapseClause();
11766 case llvm::omp::OMPC_default:
11767 C = new (Context) OMPDefaultClause();
11769 case llvm::omp::OMPC_proc_bind:
11770 C = new (Context) OMPProcBindClause();
11772 case llvm::omp::OMPC_schedule:
11773 C = new (Context) OMPScheduleClause();
11775 case llvm::omp::OMPC_ordered:
11776 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11778 case llvm::omp::OMPC_nowait:
11779 C = new (Context) OMPNowaitClause();
11781 case llvm::omp::OMPC_untied:
11782 C = new (Context) OMPUntiedClause();
11784 case llvm::omp::OMPC_mergeable:
11785 C = new (Context) OMPMergeableClause();
11787 case llvm::omp::OMPC_read:
11788 C = new (Context) OMPReadClause();
11790 case llvm::omp::OMPC_write:
11791 C = new (Context) OMPWriteClause();
11793 case llvm::omp::OMPC_update:
11794 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11796 case llvm::omp::OMPC_capture:
11797 C = new (Context) OMPCaptureClause();
11799 case llvm::omp::OMPC_seq_cst:
11800 C = new (Context) OMPSeqCstClause();
11802 case llvm::omp::OMPC_acq_rel:
11803 C = new (Context) OMPAcqRelClause();
11805 case llvm::omp::OMPC_acquire:
11806 C = new (Context) OMPAcquireClause();
11808 case llvm::omp::OMPC_release:
11809 C = new (Context) OMPReleaseClause();
11811 case llvm::omp::OMPC_relaxed:
11812 C = new (Context) OMPRelaxedClause();
11814 case llvm::omp::OMPC_threads:
11815 C = new (Context) OMPThreadsClause();
11817 case llvm::omp::OMPC_simd:
11818 C = new (Context) OMPSIMDClause();
11820 case llvm::omp::OMPC_nogroup:
11821 C = new (Context) OMPNogroupClause();
11823 case llvm::omp::OMPC_unified_address:
11824 C = new (Context) OMPUnifiedAddressClause();
11826 case llvm::omp::OMPC_unified_shared_memory:
11827 C = new (Context) OMPUnifiedSharedMemoryClause();
11829 case llvm::omp::OMPC_reverse_offload:
11830 C = new (Context) OMPReverseOffloadClause();
11832 case llvm::omp::OMPC_dynamic_allocators:
11833 C = new (Context) OMPDynamicAllocatorsClause();
11835 case llvm::omp::OMPC_atomic_default_mem_order:
11836 C = new (Context) OMPAtomicDefaultMemOrderClause();
11838 case llvm::omp::OMPC_private:
11839 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11841 case llvm::omp::OMPC_firstprivate:
11842 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11844 case llvm::omp::OMPC_lastprivate:
11845 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11847 case llvm::omp::OMPC_shared:
11848 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11850 case llvm::omp::OMPC_reduction: {
11851 unsigned N = Record.readInt();
11852 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11853 C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11856 case llvm::omp::OMPC_task_reduction:
11857 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11859 case llvm::omp::OMPC_in_reduction:
11860 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11862 case llvm::omp::OMPC_linear:
11863 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11865 case llvm::omp::OMPC_aligned:
11866 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11868 case llvm::omp::OMPC_copyin:
11869 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11871 case llvm::omp::OMPC_copyprivate:
11872 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11874 case llvm::omp::OMPC_flush:
11875 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11877 case llvm::omp::OMPC_depobj:
11878 C = OMPDepobjClause::CreateEmpty(Context);
11880 case llvm::omp::OMPC_depend: {
11881 unsigned NumVars = Record.readInt();
11882 unsigned NumLoops = Record.readInt();
11883 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11886 case llvm::omp::OMPC_device:
11887 C = new (Context) OMPDeviceClause();
11889 case llvm::omp::OMPC_map: {
11890 OMPMappableExprListSizeTy Sizes;
11891 Sizes.NumVars = Record.readInt();
11892 Sizes.NumUniqueDeclarations = Record.readInt();
11893 Sizes.NumComponentLists = Record.readInt();
11894 Sizes.NumComponents = Record.readInt();
11895 C = OMPMapClause::CreateEmpty(Context, Sizes);
11898 case llvm::omp::OMPC_num_teams:
11899 C = new (Context) OMPNumTeamsClause();
11901 case llvm::omp::OMPC_thread_limit:
11902 C = new (Context) OMPThreadLimitClause();
11904 case llvm::omp::OMPC_priority:
11905 C = new (Context) OMPPriorityClause();
11907 case llvm::omp::OMPC_grainsize:
11908 C = new (Context) OMPGrainsizeClause();
11910 case llvm::omp::OMPC_num_tasks:
11911 C = new (Context) OMPNumTasksClause();
11913 case llvm::omp::OMPC_hint:
11914 C = new (Context) OMPHintClause();
11916 case llvm::omp::OMPC_dist_schedule:
11917 C = new (Context) OMPDistScheduleClause();
11919 case llvm::omp::OMPC_defaultmap:
11920 C = new (Context) OMPDefaultmapClause();
11922 case llvm::omp::OMPC_to: {
11923 OMPMappableExprListSizeTy Sizes;
11924 Sizes.NumVars = Record.readInt();
11925 Sizes.NumUniqueDeclarations = Record.readInt();
11926 Sizes.NumComponentLists = Record.readInt();
11927 Sizes.NumComponents = Record.readInt();
11928 C = OMPToClause::CreateEmpty(Context, Sizes);
11931 case llvm::omp::OMPC_from: {
11932 OMPMappableExprListSizeTy Sizes;
11933 Sizes.NumVars = Record.readInt();
11934 Sizes.NumUniqueDeclarations = Record.readInt();
11935 Sizes.NumComponentLists = Record.readInt();
11936 Sizes.NumComponents = Record.readInt();
11937 C = OMPFromClause::CreateEmpty(Context, Sizes);
11940 case llvm::omp::OMPC_use_device_ptr: {
11941 OMPMappableExprListSizeTy Sizes;
11942 Sizes.NumVars = Record.readInt();
11943 Sizes.NumUniqueDeclarations = Record.readInt();
11944 Sizes.NumComponentLists = Record.readInt();
11945 Sizes.NumComponents = Record.readInt();
11946 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11949 case llvm::omp::OMPC_use_device_addr: {
11950 OMPMappableExprListSizeTy Sizes;
11951 Sizes.NumVars = Record.readInt();
11952 Sizes.NumUniqueDeclarations = Record.readInt();
11953 Sizes.NumComponentLists = Record.readInt();
11954 Sizes.NumComponents = Record.readInt();
11955 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11958 case llvm::omp::OMPC_is_device_ptr: {
11959 OMPMappableExprListSizeTy Sizes;
11960 Sizes.NumVars = Record.readInt();
11961 Sizes.NumUniqueDeclarations = Record.readInt();
11962 Sizes.NumComponentLists = Record.readInt();
11963 Sizes.NumComponents = Record.readInt();
11964 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11967 case llvm::omp::OMPC_allocate:
11968 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11970 case llvm::omp::OMPC_nontemporal:
11971 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11973 case llvm::omp::OMPC_inclusive:
11974 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11976 case llvm::omp::OMPC_exclusive:
11977 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11979 case llvm::omp::OMPC_order:
11980 C = new (Context) OMPOrderClause();
11982 case llvm::omp::OMPC_destroy:
11983 C = new (Context) OMPDestroyClause();
11985 case llvm::omp::OMPC_detach:
11986 C = new (Context) OMPDetachClause();
11988 case llvm::omp::OMPC_uses_allocators:
11989 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11991 case llvm::omp::OMPC_affinity:
11992 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11994 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11995 case llvm::omp::Enum: \
11997 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12001 assert(C && "Unknown OMPClause type");
12004 C->setLocStart(Record.readSourceLocation());
12005 C->setLocEnd(Record.readSourceLocation());
12010 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12011 C->setPreInitStmt(Record.readSubStmt(),
12012 static_cast<OpenMPDirectiveKind>(Record.readInt()));
12015 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12016 VisitOMPClauseWithPreInit(C);
12017 C->setPostUpdateExpr(Record.readSubExpr());
12020 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12021 VisitOMPClauseWithPreInit(C);
12022 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12023 C->setNameModifierLoc(Record.readSourceLocation());
12024 C->setColonLoc(Record.readSourceLocation());
12025 C->setCondition(Record.readSubExpr());
12026 C->setLParenLoc(Record.readSourceLocation());
12029 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12030 VisitOMPClauseWithPreInit(C);
12031 C->setCondition(Record.readSubExpr());
12032 C->setLParenLoc(Record.readSourceLocation());
12035 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12036 VisitOMPClauseWithPreInit(C);
12037 C->setNumThreads(Record.readSubExpr());
12038 C->setLParenLoc(Record.readSourceLocation());
12041 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12042 C->setSafelen(Record.readSubExpr());
12043 C->setLParenLoc(Record.readSourceLocation());
12046 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12047 C->setSimdlen(Record.readSubExpr());
12048 C->setLParenLoc(Record.readSourceLocation());
12051 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12052 C->setAllocator(Record.readExpr());
12053 C->setLParenLoc(Record.readSourceLocation());
12056 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12057 C->setNumForLoops(Record.readSubExpr());
12058 C->setLParenLoc(Record.readSourceLocation());
12061 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12062 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12063 C->setLParenLoc(Record.readSourceLocation());
12064 C->setDefaultKindKwLoc(Record.readSourceLocation());
12067 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12068 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12069 C->setLParenLoc(Record.readSourceLocation());
12070 C->setProcBindKindKwLoc(Record.readSourceLocation());
12073 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12074 VisitOMPClauseWithPreInit(C);
12075 C->setScheduleKind(
12076 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12077 C->setFirstScheduleModifier(
12078 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12079 C->setSecondScheduleModifier(
12080 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12081 C->setChunkSize(Record.readSubExpr());
12082 C->setLParenLoc(Record.readSourceLocation());
12083 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12084 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12085 C->setScheduleKindLoc(Record.readSourceLocation());
12086 C->setCommaLoc(Record.readSourceLocation());
12089 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12090 C->setNumForLoops(Record.readSubExpr());
12091 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12092 C->setLoopNumIterations(I, Record.readSubExpr());
12093 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12094 C->setLoopCounter(I, Record.readSubExpr());
12095 C->setLParenLoc(Record.readSourceLocation());
12098 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12099 C->setEventHandler(Record.readSubExpr());
12100 C->setLParenLoc(Record.readSourceLocation());
12103 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12105 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12107 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12109 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12111 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12113 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12114 if (C->isExtended()) {
12115 C->setLParenLoc(Record.readSourceLocation());
12116 C->setArgumentLoc(Record.readSourceLocation());
12117 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12121 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12123 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12125 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12127 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12129 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12131 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12133 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12135 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12137 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12139 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12141 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12143 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12144 OMPUnifiedSharedMemoryClause *) {}
12146 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12149 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12152 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12153 OMPAtomicDefaultMemOrderClause *C) {
12154 C->setAtomicDefaultMemOrderKind(
12155 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12156 C->setLParenLoc(Record.readSourceLocation());
12157 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12160 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12161 C->setLParenLoc(Record.readSourceLocation());
12162 unsigned NumVars = C->varlist_size();
12163 SmallVector<Expr *, 16> Vars;
12164 Vars.reserve(NumVars);
12165 for (unsigned i = 0; i != NumVars; ++i)
12166 Vars.push_back(Record.readSubExpr());
12167 C->setVarRefs(Vars);
12169 for (unsigned i = 0; i != NumVars; ++i)
12170 Vars.push_back(Record.readSubExpr());
12171 C->setPrivateCopies(Vars);
12174 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12175 VisitOMPClauseWithPreInit(C);
12176 C->setLParenLoc(Record.readSourceLocation());
12177 unsigned NumVars = C->varlist_size();
12178 SmallVector<Expr *, 16> Vars;
12179 Vars.reserve(NumVars);
12180 for (unsigned i = 0; i != NumVars; ++i)
12181 Vars.push_back(Record.readSubExpr());
12182 C->setVarRefs(Vars);
12184 for (unsigned i = 0; i != NumVars; ++i)
12185 Vars.push_back(Record.readSubExpr());
12186 C->setPrivateCopies(Vars);
12188 for (unsigned i = 0; i != NumVars; ++i)
12189 Vars.push_back(Record.readSubExpr());
12193 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12194 VisitOMPClauseWithPostUpdate(C);
12195 C->setLParenLoc(Record.readSourceLocation());
12196 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12197 C->setKindLoc(Record.readSourceLocation());
12198 C->setColonLoc(Record.readSourceLocation());
12199 unsigned NumVars = C->varlist_size();
12200 SmallVector<Expr *, 16> Vars;
12201 Vars.reserve(NumVars);
12202 for (unsigned i = 0; i != NumVars; ++i)
12203 Vars.push_back(Record.readSubExpr());
12204 C->setVarRefs(Vars);
12206 for (unsigned i = 0; i != NumVars; ++i)
12207 Vars.push_back(Record.readSubExpr());
12208 C->setPrivateCopies(Vars);
12210 for (unsigned i = 0; i != NumVars; ++i)
12211 Vars.push_back(Record.readSubExpr());
12212 C->setSourceExprs(Vars);
12214 for (unsigned i = 0; i != NumVars; ++i)
12215 Vars.push_back(Record.readSubExpr());
12216 C->setDestinationExprs(Vars);
12218 for (unsigned i = 0; i != NumVars; ++i)
12219 Vars.push_back(Record.readSubExpr());
12220 C->setAssignmentOps(Vars);
12223 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12224 C->setLParenLoc(Record.readSourceLocation());
12225 unsigned NumVars = C->varlist_size();
12226 SmallVector<Expr *, 16> Vars;
12227 Vars.reserve(NumVars);
12228 for (unsigned i = 0; i != NumVars; ++i)
12229 Vars.push_back(Record.readSubExpr());
12230 C->setVarRefs(Vars);
12233 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12234 VisitOMPClauseWithPostUpdate(C);
12235 C->setLParenLoc(Record.readSourceLocation());
12236 C->setModifierLoc(Record.readSourceLocation());
12237 C->setColonLoc(Record.readSourceLocation());
12238 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12239 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12240 C->setQualifierLoc(NNSL);
12241 C->setNameInfo(DNI);
12243 unsigned NumVars = C->varlist_size();
12244 SmallVector<Expr *, 16> Vars;
12245 Vars.reserve(NumVars);
12246 for (unsigned i = 0; i != NumVars; ++i)
12247 Vars.push_back(Record.readSubExpr());
12248 C->setVarRefs(Vars);
12250 for (unsigned i = 0; i != NumVars; ++i)
12251 Vars.push_back(Record.readSubExpr());
12252 C->setPrivates(Vars);
12254 for (unsigned i = 0; i != NumVars; ++i)
12255 Vars.push_back(Record.readSubExpr());
12256 C->setLHSExprs(Vars);
12258 for (unsigned i = 0; i != NumVars; ++i)
12259 Vars.push_back(Record.readSubExpr());
12260 C->setRHSExprs(Vars);
12262 for (unsigned i = 0; i != NumVars; ++i)
12263 Vars.push_back(Record.readSubExpr());
12264 C->setReductionOps(Vars);
12265 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12267 for (unsigned i = 0; i != NumVars; ++i)
12268 Vars.push_back(Record.readSubExpr());
12269 C->setInscanCopyOps(Vars);
12271 for (unsigned i = 0; i != NumVars; ++i)
12272 Vars.push_back(Record.readSubExpr());
12273 C->setInscanCopyArrayTemps(Vars);
12275 for (unsigned i = 0; i != NumVars; ++i)
12276 Vars.push_back(Record.readSubExpr());
12277 C->setInscanCopyArrayElems(Vars);
12281 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12282 VisitOMPClauseWithPostUpdate(C);
12283 C->setLParenLoc(Record.readSourceLocation());
12284 C->setColonLoc(Record.readSourceLocation());
12285 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12286 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12287 C->setQualifierLoc(NNSL);
12288 C->setNameInfo(DNI);
12290 unsigned NumVars = C->varlist_size();
12291 SmallVector<Expr *, 16> Vars;
12292 Vars.reserve(NumVars);
12293 for (unsigned I = 0; I != NumVars; ++I)
12294 Vars.push_back(Record.readSubExpr());
12295 C->setVarRefs(Vars);
12297 for (unsigned I = 0; I != NumVars; ++I)
12298 Vars.push_back(Record.readSubExpr());
12299 C->setPrivates(Vars);
12301 for (unsigned I = 0; I != NumVars; ++I)
12302 Vars.push_back(Record.readSubExpr());
12303 C->setLHSExprs(Vars);
12305 for (unsigned I = 0; I != NumVars; ++I)
12306 Vars.push_back(Record.readSubExpr());
12307 C->setRHSExprs(Vars);
12309 for (unsigned I = 0; I != NumVars; ++I)
12310 Vars.push_back(Record.readSubExpr());
12311 C->setReductionOps(Vars);
12314 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12315 VisitOMPClauseWithPostUpdate(C);
12316 C->setLParenLoc(Record.readSourceLocation());
12317 C->setColonLoc(Record.readSourceLocation());
12318 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12319 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12320 C->setQualifierLoc(NNSL);
12321 C->setNameInfo(DNI);
12323 unsigned NumVars = C->varlist_size();
12324 SmallVector<Expr *, 16> Vars;
12325 Vars.reserve(NumVars);
12326 for (unsigned I = 0; I != NumVars; ++I)
12327 Vars.push_back(Record.readSubExpr());
12328 C->setVarRefs(Vars);
12330 for (unsigned I = 0; I != NumVars; ++I)
12331 Vars.push_back(Record.readSubExpr());
12332 C->setPrivates(Vars);
12334 for (unsigned I = 0; I != NumVars; ++I)
12335 Vars.push_back(Record.readSubExpr());
12336 C->setLHSExprs(Vars);
12338 for (unsigned I = 0; I != NumVars; ++I)
12339 Vars.push_back(Record.readSubExpr());
12340 C->setRHSExprs(Vars);
12342 for (unsigned I = 0; I != NumVars; ++I)
12343 Vars.push_back(Record.readSubExpr());
12344 C->setReductionOps(Vars);
12346 for (unsigned I = 0; I != NumVars; ++I)
12347 Vars.push_back(Record.readSubExpr());
12348 C->setTaskgroupDescriptors(Vars);
12351 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12352 VisitOMPClauseWithPostUpdate(C);
12353 C->setLParenLoc(Record.readSourceLocation());
12354 C->setColonLoc(Record.readSourceLocation());
12355 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12356 C->setModifierLoc(Record.readSourceLocation());
12357 unsigned NumVars = C->varlist_size();
12358 SmallVector<Expr *, 16> Vars;
12359 Vars.reserve(NumVars);
12360 for (unsigned i = 0; i != NumVars; ++i)
12361 Vars.push_back(Record.readSubExpr());
12362 C->setVarRefs(Vars);
12364 for (unsigned i = 0; i != NumVars; ++i)
12365 Vars.push_back(Record.readSubExpr());
12366 C->setPrivates(Vars);
12368 for (unsigned i = 0; i != NumVars; ++i)
12369 Vars.push_back(Record.readSubExpr());
12372 for (unsigned i = 0; i != NumVars; ++i)
12373 Vars.push_back(Record.readSubExpr());
12374 C->setUpdates(Vars);
12376 for (unsigned i = 0; i != NumVars; ++i)
12377 Vars.push_back(Record.readSubExpr());
12378 C->setFinals(Vars);
12379 C->setStep(Record.readSubExpr());
12380 C->setCalcStep(Record.readSubExpr());
12382 for (unsigned I = 0; I != NumVars + 1; ++I)
12383 Vars.push_back(Record.readSubExpr());
12384 C->setUsedExprs(Vars);
12387 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12388 C->setLParenLoc(Record.readSourceLocation());
12389 C->setColonLoc(Record.readSourceLocation());
12390 unsigned NumVars = C->varlist_size();
12391 SmallVector<Expr *, 16> Vars;
12392 Vars.reserve(NumVars);
12393 for (unsigned i = 0; i != NumVars; ++i)
12394 Vars.push_back(Record.readSubExpr());
12395 C->setVarRefs(Vars);
12396 C->setAlignment(Record.readSubExpr());
12399 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12400 C->setLParenLoc(Record.readSourceLocation());
12401 unsigned NumVars = C->varlist_size();
12402 SmallVector<Expr *, 16> Exprs;
12403 Exprs.reserve(NumVars);
12404 for (unsigned i = 0; i != NumVars; ++i)
12405 Exprs.push_back(Record.readSubExpr());
12406 C->setVarRefs(Exprs);
12408 for (unsigned i = 0; i != NumVars; ++i)
12409 Exprs.push_back(Record.readSubExpr());
12410 C->setSourceExprs(Exprs);
12412 for (unsigned i = 0; i != NumVars; ++i)
12413 Exprs.push_back(Record.readSubExpr());
12414 C->setDestinationExprs(Exprs);
12416 for (unsigned i = 0; i != NumVars; ++i)
12417 Exprs.push_back(Record.readSubExpr());
12418 C->setAssignmentOps(Exprs);
12421 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12422 C->setLParenLoc(Record.readSourceLocation());
12423 unsigned NumVars = C->varlist_size();
12424 SmallVector<Expr *, 16> Exprs;
12425 Exprs.reserve(NumVars);
12426 for (unsigned i = 0; i != NumVars; ++i)
12427 Exprs.push_back(Record.readSubExpr());
12428 C->setVarRefs(Exprs);
12430 for (unsigned i = 0; i != NumVars; ++i)
12431 Exprs.push_back(Record.readSubExpr());
12432 C->setSourceExprs(Exprs);
12434 for (unsigned i = 0; i != NumVars; ++i)
12435 Exprs.push_back(Record.readSubExpr());
12436 C->setDestinationExprs(Exprs);
12438 for (unsigned i = 0; i != NumVars; ++i)
12439 Exprs.push_back(Record.readSubExpr());
12440 C->setAssignmentOps(Exprs);
12443 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12444 C->setLParenLoc(Record.readSourceLocation());
12445 unsigned NumVars = C->varlist_size();
12446 SmallVector<Expr *, 16> Vars;
12447 Vars.reserve(NumVars);
12448 for (unsigned i = 0; i != NumVars; ++i)
12449 Vars.push_back(Record.readSubExpr());
12450 C->setVarRefs(Vars);
12453 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12454 C->setDepobj(Record.readSubExpr());
12455 C->setLParenLoc(Record.readSourceLocation());
12458 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12459 C->setLParenLoc(Record.readSourceLocation());
12460 C->setModifier(Record.readSubExpr());
12461 C->setDependencyKind(
12462 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12463 C->setDependencyLoc(Record.readSourceLocation());
12464 C->setColonLoc(Record.readSourceLocation());
12465 unsigned NumVars = C->varlist_size();
12466 SmallVector<Expr *, 16> Vars;
12467 Vars.reserve(NumVars);
12468 for (unsigned I = 0; I != NumVars; ++I)
12469 Vars.push_back(Record.readSubExpr());
12470 C->setVarRefs(Vars);
12471 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12472 C->setLoopData(I, Record.readSubExpr());
12475 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12476 VisitOMPClauseWithPreInit(C);
12477 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12478 C->setDevice(Record.readSubExpr());
12479 C->setModifierLoc(Record.readSourceLocation());
12480 C->setLParenLoc(Record.readSourceLocation());
12483 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12484 C->setLParenLoc(Record.readSourceLocation());
12485 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12486 C->setMapTypeModifier(
12487 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12488 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12490 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12491 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12493 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12494 C->setMapLoc(Record.readSourceLocation());
12495 C->setColonLoc(Record.readSourceLocation());
12496 auto NumVars = C->varlist_size();
12497 auto UniqueDecls = C->getUniqueDeclarationsNum();
12498 auto TotalLists = C->getTotalComponentListNum();
12499 auto TotalComponents = C->getTotalComponentsNum();
12501 SmallVector<Expr *, 16> Vars;
12502 Vars.reserve(NumVars);
12503 for (unsigned i = 0; i != NumVars; ++i)
12504 Vars.push_back(Record.readExpr());
12505 C->setVarRefs(Vars);
12507 SmallVector<Expr *, 16> UDMappers;
12508 UDMappers.reserve(NumVars);
12509 for (unsigned I = 0; I < NumVars; ++I)
12510 UDMappers.push_back(Record.readExpr());
12511 C->setUDMapperRefs(UDMappers);
12513 SmallVector<ValueDecl *, 16> Decls;
12514 Decls.reserve(UniqueDecls);
12515 for (unsigned i = 0; i < UniqueDecls; ++i)
12516 Decls.push_back(Record.readDeclAs<ValueDecl>());
12517 C->setUniqueDecls(Decls);
12519 SmallVector<unsigned, 16> ListsPerDecl;
12520 ListsPerDecl.reserve(UniqueDecls);
12521 for (unsigned i = 0; i < UniqueDecls; ++i)
12522 ListsPerDecl.push_back(Record.readInt());
12523 C->setDeclNumLists(ListsPerDecl);
12525 SmallVector<unsigned, 32> ListSizes;
12526 ListSizes.reserve(TotalLists);
12527 for (unsigned i = 0; i < TotalLists; ++i)
12528 ListSizes.push_back(Record.readInt());
12529 C->setComponentListSizes(ListSizes);
12531 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12532 Components.reserve(TotalComponents);
12533 for (unsigned i = 0; i < TotalComponents; ++i) {
12534 Expr *AssociatedExpr = Record.readExpr();
12535 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12536 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12537 AssociatedExpr, AssociatedDecl));
12539 C->setComponents(Components, ListSizes);
12542 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12543 C->setLParenLoc(Record.readSourceLocation());
12544 C->setColonLoc(Record.readSourceLocation());
12545 C->setAllocator(Record.readSubExpr());
12546 unsigned NumVars = C->varlist_size();
12547 SmallVector<Expr *, 16> Vars;
12548 Vars.reserve(NumVars);
12549 for (unsigned i = 0; i != NumVars; ++i)
12550 Vars.push_back(Record.readSubExpr());
12551 C->setVarRefs(Vars);
12554 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12555 VisitOMPClauseWithPreInit(C);
12556 C->setNumTeams(Record.readSubExpr());
12557 C->setLParenLoc(Record.readSourceLocation());
12560 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12561 VisitOMPClauseWithPreInit(C);
12562 C->setThreadLimit(Record.readSubExpr());
12563 C->setLParenLoc(Record.readSourceLocation());
12566 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12567 VisitOMPClauseWithPreInit(C);
12568 C->setPriority(Record.readSubExpr());
12569 C->setLParenLoc(Record.readSourceLocation());
12572 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12573 VisitOMPClauseWithPreInit(C);
12574 C->setGrainsize(Record.readSubExpr());
12575 C->setLParenLoc(Record.readSourceLocation());
12578 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12579 VisitOMPClauseWithPreInit(C);
12580 C->setNumTasks(Record.readSubExpr());
12581 C->setLParenLoc(Record.readSourceLocation());
12584 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12585 C->setHint(Record.readSubExpr());
12586 C->setLParenLoc(Record.readSourceLocation());
12589 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12590 VisitOMPClauseWithPreInit(C);
12591 C->setDistScheduleKind(
12592 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12593 C->setChunkSize(Record.readSubExpr());
12594 C->setLParenLoc(Record.readSourceLocation());
12595 C->setDistScheduleKindLoc(Record.readSourceLocation());
12596 C->setCommaLoc(Record.readSourceLocation());
12599 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12600 C->setDefaultmapKind(
12601 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12602 C->setDefaultmapModifier(
12603 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12604 C->setLParenLoc(Record.readSourceLocation());
12605 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12606 C->setDefaultmapKindLoc(Record.readSourceLocation());
12609 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12610 C->setLParenLoc(Record.readSourceLocation());
12611 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12612 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12613 auto NumVars = C->varlist_size();
12614 auto UniqueDecls = C->getUniqueDeclarationsNum();
12615 auto TotalLists = C->getTotalComponentListNum();
12616 auto TotalComponents = C->getTotalComponentsNum();
12618 SmallVector<Expr *, 16> Vars;
12619 Vars.reserve(NumVars);
12620 for (unsigned i = 0; i != NumVars; ++i)
12621 Vars.push_back(Record.readSubExpr());
12622 C->setVarRefs(Vars);
12624 SmallVector<Expr *, 16> UDMappers;
12625 UDMappers.reserve(NumVars);
12626 for (unsigned I = 0; I < NumVars; ++I)
12627 UDMappers.push_back(Record.readSubExpr());
12628 C->setUDMapperRefs(UDMappers);
12630 SmallVector<ValueDecl *, 16> Decls;
12631 Decls.reserve(UniqueDecls);
12632 for (unsigned i = 0; i < UniqueDecls; ++i)
12633 Decls.push_back(Record.readDeclAs<ValueDecl>());
12634 C->setUniqueDecls(Decls);
12636 SmallVector<unsigned, 16> ListsPerDecl;
12637 ListsPerDecl.reserve(UniqueDecls);
12638 for (unsigned i = 0; i < UniqueDecls; ++i)
12639 ListsPerDecl.push_back(Record.readInt());
12640 C->setDeclNumLists(ListsPerDecl);
12642 SmallVector<unsigned, 32> ListSizes;
12643 ListSizes.reserve(TotalLists);
12644 for (unsigned i = 0; i < TotalLists; ++i)
12645 ListSizes.push_back(Record.readInt());
12646 C->setComponentListSizes(ListSizes);
12648 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12649 Components.reserve(TotalComponents);
12650 for (unsigned i = 0; i < TotalComponents; ++i) {
12651 Expr *AssociatedExpr = Record.readSubExpr();
12652 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12653 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12654 AssociatedExpr, AssociatedDecl));
12656 C->setComponents(Components, ListSizes);
12659 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12660 C->setLParenLoc(Record.readSourceLocation());
12661 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12662 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12663 auto NumVars = C->varlist_size();
12664 auto UniqueDecls = C->getUniqueDeclarationsNum();
12665 auto TotalLists = C->getTotalComponentListNum();
12666 auto TotalComponents = C->getTotalComponentsNum();
12668 SmallVector<Expr *, 16> Vars;
12669 Vars.reserve(NumVars);
12670 for (unsigned i = 0; i != NumVars; ++i)
12671 Vars.push_back(Record.readSubExpr());
12672 C->setVarRefs(Vars);
12674 SmallVector<Expr *, 16> UDMappers;
12675 UDMappers.reserve(NumVars);
12676 for (unsigned I = 0; I < NumVars; ++I)
12677 UDMappers.push_back(Record.readSubExpr());
12678 C->setUDMapperRefs(UDMappers);
12680 SmallVector<ValueDecl *, 16> Decls;
12681 Decls.reserve(UniqueDecls);
12682 for (unsigned i = 0; i < UniqueDecls; ++i)
12683 Decls.push_back(Record.readDeclAs<ValueDecl>());
12684 C->setUniqueDecls(Decls);
12686 SmallVector<unsigned, 16> ListsPerDecl;
12687 ListsPerDecl.reserve(UniqueDecls);
12688 for (unsigned i = 0; i < UniqueDecls; ++i)
12689 ListsPerDecl.push_back(Record.readInt());
12690 C->setDeclNumLists(ListsPerDecl);
12692 SmallVector<unsigned, 32> ListSizes;
12693 ListSizes.reserve(TotalLists);
12694 for (unsigned i = 0; i < TotalLists; ++i)
12695 ListSizes.push_back(Record.readInt());
12696 C->setComponentListSizes(ListSizes);
12698 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12699 Components.reserve(TotalComponents);
12700 for (unsigned i = 0; i < TotalComponents; ++i) {
12701 Expr *AssociatedExpr = Record.readSubExpr();
12702 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12703 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12704 AssociatedExpr, AssociatedDecl));
12706 C->setComponents(Components, ListSizes);
12709 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12710 C->setLParenLoc(Record.readSourceLocation());
12711 auto NumVars = C->varlist_size();
12712 auto UniqueDecls = C->getUniqueDeclarationsNum();
12713 auto TotalLists = C->getTotalComponentListNum();
12714 auto TotalComponents = C->getTotalComponentsNum();
12716 SmallVector<Expr *, 16> Vars;
12717 Vars.reserve(NumVars);
12718 for (unsigned i = 0; i != NumVars; ++i)
12719 Vars.push_back(Record.readSubExpr());
12720 C->setVarRefs(Vars);
12722 for (unsigned i = 0; i != NumVars; ++i)
12723 Vars.push_back(Record.readSubExpr());
12724 C->setPrivateCopies(Vars);
12726 for (unsigned i = 0; i != NumVars; ++i)
12727 Vars.push_back(Record.readSubExpr());
12730 SmallVector<ValueDecl *, 16> Decls;
12731 Decls.reserve(UniqueDecls);
12732 for (unsigned i = 0; i < UniqueDecls; ++i)
12733 Decls.push_back(Record.readDeclAs<ValueDecl>());
12734 C->setUniqueDecls(Decls);
12736 SmallVector<unsigned, 16> ListsPerDecl;
12737 ListsPerDecl.reserve(UniqueDecls);
12738 for (unsigned i = 0; i < UniqueDecls; ++i)
12739 ListsPerDecl.push_back(Record.readInt());
12740 C->setDeclNumLists(ListsPerDecl);
12742 SmallVector<unsigned, 32> ListSizes;
12743 ListSizes.reserve(TotalLists);
12744 for (unsigned i = 0; i < TotalLists; ++i)
12745 ListSizes.push_back(Record.readInt());
12746 C->setComponentListSizes(ListSizes);
12748 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12749 Components.reserve(TotalComponents);
12750 for (unsigned i = 0; i < TotalComponents; ++i) {
12751 Expr *AssociatedExpr = Record.readSubExpr();
12752 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12753 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12754 AssociatedExpr, AssociatedDecl));
12756 C->setComponents(Components, ListSizes);
12759 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12760 C->setLParenLoc(Record.readSourceLocation());
12761 auto NumVars = C->varlist_size();
12762 auto UniqueDecls = C->getUniqueDeclarationsNum();
12763 auto TotalLists = C->getTotalComponentListNum();
12764 auto TotalComponents = C->getTotalComponentsNum();
12766 SmallVector<Expr *, 16> Vars;
12767 Vars.reserve(NumVars);
12768 for (unsigned i = 0; i != NumVars; ++i)
12769 Vars.push_back(Record.readSubExpr());
12770 C->setVarRefs(Vars);
12772 SmallVector<ValueDecl *, 16> Decls;
12773 Decls.reserve(UniqueDecls);
12774 for (unsigned i = 0; i < UniqueDecls; ++i)
12775 Decls.push_back(Record.readDeclAs<ValueDecl>());
12776 C->setUniqueDecls(Decls);
12778 SmallVector<unsigned, 16> ListsPerDecl;
12779 ListsPerDecl.reserve(UniqueDecls);
12780 for (unsigned i = 0; i < UniqueDecls; ++i)
12781 ListsPerDecl.push_back(Record.readInt());
12782 C->setDeclNumLists(ListsPerDecl);
12784 SmallVector<unsigned, 32> ListSizes;
12785 ListSizes.reserve(TotalLists);
12786 for (unsigned i = 0; i < TotalLists; ++i)
12787 ListSizes.push_back(Record.readInt());
12788 C->setComponentListSizes(ListSizes);
12790 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12791 Components.reserve(TotalComponents);
12792 for (unsigned i = 0; i < TotalComponents; ++i) {
12793 Expr *AssociatedExpr = Record.readSubExpr();
12794 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12795 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12796 AssociatedExpr, AssociatedDecl));
12798 C->setComponents(Components, ListSizes);
12801 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12802 C->setLParenLoc(Record.readSourceLocation());
12803 auto NumVars = C->varlist_size();
12804 auto UniqueDecls = C->getUniqueDeclarationsNum();
12805 auto TotalLists = C->getTotalComponentListNum();
12806 auto TotalComponents = C->getTotalComponentsNum();
12808 SmallVector<Expr *, 16> Vars;
12809 Vars.reserve(NumVars);
12810 for (unsigned i = 0; i != NumVars; ++i)
12811 Vars.push_back(Record.readSubExpr());
12812 C->setVarRefs(Vars);
12815 SmallVector<ValueDecl *, 16> Decls;
12816 Decls.reserve(UniqueDecls);
12817 for (unsigned i = 0; i < UniqueDecls; ++i)
12818 Decls.push_back(Record.readDeclAs<ValueDecl>());
12819 C->setUniqueDecls(Decls);
12821 SmallVector<unsigned, 16> ListsPerDecl;
12822 ListsPerDecl.reserve(UniqueDecls);
12823 for (unsigned i = 0; i < UniqueDecls; ++i)
12824 ListsPerDecl.push_back(Record.readInt());
12825 C->setDeclNumLists(ListsPerDecl);
12827 SmallVector<unsigned, 32> ListSizes;
12828 ListSizes.reserve(TotalLists);
12829 for (unsigned i = 0; i < TotalLists; ++i)
12830 ListSizes.push_back(Record.readInt());
12831 C->setComponentListSizes(ListSizes);
12833 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12834 Components.reserve(TotalComponents);
12835 for (unsigned i = 0; i < TotalComponents; ++i) {
12836 Expr *AssociatedExpr = Record.readSubExpr();
12837 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12838 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12839 AssociatedExpr, AssociatedDecl));
12841 C->setComponents(Components, ListSizes);
12844 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12845 C->setLParenLoc(Record.readSourceLocation());
12846 unsigned NumVars = C->varlist_size();
12847 SmallVector<Expr *, 16> Vars;
12848 Vars.reserve(NumVars);
12849 for (unsigned i = 0; i != NumVars; ++i)
12850 Vars.push_back(Record.readSubExpr());
12851 C->setVarRefs(Vars);
12853 Vars.reserve(NumVars);
12854 for (unsigned i = 0; i != NumVars; ++i)
12855 Vars.push_back(Record.readSubExpr());
12856 C->setPrivateRefs(Vars);
12859 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12860 C->setLParenLoc(Record.readSourceLocation());
12861 unsigned NumVars = C->varlist_size();
12862 SmallVector<Expr *, 16> Vars;
12863 Vars.reserve(NumVars);
12864 for (unsigned i = 0; i != NumVars; ++i)
12865 Vars.push_back(Record.readSubExpr());
12866 C->setVarRefs(Vars);
12869 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12870 C->setLParenLoc(Record.readSourceLocation());
12871 unsigned NumVars = C->varlist_size();
12872 SmallVector<Expr *, 16> Vars;
12873 Vars.reserve(NumVars);
12874 for (unsigned i = 0; i != NumVars; ++i)
12875 Vars.push_back(Record.readSubExpr());
12876 C->setVarRefs(Vars);
12879 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12880 C->setLParenLoc(Record.readSourceLocation());
12881 unsigned NumOfAllocators = C->getNumberOfAllocators();
12882 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12883 Data.reserve(NumOfAllocators);
12884 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12885 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12886 D.Allocator = Record.readSubExpr();
12887 D.AllocatorTraits = Record.readSubExpr();
12888 D.LParenLoc = Record.readSourceLocation();
12889 D.RParenLoc = Record.readSourceLocation();
12891 C->setAllocatorsData(Data);
12894 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12895 C->setLParenLoc(Record.readSourceLocation());
12896 C->setModifier(Record.readSubExpr());
12897 C->setColonLoc(Record.readSourceLocation());
12898 unsigned NumOfLocators = C->varlist_size();
12899 SmallVector<Expr *, 4> Locators;
12900 Locators.reserve(NumOfLocators);
12901 for (unsigned I = 0; I != NumOfLocators; ++I)
12902 Locators.push_back(Record.readSubExpr());
12903 C->setVarRefs(Locators);
12906 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12907 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12908 C->setLParenLoc(Record.readSourceLocation());
12909 C->setKindKwLoc(Record.readSourceLocation());
12912 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12913 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12914 TI.Sets.resize(readUInt32());
12915 for (auto &Set : TI.Sets) {
12916 Set.Kind = readEnum<llvm::omp::TraitSet>();
12917 Set.Selectors.resize(readUInt32());
12918 for (auto &Selector : Set.Selectors) {
12919 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12920 Selector.ScoreOrCondition = nullptr;
12922 Selector.ScoreOrCondition = readExprRef();
12923 Selector.Properties.resize(readUInt32());
12924 for (auto &Property : Selector.Properties)
12925 Property.Kind = readEnum<llvm::omp::TraitProperty>();