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() || II.isPoisoned() ||
912 (!IsModule && II.getObjCOrBuiltinID()) ||
913 II.hasRevertedTokenIDToIdentifier() ||
914 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
915 II.getFETokenInfo());
918 static bool readBit(unsigned &Bits) {
919 bool Value = Bits & 0x1;
924 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
925 using namespace llvm::support;
927 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
928 return Reader.getGlobalIdentifierID(F, RawID >> 1);
931 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
932 if (!II.isFromAST()) {
934 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
935 if (isInterestingIdentifier(Reader, II, IsModule))
936 II.setChangedSinceDeserialization();
940 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
941 const unsigned char* d,
943 using namespace llvm::support;
945 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
946 bool IsInteresting = RawID & 0x01;
948 // Wipe out the "is interesting" bit.
951 // Build the IdentifierInfo and link the identifier ID with it.
952 IdentifierInfo *II = KnownII;
954 II = &Reader.getIdentifierTable().getOwn(k);
957 markIdentifierFromAST(Reader, *II);
958 Reader.markIdentifierUpToDate(II);
960 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
961 if (!IsInteresting) {
962 // For uninteresting identifiers, there's nothing else to do. Just notify
963 // the reader that we've finished loading this identifier.
964 Reader.SetIdentifierInfo(ID, II);
968 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
969 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
970 bool CPlusPlusOperatorKeyword = readBit(Bits);
971 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
972 bool Poisoned = readBit(Bits);
973 bool ExtensionToken = readBit(Bits);
974 bool HadMacroDefinition = readBit(Bits);
976 assert(Bits == 0 && "Extra bits in the identifier?");
979 // Set or check the various bits in the IdentifierInfo structure.
980 // Token IDs are read-only.
981 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
982 II->revertTokenIDToIdentifier();
984 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
985 assert(II->isExtensionToken() == ExtensionToken &&
986 "Incorrect extension token flag");
987 (void)ExtensionToken;
989 II->setIsPoisoned(true);
990 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
991 "Incorrect C++ operator keyword flag");
992 (void)CPlusPlusOperatorKeyword;
994 // If this identifier is a macro, deserialize the macro
996 if (HadMacroDefinition) {
997 uint32_t MacroDirectivesOffset =
998 endian::readNext<uint32_t, little, unaligned>(d);
1001 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1004 Reader.SetIdentifierInfo(ID, II);
1006 // Read all of the declarations visible at global scope with this
1009 SmallVector<uint32_t, 4> DeclIDs;
1010 for (; DataLen > 0; DataLen -= 4)
1011 DeclIDs.push_back(Reader.getGlobalDeclID(
1012 F, endian::readNext<uint32_t, little, unaligned>(d)));
1013 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1019 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1020 : Kind(Name.getNameKind()) {
1022 case DeclarationName::Identifier:
1023 Data = (uint64_t)Name.getAsIdentifierInfo();
1025 case DeclarationName::ObjCZeroArgSelector:
1026 case DeclarationName::ObjCOneArgSelector:
1027 case DeclarationName::ObjCMultiArgSelector:
1028 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1030 case DeclarationName::CXXOperatorName:
1031 Data = Name.getCXXOverloadedOperator();
1033 case DeclarationName::CXXLiteralOperatorName:
1034 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1036 case DeclarationName::CXXDeductionGuideName:
1037 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1038 ->getDeclName().getAsIdentifierInfo();
1040 case DeclarationName::CXXConstructorName:
1041 case DeclarationName::CXXDestructorName:
1042 case DeclarationName::CXXConversionFunctionName:
1043 case DeclarationName::CXXUsingDirective:
1049 unsigned DeclarationNameKey::getHash() const {
1050 llvm::FoldingSetNodeID ID;
1051 ID.AddInteger(Kind);
1054 case DeclarationName::Identifier:
1055 case DeclarationName::CXXLiteralOperatorName:
1056 case DeclarationName::CXXDeductionGuideName:
1057 ID.AddString(((IdentifierInfo*)Data)->getName());
1059 case DeclarationName::ObjCZeroArgSelector:
1060 case DeclarationName::ObjCOneArgSelector:
1061 case DeclarationName::ObjCMultiArgSelector:
1062 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1064 case DeclarationName::CXXOperatorName:
1065 ID.AddInteger((OverloadedOperatorKind)Data);
1067 case DeclarationName::CXXConstructorName:
1068 case DeclarationName::CXXDestructorName:
1069 case DeclarationName::CXXConversionFunctionName:
1070 case DeclarationName::CXXUsingDirective:
1074 return ID.ComputeHash();
1078 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1079 using namespace llvm::support;
1081 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1082 return Reader.getLocalModuleFile(F, ModuleFileID);
1085 std::pair<unsigned, unsigned>
1086 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1087 using namespace llvm::support;
1089 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1090 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1091 return std::make_pair(KeyLen, DataLen);
1094 ASTDeclContextNameLookupTrait::internal_key_type
1095 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1096 using namespace llvm::support;
1098 auto Kind = (DeclarationName::NameKind)*d++;
1101 case DeclarationName::Identifier:
1102 case DeclarationName::CXXLiteralOperatorName:
1103 case DeclarationName::CXXDeductionGuideName:
1104 Data = (uint64_t)Reader.getLocalIdentifier(
1105 F, endian::readNext<uint32_t, little, unaligned>(d));
1107 case DeclarationName::ObjCZeroArgSelector:
1108 case DeclarationName::ObjCOneArgSelector:
1109 case DeclarationName::ObjCMultiArgSelector:
1111 (uint64_t)Reader.getLocalSelector(
1112 F, endian::readNext<uint32_t, little, unaligned>(
1113 d)).getAsOpaquePtr();
1115 case DeclarationName::CXXOperatorName:
1116 Data = *d++; // OverloadedOperatorKind
1118 case DeclarationName::CXXConstructorName:
1119 case DeclarationName::CXXDestructorName:
1120 case DeclarationName::CXXConversionFunctionName:
1121 case DeclarationName::CXXUsingDirective:
1126 return DeclarationNameKey(Kind, Data);
1129 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1130 const unsigned char *d,
1132 data_type_builder &Val) {
1133 using namespace llvm::support;
1135 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1136 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1137 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1141 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1142 BitstreamCursor &Cursor,
1145 assert(Offset != 0);
1147 SavedStreamPosition SavedPosition(Cursor);
1148 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1149 Error(std::move(Err));
1155 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1157 Error(MaybeCode.takeError());
1160 unsigned Code = MaybeCode.get();
1162 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1163 if (!MaybeRecCode) {
1164 Error(MaybeRecCode.takeError());
1167 unsigned RecCode = MaybeRecCode.get();
1168 if (RecCode != DECL_CONTEXT_LEXICAL) {
1169 Error("Expected lexical block");
1173 assert(!isa<TranslationUnitDecl>(DC) &&
1174 "expected a TU_UPDATE_LEXICAL record for TU");
1175 // If we are handling a C++ class template instantiation, we can see multiple
1176 // lexical updates for the same record. It's important that we select only one
1177 // of them, so that field numbering works properly. Just pick the first one we
1179 auto &Lex = LexicalDecls[DC];
1181 Lex = std::make_pair(
1182 &M, llvm::makeArrayRef(
1183 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1187 DC->setHasExternalLexicalStorage(true);
1191 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1192 BitstreamCursor &Cursor,
1195 assert(Offset != 0);
1197 SavedStreamPosition SavedPosition(Cursor);
1198 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1199 Error(std::move(Err));
1205 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1207 Error(MaybeCode.takeError());
1210 unsigned Code = MaybeCode.get();
1212 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1213 if (!MaybeRecCode) {
1214 Error(MaybeRecCode.takeError());
1217 unsigned RecCode = MaybeRecCode.get();
1218 if (RecCode != DECL_CONTEXT_VISIBLE) {
1219 Error("Expected visible lookup table block");
1223 // We can't safely determine the primary context yet, so delay attaching the
1224 // lookup table until we're done with recursive deserialization.
1225 auto *Data = (const unsigned char*)Blob.data();
1226 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1230 void ASTReader::Error(StringRef Msg) const {
1231 Error(diag::err_fe_pch_malformed, Msg);
1232 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1233 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1234 Diag(diag::note_module_cache_path)
1235 << PP.getHeaderSearchInfo().getModuleCachePath();
1239 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1240 StringRef Arg3) const {
1241 if (Diags.isDiagnosticInFlight())
1242 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1244 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1247 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1248 unsigned Select) const {
1249 if (!Diags.isDiagnosticInFlight())
1250 Diag(DiagID) << Arg1 << Arg2 << Select;
1253 void ASTReader::Error(llvm::Error &&Err) const {
1254 Error(toString(std::move(Err)));
1257 //===----------------------------------------------------------------------===//
1258 // Source Manager Deserialization
1259 //===----------------------------------------------------------------------===//
1261 /// Read the line table in the source manager block.
1262 /// \returns true if there was an error.
1263 bool ASTReader::ParseLineTable(ModuleFile &F,
1264 const RecordData &Record) {
1266 LineTableInfo &LineTable = SourceMgr.getLineTable();
1268 // Parse the file names
1269 std::map<int, int> FileIDs;
1270 FileIDs[-1] = -1; // For unspecified filenames.
1271 for (unsigned I = 0; Record[Idx]; ++I) {
1272 // Extract the file name
1273 auto Filename = ReadPath(F, Record, Idx);
1274 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1278 // Parse the line entries
1279 std::vector<LineEntry> Entries;
1280 while (Idx < Record.size()) {
1281 int FID = Record[Idx++];
1282 assert(FID >= 0 && "Serialized line entries for non-local file.");
1283 // Remap FileID from 1-based old view.
1284 FID += F.SLocEntryBaseID - 1;
1286 // Extract the line entries
1287 unsigned NumEntries = Record[Idx++];
1288 assert(NumEntries && "no line entries for file ID");
1290 Entries.reserve(NumEntries);
1291 for (unsigned I = 0; I != NumEntries; ++I) {
1292 unsigned FileOffset = Record[Idx++];
1293 unsigned LineNo = Record[Idx++];
1294 int FilenameID = FileIDs[Record[Idx++]];
1295 SrcMgr::CharacteristicKind FileKind
1296 = (SrcMgr::CharacteristicKind)Record[Idx++];
1297 unsigned IncludeOffset = Record[Idx++];
1298 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1299 FileKind, IncludeOffset));
1301 LineTable.AddEntry(FileID::get(FID), Entries);
1307 /// Read a source manager block
1308 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1309 using namespace SrcMgr;
1311 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1313 // Set the source-location entry cursor to the current position in
1314 // the stream. This cursor will be used to read the contents of the
1315 // source manager block initially, and then lazily read
1316 // source-location entries as needed.
1317 SLocEntryCursor = F.Stream;
1319 // The stream itself is going to skip over the source manager block.
1320 if (llvm::Error Err = F.Stream.SkipBlock()) {
1321 Error(std::move(Err));
1325 // Enter the source manager block.
1326 if (llvm::Error Err =
1327 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1328 Error(std::move(Err));
1331 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1335 Expected<llvm::BitstreamEntry> MaybeE =
1336 SLocEntryCursor.advanceSkippingSubblocks();
1338 Error(MaybeE.takeError());
1341 llvm::BitstreamEntry E = MaybeE.get();
1344 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1345 case llvm::BitstreamEntry::Error:
1346 Error("malformed block record in AST file");
1348 case llvm::BitstreamEntry::EndBlock:
1350 case llvm::BitstreamEntry::Record:
1351 // The interesting case.
1358 Expected<unsigned> MaybeRecord =
1359 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1361 Error(MaybeRecord.takeError());
1364 switch (MaybeRecord.get()) {
1365 default: // Default behavior: ignore.
1368 case SM_SLOC_FILE_ENTRY:
1369 case SM_SLOC_BUFFER_ENTRY:
1370 case SM_SLOC_EXPANSION_ENTRY:
1371 // Once we hit one of the source location entries, we're done.
1377 /// If a header file is not found at the path that we expect it to be
1378 /// and the PCH file was moved from its original location, try to resolve the
1379 /// file by assuming that header+PCH were moved together and the header is in
1380 /// the same place relative to the PCH.
1382 resolveFileRelativeToOriginalDir(const std::string &Filename,
1383 const std::string &OriginalDir,
1384 const std::string &CurrDir) {
1385 assert(OriginalDir != CurrDir &&
1386 "No point trying to resolve the file if the PCH dir didn't change");
1388 using namespace llvm::sys;
1390 SmallString<128> filePath(Filename);
1391 fs::make_absolute(filePath);
1392 assert(path::is_absolute(OriginalDir));
1393 SmallString<128> currPCHPath(CurrDir);
1395 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1396 fileDirE = path::end(path::parent_path(filePath));
1397 path::const_iterator origDirI = path::begin(OriginalDir),
1398 origDirE = path::end(OriginalDir);
1399 // Skip the common path components from filePath and OriginalDir.
1400 while (fileDirI != fileDirE && origDirI != origDirE &&
1401 *fileDirI == *origDirI) {
1405 for (; origDirI != origDirE; ++origDirI)
1406 path::append(currPCHPath, "..");
1407 path::append(currPCHPath, fileDirI, fileDirE);
1408 path::append(currPCHPath, path::filename(Filename));
1409 return std::string(currPCHPath.str());
1412 bool ASTReader::ReadSLocEntry(int ID) {
1416 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1417 Error("source location entry ID out-of-range for AST file");
1421 // Local helper to read the (possibly-compressed) buffer data following the
1423 auto ReadBuffer = [this](
1424 BitstreamCursor &SLocEntryCursor,
1425 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1428 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1430 Error(MaybeCode.takeError());
1433 unsigned Code = MaybeCode.get();
1435 Expected<unsigned> MaybeRecCode =
1436 SLocEntryCursor.readRecord(Code, Record, &Blob);
1437 if (!MaybeRecCode) {
1438 Error(MaybeRecCode.takeError());
1441 unsigned RecCode = MaybeRecCode.get();
1443 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1444 if (!llvm::zlib::isAvailable()) {
1445 Error("zlib is not available");
1448 SmallString<0> Uncompressed;
1450 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1451 Error("could not decompress embedded file contents: " +
1452 llvm::toString(std::move(E)));
1455 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1456 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1457 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1459 Error("AST record has invalid code");
1464 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1465 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1466 F->SLocEntryOffsetsBase +
1467 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1468 Error(std::move(Err));
1472 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1473 unsigned BaseOffset = F->SLocEntryBaseOffset;
1475 ++NumSLocEntriesRead;
1476 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1478 Error(MaybeEntry.takeError());
1481 llvm::BitstreamEntry Entry = MaybeEntry.get();
1483 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1484 Error("incorrectly-formatted source location entry in AST file");
1490 Expected<unsigned> MaybeSLOC =
1491 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1493 Error(MaybeSLOC.takeError());
1496 switch (MaybeSLOC.get()) {
1498 Error("incorrectly-formatted source location entry in AST file");
1501 case SM_SLOC_FILE_ENTRY: {
1502 // We will detect whether a file changed and return 'Failure' for it, but
1503 // we will also try to fail gracefully by setting up the SLocEntry.
1504 unsigned InputID = Record[4];
1505 InputFile IF = getInputFile(*F, InputID);
1506 const FileEntry *File = IF.getFile();
1507 bool OverriddenBuffer = IF.isOverridden();
1509 // Note that we only check if a File was returned. If it was out-of-date
1510 // we have complained but we will continue creating a FileID to recover
1515 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1516 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1517 // This is the module's main file.
1518 IncludeLoc = getImportLocation(F);
1520 SrcMgr::CharacteristicKind
1521 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1522 // FIXME: The FileID should be created from the FileEntryRef.
1523 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1524 ID, BaseOffset + Record[0]);
1525 SrcMgr::FileInfo &FileInfo =
1526 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1527 FileInfo.NumCreatedFIDs = Record[5];
1529 FileInfo.setHasLineDirectives();
1531 unsigned NumFileDecls = Record[7];
1532 if (NumFileDecls && ContextObj) {
1533 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1534 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1535 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1539 const SrcMgr::ContentCache *ContentCache
1540 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1541 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1542 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1543 !ContentCache->getRawBuffer()) {
1544 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1547 SourceMgr.overrideFileContents(File, std::move(Buffer));
1553 case SM_SLOC_BUFFER_ENTRY: {
1554 const char *Name = Blob.data();
1555 unsigned Offset = Record[0];
1556 SrcMgr::CharacteristicKind
1557 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1558 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1559 if (IncludeLoc.isInvalid() && F->isModule()) {
1560 IncludeLoc = getImportLocation(F);
1563 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1566 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1567 BaseOffset + Offset, IncludeLoc);
1571 case SM_SLOC_EXPANSION_ENTRY: {
1572 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1573 SourceMgr.createExpansionLoc(SpellingLoc,
1574 ReadSourceLocation(*F, Record[2]),
1575 ReadSourceLocation(*F, Record[3]),
1579 BaseOffset + Record[0]);
1587 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1589 return std::make_pair(SourceLocation(), "");
1591 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1592 Error("source location entry ID out-of-range for AST file");
1593 return std::make_pair(SourceLocation(), "");
1596 // Find which module file this entry lands in.
1597 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1599 return std::make_pair(SourceLocation(), "");
1601 // FIXME: Can we map this down to a particular submodule? That would be
1603 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1606 /// Find the location where the module F is imported.
1607 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1608 if (F->ImportLoc.isValid())
1609 return F->ImportLoc;
1611 // Otherwise we have a PCH. It's considered to be "imported" at the first
1612 // location of its includer.
1613 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1614 // Main file is the importer.
1615 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1616 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1618 return F->ImportedBy[0]->FirstLoc;
1621 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1622 /// the abbreviations that are at the top of the block and then leave the cursor
1623 /// pointing into the block.
1624 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1625 uint64_t *StartOfBlockOffset) {
1626 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1627 // FIXME this drops errors on the floor.
1628 consumeError(std::move(Err));
1632 if (StartOfBlockOffset)
1633 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1636 uint64_t Offset = Cursor.GetCurrentBitNo();
1637 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1639 // FIXME this drops errors on the floor.
1640 consumeError(MaybeCode.takeError());
1643 unsigned Code = MaybeCode.get();
1645 // We expect all abbrevs to be at the start of the block.
1646 if (Code != llvm::bitc::DEFINE_ABBREV) {
1647 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1648 // FIXME this drops errors on the floor.
1649 consumeError(std::move(Err));
1654 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1655 // FIXME this drops errors on the floor.
1656 consumeError(std::move(Err));
1662 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1666 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1667 Tok.setLength(Record[Idx++]);
1668 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1669 Tok.setIdentifierInfo(II);
1670 Tok.setKind((tok::TokenKind)Record[Idx++]);
1671 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1675 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1676 BitstreamCursor &Stream = F.MacroCursor;
1678 // Keep track of where we are in the stream, then jump back there
1679 // after reading this macro.
1680 SavedStreamPosition SavedPosition(Stream);
1682 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1683 // FIXME this drops errors on the floor.
1684 consumeError(std::move(Err));
1688 SmallVector<IdentifierInfo*, 16> MacroParams;
1689 MacroInfo *Macro = nullptr;
1692 // Advance to the next record, but if we get to the end of the block, don't
1693 // pop it (removing all the abbreviations from the cursor) since we want to
1694 // be able to reseek within the block and read entries.
1695 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1696 Expected<llvm::BitstreamEntry> MaybeEntry =
1697 Stream.advanceSkippingSubblocks(Flags);
1699 Error(MaybeEntry.takeError());
1702 llvm::BitstreamEntry Entry = MaybeEntry.get();
1704 switch (Entry.Kind) {
1705 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1706 case llvm::BitstreamEntry::Error:
1707 Error("malformed block record in AST file");
1709 case llvm::BitstreamEntry::EndBlock:
1711 case llvm::BitstreamEntry::Record:
1712 // The interesting case.
1718 PreprocessorRecordTypes RecType;
1719 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1720 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1722 Error(MaybeRecType.takeError());
1726 case PP_MODULE_MACRO:
1727 case PP_MACRO_DIRECTIVE_HISTORY:
1730 case PP_MACRO_OBJECT_LIKE:
1731 case PP_MACRO_FUNCTION_LIKE: {
1732 // If we already have a macro, that means that we've hit the end
1733 // of the definition of the macro we were looking for. We're
1738 unsigned NextIndex = 1; // Skip identifier ID.
1739 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1740 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1741 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1742 MI->setIsUsed(Record[NextIndex++]);
1743 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1745 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1746 // Decode function-like macro info.
1747 bool isC99VarArgs = Record[NextIndex++];
1748 bool isGNUVarArgs = Record[NextIndex++];
1749 bool hasCommaPasting = Record[NextIndex++];
1750 MacroParams.clear();
1751 unsigned NumArgs = Record[NextIndex++];
1752 for (unsigned i = 0; i != NumArgs; ++i)
1753 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1755 // Install function-like macro info.
1756 MI->setIsFunctionLike();
1757 if (isC99VarArgs) MI->setIsC99Varargs();
1758 if (isGNUVarArgs) MI->setIsGNUVarargs();
1759 if (hasCommaPasting) MI->setHasCommaPasting();
1760 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1763 // Remember that we saw this macro last so that we add the tokens that
1764 // form its body to it.
1767 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1768 Record[NextIndex]) {
1769 // We have a macro definition. Register the association
1770 PreprocessedEntityID
1771 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1772 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1773 PreprocessingRecord::PPEntityID PPID =
1774 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1775 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1776 PPRec.getPreprocessedEntity(PPID));
1778 PPRec.RegisterMacroDefinition(Macro, PPDef);
1786 // If we see a TOKEN before a PP_MACRO_*, then the file is
1787 // erroneous, just pretend we didn't see this.
1791 Token Tok = ReadToken(F, Record, Idx);
1792 Macro->AddTokenToBody(Tok);
1799 PreprocessedEntityID
1800 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1801 unsigned LocalID) const {
1802 if (!M.ModuleOffsetMap.empty())
1803 ReadModuleOffsetMap(M);
1805 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1806 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1807 assert(I != M.PreprocessedEntityRemap.end()
1808 && "Invalid index into preprocessed entity index remap");
1810 return LocalID + I->second;
1813 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1814 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1817 HeaderFileInfoTrait::internal_key_type
1818 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1819 internal_key_type ikey = {FE->getSize(),
1820 M.HasTimestamps ? FE->getModificationTime() : 0,
1821 FE->getName(), /*Imported*/ false};
1825 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1826 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1829 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1832 // Determine whether the actual files are equivalent.
1833 FileManager &FileMgr = Reader.getFileManager();
1834 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1835 if (!Key.Imported) {
1836 if (auto File = FileMgr.getFile(Key.Filename))
1841 std::string Resolved = std::string(Key.Filename);
1842 Reader.ResolveImportedPath(M, Resolved);
1843 if (auto File = FileMgr.getFile(Resolved))
1848 const FileEntry *FEA = GetFile(a);
1849 const FileEntry *FEB = GetFile(b);
1850 return FEA && FEA == FEB;
1853 std::pair<unsigned, unsigned>
1854 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1855 using namespace llvm::support;
1857 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1858 unsigned DataLen = (unsigned) *d++;
1859 return std::make_pair(KeyLen, DataLen);
1862 HeaderFileInfoTrait::internal_key_type
1863 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1864 using namespace llvm::support;
1866 internal_key_type ikey;
1867 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1868 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1869 ikey.Filename = (const char *)d;
1870 ikey.Imported = true;
1874 HeaderFileInfoTrait::data_type
1875 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1877 using namespace llvm::support;
1879 const unsigned char *End = d + DataLen;
1881 unsigned Flags = *d++;
1882 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1883 HFI.isImport |= (Flags >> 5) & 0x01;
1884 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1885 HFI.DirInfo = (Flags >> 1) & 0x07;
1886 HFI.IndexHeaderMapHeader = Flags & 0x01;
1887 // FIXME: Find a better way to handle this. Maybe just store a
1888 // "has been included" flag?
1889 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1891 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1892 M, endian::readNext<uint32_t, little, unaligned>(d));
1893 if (unsigned FrameworkOffset =
1894 endian::readNext<uint32_t, little, unaligned>(d)) {
1895 // The framework offset is 1 greater than the actual offset,
1896 // since 0 is used as an indicator for "no framework name".
1897 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1898 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1901 assert((End - d) % 4 == 0 &&
1902 "Wrong data length in HeaderFileInfo deserialization");
1904 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1905 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1908 // This header is part of a module. Associate it with the module to enable
1909 // implicit module import.
1910 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1911 Module *Mod = Reader.getSubmodule(GlobalSMID);
1912 FileManager &FileMgr = Reader.getFileManager();
1914 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1916 std::string Filename = std::string(key.Filename);
1918 Reader.ResolveImportedPath(M, Filename);
1919 // FIXME: This is not always the right filename-as-written, but we're not
1920 // going to use this information to rebuild the module, so it doesn't make
1921 // a lot of difference.
1922 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1923 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1924 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1927 // This HeaderFileInfo was externally loaded.
1928 HFI.External = true;
1933 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1934 uint32_t MacroDirectivesOffset) {
1935 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1936 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1939 void ASTReader::ReadDefinedMacros() {
1940 // Note that we are loading defined macros.
1941 Deserializing Macros(this);
1943 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1944 BitstreamCursor &MacroCursor = I.MacroCursor;
1946 // If there was no preprocessor block, skip this file.
1947 if (MacroCursor.getBitcodeBytes().empty())
1950 BitstreamCursor Cursor = MacroCursor;
1951 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1952 Error(std::move(Err));
1958 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1960 Error(MaybeE.takeError());
1963 llvm::BitstreamEntry E = MaybeE.get();
1966 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1967 case llvm::BitstreamEntry::Error:
1968 Error("malformed block record in AST file");
1970 case llvm::BitstreamEntry::EndBlock:
1973 case llvm::BitstreamEntry::Record: {
1975 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1977 Error(MaybeRecord.takeError());
1980 switch (MaybeRecord.get()) {
1981 default: // Default behavior: ignore.
1984 case PP_MACRO_OBJECT_LIKE:
1985 case PP_MACRO_FUNCTION_LIKE: {
1986 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1987 if (II->isOutOfDate())
1988 updateOutOfDateIdentifier(*II);
2006 /// Visitor class used to look up identifirs in an AST file.
2007 class IdentifierLookupVisitor {
2010 unsigned PriorGeneration;
2011 unsigned &NumIdentifierLookups;
2012 unsigned &NumIdentifierLookupHits;
2013 IdentifierInfo *Found = nullptr;
2016 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2017 unsigned &NumIdentifierLookups,
2018 unsigned &NumIdentifierLookupHits)
2019 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2020 PriorGeneration(PriorGeneration),
2021 NumIdentifierLookups(NumIdentifierLookups),
2022 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2024 bool operator()(ModuleFile &M) {
2025 // If we've already searched this module file, skip it now.
2026 if (M.Generation <= PriorGeneration)
2029 ASTIdentifierLookupTable *IdTable
2030 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2034 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2036 ++NumIdentifierLookups;
2037 ASTIdentifierLookupTable::iterator Pos =
2038 IdTable->find_hashed(Name, NameHash, &Trait);
2039 if (Pos == IdTable->end())
2042 // Dereferencing the iterator has the effect of building the
2043 // IdentifierInfo node and populating it with the various
2044 // declarations it needs.
2045 ++NumIdentifierLookupHits;
2050 // Retrieve the identifier info found within the module
2052 IdentifierInfo *getIdentifierInfo() const { return Found; }
2057 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2058 // Note that we are loading an identifier.
2059 Deserializing AnIdentifier(this);
2061 unsigned PriorGeneration = 0;
2062 if (getContext().getLangOpts().Modules)
2063 PriorGeneration = IdentifierGeneration[&II];
2065 // If there is a global index, look there first to determine which modules
2066 // provably do not have any results for this identifier.
2067 GlobalModuleIndex::HitSet Hits;
2068 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2069 if (!loadGlobalIndex()) {
2070 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2075 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2076 NumIdentifierLookups,
2077 NumIdentifierLookupHits);
2078 ModuleMgr.visit(Visitor, HitsPtr);
2079 markIdentifierUpToDate(&II);
2082 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2086 II->setOutOfDate(false);
2088 // Update the generation for this identifier.
2089 if (getContext().getLangOpts().Modules)
2090 IdentifierGeneration[II] = getGeneration();
2093 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2094 const PendingMacroInfo &PMInfo) {
2095 ModuleFile &M = *PMInfo.M;
2097 BitstreamCursor &Cursor = M.MacroCursor;
2098 SavedStreamPosition SavedPosition(Cursor);
2099 if (llvm::Error Err =
2100 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2101 Error(std::move(Err));
2105 struct ModuleMacroRecord {
2106 SubmoduleID SubModID;
2108 SmallVector<SubmoduleID, 8> Overrides;
2110 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2112 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2113 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2117 Expected<llvm::BitstreamEntry> MaybeEntry =
2118 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2120 Error(MaybeEntry.takeError());
2123 llvm::BitstreamEntry Entry = MaybeEntry.get();
2125 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2126 Error("malformed block record in AST file");
2131 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2133 Error(MaybePP.takeError());
2136 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2137 case PP_MACRO_DIRECTIVE_HISTORY:
2140 case PP_MODULE_MACRO: {
2141 ModuleMacros.push_back(ModuleMacroRecord());
2142 auto &Info = ModuleMacros.back();
2143 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2144 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2145 for (int I = 2, N = Record.size(); I != N; ++I)
2146 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2151 Error("malformed block record in AST file");
2155 // We found the macro directive history; that's the last record
2160 // Module macros are listed in reverse dependency order.
2162 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2163 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2164 for (auto &MMR : ModuleMacros) {
2166 for (unsigned ModID : MMR.Overrides) {
2167 Module *Mod = getSubmodule(ModID);
2168 auto *Macro = PP.getModuleMacro(Mod, II);
2169 assert(Macro && "missing definition for overridden macro");
2170 Overrides.push_back(Macro);
2173 bool Inserted = false;
2174 Module *Owner = getSubmodule(MMR.SubModID);
2175 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2179 // Don't read the directive history for a module; we don't have anywhere
2184 // Deserialize the macro directives history in reverse source-order.
2185 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2186 unsigned Idx = 0, N = Record.size();
2188 MacroDirective *MD = nullptr;
2189 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2190 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2192 case MacroDirective::MD_Define: {
2193 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2194 MD = PP.AllocateDefMacroDirective(MI, Loc);
2197 case MacroDirective::MD_Undefine:
2198 MD = PP.AllocateUndefMacroDirective(Loc);
2200 case MacroDirective::MD_Visibility:
2201 bool isPublic = Record[Idx++];
2202 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2209 Earliest->setPrevious(MD);
2214 PP.setLoadedMacroDirective(II, Earliest, Latest);
2217 ASTReader::InputFileInfo
2218 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2219 // Go find this input file.
2220 BitstreamCursor &Cursor = F.InputFilesCursor;
2221 SavedStreamPosition SavedPosition(Cursor);
2222 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2223 // FIXME this drops errors on the floor.
2224 consumeError(std::move(Err));
2227 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2229 // FIXME this drops errors on the floor.
2230 consumeError(MaybeCode.takeError());
2232 unsigned Code = MaybeCode.get();
2236 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2237 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2238 "invalid record type for input file");
2240 // FIXME this drops errors on the floor.
2241 consumeError(Maybe.takeError());
2244 assert(Record[0] == ID && "Bogus stored ID or offset");
2246 R.StoredSize = static_cast<off_t>(Record[1]);
2247 R.StoredTime = static_cast<time_t>(Record[2]);
2248 R.Overridden = static_cast<bool>(Record[3]);
2249 R.Transient = static_cast<bool>(Record[4]);
2250 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2251 R.Filename = std::string(Blob);
2252 ResolveImportedPath(F, R.Filename);
2254 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2255 if (!MaybeEntry) // FIXME this drops errors on the floor.
2256 consumeError(MaybeEntry.takeError());
2257 llvm::BitstreamEntry Entry = MaybeEntry.get();
2258 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2259 "expected record type for input file hash");
2262 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2263 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2264 "invalid record type for input file hash");
2266 // FIXME this drops errors on the floor.
2267 consumeError(Maybe.takeError());
2269 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2270 static_cast<uint64_t>(Record[0]);
2274 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2275 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2276 // If this ID is bogus, just return an empty input file.
2277 if (ID == 0 || ID > F.InputFilesLoaded.size())
2280 // If we've already loaded this input file, return it.
2281 if (F.InputFilesLoaded[ID-1].getFile())
2282 return F.InputFilesLoaded[ID-1];
2284 if (F.InputFilesLoaded[ID-1].isNotFound())
2287 // Go find this input file.
2288 BitstreamCursor &Cursor = F.InputFilesCursor;
2289 SavedStreamPosition SavedPosition(Cursor);
2290 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2291 // FIXME this drops errors on the floor.
2292 consumeError(std::move(Err));
2295 InputFileInfo FI = readInputFileInfo(F, ID);
2296 off_t StoredSize = FI.StoredSize;
2297 time_t StoredTime = FI.StoredTime;
2298 bool Overridden = FI.Overridden;
2299 bool Transient = FI.Transient;
2300 StringRef Filename = FI.Filename;
2301 uint64_t StoredContentHash = FI.ContentHash;
2303 const FileEntry *File = nullptr;
2304 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2307 // If we didn't find the file, resolve it relative to the
2308 // original directory from which this AST file was created.
2309 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2310 F.OriginalDir != F.BaseDirectory) {
2311 std::string Resolved = resolveFileRelativeToOriginalDir(
2312 std::string(Filename), F.OriginalDir, F.BaseDirectory);
2313 if (!Resolved.empty())
2314 if (auto FE = FileMgr.getFile(Resolved))
2318 // For an overridden file, create a virtual file with the stored
2320 if ((Overridden || Transient) && File == nullptr)
2321 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2323 if (File == nullptr) {
2325 std::string ErrorStr = "could not find file '";
2326 ErrorStr += Filename;
2327 ErrorStr += "' referenced by AST file '";
2328 ErrorStr += F.FileName;
2332 // Record that we didn't find the file.
2333 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2337 // Check if there was a request to override the contents of the file
2338 // that was part of the precompiled header. Overriding such a file
2339 // can lead to problems when lexing using the source locations from the
2341 SourceManager &SM = getSourceManager();
2342 // FIXME: Reject if the overrides are different.
2343 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2345 Error(diag::err_fe_pch_file_overridden, Filename);
2347 // After emitting the diagnostic, bypass the overriding file to recover
2348 // (this creates a separate FileEntry).
2349 File = SM.bypassFileContentsOverride(*File);
2351 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2356 enum ModificationType {
2362 auto HasInputFileChanged = [&]() {
2363 if (StoredSize != File->getSize())
2364 return ModificationType::Size;
2365 if (!DisableValidation && StoredTime &&
2366 StoredTime != File->getModificationTime()) {
2367 // In case the modification time changes but not the content,
2368 // accept the cached file as legit.
2369 if (ValidateASTInputFilesContent &&
2370 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2371 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2372 if (!MemBuffOrError) {
2374 return ModificationType::ModTime;
2375 std::string ErrorStr = "could not get buffer for file '";
2376 ErrorStr += File->getName();
2379 return ModificationType::ModTime;
2382 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2383 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2384 return ModificationType::None;
2385 return ModificationType::Content;
2387 return ModificationType::ModTime;
2389 return ModificationType::None;
2392 bool IsOutOfDate = false;
2393 auto FileChange = HasInputFileChanged();
2394 // For an overridden file, there is nothing to validate.
2395 if (!Overridden && FileChange != ModificationType::None) {
2397 // Build a list of the PCH imports that got us here (in reverse).
2398 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2399 while (!ImportStack.back()->ImportedBy.empty())
2400 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2402 // The top-level PCH is stale.
2403 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2404 unsigned DiagnosticKind =
2405 moduleKindForDiagnostic(ImportStack.back()->Kind);
2406 if (DiagnosticKind == 0)
2407 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2408 (unsigned)FileChange);
2409 else if (DiagnosticKind == 1)
2410 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2411 (unsigned)FileChange);
2413 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2414 (unsigned)FileChange);
2416 // Print the import stack.
2417 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2418 Diag(diag::note_pch_required_by)
2419 << Filename << ImportStack[0]->FileName;
2420 for (unsigned I = 1; I < ImportStack.size(); ++I)
2421 Diag(diag::note_pch_required_by)
2422 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2425 if (!Diags.isDiagnosticInFlight())
2426 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2431 // FIXME: If the file is overridden and we've already opened it,
2432 // issue an error (or split it into a separate FileEntry).
2434 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2436 // Note that we've loaded this input file.
2437 F.InputFilesLoaded[ID-1] = IF;
2441 /// If we are loading a relocatable PCH or module file, and the filename
2442 /// is not an absolute path, add the system or module root to the beginning of
2444 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2445 // Resolve relative to the base directory, if we have one.
2446 if (!M.BaseDirectory.empty())
2447 return ResolveImportedPath(Filename, M.BaseDirectory);
2450 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2451 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2454 SmallString<128> Buffer;
2455 llvm::sys::path::append(Buffer, Prefix, Filename);
2456 Filename.assign(Buffer.begin(), Buffer.end());
2459 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2461 case ASTReader::Failure: return true;
2462 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2463 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2464 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2465 case ASTReader::ConfigurationMismatch:
2466 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2467 case ASTReader::HadErrors: return true;
2468 case ASTReader::Success: return false;
2471 llvm_unreachable("unknown ASTReadResult");
2474 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2475 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2476 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2477 std::string &SuggestedPredefines) {
2478 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2479 // FIXME this drops errors on the floor.
2480 consumeError(std::move(Err));
2484 // Read all of the records in the options block.
2486 ASTReadResult Result = Success;
2488 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2490 // FIXME this drops errors on the floor.
2491 consumeError(MaybeEntry.takeError());
2494 llvm::BitstreamEntry Entry = MaybeEntry.get();
2496 switch (Entry.Kind) {
2497 case llvm::BitstreamEntry::Error:
2498 case llvm::BitstreamEntry::SubBlock:
2501 case llvm::BitstreamEntry::EndBlock:
2504 case llvm::BitstreamEntry::Record:
2505 // The interesting case.
2509 // Read and process a record.
2511 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2512 if (!MaybeRecordType) {
2513 // FIXME this drops errors on the floor.
2514 consumeError(MaybeRecordType.takeError());
2517 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2518 case LANGUAGE_OPTIONS: {
2519 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2520 if (ParseLanguageOptions(Record, Complain, Listener,
2521 AllowCompatibleConfigurationMismatch))
2522 Result = ConfigurationMismatch;
2526 case TARGET_OPTIONS: {
2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528 if (ParseTargetOptions(Record, Complain, Listener,
2529 AllowCompatibleConfigurationMismatch))
2530 Result = ConfigurationMismatch;
2534 case FILE_SYSTEM_OPTIONS: {
2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536 if (!AllowCompatibleConfigurationMismatch &&
2537 ParseFileSystemOptions(Record, Complain, Listener))
2538 Result = ConfigurationMismatch;
2542 case HEADER_SEARCH_OPTIONS: {
2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544 if (!AllowCompatibleConfigurationMismatch &&
2545 ParseHeaderSearchOptions(Record, Complain, Listener))
2546 Result = ConfigurationMismatch;
2550 case PREPROCESSOR_OPTIONS:
2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552 if (!AllowCompatibleConfigurationMismatch &&
2553 ParsePreprocessorOptions(Record, Complain, Listener,
2554 SuggestedPredefines))
2555 Result = ConfigurationMismatch;
2561 ASTReader::ASTReadResult
2562 ASTReader::ReadControlBlock(ModuleFile &F,
2563 SmallVectorImpl<ImportedModule> &Loaded,
2564 const ModuleFile *ImportedBy,
2565 unsigned ClientLoadCapabilities) {
2566 BitstreamCursor &Stream = F.Stream;
2568 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2569 Error(std::move(Err));
2573 // Lambda to read the unhashed control block the first time it's called.
2575 // For PCM files, the unhashed control block cannot be read until after the
2576 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2577 // need to look ahead before reading the IMPORTS record. For consistency,
2578 // this block is always read somehow (see BitstreamEntry::EndBlock).
2579 bool HasReadUnhashedControlBlock = false;
2580 auto readUnhashedControlBlockOnce = [&]() {
2581 if (!HasReadUnhashedControlBlock) {
2582 HasReadUnhashedControlBlock = true;
2583 if (ASTReadResult Result =
2584 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2590 // Read all of the records and blocks in the control block.
2592 unsigned NumInputs = 0;
2593 unsigned NumUserInputs = 0;
2594 StringRef BaseDirectoryAsWritten;
2596 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2598 Error(MaybeEntry.takeError());
2601 llvm::BitstreamEntry Entry = MaybeEntry.get();
2603 switch (Entry.Kind) {
2604 case llvm::BitstreamEntry::Error:
2605 Error("malformed block record in AST file");
2607 case llvm::BitstreamEntry::EndBlock: {
2608 // Validate the module before returning. This call catches an AST with
2609 // no module name and no imports.
2610 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2613 // Validate input files.
2614 const HeaderSearchOptions &HSOpts =
2615 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2617 // All user input files reside at the index range [0, NumUserInputs), and
2618 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2619 // loaded module files, ignore missing inputs.
2620 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2621 F.Kind != MK_PrebuiltModule) {
2622 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2624 // If we are reading a module, we will create a verification timestamp,
2625 // so we verify all input files. Otherwise, verify only user input
2628 unsigned N = NumUserInputs;
2629 if (ValidateSystemInputs ||
2630 (HSOpts.ModulesValidateOncePerBuildSession &&
2631 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2632 F.Kind == MK_ImplicitModule))
2635 for (unsigned I = 0; I < N; ++I) {
2636 InputFile IF = getInputFile(F, I+1, Complain);
2637 if (!IF.getFile() || IF.isOutOfDate())
2643 Listener->visitModuleFile(F.FileName, F.Kind);
2645 if (Listener && Listener->needsInputFileVisitation()) {
2646 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2648 for (unsigned I = 0; I < N; ++I) {
2649 bool IsSystem = I >= NumUserInputs;
2650 InputFileInfo FI = readInputFileInfo(F, I+1);
2651 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2652 F.Kind == MK_ExplicitModule ||
2653 F.Kind == MK_PrebuiltModule);
2660 case llvm::BitstreamEntry::SubBlock:
2662 case INPUT_FILES_BLOCK_ID:
2663 F.InputFilesCursor = Stream;
2664 if (llvm::Error Err = Stream.SkipBlock()) {
2665 Error(std::move(Err));
2668 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2669 Error("malformed block record in AST file");
2674 case OPTIONS_BLOCK_ID:
2675 // If we're reading the first module for this group, check its options
2676 // are compatible with ours. For modules it imports, no further checking
2677 // is required, because we checked them when we built it.
2678 if (Listener && !ImportedBy) {
2679 // Should we allow the configuration of the module file to differ from
2680 // the configuration of the current translation unit in a compatible
2683 // FIXME: Allow this for files explicitly specified with -include-pch.
2684 bool AllowCompatibleConfigurationMismatch =
2685 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2687 ASTReadResult Result =
2688 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2689 AllowCompatibleConfigurationMismatch, *Listener,
2690 SuggestedPredefines);
2691 if (Result == Failure) {
2692 Error("malformed block record in AST file");
2696 if (DisableValidation ||
2697 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2700 // If we can't load the module, exit early since we likely
2701 // will rebuild the module anyway. The stream may be in the
2702 // middle of a block.
2703 if (Result != Success)
2705 } else if (llvm::Error Err = Stream.SkipBlock()) {
2706 Error(std::move(Err));
2712 if (llvm::Error Err = Stream.SkipBlock()) {
2713 Error(std::move(Err));
2719 case llvm::BitstreamEntry::Record:
2720 // The interesting case.
2724 // Read and process a record.
2727 Expected<unsigned> MaybeRecordType =
2728 Stream.readRecord(Entry.ID, Record, &Blob);
2729 if (!MaybeRecordType) {
2730 Error(MaybeRecordType.takeError());
2733 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2735 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2736 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2737 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2738 : diag::err_pch_version_too_new);
2739 return VersionMismatch;
2742 bool hasErrors = Record[7];
2743 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2744 Diag(diag::err_pch_with_compiler_errors);
2748 Diags.ErrorOccurred = true;
2749 Diags.UncompilableErrorOccurred = true;
2750 Diags.UnrecoverableErrorOccurred = true;
2753 F.RelocatablePCH = Record[4];
2754 // Relative paths in a relocatable PCH are relative to our sysroot.
2755 if (F.RelocatablePCH)
2756 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2758 F.HasTimestamps = Record[5];
2760 F.PCHHasObjectFile = Record[6];
2762 const std::string &CurBranch = getClangFullRepositoryVersion();
2763 StringRef ASTBranch = Blob;
2764 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2765 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2766 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2767 return VersionMismatch;
2773 // Validate the AST before processing any imports (otherwise, untangling
2774 // them can be error-prone and expensive). A module will have a name and
2775 // will already have been validated, but this catches the PCH case.
2776 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2779 // Load each of the imported PCH files.
2780 unsigned Idx = 0, N = Record.size();
2782 // Read information about the AST file.
2783 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2784 // The import location will be the local one for now; we will adjust
2785 // all import locations of module imports after the global source
2786 // location info are setup, in ReadAST.
2787 SourceLocation ImportLoc =
2788 ReadUntranslatedSourceLocation(Record[Idx++]);
2789 off_t StoredSize = (off_t)Record[Idx++];
2790 time_t StoredModTime = (time_t)Record[Idx++];
2791 auto FirstSignatureByte = Record.begin() + Idx;
2792 ASTFileSignature StoredSignature = ASTFileSignature::create(
2793 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2794 Idx += ASTFileSignature::size;
2796 std::string ImportedName = ReadString(Record, Idx);
2797 std::string ImportedFile;
2799 // For prebuilt and explicit modules first consult the file map for
2800 // an override. Note that here we don't search prebuilt module
2801 // directories, only the explicit name to file mappings. Also, we will
2802 // still verify the size/signature making sure it is essentially the
2803 // same file but perhaps in a different location.
2804 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2805 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2806 ImportedName, /*FileMapOnly*/ true);
2808 if (ImportedFile.empty())
2809 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2810 // ModuleCache as when writing.
2811 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2813 SkipPath(Record, Idx);
2815 // If our client can't cope with us being out of date, we can't cope with
2816 // our dependency being missing.
2817 unsigned Capabilities = ClientLoadCapabilities;
2818 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2819 Capabilities &= ~ARR_Missing;
2821 // Load the AST file.
2822 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2823 Loaded, StoredSize, StoredModTime,
2824 StoredSignature, Capabilities);
2826 // If we diagnosed a problem, produce a backtrace.
2827 if (isDiagnosedResult(Result, Capabilities))
2828 Diag(diag::note_module_file_imported_by)
2829 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2832 case Failure: return Failure;
2833 // If we have to ignore the dependency, we'll have to ignore this too.
2835 case OutOfDate: return OutOfDate;
2836 case VersionMismatch: return VersionMismatch;
2837 case ConfigurationMismatch: return ConfigurationMismatch;
2838 case HadErrors: return HadErrors;
2839 case Success: break;
2846 F.OriginalSourceFileID = FileID::get(Record[0]);
2847 F.ActualOriginalSourceFileName = std::string(Blob);
2848 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2849 ResolveImportedPath(F, F.OriginalSourceFileName);
2852 case ORIGINAL_FILE_ID:
2853 F.OriginalSourceFileID = FileID::get(Record[0]);
2856 case ORIGINAL_PCH_DIR:
2857 F.OriginalDir = std::string(Blob);
2861 F.ModuleName = std::string(Blob);
2862 Diag(diag::remark_module_import)
2863 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2864 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2866 Listener->ReadModuleName(F.ModuleName);
2868 // Validate the AST as soon as we have a name so we can exit early on
2870 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2875 case MODULE_DIRECTORY: {
2876 // Save the BaseDirectory as written in the PCM for computing the module
2877 // filename for the ModuleCache.
2878 BaseDirectoryAsWritten = Blob;
2879 assert(!F.ModuleName.empty() &&
2880 "MODULE_DIRECTORY found before MODULE_NAME");
2881 // If we've already loaded a module map file covering this module, we may
2882 // have a better path for it (relative to the current build).
2883 Module *M = PP.getHeaderSearchInfo().lookupModule(
2884 F.ModuleName, /*AllowSearch*/ true,
2885 /*AllowExtraModuleMapSearch*/ true);
2886 if (M && M->Directory) {
2887 // If we're implicitly loading a module, the base directory can't
2888 // change between the build and use.
2889 // Don't emit module relocation error if we have -fno-validate-pch
2890 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2891 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2892 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2893 if (!BuildDir || *BuildDir != M->Directory) {
2894 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2895 Diag(diag::err_imported_module_relocated)
2896 << F.ModuleName << Blob << M->Directory->getName();
2900 F.BaseDirectory = std::string(M->Directory->getName());
2902 F.BaseDirectory = std::string(Blob);
2907 case MODULE_MAP_FILE:
2908 if (ASTReadResult Result =
2909 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2913 case INPUT_FILE_OFFSETS:
2914 NumInputs = Record[0];
2915 NumUserInputs = Record[1];
2916 F.InputFileOffsets =
2917 (const llvm::support::unaligned_uint64_t *)Blob.data();
2918 F.InputFilesLoaded.resize(NumInputs);
2919 F.NumUserInputFiles = NumUserInputs;
2925 ASTReader::ASTReadResult
2926 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2927 BitstreamCursor &Stream = F.Stream;
2929 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2930 Error(std::move(Err));
2933 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2935 // Read all of the records and blocks for the AST file.
2938 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2940 Error(MaybeEntry.takeError());
2943 llvm::BitstreamEntry Entry = MaybeEntry.get();
2945 switch (Entry.Kind) {
2946 case llvm::BitstreamEntry::Error:
2947 Error("error at end of module block in AST file");
2949 case llvm::BitstreamEntry::EndBlock:
2950 // Outside of C++, we do not store a lookup map for the translation unit.
2951 // Instead, mark it as needing a lookup map to be built if this module
2952 // contains any declarations lexically within it (which it always does!).
2953 // This usually has no cost, since we very rarely need the lookup map for
2954 // the translation unit outside C++.
2955 if (ASTContext *Ctx = ContextObj) {
2956 DeclContext *DC = Ctx->getTranslationUnitDecl();
2957 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2958 DC->setMustBuildLookupTable();
2962 case llvm::BitstreamEntry::SubBlock:
2964 case DECLTYPES_BLOCK_ID:
2965 // We lazily load the decls block, but we want to set up the
2966 // DeclsCursor cursor to point into it. Clone our current bitcode
2967 // cursor to it, enter the block and read the abbrevs in that block.
2968 // With the main cursor, we just skip over it.
2969 F.DeclsCursor = Stream;
2970 if (llvm::Error Err = Stream.SkipBlock()) {
2971 Error(std::move(Err));
2974 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2975 &F.DeclsBlockStartOffset)) {
2976 Error("malformed block record in AST file");
2981 case PREPROCESSOR_BLOCK_ID:
2982 F.MacroCursor = Stream;
2983 if (!PP.getExternalSource())
2984 PP.setExternalSource(this);
2986 if (llvm::Error Err = Stream.SkipBlock()) {
2987 Error(std::move(Err));
2990 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2991 Error("malformed block record in AST file");
2994 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2997 case PREPROCESSOR_DETAIL_BLOCK_ID:
2998 F.PreprocessorDetailCursor = Stream;
3000 if (llvm::Error Err = Stream.SkipBlock()) {
3001 Error(std::move(Err));
3004 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3005 PREPROCESSOR_DETAIL_BLOCK_ID)) {
3006 Error("malformed preprocessor detail record in AST file");
3009 F.PreprocessorDetailStartOffset
3010 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3012 if (!PP.getPreprocessingRecord())
3013 PP.createPreprocessingRecord();
3014 if (!PP.getPreprocessingRecord()->getExternalSource())
3015 PP.getPreprocessingRecord()->SetExternalSource(*this);
3018 case SOURCE_MANAGER_BLOCK_ID:
3019 if (ReadSourceManagerBlock(F))
3023 case SUBMODULE_BLOCK_ID:
3024 if (ASTReadResult Result =
3025 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3029 case COMMENTS_BLOCK_ID: {
3030 BitstreamCursor C = Stream;
3032 if (llvm::Error Err = Stream.SkipBlock()) {
3033 Error(std::move(Err));
3036 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3037 Error("malformed comments block in AST file");
3040 CommentsCursors.push_back(std::make_pair(C, &F));
3045 if (llvm::Error Err = Stream.SkipBlock()) {
3046 Error(std::move(Err));
3053 case llvm::BitstreamEntry::Record:
3054 // The interesting case.
3058 // Read and process a record.
3061 Expected<unsigned> MaybeRecordType =
3062 Stream.readRecord(Entry.ID, Record, &Blob);
3063 if (!MaybeRecordType) {
3064 Error(MaybeRecordType.takeError());
3067 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3069 // If we're not loading an AST context, we don't care about most records.
3071 switch (RecordType) {
3072 case IDENTIFIER_TABLE:
3073 case IDENTIFIER_OFFSET:
3074 case INTERESTING_IDENTIFIERS:
3076 case PP_CONDITIONAL_STACK:
3077 case PP_COUNTER_VALUE:
3078 case SOURCE_LOCATION_OFFSETS:
3079 case MODULE_OFFSET_MAP:
3080 case SOURCE_MANAGER_LINE_TABLE:
3081 case SOURCE_LOCATION_PRELOADS:
3082 case PPD_ENTITIES_OFFSETS:
3083 case HEADER_SEARCH_TABLE:
3084 case IMPORTED_MODULES:
3092 switch (RecordType) {
3093 default: // Default behavior: ignore.
3097 if (F.LocalNumTypes != 0) {
3098 Error("duplicate TYPE_OFFSET record in AST file");
3101 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3102 F.LocalNumTypes = Record[0];
3103 unsigned LocalBaseTypeIndex = Record[1];
3104 F.BaseTypeIndex = getTotalNumTypes();
3106 if (F.LocalNumTypes > 0) {
3107 // Introduce the global -> local mapping for types within this module.
3108 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3110 // Introduce the local -> global mapping for types within this module.
3111 F.TypeRemap.insertOrReplace(
3112 std::make_pair(LocalBaseTypeIndex,
3113 F.BaseTypeIndex - LocalBaseTypeIndex));
3115 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3121 if (F.LocalNumDecls != 0) {
3122 Error("duplicate DECL_OFFSET record in AST file");
3125 F.DeclOffsets = (const DeclOffset *)Blob.data();
3126 F.LocalNumDecls = Record[0];
3127 unsigned LocalBaseDeclID = Record[1];
3128 F.BaseDeclID = getTotalNumDecls();
3130 if (F.LocalNumDecls > 0) {
3131 // Introduce the global -> local mapping for declarations within this
3133 GlobalDeclMap.insert(
3134 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3136 // Introduce the local -> global mapping for declarations within this
3138 F.DeclRemap.insertOrReplace(
3139 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3141 // Introduce the global -> local mapping for declarations within this
3143 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3145 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3150 case TU_UPDATE_LEXICAL: {
3151 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3152 LexicalContents Contents(
3153 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3155 static_cast<unsigned int>(Blob.size() / 4));
3156 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3157 TU->setHasExternalLexicalStorage(true);
3161 case UPDATE_VISIBLE: {
3163 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3164 auto *Data = (const unsigned char*)Blob.data();
3165 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3166 // If we've already loaded the decl, perform the updates when we finish
3167 // loading this block.
3168 if (Decl *D = GetExistingDecl(ID))
3169 PendingUpdateRecords.push_back(
3170 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3174 case IDENTIFIER_TABLE:
3175 F.IdentifierTableData = Blob.data();
3177 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3178 (const unsigned char *)F.IdentifierTableData + Record[0],
3179 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3180 (const unsigned char *)F.IdentifierTableData,
3181 ASTIdentifierLookupTrait(*this, F));
3183 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3187 case IDENTIFIER_OFFSET: {
3188 if (F.LocalNumIdentifiers != 0) {
3189 Error("duplicate IDENTIFIER_OFFSET record in AST file");
3192 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3193 F.LocalNumIdentifiers = Record[0];
3194 unsigned LocalBaseIdentifierID = Record[1];
3195 F.BaseIdentifierID = getTotalNumIdentifiers();
3197 if (F.LocalNumIdentifiers > 0) {
3198 // Introduce the global -> local mapping for identifiers within this
3200 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3203 // Introduce the local -> global mapping for identifiers within this
3205 F.IdentifierRemap.insertOrReplace(
3206 std::make_pair(LocalBaseIdentifierID,
3207 F.BaseIdentifierID - LocalBaseIdentifierID));
3209 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3210 + F.LocalNumIdentifiers);
3215 case INTERESTING_IDENTIFIERS:
3216 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3219 case EAGERLY_DESERIALIZED_DECLS:
3220 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3221 // about "interesting" decls (for instance, if we're building a module).
3222 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3223 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3226 case MODULAR_CODEGEN_DECLS:
3227 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3228 // them (ie: if we're not codegenerating this module).
3229 if (F.Kind == MK_MainFile ||
3230 getContext().getLangOpts().BuildingPCHWithObjectFile)
3231 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3236 if (SpecialTypes.empty()) {
3237 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3238 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3242 if (SpecialTypes.size() != Record.size()) {
3243 Error("invalid special-types record");
3247 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3248 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3249 if (!SpecialTypes[I])
3250 SpecialTypes[I] = ID;
3251 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3257 TotalNumStatements += Record[0];
3258 TotalNumMacros += Record[1];
3259 TotalLexicalDeclContexts += Record[2];
3260 TotalVisibleDeclContexts += Record[3];
3263 case UNUSED_FILESCOPED_DECLS:
3264 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3265 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3268 case DELEGATING_CTORS:
3269 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3270 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3273 case WEAK_UNDECLARED_IDENTIFIERS:
3274 if (Record.size() % 4 != 0) {
3275 Error("invalid weak identifiers record");
3279 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3280 // files. This isn't the way to do it :)
3281 WeakUndeclaredIdentifiers.clear();
3283 // Translate the weak, undeclared identifiers into global IDs.
3284 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3285 WeakUndeclaredIdentifiers.push_back(
3286 getGlobalIdentifierID(F, Record[I++]));
3287 WeakUndeclaredIdentifiers.push_back(
3288 getGlobalIdentifierID(F, Record[I++]));
3289 WeakUndeclaredIdentifiers.push_back(
3290 ReadSourceLocation(F, Record, I).getRawEncoding());
3291 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3295 case SELECTOR_OFFSETS: {
3296 F.SelectorOffsets = (const uint32_t *)Blob.data();
3297 F.LocalNumSelectors = Record[0];
3298 unsigned LocalBaseSelectorID = Record[1];
3299 F.BaseSelectorID = getTotalNumSelectors();
3301 if (F.LocalNumSelectors > 0) {
3302 // Introduce the global -> local mapping for selectors within this
3304 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3306 // Introduce the local -> global mapping for selectors within this
3308 F.SelectorRemap.insertOrReplace(
3309 std::make_pair(LocalBaseSelectorID,
3310 F.BaseSelectorID - LocalBaseSelectorID));
3312 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3318 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3320 F.SelectorLookupTable
3321 = ASTSelectorLookupTable::Create(
3322 F.SelectorLookupTableData + Record[0],
3323 F.SelectorLookupTableData,
3324 ASTSelectorLookupTrait(*this, F));
3325 TotalNumMethodPoolEntries += Record[1];
3328 case REFERENCED_SELECTOR_POOL:
3329 if (!Record.empty()) {
3330 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3331 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3333 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3339 case PP_CONDITIONAL_STACK:
3340 if (!Record.empty()) {
3341 unsigned Idx = 0, End = Record.size() - 1;
3342 bool ReachedEOFWhileSkipping = Record[Idx++];
3343 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3344 if (ReachedEOFWhileSkipping) {
3345 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3346 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3347 bool FoundNonSkipPortion = Record[Idx++];
3348 bool FoundElse = Record[Idx++];
3349 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3350 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3351 FoundElse, ElseLoc);
3353 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3355 auto Loc = ReadSourceLocation(F, Record, Idx);
3356 bool WasSkipping = Record[Idx++];
3357 bool FoundNonSkip = Record[Idx++];
3358 bool FoundElse = Record[Idx++];
3359 ConditionalStack.push_back(
3360 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3362 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3366 case PP_COUNTER_VALUE:
3367 if (!Record.empty() && Listener)
3368 Listener->ReadCounter(F, Record[0]);
3371 case FILE_SORTED_DECLS:
3372 F.FileSortedDecls = (const DeclID *)Blob.data();
3373 F.NumFileSortedDecls = Record[0];
3376 case SOURCE_LOCATION_OFFSETS: {
3377 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3378 F.LocalNumSLocEntries = Record[0];
3379 unsigned SLocSpaceSize = Record[1];
3380 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3381 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3382 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3384 if (!F.SLocEntryBaseID) {
3385 Error("ran out of source locations");
3388 // Make our entry in the range map. BaseID is negative and growing, so
3389 // we invert it. Because we invert it, though, we need the other end of
3391 unsigned RangeStart =
3392 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3393 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3394 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3396 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3397 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3398 GlobalSLocOffsetMap.insert(
3399 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3400 - SLocSpaceSize,&F));
3402 // Initialize the remapping table.
3403 // Invalid stays invalid.
3404 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3405 // This module. Base was 2 when being compiled.
3406 F.SLocRemap.insertOrReplace(std::make_pair(2U,
3407 static_cast<int>(F.SLocEntryBaseOffset - 2)));
3409 TotalNumSLocEntries += F.LocalNumSLocEntries;
3413 case MODULE_OFFSET_MAP:
3414 F.ModuleOffsetMap = Blob;
3417 case SOURCE_MANAGER_LINE_TABLE:
3418 if (ParseLineTable(F, Record)) {
3419 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3424 case SOURCE_LOCATION_PRELOADS: {
3425 // Need to transform from the local view (1-based IDs) to the global view,
3426 // which is based off F.SLocEntryBaseID.
3427 if (!F.PreloadSLocEntries.empty()) {
3428 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3432 F.PreloadSLocEntries.swap(Record);
3436 case EXT_VECTOR_DECLS:
3437 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3438 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3442 if (Record.size() % 3 != 0) {
3443 Error("Invalid VTABLE_USES record");
3447 // Later tables overwrite earlier ones.
3448 // FIXME: Modules will have some trouble with this. This is clearly not
3449 // the right way to do this.
3452 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3453 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3454 VTableUses.push_back(
3455 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3456 VTableUses.push_back(Record[Idx++]);
3460 case PENDING_IMPLICIT_INSTANTIATIONS:
3461 if (PendingInstantiations.size() % 2 != 0) {
3462 Error("Invalid existing PendingInstantiations");
3466 if (Record.size() % 2 != 0) {
3467 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3471 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3472 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3473 PendingInstantiations.push_back(
3474 ReadSourceLocation(F, Record, I).getRawEncoding());
3478 case SEMA_DECL_REFS:
3479 if (Record.size() != 3) {
3480 Error("Invalid SEMA_DECL_REFS block");
3483 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3484 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3487 case PPD_ENTITIES_OFFSETS: {
3488 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3489 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3490 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3492 unsigned LocalBasePreprocessedEntityID = Record[0];
3494 unsigned StartingID;
3495 if (!PP.getPreprocessingRecord())
3496 PP.createPreprocessingRecord();
3497 if (!PP.getPreprocessingRecord()->getExternalSource())
3498 PP.getPreprocessingRecord()->SetExternalSource(*this);
3500 = PP.getPreprocessingRecord()
3501 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3502 F.BasePreprocessedEntityID = StartingID;
3504 if (F.NumPreprocessedEntities > 0) {
3505 // Introduce the global -> local mapping for preprocessed entities in
3507 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3509 // Introduce the local -> global mapping for preprocessed entities in
3511 F.PreprocessedEntityRemap.insertOrReplace(
3512 std::make_pair(LocalBasePreprocessedEntityID,
3513 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3519 case PPD_SKIPPED_RANGES: {
3520 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3521 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3522 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3524 if (!PP.getPreprocessingRecord())
3525 PP.createPreprocessingRecord();
3526 if (!PP.getPreprocessingRecord()->getExternalSource())
3527 PP.getPreprocessingRecord()->SetExternalSource(*this);
3528 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3529 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3531 if (F.NumPreprocessedSkippedRanges > 0)
3532 GlobalSkippedRangeMap.insert(
3533 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3537 case DECL_UPDATE_OFFSETS:
3538 if (Record.size() % 2 != 0) {
3539 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3542 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3543 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3544 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3546 // If we've already loaded the decl, perform the updates when we finish
3547 // loading this block.
3548 if (Decl *D = GetExistingDecl(ID))
3549 PendingUpdateRecords.push_back(
3550 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3554 case OBJC_CATEGORIES_MAP:
3555 if (F.LocalNumObjCCategoriesInMap != 0) {
3556 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3560 F.LocalNumObjCCategoriesInMap = Record[0];
3561 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3564 case OBJC_CATEGORIES:
3565 F.ObjCCategories.swap(Record);
3568 case CUDA_SPECIAL_DECL_REFS:
3569 // Later tables overwrite earlier ones.
3570 // FIXME: Modules will have trouble with this.
3571 CUDASpecialDeclRefs.clear();
3572 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3573 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3576 case HEADER_SEARCH_TABLE:
3577 F.HeaderFileInfoTableData = Blob.data();
3578 F.LocalNumHeaderFileInfos = Record[1];
3580 F.HeaderFileInfoTable
3581 = HeaderFileInfoLookupTable::Create(
3582 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3583 (const unsigned char *)F.HeaderFileInfoTableData,
3584 HeaderFileInfoTrait(*this, F,
3585 &PP.getHeaderSearchInfo(),
3586 Blob.data() + Record[2]));
3588 PP.getHeaderSearchInfo().SetExternalSource(this);
3589 if (!PP.getHeaderSearchInfo().getExternalLookup())
3590 PP.getHeaderSearchInfo().SetExternalLookup(this);
3594 case FP_PRAGMA_OPTIONS:
3595 // Later tables overwrite earlier ones.
3596 FPPragmaOptions.swap(Record);
3599 case OPENCL_EXTENSIONS:
3600 for (unsigned I = 0, E = Record.size(); I != E; ) {
3601 auto Name = ReadString(Record, I);
3602 auto &Opt = OpenCLExtensions.OptMap[Name];
3603 Opt.Supported = Record[I++] != 0;
3604 Opt.Enabled = Record[I++] != 0;
3605 Opt.Avail = Record[I++];
3606 Opt.Core = Record[I++];
3610 case OPENCL_EXTENSION_TYPES:
3611 for (unsigned I = 0, E = Record.size(); I != E;) {
3612 auto TypeID = static_cast<::TypeID>(Record[I++]);
3613 auto *Type = GetType(TypeID).getTypePtr();
3614 auto NumExt = static_cast<unsigned>(Record[I++]);
3615 for (unsigned II = 0; II != NumExt; ++II) {
3616 auto Ext = ReadString(Record, I);
3617 OpenCLTypeExtMap[Type].insert(Ext);
3622 case OPENCL_EXTENSION_DECLS:
3623 for (unsigned I = 0, E = Record.size(); I != E;) {
3624 auto DeclID = static_cast<::DeclID>(Record[I++]);
3625 auto *Decl = GetDecl(DeclID);
3626 auto NumExt = static_cast<unsigned>(Record[I++]);
3627 for (unsigned II = 0; II != NumExt; ++II) {
3628 auto Ext = ReadString(Record, I);
3629 OpenCLDeclExtMap[Decl].insert(Ext);
3634 case TENTATIVE_DEFINITIONS:
3635 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3636 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3639 case KNOWN_NAMESPACES:
3640 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3641 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3644 case UNDEFINED_BUT_USED:
3645 if (UndefinedButUsed.size() % 2 != 0) {
3646 Error("Invalid existing UndefinedButUsed");
3650 if (Record.size() % 2 != 0) {
3651 Error("invalid undefined-but-used record");
3654 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3655 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3656 UndefinedButUsed.push_back(
3657 ReadSourceLocation(F, Record, I).getRawEncoding());
3661 case DELETE_EXPRS_TO_ANALYZE:
3662 for (unsigned I = 0, N = Record.size(); I != N;) {
3663 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3664 const uint64_t Count = Record[I++];
3665 DelayedDeleteExprs.push_back(Count);
3666 for (uint64_t C = 0; C < Count; ++C) {
3667 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3668 bool IsArrayForm = Record[I++] == 1;
3669 DelayedDeleteExprs.push_back(IsArrayForm);
3674 case IMPORTED_MODULES:
3675 if (!F.isModule()) {
3676 // If we aren't loading a module (which has its own exports), make
3677 // all of the imported modules visible.
3678 // FIXME: Deal with macros-only imports.
3679 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3680 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3681 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3683 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3684 if (DeserializationListener)
3685 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3691 case MACRO_OFFSET: {
3692 if (F.LocalNumMacros != 0) {
3693 Error("duplicate MACRO_OFFSET record in AST file");
3696 F.MacroOffsets = (const uint32_t *)Blob.data();
3697 F.LocalNumMacros = Record[0];
3698 unsigned LocalBaseMacroID = Record[1];
3699 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3700 F.BaseMacroID = getTotalNumMacros();
3702 if (F.LocalNumMacros > 0) {
3703 // Introduce the global -> local mapping for macros within this module.
3704 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3706 // Introduce the local -> global mapping for macros within this module.
3707 F.MacroRemap.insertOrReplace(
3708 std::make_pair(LocalBaseMacroID,
3709 F.BaseMacroID - LocalBaseMacroID));
3711 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3716 case LATE_PARSED_TEMPLATE:
3717 LateParsedTemplates.append(Record.begin(), Record.end());
3720 case OPTIMIZE_PRAGMA_OPTIONS:
3721 if (Record.size() != 1) {
3722 Error("invalid pragma optimize record");
3725 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3728 case MSSTRUCT_PRAGMA_OPTIONS:
3729 if (Record.size() != 1) {
3730 Error("invalid pragma ms_struct record");
3733 PragmaMSStructState = Record[0];
3736 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3737 if (Record.size() != 2) {
3738 Error("invalid pragma ms_struct record");
3741 PragmaMSPointersToMembersState = Record[0];
3742 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3745 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3746 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3747 UnusedLocalTypedefNameCandidates.push_back(
3748 getGlobalDeclID(F, Record[I]));
3751 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3752 if (Record.size() != 1) {
3753 Error("invalid cuda pragma options record");
3756 ForceCUDAHostDeviceDepth = Record[0];
3759 case PACK_PRAGMA_OPTIONS: {
3760 if (Record.size() < 3) {
3761 Error("invalid pragma pack record");
3764 PragmaPackCurrentValue = Record[0];
3765 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3766 unsigned NumStackEntries = Record[2];
3768 // Reset the stack when importing a new module.
3769 PragmaPackStack.clear();
3770 for (unsigned I = 0; I < NumStackEntries; ++I) {
3771 PragmaPackStackEntry Entry;
3772 Entry.Value = Record[Idx++];
3773 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3774 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3775 PragmaPackStrings.push_back(ReadString(Record, Idx));
3776 Entry.SlotLabel = PragmaPackStrings.back();
3777 PragmaPackStack.push_back(Entry);
3782 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3783 if (Record.size() < 3) {
3784 Error("invalid pragma pack record");
3787 FpPragmaCurrentValue = Record[0];
3788 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3789 unsigned NumStackEntries = Record[2];
3791 // Reset the stack when importing a new module.
3792 FpPragmaStack.clear();
3793 for (unsigned I = 0; I < NumStackEntries; ++I) {
3794 FpPragmaStackEntry Entry;
3795 Entry.Value = Record[Idx++];
3796 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3797 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3798 FpPragmaStrings.push_back(ReadString(Record, Idx));
3799 Entry.SlotLabel = FpPragmaStrings.back();
3800 FpPragmaStack.push_back(Entry);
3805 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3806 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3807 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3813 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3814 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3816 // Additional remapping information.
3817 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3818 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3819 F.ModuleOffsetMap = StringRef();
3821 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3822 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3823 F.SLocRemap.insert(std::make_pair(0U, 0));
3824 F.SLocRemap.insert(std::make_pair(2U, 1));
3827 // Continuous range maps we may be updating in our module.
3828 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3829 RemapBuilder SLocRemap(F.SLocRemap);
3830 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3831 RemapBuilder MacroRemap(F.MacroRemap);
3832 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3833 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3834 RemapBuilder SelectorRemap(F.SelectorRemap);
3835 RemapBuilder DeclRemap(F.DeclRemap);
3836 RemapBuilder TypeRemap(F.TypeRemap);
3838 while (Data < DataEnd) {
3839 // FIXME: Looking up dependency modules by filename is horrible. Let's
3840 // start fixing this with prebuilt, explicit and implicit modules and see
3842 using namespace llvm::support;
3843 ModuleKind Kind = static_cast<ModuleKind>(
3844 endian::readNext<uint8_t, little, unaligned>(Data));
3845 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3846 StringRef Name = StringRef((const char*)Data, Len);
3848 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3849 Kind == MK_ImplicitModule
3850 ? ModuleMgr.lookupByModuleName(Name)
3851 : ModuleMgr.lookupByFileName(Name));
3854 "SourceLocation remap refers to unknown module, cannot find ";
3855 Msg.append(std::string(Name));
3860 uint32_t SLocOffset =
3861 endian::readNext<uint32_t, little, unaligned>(Data);
3862 uint32_t IdentifierIDOffset =
3863 endian::readNext<uint32_t, little, unaligned>(Data);
3864 uint32_t MacroIDOffset =
3865 endian::readNext<uint32_t, little, unaligned>(Data);
3866 uint32_t PreprocessedEntityIDOffset =
3867 endian::readNext<uint32_t, little, unaligned>(Data);
3868 uint32_t SubmoduleIDOffset =
3869 endian::readNext<uint32_t, little, unaligned>(Data);
3870 uint32_t SelectorIDOffset =
3871 endian::readNext<uint32_t, little, unaligned>(Data);
3872 uint32_t DeclIDOffset =
3873 endian::readNext<uint32_t, little, unaligned>(Data);
3874 uint32_t TypeIndexOffset =
3875 endian::readNext<uint32_t, little, unaligned>(Data);
3877 uint32_t None = std::numeric_limits<uint32_t>::max();
3879 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3880 RemapBuilder &Remap) {
3882 Remap.insert(std::make_pair(Offset,
3883 static_cast<int>(BaseOffset - Offset)));
3885 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3886 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3887 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3888 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3889 PreprocessedEntityRemap);
3890 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3891 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3892 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3893 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3895 // Global -> local mappings.
3896 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3900 ASTReader::ASTReadResult
3901 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3902 const ModuleFile *ImportedBy,
3903 unsigned ClientLoadCapabilities) {
3905 F.ModuleMapPath = ReadPath(F, Record, Idx);
3907 // Try to resolve ModuleName in the current header search context and
3908 // verify that it is found in the same module map file as we saved. If the
3909 // top-level AST file is a main file, skip this check because there is no
3910 // usable header search context.
3911 assert(!F.ModuleName.empty() &&
3912 "MODULE_NAME should come before MODULE_MAP_FILE");
3913 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3914 // An implicitly-loaded module file should have its module listed in some
3915 // module map file that we've already loaded.
3916 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3917 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3918 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3919 // Don't emit module relocation error if we have -fno-validate-pch
3920 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3921 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3922 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3923 // This module was defined by an imported (explicit) module.
3924 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3925 << ASTFE->getName();
3927 // This module was built with a different module map.
3928 Diag(diag::err_imported_module_not_found)
3929 << F.ModuleName << F.FileName
3930 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3932 // In case it was imported by a PCH, there's a chance the user is
3933 // just missing to include the search path to the directory containing
3935 if (ImportedBy && ImportedBy->Kind == MK_PCH)
3936 Diag(diag::note_imported_by_pch_module_not_found)
3937 << llvm::sys::path::parent_path(F.ModuleMapPath);
3943 assert(M->Name == F.ModuleName && "found module with different name");
3945 // Check the primary module map file.
3946 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3947 if (!StoredModMap || *StoredModMap != ModMap) {
3948 assert(ModMap && "found module is missing module map file");
3949 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3950 "top-level import should be verified");
3951 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3952 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3953 Diag(diag::err_imported_module_modmap_changed)
3954 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3955 << ModMap->getName() << F.ModuleMapPath << NotImported;
3959 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3960 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3961 // FIXME: we should use input files rather than storing names.
3962 std::string Filename = ReadPath(F, Record, Idx);
3963 auto F = FileMgr.getFile(Filename, false, false);
3965 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3966 Error("could not find file '" + Filename +"' referenced by AST file");
3969 AdditionalStoredMaps.insert(*F);
3972 // Check any additional module map files (e.g. module.private.modulemap)
3973 // that are not in the pcm.
3974 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3975 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3976 // Remove files that match
3977 // Note: SmallPtrSet::erase is really remove
3978 if (!AdditionalStoredMaps.erase(ModMap)) {
3979 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3980 Diag(diag::err_module_different_modmap)
3981 << F.ModuleName << /*new*/0 << ModMap->getName();
3987 // Check any additional module map files that are in the pcm, but not
3988 // found in header search. Cases that match are already removed.
3989 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3990 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3991 Diag(diag::err_module_different_modmap)
3992 << F.ModuleName << /*not new*/1 << ModMap->getName();
3998 Listener->ReadModuleMapFile(F.ModuleMapPath);
4002 /// Move the given method to the back of the global list of methods.
4003 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4004 // Find the entry for this selector in the method pool.
4005 Sema::GlobalMethodPool::iterator Known
4006 = S.MethodPool.find(Method->getSelector());
4007 if (Known == S.MethodPool.end())
4010 // Retrieve the appropriate method list.
4011 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4012 : Known->second.second;
4014 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4016 if (List->getMethod() == Method) {
4024 if (List->getNext())
4025 List->setMethod(List->getNext()->getMethod());
4027 List->setMethod(Method);
4031 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4032 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4033 for (Decl *D : Names) {
4034 bool wasHidden = !D->isUnconditionallyVisible();
4035 D->setVisibleDespiteOwningModule();
4037 if (wasHidden && SemaObj) {
4038 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4039 moveMethodToBackOfGlobalList(*SemaObj, Method);
4045 void ASTReader::makeModuleVisible(Module *Mod,
4046 Module::NameVisibilityKind NameVisibility,
4047 SourceLocation ImportLoc) {
4048 llvm::SmallPtrSet<Module *, 4> Visited;
4049 SmallVector<Module *, 4> Stack;
4050 Stack.push_back(Mod);
4051 while (!Stack.empty()) {
4052 Mod = Stack.pop_back_val();
4054 if (NameVisibility <= Mod->NameVisibility) {
4055 // This module already has this level of visibility (or greater), so
4056 // there is nothing more to do.
4060 if (Mod->isUnimportable()) {
4061 // Modules that aren't importable cannot be made visible.
4065 // Update the module's name visibility.
4066 Mod->NameVisibility = NameVisibility;
4068 // If we've already deserialized any names from this module,
4069 // mark them as visible.
4070 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4071 if (Hidden != HiddenNamesMap.end()) {
4072 auto HiddenNames = std::move(*Hidden);
4073 HiddenNamesMap.erase(Hidden);
4074 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4075 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4076 "making names visible added hidden names");
4079 // Push any exported modules onto the stack to be marked as visible.
4080 SmallVector<Module *, 16> Exports;
4081 Mod->getExportedModules(Exports);
4082 for (SmallVectorImpl<Module *>::iterator
4083 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4084 Module *Exported = *I;
4085 if (Visited.insert(Exported).second)
4086 Stack.push_back(Exported);
4091 /// We've merged the definition \p MergedDef into the existing definition
4092 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4094 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4095 NamedDecl *MergedDef) {
4096 if (!Def->isUnconditionallyVisible()) {
4097 // If MergedDef is visible or becomes visible, make the definition visible.
4098 if (MergedDef->isUnconditionallyVisible())
4099 Def->setVisibleDespiteOwningModule();
4101 getContext().mergeDefinitionIntoModule(
4102 Def, MergedDef->getImportedOwningModule(),
4103 /*NotifyListeners*/ false);
4104 PendingMergedDefinitionsToDeduplicate.insert(Def);
4109 bool ASTReader::loadGlobalIndex() {
4113 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4114 !PP.getLangOpts().Modules)
4117 // Try to load the global index.
4118 TriedLoadingGlobalIndex = true;
4119 StringRef ModuleCachePath
4120 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4121 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4122 GlobalModuleIndex::readIndex(ModuleCachePath);
4123 if (llvm::Error Err = std::move(Result.second)) {
4124 assert(!Result.first);
4125 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4129 GlobalIndex.reset(Result.first);
4130 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4134 bool ASTReader::isGlobalIndexUnavailable() const {
4135 return PP.getLangOpts().Modules && UseGlobalIndex &&
4136 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4139 static void updateModuleTimestamp(ModuleFile &MF) {
4140 // Overwrite the timestamp file contents so that file's mtime changes.
4141 std::string TimestampFilename = MF.getTimestampFilename();
4143 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4146 OS << "Timestamp file\n";
4148 OS.clear_error(); // Avoid triggering a fatal error.
4151 /// Given a cursor at the start of an AST file, scan ahead and drop the
4152 /// cursor into the start of the given block ID, returning false on success and
4153 /// true on failure.
4154 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4156 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4158 // FIXME this drops errors on the floor.
4159 consumeError(MaybeEntry.takeError());
4162 llvm::BitstreamEntry Entry = MaybeEntry.get();
4164 switch (Entry.Kind) {
4165 case llvm::BitstreamEntry::Error:
4166 case llvm::BitstreamEntry::EndBlock:
4169 case llvm::BitstreamEntry::Record:
4170 // Ignore top-level records.
4171 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4174 // FIXME this drops errors on the floor.
4175 consumeError(Skipped.takeError());
4179 case llvm::BitstreamEntry::SubBlock:
4180 if (Entry.ID == BlockID) {
4181 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4182 // FIXME this drops the error on the floor.
4183 consumeError(std::move(Err));
4190 if (llvm::Error Err = Cursor.SkipBlock()) {
4191 // FIXME this drops the error on the floor.
4192 consumeError(std::move(Err));
4199 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4201 SourceLocation ImportLoc,
4202 unsigned ClientLoadCapabilities,
4203 SmallVectorImpl<ImportedSubmodule> *Imported) {
4204 llvm::SaveAndRestore<SourceLocation>
4205 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4207 // Defer any pending actions until we get to the end of reading the AST file.
4208 Deserializing AnASTFile(this);
4210 // Bump the generation number.
4211 unsigned PreviousGeneration = 0;
4213 PreviousGeneration = incrementGeneration(*ContextObj);
4215 unsigned NumModules = ModuleMgr.size();
4216 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4217 assert(ReadResult && "expected to return error");
4218 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4219 PP.getLangOpts().Modules
4220 ? &PP.getHeaderSearchInfo().getModuleMap()
4223 // If we find that any modules are unusable, the global index is going
4224 // to be out-of-date. Just remove it.
4225 GlobalIndex.reset();
4226 ModuleMgr.setGlobalIndex(nullptr);
4230 SmallVector<ImportedModule, 4> Loaded;
4231 switch (ASTReadResult ReadResult =
4232 ReadASTCore(FileName, Type, ImportLoc,
4233 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4234 ASTFileSignature(), ClientLoadCapabilities)) {
4238 case VersionMismatch:
4239 case ConfigurationMismatch:
4241 return removeModulesAndReturn(ReadResult);
4246 // Here comes stuff that we only do once the entire chain is loaded.
4248 // Load the AST blocks of all of the modules that we loaded. We can still
4249 // hit errors parsing the ASTs at this point.
4250 for (ImportedModule &M : Loaded) {
4251 ModuleFile &F = *M.Mod;
4253 // Read the AST block.
4254 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4255 return removeModulesAndReturn(Result);
4257 // The AST block should always have a definition for the main module.
4258 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4259 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4260 return removeModulesAndReturn(Failure);
4263 // Read the extension blocks.
4264 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4265 if (ASTReadResult Result = ReadExtensionBlock(F))
4266 return removeModulesAndReturn(Result);
4269 // Once read, set the ModuleFile bit base offset and update the size in
4270 // bits of all files we've seen.
4271 F.GlobalBitOffset = TotalModulesSizeInBits;
4272 TotalModulesSizeInBits += F.SizeInBits;
4273 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4276 // Preload source locations and interesting indentifiers.
4277 for (ImportedModule &M : Loaded) {
4278 ModuleFile &F = *M.Mod;
4280 // Preload SLocEntries.
4281 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4282 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4283 // Load it through the SourceManager and don't call ReadSLocEntry()
4284 // directly because the entry may have already been loaded in which case
4285 // calling ReadSLocEntry() directly would trigger an assertion in
4287 SourceMgr.getLoadedSLocEntryByID(Index);
4290 // Map the original source file ID into the ID space of the current
4292 if (F.OriginalSourceFileID.isValid()) {
4293 F.OriginalSourceFileID = FileID::get(
4294 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4297 // Preload all the pending interesting identifiers by marking them out of
4299 for (auto Offset : F.PreloadIdentifierOffsets) {
4300 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4301 F.IdentifierTableData + Offset);
4303 ASTIdentifierLookupTrait Trait(*this, F);
4304 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4305 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4306 auto &II = PP.getIdentifierTable().getOwn(Key);
4307 II.setOutOfDate(true);
4309 // Mark this identifier as being from an AST file so that we can track
4310 // whether we need to serialize it.
4311 markIdentifierFromAST(*this, II);
4313 // Associate the ID with the identifier so that the writer can reuse it.
4314 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4315 SetIdentifierInfo(ID, &II);
4319 // Setup the import locations and notify the module manager that we've
4320 // committed to these module files.
4321 for (ImportedModule &M : Loaded) {
4322 ModuleFile &F = *M.Mod;
4324 ModuleMgr.moduleFileAccepted(&F);
4326 // Set the import location.
4327 F.DirectImportLoc = ImportLoc;
4328 // FIXME: We assume that locations from PCH / preamble do not need
4331 F.ImportLoc = M.ImportLoc;
4333 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4336 if (!PP.getLangOpts().CPlusPlus ||
4337 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4338 Type != MK_PrebuiltModule)) {
4339 // Mark all of the identifiers in the identifier table as being out of date,
4340 // so that various accessors know to check the loaded modules when the
4341 // identifier is used.
4343 // For C++ modules, we don't need information on many identifiers (just
4344 // those that provide macros or are poisoned), so we mark all of
4345 // the interesting ones via PreloadIdentifierOffsets.
4346 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4347 IdEnd = PP.getIdentifierTable().end();
4349 Id->second->setOutOfDate(true);
4351 // Mark selectors as out of date.
4352 for (auto Sel : SelectorGeneration)
4353 SelectorOutOfDate[Sel.first] = true;
4355 // Resolve any unresolved module exports.
4356 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4357 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4358 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4359 Module *ResolvedMod = getSubmodule(GlobalID);
4361 switch (Unresolved.Kind) {
4362 case UnresolvedModuleRef::Conflict:
4364 Module::Conflict Conflict;
4365 Conflict.Other = ResolvedMod;
4366 Conflict.Message = Unresolved.String.str();
4367 Unresolved.Mod->Conflicts.push_back(Conflict);
4371 case UnresolvedModuleRef::Import:
4373 Unresolved.Mod->Imports.insert(ResolvedMod);
4376 case UnresolvedModuleRef::Export:
4377 if (ResolvedMod || Unresolved.IsWildcard)
4378 Unresolved.Mod->Exports.push_back(
4379 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4383 UnresolvedModuleRefs.clear();
4386 Imported->append(ImportedModules.begin(),
4387 ImportedModules.end());
4389 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4390 // Might be unnecessary as use declarations are only used to build the
4394 InitializeContext();
4399 if (DeserializationListener)
4400 DeserializationListener->ReaderInitialized(this);
4402 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4403 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4404 // If this AST file is a precompiled preamble, then set the
4405 // preamble file ID of the source manager to the file source file
4406 // from which the preamble was built.
4407 if (Type == MK_Preamble) {
4408 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4409 } else if (Type == MK_MainFile) {
4410 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4414 // For any Objective-C class definitions we have already loaded, make sure
4415 // that we load any additional categories.
4417 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4418 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4419 ObjCClassesLoaded[I],
4420 PreviousGeneration);
4424 if (PP.getHeaderSearchInfo()
4425 .getHeaderSearchOpts()
4426 .ModulesValidateOncePerBuildSession) {
4427 // Now we are certain that the module and all modules it depends on are
4428 // up to date. Create or update timestamp files for modules that are
4429 // located in the module cache (not for PCH files that could be anywhere
4430 // in the filesystem).
4431 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4432 ImportedModule &M = Loaded[I];
4433 if (M.Mod->Kind == MK_ImplicitModule) {
4434 updateModuleTimestamp(*M.Mod);
4442 static ASTFileSignature readASTFileSignature(StringRef PCH);
4444 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4445 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4446 // FIXME checking magic headers is done in other places such as
4447 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4448 // always done the same. Unify it all with a helper.
4449 if (!Stream.canSkipToPos(4))
4450 return llvm::createStringError(std::errc::illegal_byte_sequence,
4451 "file too small to contain AST file magic");
4452 for (unsigned C : {'C', 'P', 'C', 'H'})
4453 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4455 return llvm::createStringError(
4456 std::errc::illegal_byte_sequence,
4457 "file doesn't start with AST file magic");
4459 return Res.takeError();
4460 return llvm::Error::success();
4463 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4467 case MK_ImplicitModule:
4468 case MK_ExplicitModule:
4469 case MK_PrebuiltModule:
4473 return 2; // main source file
4475 llvm_unreachable("unknown module kind");
4478 ASTReader::ASTReadResult
4479 ASTReader::ReadASTCore(StringRef FileName,
4481 SourceLocation ImportLoc,
4482 ModuleFile *ImportedBy,
4483 SmallVectorImpl<ImportedModule> &Loaded,
4484 off_t ExpectedSize, time_t ExpectedModTime,
4485 ASTFileSignature ExpectedSignature,
4486 unsigned ClientLoadCapabilities) {
4488 std::string ErrorStr;
4489 ModuleManager::AddModuleResult AddResult
4490 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4491 getGeneration(), ExpectedSize, ExpectedModTime,
4492 ExpectedSignature, readASTFileSignature,
4495 switch (AddResult) {
4496 case ModuleManager::AlreadyLoaded:
4497 Diag(diag::remark_module_import)
4498 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4499 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4502 case ModuleManager::NewlyLoaded:
4503 // Load module file below.
4506 case ModuleManager::Missing:
4507 // The module file was missing; if the client can handle that, return
4509 if (ClientLoadCapabilities & ARR_Missing)
4512 // Otherwise, return an error.
4513 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4514 << FileName << !ErrorStr.empty()
4518 case ModuleManager::OutOfDate:
4519 // We couldn't load the module file because it is out-of-date. If the
4520 // client can handle out-of-date, return it.
4521 if (ClientLoadCapabilities & ARR_OutOfDate)
4524 // Otherwise, return an error.
4525 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4526 << FileName << !ErrorStr.empty()
4531 assert(M && "Missing module file");
4533 bool ShouldFinalizePCM = false;
4534 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4535 auto &MC = getModuleManager().getModuleCache();
4536 if (ShouldFinalizePCM)
4537 MC.finalizePCM(FileName);
4539 MC.tryToDropPCM(FileName);
4542 BitstreamCursor &Stream = F.Stream;
4543 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4544 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4546 // Sniff for the signature.
4547 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4548 Diag(diag::err_module_file_invalid)
4549 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4553 // This is used for compatibility with older PCH formats.
4554 bool HaveReadControlBlock = false;
4556 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4558 Error(MaybeEntry.takeError());
4561 llvm::BitstreamEntry Entry = MaybeEntry.get();
4563 switch (Entry.Kind) {
4564 case llvm::BitstreamEntry::Error:
4565 case llvm::BitstreamEntry::Record:
4566 case llvm::BitstreamEntry::EndBlock:
4567 Error("invalid record at top-level of AST file");
4570 case llvm::BitstreamEntry::SubBlock:
4575 case CONTROL_BLOCK_ID:
4576 HaveReadControlBlock = true;
4577 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4579 // Check that we didn't try to load a non-module AST file as a module.
4581 // FIXME: Should we also perform the converse check? Loading a module as
4582 // a PCH file sort of works, but it's a bit wonky.
4583 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4584 Type == MK_PrebuiltModule) &&
4585 F.ModuleName.empty()) {
4586 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4587 if (Result != OutOfDate ||
4588 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4589 Diag(diag::err_module_file_not_module) << FileName;
4594 case Failure: return Failure;
4595 case Missing: return Missing;
4596 case OutOfDate: return OutOfDate;
4597 case VersionMismatch: return VersionMismatch;
4598 case ConfigurationMismatch: return ConfigurationMismatch;
4599 case HadErrors: return HadErrors;
4604 if (!HaveReadControlBlock) {
4605 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4606 Diag(diag::err_pch_version_too_old);
4607 return VersionMismatch;
4610 // Record that we've loaded this module.
4611 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4612 ShouldFinalizePCM = true;
4615 case UNHASHED_CONTROL_BLOCK_ID:
4616 // This block is handled using look-ahead during ReadControlBlock. We
4617 // shouldn't get here!
4618 Error("malformed block record in AST file");
4622 if (llvm::Error Err = Stream.SkipBlock()) {
4623 Error(std::move(Err));
4630 llvm_unreachable("unexpected break; expected return");
4633 ASTReader::ASTReadResult
4634 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4635 unsigned ClientLoadCapabilities) {
4636 const HeaderSearchOptions &HSOpts =
4637 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4638 bool AllowCompatibleConfigurationMismatch =
4639 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4641 ASTReadResult Result = readUnhashedControlBlockImpl(
4642 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4644 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4646 // If F was directly imported by another module, it's implicitly validated by
4647 // the importing module.
4648 if (DisableValidation || WasImportedBy ||
4649 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4652 if (Result == Failure) {
4653 Error("malformed block record in AST file");
4657 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4658 // If this module has already been finalized in the ModuleCache, we're stuck
4659 // with it; we can only load a single version of each module.
4661 // This can happen when a module is imported in two contexts: in one, as a
4662 // user module; in another, as a system module (due to an import from
4663 // another module marked with the [system] flag). It usually indicates a
4664 // bug in the module map: this module should also be marked with [system].
4666 // If -Wno-system-headers (the default), and the first import is as a
4667 // system module, then validation will fail during the as-user import,
4668 // since -Werror flags won't have been validated. However, it's reasonable
4669 // to treat this consistently as a system module.
4671 // If -Wsystem-headers, the PCM on disk was built with
4672 // -Wno-system-headers, and the first import is as a user module, then
4673 // validation will fail during the as-system import since the PCM on disk
4674 // doesn't guarantee that -Werror was respected. However, the -Werror
4675 // flags were checked during the initial as-user import.
4676 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4677 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4685 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4686 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4687 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4688 bool ValidateDiagnosticOptions) {
4689 // Initialize a stream.
4690 BitstreamCursor Stream(StreamData);
4692 // Sniff for the signature.
4693 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4694 // FIXME this drops the error on the floor.
4695 consumeError(std::move(Err));
4699 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4700 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4703 // Read all of the records in the options block.
4705 ASTReadResult Result = Success;
4707 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4709 // FIXME this drops the error on the floor.
4710 consumeError(MaybeEntry.takeError());
4713 llvm::BitstreamEntry Entry = MaybeEntry.get();
4715 switch (Entry.Kind) {
4716 case llvm::BitstreamEntry::Error:
4717 case llvm::BitstreamEntry::SubBlock:
4720 case llvm::BitstreamEntry::EndBlock:
4723 case llvm::BitstreamEntry::Record:
4724 // The interesting case.
4728 // Read and process a record.
4730 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4731 if (!MaybeRecordType) {
4732 // FIXME this drops the error.
4735 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4738 F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4740 case AST_BLOCK_HASH:
4743 ASTFileSignature::create(Record.begin(), Record.end());
4745 case DIAGNOSTIC_OPTIONS: {
4746 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4747 if (Listener && ValidateDiagnosticOptions &&
4748 !AllowCompatibleConfigurationMismatch &&
4749 ParseDiagnosticOptions(Record, Complain, *Listener))
4750 Result = OutOfDate; // Don't return early. Read the signature.
4753 case DIAG_PRAGMA_MAPPINGS:
4756 if (F->PragmaDiagMappings.empty())
4757 F->PragmaDiagMappings.swap(Record);
4759 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4760 Record.begin(), Record.end());
4766 /// Parse a record and blob containing module file extension metadata.
4767 static bool parseModuleFileExtensionMetadata(
4768 const SmallVectorImpl<uint64_t> &Record,
4770 ModuleFileExtensionMetadata &Metadata) {
4771 if (Record.size() < 4) return true;
4773 Metadata.MajorVersion = Record[0];
4774 Metadata.MinorVersion = Record[1];
4776 unsigned BlockNameLen = Record[2];
4777 unsigned UserInfoLen = Record[3];
4779 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4781 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4782 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4783 Blob.data() + BlockNameLen + UserInfoLen);
4787 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4788 BitstreamCursor &Stream = F.Stream;
4792 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4794 Error(MaybeEntry.takeError());
4797 llvm::BitstreamEntry Entry = MaybeEntry.get();
4799 switch (Entry.Kind) {
4800 case llvm::BitstreamEntry::SubBlock:
4801 if (llvm::Error Err = Stream.SkipBlock()) {
4802 Error(std::move(Err));
4807 case llvm::BitstreamEntry::EndBlock:
4810 case llvm::BitstreamEntry::Error:
4813 case llvm::BitstreamEntry::Record:
4819 Expected<unsigned> MaybeRecCode =
4820 Stream.readRecord(Entry.ID, Record, &Blob);
4821 if (!MaybeRecCode) {
4822 Error(MaybeRecCode.takeError());
4825 switch (MaybeRecCode.get()) {
4826 case EXTENSION_METADATA: {
4827 ModuleFileExtensionMetadata Metadata;
4828 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4829 Error("malformed EXTENSION_METADATA in AST file");
4833 // Find a module file extension with this block name.
4834 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4835 if (Known == ModuleFileExtensions.end()) break;
4838 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4840 F.ExtensionReaders.push_back(std::move(Reader));
4851 void ASTReader::InitializeContext() {
4852 assert(ContextObj && "no context to initialize");
4853 ASTContext &Context = *ContextObj;
4855 // If there's a listener, notify them that we "read" the translation unit.
4856 if (DeserializationListener)
4857 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4858 Context.getTranslationUnitDecl());
4860 // FIXME: Find a better way to deal with collisions between these
4861 // built-in types. Right now, we just ignore the problem.
4863 // Load the special types.
4864 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4865 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4866 if (!Context.CFConstantStringTypeDecl)
4867 Context.setCFConstantStringType(GetType(String));
4870 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4871 QualType FileType = GetType(File);
4872 if (FileType.isNull()) {
4873 Error("FILE type is NULL");
4877 if (!Context.FILEDecl) {
4878 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4879 Context.setFILEDecl(Typedef->getDecl());
4881 const TagType *Tag = FileType->getAs<TagType>();
4883 Error("Invalid FILE type in AST file");
4886 Context.setFILEDecl(Tag->getDecl());
4891 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4892 QualType Jmp_bufType = GetType(Jmp_buf);
4893 if (Jmp_bufType.isNull()) {
4894 Error("jmp_buf type is NULL");
4898 if (!Context.jmp_bufDecl) {
4899 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4900 Context.setjmp_bufDecl(Typedef->getDecl());
4902 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4904 Error("Invalid jmp_buf type in AST file");
4907 Context.setjmp_bufDecl(Tag->getDecl());
4912 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4913 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4914 if (Sigjmp_bufType.isNull()) {
4915 Error("sigjmp_buf type is NULL");
4919 if (!Context.sigjmp_bufDecl) {
4920 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4921 Context.setsigjmp_bufDecl(Typedef->getDecl());
4923 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4924 assert(Tag && "Invalid sigjmp_buf type in AST file");
4925 Context.setsigjmp_bufDecl(Tag->getDecl());
4930 if (unsigned ObjCIdRedef
4931 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4932 if (Context.ObjCIdRedefinitionType.isNull())
4933 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4936 if (unsigned ObjCClassRedef
4937 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4938 if (Context.ObjCClassRedefinitionType.isNull())
4939 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4942 if (unsigned ObjCSelRedef
4943 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4944 if (Context.ObjCSelRedefinitionType.isNull())
4945 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4948 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4949 QualType Ucontext_tType = GetType(Ucontext_t);
4950 if (Ucontext_tType.isNull()) {
4951 Error("ucontext_t type is NULL");
4955 if (!Context.ucontext_tDecl) {
4956 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4957 Context.setucontext_tDecl(Typedef->getDecl());
4959 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4960 assert(Tag && "Invalid ucontext_t type in AST file");
4961 Context.setucontext_tDecl(Tag->getDecl());
4967 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4969 // If there were any CUDA special declarations, deserialize them.
4970 if (!CUDASpecialDeclRefs.empty()) {
4971 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4972 Context.setcudaConfigureCallDecl(
4973 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4976 // Re-export any modules that were imported by a non-module AST file.
4977 // FIXME: This does not make macro-only imports visible again.
4978 for (auto &Import : ImportedModules) {
4979 if (Module *Imported = getSubmodule(Import.ID)) {
4980 makeModuleVisible(Imported, Module::AllVisible,
4981 /*ImportLoc=*/Import.ImportLoc);
4982 if (Import.ImportLoc.isValid())
4983 PP.makeModuleVisible(Imported, Import.ImportLoc);
4984 // FIXME: should we tell Sema to make the module visible too?
4987 ImportedModules.clear();
4990 void ASTReader::finalizeForWriting() {
4991 // Nothing to do for now.
4994 /// Reads and return the signature record from \p PCH's control block, or
4996 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4997 BitstreamCursor Stream(PCH);
4998 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4999 // FIXME this drops the error on the floor.
5000 consumeError(std::move(Err));
5001 return ASTFileSignature();
5004 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5005 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5006 return ASTFileSignature();
5008 // Scan for SIGNATURE inside the diagnostic options block.
5009 ASTReader::RecordData Record;
5011 Expected<llvm::BitstreamEntry> MaybeEntry =
5012 Stream.advanceSkippingSubblocks();
5014 // FIXME this drops the error on the floor.
5015 consumeError(MaybeEntry.takeError());
5016 return ASTFileSignature();
5018 llvm::BitstreamEntry Entry = MaybeEntry.get();
5020 if (Entry.Kind != llvm::BitstreamEntry::Record)
5021 return ASTFileSignature();
5025 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5027 // FIXME this drops the error on the floor.
5028 consumeError(MaybeRecord.takeError());
5029 return ASTFileSignature();
5031 if (SIGNATURE == MaybeRecord.get())
5032 return ASTFileSignature::create(Record.begin(),
5033 Record.begin() + ASTFileSignature::size);
5037 /// Retrieve the name of the original source file name
5038 /// directly from the AST file, without actually loading the AST
5040 std::string ASTReader::getOriginalSourceFile(
5041 const std::string &ASTFileName, FileManager &FileMgr,
5042 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5043 // Open the AST file.
5044 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5046 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5047 << ASTFileName << Buffer.getError().message();
5048 return std::string();
5051 // Initialize the stream
5052 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5054 // Sniff for the signature.
5055 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5056 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5057 return std::string();
5060 // Scan for the CONTROL_BLOCK_ID block.
5061 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5062 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5063 return std::string();
5066 // Scan for ORIGINAL_FILE inside the control block.
5069 Expected<llvm::BitstreamEntry> MaybeEntry =
5070 Stream.advanceSkippingSubblocks();
5072 // FIXME this drops errors on the floor.
5073 consumeError(MaybeEntry.takeError());
5074 return std::string();
5076 llvm::BitstreamEntry Entry = MaybeEntry.get();
5078 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5079 return std::string();
5081 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5082 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5083 return std::string();
5088 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5090 // FIXME this drops the errors on the floor.
5091 consumeError(MaybeRecord.takeError());
5092 return std::string();
5094 if (ORIGINAL_FILE == MaybeRecord.get())
5101 class SimplePCHValidator : public ASTReaderListener {
5102 const LangOptions &ExistingLangOpts;
5103 const TargetOptions &ExistingTargetOpts;
5104 const PreprocessorOptions &ExistingPPOpts;
5105 std::string ExistingModuleCachePath;
5106 FileManager &FileMgr;
5109 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5110 const TargetOptions &ExistingTargetOpts,
5111 const PreprocessorOptions &ExistingPPOpts,
5112 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5113 : ExistingLangOpts(ExistingLangOpts),
5114 ExistingTargetOpts(ExistingTargetOpts),
5115 ExistingPPOpts(ExistingPPOpts),
5116 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5118 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5119 bool AllowCompatibleDifferences) override {
5120 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5121 AllowCompatibleDifferences);
5124 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5125 bool AllowCompatibleDifferences) override {
5126 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5127 AllowCompatibleDifferences);
5130 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5131 StringRef SpecificModuleCachePath,
5132 bool Complain) override {
5133 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5134 ExistingModuleCachePath,
5135 nullptr, ExistingLangOpts);
5138 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5140 std::string &SuggestedPredefines) override {
5141 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5142 SuggestedPredefines, ExistingLangOpts);
5148 bool ASTReader::readASTFileControlBlock(
5149 StringRef Filename, FileManager &FileMgr,
5150 const PCHContainerReader &PCHContainerRdr,
5151 bool FindModuleFileExtensions,
5152 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5153 // Open the AST file.
5154 // FIXME: This allows use of the VFS; we do not allow use of the
5155 // VFS when actually loading a module.
5156 auto Buffer = FileMgr.getBufferForFile(Filename);
5161 // Initialize the stream
5162 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5163 BitstreamCursor Stream(Bytes);
5165 // Sniff for the signature.
5166 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5167 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5171 // Scan for the CONTROL_BLOCK_ID block.
5172 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5175 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5176 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5177 bool NeedsImports = Listener.needsImportVisitation();
5178 BitstreamCursor InputFilesCursor;
5181 std::string ModuleDir;
5182 bool DoneWithControlBlock = false;
5183 while (!DoneWithControlBlock) {
5184 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5186 // FIXME this drops the error on the floor.
5187 consumeError(MaybeEntry.takeError());
5190 llvm::BitstreamEntry Entry = MaybeEntry.get();
5192 switch (Entry.Kind) {
5193 case llvm::BitstreamEntry::SubBlock: {
5195 case OPTIONS_BLOCK_ID: {
5196 std::string IgnoredSuggestedPredefines;
5197 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5198 /*AllowCompatibleConfigurationMismatch*/ false,
5199 Listener, IgnoredSuggestedPredefines) != Success)
5204 case INPUT_FILES_BLOCK_ID:
5205 InputFilesCursor = Stream;
5206 if (llvm::Error Err = Stream.SkipBlock()) {
5207 // FIXME this drops the error on the floor.
5208 consumeError(std::move(Err));
5211 if (NeedsInputFiles &&
5212 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5217 if (llvm::Error Err = Stream.SkipBlock()) {
5218 // FIXME this drops the error on the floor.
5219 consumeError(std::move(Err));
5228 case llvm::BitstreamEntry::EndBlock:
5229 DoneWithControlBlock = true;
5232 case llvm::BitstreamEntry::Error:
5235 case llvm::BitstreamEntry::Record:
5239 if (DoneWithControlBlock) break;
5243 Expected<unsigned> MaybeRecCode =
5244 Stream.readRecord(Entry.ID, Record, &Blob);
5245 if (!MaybeRecCode) {
5246 // FIXME this drops the error.
5249 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5251 if (Record[0] != VERSION_MAJOR)
5253 if (Listener.ReadFullVersionInformation(Blob))
5257 Listener.ReadModuleName(Blob);
5259 case MODULE_DIRECTORY:
5260 ModuleDir = std::string(Blob);
5262 case MODULE_MAP_FILE: {
5264 auto Path = ReadString(Record, Idx);
5265 ResolveImportedPath(Path, ModuleDir);
5266 Listener.ReadModuleMapFile(Path);
5269 case INPUT_FILE_OFFSETS: {
5270 if (!NeedsInputFiles)
5273 unsigned NumInputFiles = Record[0];
5274 unsigned NumUserFiles = Record[1];
5275 const llvm::support::unaligned_uint64_t *InputFileOffs =
5276 (const llvm::support::unaligned_uint64_t *)Blob.data();
5277 for (unsigned I = 0; I != NumInputFiles; ++I) {
5278 // Go find this input file.
5279 bool isSystemFile = I >= NumUserFiles;
5281 if (isSystemFile && !NeedsSystemInputFiles)
5282 break; // the rest are system input files
5284 BitstreamCursor &Cursor = InputFilesCursor;
5285 SavedStreamPosition SavedPosition(Cursor);
5286 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5287 // FIXME this drops errors on the floor.
5288 consumeError(std::move(Err));
5291 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5293 // FIXME this drops errors on the floor.
5294 consumeError(MaybeCode.takeError());
5296 unsigned Code = MaybeCode.get();
5300 bool shouldContinue = false;
5301 Expected<unsigned> MaybeRecordType =
5302 Cursor.readRecord(Code, Record, &Blob);
5303 if (!MaybeRecordType) {
5304 // FIXME this drops errors on the floor.
5305 consumeError(MaybeRecordType.takeError());
5307 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5308 case INPUT_FILE_HASH:
5311 bool Overridden = static_cast<bool>(Record[3]);
5312 std::string Filename = std::string(Blob);
5313 ResolveImportedPath(Filename, ModuleDir);
5314 shouldContinue = Listener.visitInputFile(
5315 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5318 if (!shouldContinue)
5328 unsigned Idx = 0, N = Record.size();
5330 // Read information about the AST file.
5333 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5334 std::string ModuleName = ReadString(Record, Idx);
5335 std::string Filename = ReadString(Record, Idx);
5336 ResolveImportedPath(Filename, ModuleDir);
5337 Listener.visitImport(ModuleName, Filename);
5343 // No other validation to perform.
5348 // Look for module file extension blocks, if requested.
5349 if (FindModuleFileExtensions) {
5350 BitstreamCursor SavedStream = Stream;
5351 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5352 bool DoneWithExtensionBlock = false;
5353 while (!DoneWithExtensionBlock) {
5354 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5356 // FIXME this drops the error.
5359 llvm::BitstreamEntry Entry = MaybeEntry.get();
5361 switch (Entry.Kind) {
5362 case llvm::BitstreamEntry::SubBlock:
5363 if (llvm::Error Err = Stream.SkipBlock()) {
5364 // FIXME this drops the error on the floor.
5365 consumeError(std::move(Err));
5370 case llvm::BitstreamEntry::EndBlock:
5371 DoneWithExtensionBlock = true;
5374 case llvm::BitstreamEntry::Error:
5377 case llvm::BitstreamEntry::Record:
5383 Expected<unsigned> MaybeRecCode =
5384 Stream.readRecord(Entry.ID, Record, &Blob);
5385 if (!MaybeRecCode) {
5386 // FIXME this drops the error.
5389 switch (MaybeRecCode.get()) {
5390 case EXTENSION_METADATA: {
5391 ModuleFileExtensionMetadata Metadata;
5392 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5395 Listener.readModuleFileExtension(Metadata);
5401 Stream = SavedStream;
5404 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5405 if (readUnhashedControlBlockImpl(
5406 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5407 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5408 ValidateDiagnosticOptions) != Success)
5414 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5415 const PCHContainerReader &PCHContainerRdr,
5416 const LangOptions &LangOpts,
5417 const TargetOptions &TargetOpts,
5418 const PreprocessorOptions &PPOpts,
5419 StringRef ExistingModuleCachePath) {
5420 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5421 ExistingModuleCachePath, FileMgr);
5422 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5423 /*FindModuleFileExtensions=*/false,
5425 /*ValidateDiagnosticOptions=*/true);
5428 ASTReader::ASTReadResult
5429 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5430 // Enter the submodule block.
5431 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5432 Error(std::move(Err));
5436 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5438 Module *CurrentModule = nullptr;
5441 Expected<llvm::BitstreamEntry> MaybeEntry =
5442 F.Stream.advanceSkippingSubblocks();
5444 Error(MaybeEntry.takeError());
5447 llvm::BitstreamEntry Entry = MaybeEntry.get();
5449 switch (Entry.Kind) {
5450 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5451 case llvm::BitstreamEntry::Error:
5452 Error("malformed block record in AST file");
5454 case llvm::BitstreamEntry::EndBlock:
5456 case llvm::BitstreamEntry::Record:
5457 // The interesting case.
5464 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5466 Error(MaybeKind.takeError());
5469 unsigned Kind = MaybeKind.get();
5471 if ((Kind == SUBMODULE_METADATA) != First) {
5472 Error("submodule metadata record should be at beginning of block");
5477 // Submodule information is only valid if we have a current module.
5478 // FIXME: Should we error on these cases?
5479 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5480 Kind != SUBMODULE_DEFINITION)
5484 default: // Default behavior: ignore.
5487 case SUBMODULE_DEFINITION: {
5488 if (Record.size() < 12) {
5489 Error("malformed module definition");
5493 StringRef Name = Blob;
5495 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5496 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5497 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5498 bool IsFramework = Record[Idx++];
5499 bool IsExplicit = Record[Idx++];
5500 bool IsSystem = Record[Idx++];
5501 bool IsExternC = Record[Idx++];
5502 bool InferSubmodules = Record[Idx++];
5503 bool InferExplicitSubmodules = Record[Idx++];
5504 bool InferExportWildcard = Record[Idx++];
5505 bool ConfigMacrosExhaustive = Record[Idx++];
5506 bool ModuleMapIsPrivate = Record[Idx++];
5508 Module *ParentModule = nullptr;
5510 ParentModule = getSubmodule(Parent);
5512 // Retrieve this (sub)module from the module map, creating it if
5515 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5518 // FIXME: set the definition loc for CurrentModule, or call
5519 // ModMap.setInferredModuleAllowedBy()
5521 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5522 if (GlobalIndex >= SubmodulesLoaded.size() ||
5523 SubmodulesLoaded[GlobalIndex]) {
5524 Error("too many submodules");
5528 if (!ParentModule) {
5529 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5530 // Don't emit module relocation error if we have -fno-validate-pch
5531 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5532 CurFile != F.File) {
5533 Error(diag::err_module_file_conflict,
5534 CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5540 F.DidReadTopLevelSubmodule = true;
5541 CurrentModule->setASTFile(F.File);
5542 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5545 CurrentModule->Kind = Kind;
5546 CurrentModule->Signature = F.Signature;
5547 CurrentModule->IsFromModuleFile = true;
5548 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5549 CurrentModule->IsExternC = IsExternC;
5550 CurrentModule->InferSubmodules = InferSubmodules;
5551 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5552 CurrentModule->InferExportWildcard = InferExportWildcard;
5553 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5554 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5555 if (DeserializationListener)
5556 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5558 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5560 // Clear out data that will be replaced by what is in the module file.
5561 CurrentModule->LinkLibraries.clear();
5562 CurrentModule->ConfigMacros.clear();
5563 CurrentModule->UnresolvedConflicts.clear();
5564 CurrentModule->Conflicts.clear();
5566 // The module is available unless it's missing a requirement; relevant
5567 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5568 // Missing headers that were present when the module was built do not
5569 // make it unavailable -- if we got this far, this must be an explicitly
5570 // imported module file.
5571 CurrentModule->Requirements.clear();
5572 CurrentModule->MissingHeaders.clear();
5573 CurrentModule->IsUnimportable =
5574 ParentModule && ParentModule->IsUnimportable;
5575 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5579 case SUBMODULE_UMBRELLA_HEADER: {
5580 std::string Filename = std::string(Blob);
5581 ResolveImportedPath(F, Filename);
5582 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5583 if (!CurrentModule->getUmbrellaHeader())
5584 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5585 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5586 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5587 Error("mismatched umbrella headers in submodule");
5594 case SUBMODULE_HEADER:
5595 case SUBMODULE_EXCLUDED_HEADER:
5596 case SUBMODULE_PRIVATE_HEADER:
5597 // We lazily associate headers with their modules via the HeaderInfo table.
5598 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5599 // of complete filenames or remove it entirely.
5602 case SUBMODULE_TEXTUAL_HEADER:
5603 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5604 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5608 case SUBMODULE_TOPHEADER:
5609 CurrentModule->addTopHeaderFilename(Blob);
5612 case SUBMODULE_UMBRELLA_DIR: {
5613 std::string Dirname = std::string(Blob);
5614 ResolveImportedPath(F, Dirname);
5615 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5616 if (!CurrentModule->getUmbrellaDir())
5617 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5618 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5619 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5620 Error("mismatched umbrella directories in submodule");
5627 case SUBMODULE_METADATA: {
5628 F.BaseSubmoduleID = getTotalNumSubmodules();
5629 F.LocalNumSubmodules = Record[0];
5630 unsigned LocalBaseSubmoduleID = Record[1];
5631 if (F.LocalNumSubmodules > 0) {
5632 // Introduce the global -> local mapping for submodules within this
5634 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5636 // Introduce the local -> global mapping for submodules within this
5638 F.SubmoduleRemap.insertOrReplace(
5639 std::make_pair(LocalBaseSubmoduleID,
5640 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5642 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5647 case SUBMODULE_IMPORTS:
5648 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5649 UnresolvedModuleRef Unresolved;
5650 Unresolved.File = &F;
5651 Unresolved.Mod = CurrentModule;
5652 Unresolved.ID = Record[Idx];
5653 Unresolved.Kind = UnresolvedModuleRef::Import;
5654 Unresolved.IsWildcard = false;
5655 UnresolvedModuleRefs.push_back(Unresolved);
5659 case SUBMODULE_EXPORTS:
5660 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5661 UnresolvedModuleRef Unresolved;
5662 Unresolved.File = &F;
5663 Unresolved.Mod = CurrentModule;
5664 Unresolved.ID = Record[Idx];
5665 Unresolved.Kind = UnresolvedModuleRef::Export;
5666 Unresolved.IsWildcard = Record[Idx + 1];
5667 UnresolvedModuleRefs.push_back(Unresolved);
5670 // Once we've loaded the set of exports, there's no reason to keep
5671 // the parsed, unresolved exports around.
5672 CurrentModule->UnresolvedExports.clear();
5675 case SUBMODULE_REQUIRES:
5676 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5677 PP.getTargetInfo());
5680 case SUBMODULE_LINK_LIBRARY:
5681 ModMap.resolveLinkAsDependencies(CurrentModule);
5682 CurrentModule->LinkLibraries.push_back(
5683 Module::LinkLibrary(std::string(Blob), Record[0]));
5686 case SUBMODULE_CONFIG_MACRO:
5687 CurrentModule->ConfigMacros.push_back(Blob.str());
5690 case SUBMODULE_CONFLICT: {
5691 UnresolvedModuleRef Unresolved;
5692 Unresolved.File = &F;
5693 Unresolved.Mod = CurrentModule;
5694 Unresolved.ID = Record[0];
5695 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5696 Unresolved.IsWildcard = false;
5697 Unresolved.String = Blob;
5698 UnresolvedModuleRefs.push_back(Unresolved);
5702 case SUBMODULE_INITIALIZERS: {
5705 SmallVector<uint32_t, 16> Inits;
5706 for (auto &ID : Record)
5707 Inits.push_back(getGlobalDeclID(F, ID));
5708 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5712 case SUBMODULE_EXPORT_AS:
5713 CurrentModule->ExportAsModule = Blob.str();
5714 ModMap.addLinkAsDependency(CurrentModule);
5720 /// Parse the record that corresponds to a LangOptions data
5723 /// This routine parses the language options from the AST file and then gives
5724 /// them to the AST listener if one is set.
5726 /// \returns true if the listener deems the file unacceptable, false otherwise.
5727 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5729 ASTReaderListener &Listener,
5730 bool AllowCompatibleDifferences) {
5731 LangOptions LangOpts;
5733 #define LANGOPT(Name, Bits, Default, Description) \
5734 LangOpts.Name = Record[Idx++];
5735 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5736 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5737 #include "clang/Basic/LangOptions.def"
5738 #define SANITIZER(NAME, ID) \
5739 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5740 #include "clang/Basic/Sanitizers.def"
5742 for (unsigned N = Record[Idx++]; N; --N)
5743 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5745 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5746 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5747 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5749 LangOpts.CurrentModule = ReadString(Record, Idx);
5752 for (unsigned N = Record[Idx++]; N; --N) {
5753 LangOpts.CommentOpts.BlockCommandNames.push_back(
5754 ReadString(Record, Idx));
5756 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5758 // OpenMP offloading options.
5759 for (unsigned N = Record[Idx++]; N; --N) {
5760 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5763 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5765 return Listener.ReadLanguageOptions(LangOpts, Complain,
5766 AllowCompatibleDifferences);
5769 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5770 ASTReaderListener &Listener,
5771 bool AllowCompatibleDifferences) {
5773 TargetOptions TargetOpts;
5774 TargetOpts.Triple = ReadString(Record, Idx);
5775 TargetOpts.CPU = ReadString(Record, Idx);
5776 TargetOpts.ABI = ReadString(Record, Idx);
5777 for (unsigned N = Record[Idx++]; N; --N) {
5778 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5780 for (unsigned N = Record[Idx++]; N; --N) {
5781 TargetOpts.Features.push_back(ReadString(Record, Idx));
5784 return Listener.ReadTargetOptions(TargetOpts, Complain,
5785 AllowCompatibleDifferences);
5788 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5789 ASTReaderListener &Listener) {
5790 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5792 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5793 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5794 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5795 #include "clang/Basic/DiagnosticOptions.def"
5797 for (unsigned N = Record[Idx++]; N; --N)
5798 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5799 for (unsigned N = Record[Idx++]; N; --N)
5800 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5802 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5805 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5806 ASTReaderListener &Listener) {
5807 FileSystemOptions FSOpts;
5809 FSOpts.WorkingDir = ReadString(Record, Idx);
5810 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5813 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5815 ASTReaderListener &Listener) {
5816 HeaderSearchOptions HSOpts;
5818 HSOpts.Sysroot = ReadString(Record, Idx);
5821 for (unsigned N = Record[Idx++]; N; --N) {
5822 std::string Path = ReadString(Record, Idx);
5823 frontend::IncludeDirGroup Group
5824 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5825 bool IsFramework = Record[Idx++];
5826 bool IgnoreSysRoot = Record[Idx++];
5827 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5831 // System header prefixes.
5832 for (unsigned N = Record[Idx++]; N; --N) {
5833 std::string Prefix = ReadString(Record, Idx);
5834 bool IsSystemHeader = Record[Idx++];
5835 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5838 HSOpts.ResourceDir = ReadString(Record, Idx);
5839 HSOpts.ModuleCachePath = ReadString(Record, Idx);
5840 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5841 HSOpts.DisableModuleHash = Record[Idx++];
5842 HSOpts.ImplicitModuleMaps = Record[Idx++];
5843 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5844 HSOpts.UseBuiltinIncludes = Record[Idx++];
5845 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5846 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5847 HSOpts.UseLibcxx = Record[Idx++];
5848 std::string SpecificModuleCachePath = ReadString(Record, Idx);
5850 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5854 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5856 ASTReaderListener &Listener,
5857 std::string &SuggestedPredefines) {
5858 PreprocessorOptions PPOpts;
5861 // Macro definitions/undefs
5862 for (unsigned N = Record[Idx++]; N; --N) {
5863 std::string Macro = ReadString(Record, Idx);
5864 bool IsUndef = Record[Idx++];
5865 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5869 for (unsigned N = Record[Idx++]; N; --N) {
5870 PPOpts.Includes.push_back(ReadString(Record, Idx));
5874 for (unsigned N = Record[Idx++]; N; --N) {
5875 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5878 PPOpts.UsePredefines = Record[Idx++];
5879 PPOpts.DetailedRecord = Record[Idx++];
5880 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5881 PPOpts.ObjCXXARCStandardLibrary =
5882 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5883 SuggestedPredefines.clear();
5884 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5885 SuggestedPredefines);
5888 std::pair<ModuleFile *, unsigned>
5889 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5890 GlobalPreprocessedEntityMapType::iterator
5891 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5892 assert(I != GlobalPreprocessedEntityMap.end() &&
5893 "Corrupted global preprocessed entity map");
5894 ModuleFile *M = I->second;
5895 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5896 return std::make_pair(M, LocalIndex);
5899 llvm::iterator_range<PreprocessingRecord::iterator>
5900 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5901 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5902 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5903 Mod.NumPreprocessedEntities);
5905 return llvm::make_range(PreprocessingRecord::iterator(),
5906 PreprocessingRecord::iterator());
5909 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5910 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5911 return llvm::make_range(
5912 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5913 ModuleDeclIterator(this, &Mod,
5914 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5917 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5918 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5919 assert(I != GlobalSkippedRangeMap.end() &&
5920 "Corrupted global skipped range map");
5921 ModuleFile *M = I->second;
5922 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5923 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5924 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5925 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5926 TranslateSourceLocation(*M, RawRange.getEnd()));
5927 assert(Range.isValid());
5931 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5932 PreprocessedEntityID PPID = Index+1;
5933 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5934 ModuleFile &M = *PPInfo.first;
5935 unsigned LocalIndex = PPInfo.second;
5936 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5938 if (!PP.getPreprocessingRecord()) {
5939 Error("no preprocessing record");
5943 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5944 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5945 M.MacroOffsetsBase + PPOffs.BitOffset)) {
5946 Error(std::move(Err));
5950 Expected<llvm::BitstreamEntry> MaybeEntry =
5951 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5953 Error(MaybeEntry.takeError());
5956 llvm::BitstreamEntry Entry = MaybeEntry.get();
5958 if (Entry.Kind != llvm::BitstreamEntry::Record)
5962 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5963 TranslateSourceLocation(M, PPOffs.getEnd()));
5964 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5967 Expected<unsigned> MaybeRecType =
5968 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5969 if (!MaybeRecType) {
5970 Error(MaybeRecType.takeError());
5973 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5974 case PPD_MACRO_EXPANSION: {
5975 bool isBuiltin = Record[0];
5976 IdentifierInfo *Name = nullptr;
5977 MacroDefinitionRecord *Def = nullptr;
5979 Name = getLocalIdentifier(M, Record[1]);
5981 PreprocessedEntityID GlobalID =
5982 getGlobalPreprocessedEntityID(M, Record[1]);
5983 Def = cast<MacroDefinitionRecord>(
5984 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5989 ME = new (PPRec) MacroExpansion(Name, Range);
5991 ME = new (PPRec) MacroExpansion(Def, Range);
5996 case PPD_MACRO_DEFINITION: {
5997 // Decode the identifier info and then check again; if the macro is
5998 // still defined and associated with the identifier,
5999 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6000 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6002 if (DeserializationListener)
6003 DeserializationListener->MacroDefinitionRead(PPID, MD);
6008 case PPD_INCLUSION_DIRECTIVE: {
6009 const char *FullFileNameStart = Blob.data() + Record[0];
6010 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6011 const FileEntry *File = nullptr;
6012 if (!FullFileName.empty())
6013 if (auto FE = PP.getFileManager().getFile(FullFileName))
6016 // FIXME: Stable encoding
6017 InclusionDirective::InclusionKind Kind
6018 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6019 InclusionDirective *ID
6020 = new (PPRec) InclusionDirective(PPRec, Kind,
6021 StringRef(Blob.data(), Record[0]),
6022 Record[1], Record[3],
6029 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6032 /// Find the next module that contains entities and return the ID
6033 /// of the first entry.
6035 /// \param SLocMapI points at a chunk of a module that contains no
6036 /// preprocessed entities or the entities it contains are not the ones we are
6038 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6039 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6041 for (GlobalSLocOffsetMapType::const_iterator
6042 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6043 ModuleFile &M = *SLocMapI->second;
6044 if (M.NumPreprocessedEntities)
6045 return M.BasePreprocessedEntityID;
6048 return getTotalNumPreprocessedEntities();
6053 struct PPEntityComp {
6054 const ASTReader &Reader;
6057 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6059 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6060 SourceLocation LHS = getLoc(L);
6061 SourceLocation RHS = getLoc(R);
6062 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6065 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6066 SourceLocation LHS = getLoc(L);
6067 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6070 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6071 SourceLocation RHS = getLoc(R);
6072 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6075 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6076 return Reader.TranslateSourceLocation(M, PPE.getBegin());
6082 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6083 bool EndsAfter) const {
6084 if (SourceMgr.isLocalSourceLocation(Loc))
6085 return getTotalNumPreprocessedEntities();
6087 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6088 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6089 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6090 "Corrupted global sloc offset map");
6092 if (SLocMapI->second->NumPreprocessedEntities == 0)
6093 return findNextPreprocessedEntity(SLocMapI);
6095 ModuleFile &M = *SLocMapI->second;
6097 using pp_iterator = const PPEntityOffset *;
6099 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6100 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6102 size_t Count = M.NumPreprocessedEntities;
6104 pp_iterator First = pp_begin;
6108 PPI = std::upper_bound(pp_begin, pp_end, Loc,
6109 PPEntityComp(*this, M));
6111 // Do a binary search manually instead of using std::lower_bound because
6112 // The end locations of entities may be unordered (when a macro expansion
6113 // is inside another macro argument), but for this case it is not important
6114 // whether we get the first macro expansion or its containing macro.
6118 std::advance(PPI, Half);
6119 if (SourceMgr.isBeforeInTranslationUnit(
6120 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6123 Count = Count - Half - 1;
6130 return findNextPreprocessedEntity(SLocMapI);
6132 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6135 /// Returns a pair of [Begin, End) indices of preallocated
6136 /// preprocessed entities that \arg Range encompasses.
6137 std::pair<unsigned, unsigned>
6138 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6139 if (Range.isInvalid())
6140 return std::make_pair(0,0);
6141 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6143 PreprocessedEntityID BeginID =
6144 findPreprocessedEntity(Range.getBegin(), false);
6145 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6146 return std::make_pair(BeginID, EndID);
6149 /// Optionally returns true or false if the preallocated preprocessed
6150 /// entity with index \arg Index came from file \arg FID.
6151 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6153 if (FID.isInvalid())
6156 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6157 ModuleFile &M = *PPInfo.first;
6158 unsigned LocalIndex = PPInfo.second;
6159 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6161 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6162 if (Loc.isInvalid())
6165 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6173 /// Visitor used to search for information about a header file.
6174 class HeaderFileInfoVisitor {
6175 const FileEntry *FE;
6176 Optional<HeaderFileInfo> HFI;
6179 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6181 bool operator()(ModuleFile &M) {
6182 HeaderFileInfoLookupTable *Table
6183 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6187 // Look in the on-disk hash table for an entry for this file name.
6188 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6189 if (Pos == Table->end())
6196 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6201 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6202 HeaderFileInfoVisitor Visitor(FE);
6203 ModuleMgr.visit(Visitor);
6204 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6207 return HeaderFileInfo();
6210 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6211 using DiagState = DiagnosticsEngine::DiagState;
6212 SmallVector<DiagState *, 32> DiagStates;
6214 for (ModuleFile &F : ModuleMgr) {
6216 auto &Record = F.PragmaDiagMappings;
6222 auto ReadDiagState =
6223 [&](const DiagState &BasedOn, SourceLocation Loc,
6224 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6225 unsigned BackrefID = Record[Idx++];
6227 return DiagStates[BackrefID - 1];
6229 // A new DiagState was created here.
6230 Diag.DiagStates.push_back(BasedOn);
6231 DiagState *NewState = &Diag.DiagStates.back();
6232 DiagStates.push_back(NewState);
6233 unsigned Size = Record[Idx++];
6234 assert(Idx + Size * 2 <= Record.size() &&
6235 "Invalid data, not enough diag/map pairs");
6237 unsigned DiagID = Record[Idx++];
6238 DiagnosticMapping NewMapping =
6239 DiagnosticMapping::deserialize(Record[Idx++]);
6240 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6243 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6245 // If this mapping was specified as a warning but the severity was
6246 // upgraded due to diagnostic settings, simulate the current diagnostic
6247 // settings (and use a warning).
6248 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6249 NewMapping.setSeverity(diag::Severity::Warning);
6250 NewMapping.setUpgradedFromWarning(false);
6253 Mapping = NewMapping;
6258 // Read the first state.
6259 DiagState *FirstState;
6260 if (F.Kind == MK_ImplicitModule) {
6261 // Implicitly-built modules are reused with different diagnostic
6262 // settings. Use the initial diagnostic state from Diag to simulate this
6263 // compilation's diagnostic settings.
6264 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6265 DiagStates.push_back(FirstState);
6267 // Skip the initial diagnostic state from the serialized module.
6268 assert(Record[1] == 0 &&
6269 "Invalid data, unexpected backref in initial state");
6270 Idx = 3 + Record[2] * 2;
6271 assert(Idx < Record.size() &&
6272 "Invalid data, not enough state change pairs in initial state");
6273 } else if (F.isModule()) {
6274 // For an explicit module, preserve the flags from the module build
6275 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6277 unsigned Flags = Record[Idx++];
6279 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6280 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6281 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6282 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6283 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6284 Initial.ExtBehavior = (diag::Severity)Flags;
6285 FirstState = ReadDiagState(Initial, SourceLocation(), true);
6287 assert(F.OriginalSourceFileID.isValid());
6289 // Set up the root buffer of the module to start with the initial
6290 // diagnostic state of the module itself, to cover files that contain no
6291 // explicit transitions (for which we did not serialize anything).
6292 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6293 .StateTransitions.push_back({FirstState, 0});
6295 // For prefix ASTs, start with whatever the user configured on the
6297 Idx++; // Skip flags.
6298 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6299 SourceLocation(), false);
6302 // Read the state transitions.
6303 unsigned NumLocations = Record[Idx++];
6304 while (NumLocations--) {
6305 assert(Idx < Record.size() &&
6306 "Invalid data, missing pragma diagnostic states");
6307 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6308 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6309 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6310 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6311 unsigned Transitions = Record[Idx++];
6313 // Note that we don't need to set up Parent/ParentOffset here, because
6314 // we won't be changing the diagnostic state within imported FileIDs
6315 // (other than perhaps appending to the main source file, which has no
6317 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6318 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6319 for (unsigned I = 0; I != Transitions; ++I) {
6320 unsigned Offset = Record[Idx++];
6322 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6323 F.StateTransitions.push_back({State, Offset});
6327 // Read the final state.
6328 assert(Idx < Record.size() &&
6329 "Invalid data, missing final pragma diagnostic state");
6330 SourceLocation CurStateLoc =
6331 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6332 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6334 if (!F.isModule()) {
6335 Diag.DiagStatesByLoc.CurDiagState = CurState;
6336 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6338 // Preserve the property that the imaginary root file describes the
6341 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6343 T.push_back({CurState, 0});
6345 T[0].State = CurState;
6348 // Don't try to read these mappings again.
6353 /// Get the correct cursor and offset for loading a type.
6354 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6355 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6356 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6357 ModuleFile *M = I->second;
6358 return RecordLocation(
6359 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6360 M->DeclsBlockStartOffset);
6363 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6365 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6366 case TYPE_##CODE_ID: return Type::CLASS_ID;
6367 #include "clang/Serialization/TypeBitCodes.def"
6368 default: return llvm::None;
6372 /// Read and return the type with the given index..
6374 /// The index is the type ID, shifted and minus the number of predefs. This
6375 /// routine actually reads the record corresponding to the type at the given
6376 /// location. It is a helper routine for GetType, which deals with reading type
6378 QualType ASTReader::readTypeRecord(unsigned Index) {
6379 assert(ContextObj && "reading type with no AST context");
6380 ASTContext &Context = *ContextObj;
6381 RecordLocation Loc = TypeCursorForIndex(Index);
6382 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6384 // Keep track of where we are in the stream, then jump back there
6385 // after reading this type.
6386 SavedStreamPosition SavedPosition(DeclsCursor);
6388 ReadingKindTracker ReadingKind(Read_Type, *this);
6390 // Note that we are loading a type record.
6391 Deserializing AType(this);
6393 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6394 Error(std::move(Err));
6397 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6399 Error(RawCode.takeError());
6403 ASTRecordReader Record(*this, *Loc.F);
6404 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6406 Error(Code.takeError());
6409 if (Code.get() == TYPE_EXT_QUAL) {
6410 QualType baseType = Record.readQualType();
6411 Qualifiers quals = Record.readQualifiers();
6412 return Context.getQualifiedType(baseType, quals);
6415 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6417 Error("Unexpected code for type");
6421 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6422 return TypeReader.read(*maybeClass);
6427 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6428 ASTRecordReader &Reader;
6430 SourceLocation readSourceLocation() {
6431 return Reader.readSourceLocation();
6434 TypeSourceInfo *GetTypeSourceInfo() {
6435 return Reader.readTypeSourceInfo();
6438 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6439 return Reader.readNestedNameSpecifierLoc();
6443 return Reader.readAttr();
6447 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6449 // We want compile-time assurance that we've enumerated all of
6450 // these, so unfortunately we have to declare them first, then
6451 // define them out-of-line.
6452 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6453 #define TYPELOC(CLASS, PARENT) \
6454 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6455 #include "clang/AST/TypeLocNodes.def"
6457 void VisitFunctionTypeLoc(FunctionTypeLoc);
6458 void VisitArrayTypeLoc(ArrayTypeLoc);
6461 } // namespace clang
6463 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6467 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6468 TL.setBuiltinLoc(readSourceLocation());
6469 if (TL.needsExtraLocalData()) {
6470 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6471 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6472 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6473 TL.setModeAttr(Reader.readInt());
6477 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6478 TL.setNameLoc(readSourceLocation());
6481 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6482 TL.setStarLoc(readSourceLocation());
6485 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6489 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6493 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6494 TL.setExpansionLoc(readSourceLocation());
6497 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6498 TL.setCaretLoc(readSourceLocation());
6501 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6502 TL.setAmpLoc(readSourceLocation());
6505 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6506 TL.setAmpAmpLoc(readSourceLocation());
6509 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6510 TL.setStarLoc(readSourceLocation());
6511 TL.setClassTInfo(GetTypeSourceInfo());
6514 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6515 TL.setLBracketLoc(readSourceLocation());
6516 TL.setRBracketLoc(readSourceLocation());
6517 if (Reader.readBool())
6518 TL.setSizeExpr(Reader.readExpr());
6520 TL.setSizeExpr(nullptr);
6523 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6524 VisitArrayTypeLoc(TL);
6527 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6528 VisitArrayTypeLoc(TL);
6531 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6532 VisitArrayTypeLoc(TL);
6535 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6536 DependentSizedArrayTypeLoc TL) {
6537 VisitArrayTypeLoc(TL);
6540 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6541 DependentAddressSpaceTypeLoc TL) {
6543 TL.setAttrNameLoc(readSourceLocation());
6544 TL.setAttrOperandParensRange(Reader.readSourceRange());
6545 TL.setAttrExprOperand(Reader.readExpr());
6548 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6549 DependentSizedExtVectorTypeLoc TL) {
6550 TL.setNameLoc(readSourceLocation());
6553 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6554 TL.setNameLoc(readSourceLocation());
6557 void TypeLocReader::VisitDependentVectorTypeLoc(
6558 DependentVectorTypeLoc TL) {
6559 TL.setNameLoc(readSourceLocation());
6562 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6563 TL.setNameLoc(readSourceLocation());
6566 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6567 TL.setAttrNameLoc(readSourceLocation());
6568 TL.setAttrOperandParensRange(Reader.readSourceRange());
6569 TL.setAttrRowOperand(Reader.readExpr());
6570 TL.setAttrColumnOperand(Reader.readExpr());
6573 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6574 DependentSizedMatrixTypeLoc TL) {
6575 TL.setAttrNameLoc(readSourceLocation());
6576 TL.setAttrOperandParensRange(Reader.readSourceRange());
6577 TL.setAttrRowOperand(Reader.readExpr());
6578 TL.setAttrColumnOperand(Reader.readExpr());
6581 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6582 TL.setLocalRangeBegin(readSourceLocation());
6583 TL.setLParenLoc(readSourceLocation());
6584 TL.setRParenLoc(readSourceLocation());
6585 TL.setExceptionSpecRange(Reader.readSourceRange());
6586 TL.setLocalRangeEnd(readSourceLocation());
6587 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6588 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6592 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6593 VisitFunctionTypeLoc(TL);
6596 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6597 VisitFunctionTypeLoc(TL);
6600 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6601 TL.setNameLoc(readSourceLocation());
6604 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6605 TL.setNameLoc(readSourceLocation());
6608 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6609 TL.setTypeofLoc(readSourceLocation());
6610 TL.setLParenLoc(readSourceLocation());
6611 TL.setRParenLoc(readSourceLocation());
6614 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6615 TL.setTypeofLoc(readSourceLocation());
6616 TL.setLParenLoc(readSourceLocation());
6617 TL.setRParenLoc(readSourceLocation());
6618 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6621 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6622 TL.setNameLoc(readSourceLocation());
6625 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6626 TL.setKWLoc(readSourceLocation());
6627 TL.setLParenLoc(readSourceLocation());
6628 TL.setRParenLoc(readSourceLocation());
6629 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6632 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6633 TL.setNameLoc(readSourceLocation());
6634 if (Reader.readBool()) {
6635 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6636 TL.setTemplateKWLoc(readSourceLocation());
6637 TL.setConceptNameLoc(readSourceLocation());
6638 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6639 TL.setLAngleLoc(readSourceLocation());
6640 TL.setRAngleLoc(readSourceLocation());
6641 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6642 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6643 TL.getTypePtr()->getArg(i).getKind()));
6647 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6648 DeducedTemplateSpecializationTypeLoc TL) {
6649 TL.setTemplateNameLoc(readSourceLocation());
6652 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6653 TL.setNameLoc(readSourceLocation());
6656 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6657 TL.setNameLoc(readSourceLocation());
6660 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6661 TL.setAttr(ReadAttr());
6664 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6665 TL.setNameLoc(readSourceLocation());
6668 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6669 SubstTemplateTypeParmTypeLoc TL) {
6670 TL.setNameLoc(readSourceLocation());
6673 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6674 SubstTemplateTypeParmPackTypeLoc TL) {
6675 TL.setNameLoc(readSourceLocation());
6678 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6679 TemplateSpecializationTypeLoc TL) {
6680 TL.setTemplateKeywordLoc(readSourceLocation());
6681 TL.setTemplateNameLoc(readSourceLocation());
6682 TL.setLAngleLoc(readSourceLocation());
6683 TL.setRAngleLoc(readSourceLocation());
6684 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6687 Reader.readTemplateArgumentLocInfo(
6688 TL.getTypePtr()->getArg(i).getKind()));
6691 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6692 TL.setLParenLoc(readSourceLocation());
6693 TL.setRParenLoc(readSourceLocation());
6696 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6697 TL.setElaboratedKeywordLoc(readSourceLocation());
6698 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6701 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6702 TL.setNameLoc(readSourceLocation());
6705 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6706 TL.setElaboratedKeywordLoc(readSourceLocation());
6707 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6708 TL.setNameLoc(readSourceLocation());
6711 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6712 DependentTemplateSpecializationTypeLoc TL) {
6713 TL.setElaboratedKeywordLoc(readSourceLocation());
6714 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6715 TL.setTemplateKeywordLoc(readSourceLocation());
6716 TL.setTemplateNameLoc(readSourceLocation());
6717 TL.setLAngleLoc(readSourceLocation());
6718 TL.setRAngleLoc(readSourceLocation());
6719 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6722 Reader.readTemplateArgumentLocInfo(
6723 TL.getTypePtr()->getArg(I).getKind()));
6726 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6727 TL.setEllipsisLoc(readSourceLocation());
6730 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6731 TL.setNameLoc(readSourceLocation());
6734 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6735 if (TL.getNumProtocols()) {
6736 TL.setProtocolLAngleLoc(readSourceLocation());
6737 TL.setProtocolRAngleLoc(readSourceLocation());
6739 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6740 TL.setProtocolLoc(i, readSourceLocation());
6743 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6744 TL.setHasBaseTypeAsWritten(Reader.readBool());
6745 TL.setTypeArgsLAngleLoc(readSourceLocation());
6746 TL.setTypeArgsRAngleLoc(readSourceLocation());
6747 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6748 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6749 TL.setProtocolLAngleLoc(readSourceLocation());
6750 TL.setProtocolRAngleLoc(readSourceLocation());
6751 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6752 TL.setProtocolLoc(i, readSourceLocation());
6755 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6756 TL.setStarLoc(readSourceLocation());
6759 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6760 TL.setKWLoc(readSourceLocation());
6761 TL.setLParenLoc(readSourceLocation());
6762 TL.setRParenLoc(readSourceLocation());
6765 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6766 TL.setKWLoc(readSourceLocation());
6769 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6770 TL.setNameLoc(readSourceLocation());
6772 void TypeLocReader::VisitDependentExtIntTypeLoc(
6773 clang::DependentExtIntTypeLoc TL) {
6774 TL.setNameLoc(readSourceLocation());
6778 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6779 TypeLocReader TLR(*this);
6780 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6784 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6785 QualType InfoTy = readType();
6786 if (InfoTy.isNull())
6789 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6790 readTypeLoc(TInfo->getTypeLoc());
6794 QualType ASTReader::GetType(TypeID ID) {
6795 assert(ContextObj && "reading type with no AST context");
6796 ASTContext &Context = *ContextObj;
6798 unsigned FastQuals = ID & Qualifiers::FastMask;
6799 unsigned Index = ID >> Qualifiers::FastWidth;
6801 if (Index < NUM_PREDEF_TYPE_IDS) {
6803 switch ((PredefinedTypeIDs)Index) {
6804 case PREDEF_TYPE_NULL_ID:
6806 case PREDEF_TYPE_VOID_ID:
6809 case PREDEF_TYPE_BOOL_ID:
6812 case PREDEF_TYPE_CHAR_U_ID:
6813 case PREDEF_TYPE_CHAR_S_ID:
6814 // FIXME: Check that the signedness of CharTy is correct!
6817 case PREDEF_TYPE_UCHAR_ID:
6818 T = Context.UnsignedCharTy;
6820 case PREDEF_TYPE_USHORT_ID:
6821 T = Context.UnsignedShortTy;
6823 case PREDEF_TYPE_UINT_ID:
6824 T = Context.UnsignedIntTy;
6826 case PREDEF_TYPE_ULONG_ID:
6827 T = Context.UnsignedLongTy;
6829 case PREDEF_TYPE_ULONGLONG_ID:
6830 T = Context.UnsignedLongLongTy;
6832 case PREDEF_TYPE_UINT128_ID:
6833 T = Context.UnsignedInt128Ty;
6835 case PREDEF_TYPE_SCHAR_ID:
6836 T = Context.SignedCharTy;
6838 case PREDEF_TYPE_WCHAR_ID:
6839 T = Context.WCharTy;
6841 case PREDEF_TYPE_SHORT_ID:
6842 T = Context.ShortTy;
6844 case PREDEF_TYPE_INT_ID:
6847 case PREDEF_TYPE_LONG_ID:
6850 case PREDEF_TYPE_LONGLONG_ID:
6851 T = Context.LongLongTy;
6853 case PREDEF_TYPE_INT128_ID:
6854 T = Context.Int128Ty;
6856 case PREDEF_TYPE_BFLOAT16_ID:
6857 T = Context.BFloat16Ty;
6859 case PREDEF_TYPE_HALF_ID:
6862 case PREDEF_TYPE_FLOAT_ID:
6863 T = Context.FloatTy;
6865 case PREDEF_TYPE_DOUBLE_ID:
6866 T = Context.DoubleTy;
6868 case PREDEF_TYPE_LONGDOUBLE_ID:
6869 T = Context.LongDoubleTy;
6871 case PREDEF_TYPE_SHORT_ACCUM_ID:
6872 T = Context.ShortAccumTy;
6874 case PREDEF_TYPE_ACCUM_ID:
6875 T = Context.AccumTy;
6877 case PREDEF_TYPE_LONG_ACCUM_ID:
6878 T = Context.LongAccumTy;
6880 case PREDEF_TYPE_USHORT_ACCUM_ID:
6881 T = Context.UnsignedShortAccumTy;
6883 case PREDEF_TYPE_UACCUM_ID:
6884 T = Context.UnsignedAccumTy;
6886 case PREDEF_TYPE_ULONG_ACCUM_ID:
6887 T = Context.UnsignedLongAccumTy;
6889 case PREDEF_TYPE_SHORT_FRACT_ID:
6890 T = Context.ShortFractTy;
6892 case PREDEF_TYPE_FRACT_ID:
6893 T = Context.FractTy;
6895 case PREDEF_TYPE_LONG_FRACT_ID:
6896 T = Context.LongFractTy;
6898 case PREDEF_TYPE_USHORT_FRACT_ID:
6899 T = Context.UnsignedShortFractTy;
6901 case PREDEF_TYPE_UFRACT_ID:
6902 T = Context.UnsignedFractTy;
6904 case PREDEF_TYPE_ULONG_FRACT_ID:
6905 T = Context.UnsignedLongFractTy;
6907 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6908 T = Context.SatShortAccumTy;
6910 case PREDEF_TYPE_SAT_ACCUM_ID:
6911 T = Context.SatAccumTy;
6913 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6914 T = Context.SatLongAccumTy;
6916 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6917 T = Context.SatUnsignedShortAccumTy;
6919 case PREDEF_TYPE_SAT_UACCUM_ID:
6920 T = Context.SatUnsignedAccumTy;
6922 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6923 T = Context.SatUnsignedLongAccumTy;
6925 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6926 T = Context.SatShortFractTy;
6928 case PREDEF_TYPE_SAT_FRACT_ID:
6929 T = Context.SatFractTy;
6931 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6932 T = Context.SatLongFractTy;
6934 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6935 T = Context.SatUnsignedShortFractTy;
6937 case PREDEF_TYPE_SAT_UFRACT_ID:
6938 T = Context.SatUnsignedFractTy;
6940 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6941 T = Context.SatUnsignedLongFractTy;
6943 case PREDEF_TYPE_FLOAT16_ID:
6944 T = Context.Float16Ty;
6946 case PREDEF_TYPE_FLOAT128_ID:
6947 T = Context.Float128Ty;
6949 case PREDEF_TYPE_OVERLOAD_ID:
6950 T = Context.OverloadTy;
6952 case PREDEF_TYPE_BOUND_MEMBER:
6953 T = Context.BoundMemberTy;
6955 case PREDEF_TYPE_PSEUDO_OBJECT:
6956 T = Context.PseudoObjectTy;
6958 case PREDEF_TYPE_DEPENDENT_ID:
6959 T = Context.DependentTy;
6961 case PREDEF_TYPE_UNKNOWN_ANY:
6962 T = Context.UnknownAnyTy;
6964 case PREDEF_TYPE_NULLPTR_ID:
6965 T = Context.NullPtrTy;
6967 case PREDEF_TYPE_CHAR8_ID:
6968 T = Context.Char8Ty;
6970 case PREDEF_TYPE_CHAR16_ID:
6971 T = Context.Char16Ty;
6973 case PREDEF_TYPE_CHAR32_ID:
6974 T = Context.Char32Ty;
6976 case PREDEF_TYPE_OBJC_ID:
6977 T = Context.ObjCBuiltinIdTy;
6979 case PREDEF_TYPE_OBJC_CLASS:
6980 T = Context.ObjCBuiltinClassTy;
6982 case PREDEF_TYPE_OBJC_SEL:
6983 T = Context.ObjCBuiltinSelTy;
6985 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6986 case PREDEF_TYPE_##Id##_ID: \
6987 T = Context.SingletonId; \
6989 #include "clang/Basic/OpenCLImageTypes.def"
6990 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6991 case PREDEF_TYPE_##Id##_ID: \
6992 T = Context.Id##Ty; \
6994 #include "clang/Basic/OpenCLExtensionTypes.def"
6995 case PREDEF_TYPE_SAMPLER_ID:
6996 T = Context.OCLSamplerTy;
6998 case PREDEF_TYPE_EVENT_ID:
6999 T = Context.OCLEventTy;
7001 case PREDEF_TYPE_CLK_EVENT_ID:
7002 T = Context.OCLClkEventTy;
7004 case PREDEF_TYPE_QUEUE_ID:
7005 T = Context.OCLQueueTy;
7007 case PREDEF_TYPE_RESERVE_ID_ID:
7008 T = Context.OCLReserveIDTy;
7010 case PREDEF_TYPE_AUTO_DEDUCT:
7011 T = Context.getAutoDeductType();
7013 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7014 T = Context.getAutoRRefDeductType();
7016 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7017 T = Context.ARCUnbridgedCastTy;
7019 case PREDEF_TYPE_BUILTIN_FN:
7020 T = Context.BuiltinFnTy;
7022 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7023 T = Context.IncompleteMatrixIdxTy;
7025 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7026 T = Context.OMPArraySectionTy;
7028 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7029 T = Context.OMPArraySectionTy;
7031 case PREDEF_TYPE_OMP_ITERATOR:
7032 T = Context.OMPIteratorTy;
7034 #define SVE_TYPE(Name, Id, SingletonId) \
7035 case PREDEF_TYPE_##Id##_ID: \
7036 T = Context.SingletonId; \
7038 #include "clang/Basic/AArch64SVEACLETypes.def"
7041 assert(!T.isNull() && "Unknown predefined type");
7042 return T.withFastQualifiers(FastQuals);
7045 Index -= NUM_PREDEF_TYPE_IDS;
7046 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7047 if (TypesLoaded[Index].isNull()) {
7048 TypesLoaded[Index] = readTypeRecord(Index);
7049 if (TypesLoaded[Index].isNull())
7052 TypesLoaded[Index]->setFromAST();
7053 if (DeserializationListener)
7054 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7055 TypesLoaded[Index]);
7058 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7061 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7062 return GetType(getGlobalTypeID(F, LocalID));
7065 serialization::TypeID
7066 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7067 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7068 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7070 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7073 if (!F.ModuleOffsetMap.empty())
7074 ReadModuleOffsetMap(F);
7076 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7077 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7078 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7080 unsigned GlobalIndex = LocalIndex + I->second;
7081 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7084 TemplateArgumentLocInfo
7085 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7087 case TemplateArgument::Expression:
7089 case TemplateArgument::Type:
7090 return readTypeSourceInfo();
7091 case TemplateArgument::Template: {
7092 NestedNameSpecifierLoc QualifierLoc =
7093 readNestedNameSpecifierLoc();
7094 SourceLocation TemplateNameLoc = readSourceLocation();
7095 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7098 case TemplateArgument::TemplateExpansion: {
7099 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7100 SourceLocation TemplateNameLoc = readSourceLocation();
7101 SourceLocation EllipsisLoc = readSourceLocation();
7102 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7105 case TemplateArgument::Null:
7106 case TemplateArgument::Integral:
7107 case TemplateArgument::Declaration:
7108 case TemplateArgument::NullPtr:
7109 case TemplateArgument::Pack:
7110 // FIXME: Is this right?
7111 return TemplateArgumentLocInfo();
7113 llvm_unreachable("unexpected template argument loc");
7116 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7117 TemplateArgument Arg = readTemplateArgument();
7119 if (Arg.getKind() == TemplateArgument::Expression) {
7120 if (readBool()) // bool InfoHasSameExpr.
7121 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7123 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7126 const ASTTemplateArgumentListInfo *
7127 ASTRecordReader::readASTTemplateArgumentListInfo() {
7128 SourceLocation LAngleLoc = readSourceLocation();
7129 SourceLocation RAngleLoc = readSourceLocation();
7130 unsigned NumArgsAsWritten = readInt();
7131 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7132 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7133 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7134 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7137 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7141 void ASTReader::CompleteRedeclChain(const Decl *D) {
7142 if (NumCurrentElementsDeserializing) {
7143 // We arrange to not care about the complete redeclaration chain while we're
7144 // deserializing. Just remember that the AST has marked this one as complete
7145 // but that it's not actually complete yet, so we know we still need to
7146 // complete it later.
7147 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7151 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7153 // If this is a named declaration, complete it by looking it up
7154 // within its context.
7156 // FIXME: Merging a function definition should merge
7157 // all mergeable entities within it.
7158 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7159 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7160 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7161 if (!getContext().getLangOpts().CPlusPlus &&
7162 isa<TranslationUnitDecl>(DC)) {
7163 // Outside of C++, we don't have a lookup table for the TU, so update
7164 // the identifier instead. (For C++ modules, we don't store decls
7165 // in the serialized identifier table, so we do the lookup in the TU.)
7166 auto *II = Name.getAsIdentifierInfo();
7167 assert(II && "non-identifier name in C?");
7168 if (II->isOutOfDate())
7169 updateOutOfDateIdentifier(*II);
7172 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7173 // Find all declarations of this kind from the relevant context.
7174 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7175 auto *DC = cast<DeclContext>(DCDecl);
7176 SmallVector<Decl*, 8> Decls;
7177 FindExternalLexicalDecls(
7178 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7183 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7184 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7185 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7186 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7187 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7188 if (auto *Template = FD->getPrimaryTemplate())
7189 Template->LoadLazySpecializations();
7193 CXXCtorInitializer **
7194 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7195 RecordLocation Loc = getLocalBitOffset(Offset);
7196 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7197 SavedStreamPosition SavedPosition(Cursor);
7198 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7199 Error(std::move(Err));
7202 ReadingKindTracker ReadingKind(Read_Decl, *this);
7204 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7206 Error(MaybeCode.takeError());
7209 unsigned Code = MaybeCode.get();
7211 ASTRecordReader Record(*this, *Loc.F);
7212 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7213 if (!MaybeRecCode) {
7214 Error(MaybeRecCode.takeError());
7217 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7218 Error("malformed AST file: missing C++ ctor initializers");
7222 return Record.readCXXCtorInitializers();
7225 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7226 assert(ContextObj && "reading base specifiers with no AST context");
7227 ASTContext &Context = *ContextObj;
7229 RecordLocation Loc = getLocalBitOffset(Offset);
7230 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7231 SavedStreamPosition SavedPosition(Cursor);
7232 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7233 Error(std::move(Err));
7236 ReadingKindTracker ReadingKind(Read_Decl, *this);
7238 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7240 Error(MaybeCode.takeError());
7243 unsigned Code = MaybeCode.get();
7245 ASTRecordReader Record(*this, *Loc.F);
7246 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7247 if (!MaybeRecCode) {
7248 Error(MaybeCode.takeError());
7251 unsigned RecCode = MaybeRecCode.get();
7253 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7254 Error("malformed AST file: missing C++ base specifiers");
7258 unsigned NumBases = Record.readInt();
7259 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7260 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7261 for (unsigned I = 0; I != NumBases; ++I)
7262 Bases[I] = Record.readCXXBaseSpecifier();
7266 serialization::DeclID
7267 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7268 if (LocalID < NUM_PREDEF_DECL_IDS)
7271 if (!F.ModuleOffsetMap.empty())
7272 ReadModuleOffsetMap(F);
7274 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7275 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7276 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7278 return LocalID + I->second;
7281 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7282 ModuleFile &M) const {
7283 // Predefined decls aren't from any module.
7284 if (ID < NUM_PREDEF_DECL_IDS)
7287 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7288 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7291 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7292 if (!D->isFromASTFile())
7294 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7295 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7299 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7300 if (ID < NUM_PREDEF_DECL_IDS)
7301 return SourceLocation();
7303 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7305 if (Index > DeclsLoaded.size()) {
7306 Error("declaration ID out-of-range for AST file");
7307 return SourceLocation();
7310 if (Decl *D = DeclsLoaded[Index])
7311 return D->getLocation();
7314 DeclCursorForID(ID, Loc);
7318 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7320 case PREDEF_DECL_NULL_ID:
7323 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7324 return Context.getTranslationUnitDecl();
7326 case PREDEF_DECL_OBJC_ID_ID:
7327 return Context.getObjCIdDecl();
7329 case PREDEF_DECL_OBJC_SEL_ID:
7330 return Context.getObjCSelDecl();
7332 case PREDEF_DECL_OBJC_CLASS_ID:
7333 return Context.getObjCClassDecl();
7335 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7336 return Context.getObjCProtocolDecl();
7338 case PREDEF_DECL_INT_128_ID:
7339 return Context.getInt128Decl();
7341 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7342 return Context.getUInt128Decl();
7344 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7345 return Context.getObjCInstanceTypeDecl();
7347 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7348 return Context.getBuiltinVaListDecl();
7350 case PREDEF_DECL_VA_LIST_TAG:
7351 return Context.getVaListTagDecl();
7353 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7354 return Context.getBuiltinMSVaListDecl();
7356 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7357 return Context.getMSGuidTagDecl();
7359 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7360 return Context.getExternCContextDecl();
7362 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7363 return Context.getMakeIntegerSeqDecl();
7365 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7366 return Context.getCFConstantStringDecl();
7368 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7369 return Context.getCFConstantStringTagDecl();
7371 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7372 return Context.getTypePackElementDecl();
7374 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7377 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7378 assert(ContextObj && "reading decl with no AST context");
7379 if (ID < NUM_PREDEF_DECL_IDS) {
7380 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7382 // Track that we have merged the declaration with ID \p ID into the
7383 // pre-existing predefined declaration \p D.
7384 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7386 Merged.push_back(ID);
7391 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7393 if (Index >= DeclsLoaded.size()) {
7394 assert(0 && "declaration ID out-of-range for AST file");
7395 Error("declaration ID out-of-range for AST file");
7399 return DeclsLoaded[Index];
7402 Decl *ASTReader::GetDecl(DeclID ID) {
7403 if (ID < NUM_PREDEF_DECL_IDS)
7404 return GetExistingDecl(ID);
7406 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7408 if (Index >= DeclsLoaded.size()) {
7409 assert(0 && "declaration ID out-of-range for AST file");
7410 Error("declaration ID out-of-range for AST file");
7414 if (!DeclsLoaded[Index]) {
7416 if (DeserializationListener)
7417 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7420 return DeclsLoaded[Index];
7423 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7425 if (GlobalID < NUM_PREDEF_DECL_IDS)
7428 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7429 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7430 ModuleFile *Owner = I->second;
7432 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7433 = M.GlobalToLocalDeclIDs.find(Owner);
7434 if (Pos == M.GlobalToLocalDeclIDs.end())
7437 return GlobalID - Owner->BaseDeclID + Pos->second;
7440 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7441 const RecordData &Record,
7443 if (Idx >= Record.size()) {
7444 Error("Corrupted AST file");
7448 return getGlobalDeclID(F, Record[Idx++]);
7451 /// Resolve the offset of a statement into a statement.
7453 /// This operation will read a new statement from the external
7454 /// source each time it is called, and is meant to be used via a
7455 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7456 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7457 // Switch case IDs are per Decl.
7458 ClearSwitchCaseIDs();
7460 // Offset here is a global offset across the entire chain.
7461 RecordLocation Loc = getLocalBitOffset(Offset);
7462 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7463 Error(std::move(Err));
7466 assert(NumCurrentElementsDeserializing == 0 &&
7467 "should not be called while already deserializing");
7468 Deserializing D(this);
7469 return ReadStmtFromStream(*Loc.F);
7472 void ASTReader::FindExternalLexicalDecls(
7473 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7474 SmallVectorImpl<Decl *> &Decls) {
7475 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7477 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7478 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7479 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7480 auto K = (Decl::Kind)+LexicalDecls[I];
7481 if (!IsKindWeWant(K))
7484 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7486 // Don't add predefined declarations to the lexical context more
7488 if (ID < NUM_PREDEF_DECL_IDS) {
7489 if (PredefsVisited[ID])
7492 PredefsVisited[ID] = true;
7495 if (Decl *D = GetLocalDecl(*M, ID)) {
7496 assert(D->getKind() == K && "wrong kind for lexical decl");
7497 if (!DC->isDeclInLexicalTraversal(D))
7503 if (isa<TranslationUnitDecl>(DC)) {
7504 for (auto Lexical : TULexicalDecls)
7505 Visit(Lexical.first, Lexical.second);
7507 auto I = LexicalDecls.find(DC);
7508 if (I != LexicalDecls.end())
7509 Visit(I->second.first, I->second.second);
7512 ++NumLexicalDeclContextsRead;
7522 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7524 bool operator()(LocalDeclID L, LocalDeclID R) const {
7525 SourceLocation LHS = getLocation(L);
7526 SourceLocation RHS = getLocation(R);
7527 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7530 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7531 SourceLocation RHS = getLocation(R);
7532 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7535 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7536 SourceLocation LHS = getLocation(L);
7537 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7540 SourceLocation getLocation(LocalDeclID ID) const {
7541 return Reader.getSourceManager().getFileLoc(
7542 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7548 void ASTReader::FindFileRegionDecls(FileID File,
7549 unsigned Offset, unsigned Length,
7550 SmallVectorImpl<Decl *> &Decls) {
7551 SourceManager &SM = getSourceManager();
7553 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7554 if (I == FileDeclIDs.end())
7557 FileDeclsInfo &DInfo = I->second;
7558 if (DInfo.Decls.empty())
7562 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7563 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7565 DeclIDComp DIDComp(*this, *DInfo.Mod);
7566 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7567 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7568 if (BeginIt != DInfo.Decls.begin())
7571 // If we are pointing at a top-level decl inside an objc container, we need
7572 // to backtrack until we find it otherwise we will fail to report that the
7573 // region overlaps with an objc container.
7574 while (BeginIt != DInfo.Decls.begin() &&
7575 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7576 ->isTopLevelDeclInObjCContainer())
7579 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7580 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7581 if (EndIt != DInfo.Decls.end())
7584 for (ArrayRef<serialization::LocalDeclID>::iterator
7585 DIt = BeginIt; DIt != EndIt; ++DIt)
7586 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7590 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7591 DeclarationName Name) {
7592 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7593 "DeclContext has no visible decls in storage");
7597 auto It = Lookups.find(DC);
7598 if (It == Lookups.end())
7601 Deserializing LookupResults(this);
7603 // Load the list of declarations.
7604 SmallVector<NamedDecl *, 64> Decls;
7605 for (DeclID ID : It->second.Table.find(Name)) {
7606 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7607 if (ND->getDeclName() == Name)
7608 Decls.push_back(ND);
7611 ++NumVisibleDeclContextsRead;
7612 SetExternalVisibleDeclsForName(DC, Name, Decls);
7613 return !Decls.empty();
7616 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7617 if (!DC->hasExternalVisibleStorage())
7620 auto It = Lookups.find(DC);
7621 assert(It != Lookups.end() &&
7622 "have external visible storage but no lookup tables");
7626 for (DeclID ID : It->second.Table.findAll()) {
7627 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7628 Decls[ND->getDeclName()].push_back(ND);
7631 ++NumVisibleDeclContextsRead;
7633 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7634 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7636 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7639 const serialization::reader::DeclContextLookupTable *
7640 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7641 auto I = Lookups.find(Primary);
7642 return I == Lookups.end() ? nullptr : &I->second;
7645 /// Under non-PCH compilation the consumer receives the objc methods
7646 /// before receiving the implementation, and codegen depends on this.
7647 /// We simulate this by deserializing and passing to consumer the methods of the
7648 /// implementation before passing the deserialized implementation decl.
7649 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7650 ASTConsumer *Consumer) {
7651 assert(ImplD && Consumer);
7653 for (auto *I : ImplD->methods())
7654 Consumer->HandleInterestingDecl(DeclGroupRef(I));
7656 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7659 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7660 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7661 PassObjCImplDeclToConsumer(ImplD, Consumer);
7663 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7666 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7667 this->Consumer = Consumer;
7670 PassInterestingDeclsToConsumer();
7672 if (DeserializationListener)
7673 DeserializationListener->ReaderInitialized(this);
7676 void ASTReader::PrintStats() {
7677 std::fprintf(stderr, "*** AST File Statistics:\n");
7679 unsigned NumTypesLoaded
7680 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7682 unsigned NumDeclsLoaded
7683 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7685 unsigned NumIdentifiersLoaded
7686 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7687 IdentifiersLoaded.end(),
7688 (IdentifierInfo *)nullptr);
7689 unsigned NumMacrosLoaded
7690 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7692 (MacroInfo *)nullptr);
7693 unsigned NumSelectorsLoaded
7694 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7695 SelectorsLoaded.end(),
7698 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7699 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7700 NumSLocEntriesRead, TotalNumSLocEntries,
7701 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7702 if (!TypesLoaded.empty())
7703 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7704 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7705 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7706 if (!DeclsLoaded.empty())
7707 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7708 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7709 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7710 if (!IdentifiersLoaded.empty())
7711 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7712 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7713 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7714 if (!MacrosLoaded.empty())
7715 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7716 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7717 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7718 if (!SelectorsLoaded.empty())
7719 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7720 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7721 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7722 if (TotalNumStatements)
7723 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7724 NumStatementsRead, TotalNumStatements,
7725 ((float)NumStatementsRead/TotalNumStatements * 100));
7727 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7728 NumMacrosRead, TotalNumMacros,
7729 ((float)NumMacrosRead/TotalNumMacros * 100));
7730 if (TotalLexicalDeclContexts)
7731 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7732 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7733 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7735 if (TotalVisibleDeclContexts)
7736 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7737 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7738 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7740 if (TotalNumMethodPoolEntries)
7741 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7742 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7743 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7745 if (NumMethodPoolLookups)
7746 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7747 NumMethodPoolHits, NumMethodPoolLookups,
7748 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7749 if (NumMethodPoolTableLookups)
7750 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7751 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7752 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7754 if (NumIdentifierLookupHits)
7755 std::fprintf(stderr,
7756 " %u / %u identifier table lookups succeeded (%f%%)\n",
7757 NumIdentifierLookupHits, NumIdentifierLookups,
7758 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7761 std::fprintf(stderr, "\n");
7762 GlobalIndex->printStats();
7765 std::fprintf(stderr, "\n");
7767 std::fprintf(stderr, "\n");
7770 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7771 LLVM_DUMP_METHOD static void
7772 dumpModuleIDMap(StringRef Name,
7773 const ContinuousRangeMap<Key, ModuleFile *,
7774 InitialCapacity> &Map) {
7775 if (Map.begin() == Map.end())
7778 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7780 llvm::errs() << Name << ":\n";
7781 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7783 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7788 LLVM_DUMP_METHOD void ASTReader::dump() {
7789 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7790 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7791 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7792 dumpModuleIDMap("Global type map", GlobalTypeMap);
7793 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7794 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7795 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7796 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7797 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7798 dumpModuleIDMap("Global preprocessed entity map",
7799 GlobalPreprocessedEntityMap);
7801 llvm::errs() << "\n*** PCH/Modules Loaded:";
7802 for (ModuleFile &M : ModuleMgr)
7806 /// Return the amount of memory used by memory buffers, breaking down
7807 /// by heap-backed versus mmap'ed memory.
7808 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7809 for (ModuleFile &I : ModuleMgr) {
7810 if (llvm::MemoryBuffer *buf = I.Buffer) {
7811 size_t bytes = buf->getBufferSize();
7812 switch (buf->getBufferKind()) {
7813 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7814 sizes.malloc_bytes += bytes;
7816 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7817 sizes.mmap_bytes += bytes;
7824 void ASTReader::InitializeSema(Sema &S) {
7826 S.addExternalSource(this);
7828 // Makes sure any declarations that were deserialized "too early"
7829 // still get added to the identifier's declaration chains.
7830 for (uint64_t ID : PreloadedDeclIDs) {
7831 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7832 pushExternalDeclIntoScope(D, D->getDeclName());
7834 PreloadedDeclIDs.clear();
7836 // FIXME: What happens if these are changed by a module import?
7837 if (!FPPragmaOptions.empty()) {
7838 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7839 FPOptionsOverride NewOverrides(FPPragmaOptions[0]);
7840 SemaObj->CurFPFeatures =
7841 NewOverrides.applyOverrides(SemaObj->getLangOpts());
7844 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7845 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7846 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7851 void ASTReader::UpdateSema() {
7852 assert(SemaObj && "no Sema to update");
7854 // Load the offsets of the declarations that Sema references.
7855 // They will be lazily deserialized when needed.
7856 if (!SemaDeclRefs.empty()) {
7857 assert(SemaDeclRefs.size() % 3 == 0);
7858 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7859 if (!SemaObj->StdNamespace)
7860 SemaObj->StdNamespace = SemaDeclRefs[I];
7861 if (!SemaObj->StdBadAlloc)
7862 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7863 if (!SemaObj->StdAlignValT)
7864 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7866 SemaDeclRefs.clear();
7869 // Update the state of pragmas. Use the same API as if we had encountered the
7870 // pragma in the source.
7871 if(OptimizeOffPragmaLocation.isValid())
7872 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7873 if (PragmaMSStructState != -1)
7874 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7875 if (PointersToMembersPragmaLocation.isValid()) {
7876 SemaObj->ActOnPragmaMSPointersToMembers(
7877 (LangOptions::PragmaMSPointersToMembersKind)
7878 PragmaMSPointersToMembersState,
7879 PointersToMembersPragmaLocation);
7881 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7883 if (PragmaPackCurrentValue) {
7884 // The bottom of the stack might have a default value. It must be adjusted
7885 // to the current value to ensure that the packing state is preserved after
7886 // popping entries that were included/imported from a PCH/module.
7887 bool DropFirst = false;
7888 if (!PragmaPackStack.empty() &&
7889 PragmaPackStack.front().Location.isInvalid()) {
7890 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7891 "Expected a default alignment value");
7892 SemaObj->PackStack.Stack.emplace_back(
7893 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7894 SemaObj->PackStack.CurrentPragmaLocation,
7895 PragmaPackStack.front().PushLocation);
7898 for (const auto &Entry :
7899 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7900 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7901 Entry.Location, Entry.PushLocation);
7902 if (PragmaPackCurrentLocation.isInvalid()) {
7903 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7904 "Expected a default alignment value");
7905 // Keep the current values.
7907 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7908 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7911 if (FpPragmaCurrentValue) {
7912 // The bottom of the stack might have a default value. It must be adjusted
7913 // to the current value to ensure that fp-pragma state is preserved after
7914 // popping entries that were included/imported from a PCH/module.
7915 bool DropFirst = false;
7916 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7917 assert(FpPragmaStack.front().Value ==
7918 SemaObj->FpPragmaStack.DefaultValue &&
7919 "Expected a default pragma float_control value");
7920 SemaObj->FpPragmaStack.Stack.emplace_back(
7921 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7922 SemaObj->FpPragmaStack.CurrentPragmaLocation,
7923 FpPragmaStack.front().PushLocation);
7926 for (const auto &Entry :
7927 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7928 SemaObj->FpPragmaStack.Stack.emplace_back(
7929 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7930 if (FpPragmaCurrentLocation.isInvalid()) {
7931 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7932 "Expected a default pragma float_control value");
7933 // Keep the current values.
7935 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7936 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7941 IdentifierInfo *ASTReader::get(StringRef Name) {
7942 // Note that we are loading an identifier.
7943 Deserializing AnIdentifier(this);
7945 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7946 NumIdentifierLookups,
7947 NumIdentifierLookupHits);
7949 // We don't need to do identifier table lookups in C++ modules (we preload
7950 // all interesting declarations, and don't need to use the scope for name
7951 // lookups). Perform the lookup in PCH files, though, since we don't build
7952 // a complete initial identifier table if we're carrying on from a PCH.
7953 if (PP.getLangOpts().CPlusPlus) {
7954 for (auto F : ModuleMgr.pch_modules())
7958 // If there is a global index, look there first to determine which modules
7959 // provably do not have any results for this identifier.
7960 GlobalModuleIndex::HitSet Hits;
7961 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7962 if (!loadGlobalIndex()) {
7963 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7968 ModuleMgr.visit(Visitor, HitsPtr);
7971 IdentifierInfo *II = Visitor.getIdentifierInfo();
7972 markIdentifierUpToDate(II);
7978 /// An identifier-lookup iterator that enumerates all of the
7979 /// identifiers stored within a set of AST files.
7980 class ASTIdentifierIterator : public IdentifierIterator {
7981 /// The AST reader whose identifiers are being enumerated.
7982 const ASTReader &Reader;
7984 /// The current index into the chain of AST files stored in
7988 /// The current position within the identifier lookup table
7989 /// of the current AST file.
7990 ASTIdentifierLookupTable::key_iterator Current;
7992 /// The end position within the identifier lookup table of
7993 /// the current AST file.
7994 ASTIdentifierLookupTable::key_iterator End;
7996 /// Whether to skip any modules in the ASTReader.
8000 explicit ASTIdentifierIterator(const ASTReader &Reader,
8001 bool SkipModules = false);
8003 StringRef Next() override;
8006 } // namespace clang
8008 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8010 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8013 StringRef ASTIdentifierIterator::Next() {
8014 while (Current == End) {
8015 // If we have exhausted all of our AST files, we're done.
8020 ModuleFile &F = Reader.ModuleMgr[Index];
8021 if (SkipModules && F.isModule())
8024 ASTIdentifierLookupTable *IdTable =
8025 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8026 Current = IdTable->key_begin();
8027 End = IdTable->key_end();
8030 // We have any identifiers remaining in the current AST file; return
8032 StringRef Result = *Current;
8039 /// A utility for appending two IdentifierIterators.
8040 class ChainedIdentifierIterator : public IdentifierIterator {
8041 std::unique_ptr<IdentifierIterator> Current;
8042 std::unique_ptr<IdentifierIterator> Queued;
8045 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8046 std::unique_ptr<IdentifierIterator> Second)
8047 : Current(std::move(First)), Queued(std::move(Second)) {}
8049 StringRef Next() override {
8053 StringRef result = Current->Next();
8054 if (!result.empty())
8057 // Try the queued iterator, which may itself be empty.
8059 std::swap(Current, Queued);
8066 IdentifierIterator *ASTReader::getIdentifiers() {
8067 if (!loadGlobalIndex()) {
8068 std::unique_ptr<IdentifierIterator> ReaderIter(
8069 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8070 std::unique_ptr<IdentifierIterator> ModulesIter(
8071 GlobalIndex->createIdentifierIterator());
8072 return new ChainedIdentifierIterator(std::move(ReaderIter),
8073 std::move(ModulesIter));
8076 return new ASTIdentifierIterator(*this);
8080 namespace serialization {
8082 class ReadMethodPoolVisitor {
8085 unsigned PriorGeneration;
8086 unsigned InstanceBits = 0;
8087 unsigned FactoryBits = 0;
8088 bool InstanceHasMoreThanOneDecl = false;
8089 bool FactoryHasMoreThanOneDecl = false;
8090 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8091 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8094 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8095 unsigned PriorGeneration)
8096 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8098 bool operator()(ModuleFile &M) {
8099 if (!M.SelectorLookupTable)
8102 // If we've already searched this module file, skip it now.
8103 if (M.Generation <= PriorGeneration)
8106 ++Reader.NumMethodPoolTableLookups;
8107 ASTSelectorLookupTable *PoolTable
8108 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8109 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8110 if (Pos == PoolTable->end())
8113 ++Reader.NumMethodPoolTableHits;
8114 ++Reader.NumSelectorsRead;
8115 // FIXME: Not quite happy with the statistics here. We probably should
8116 // disable this tracking when called via LoadSelector.
8117 // Also, should entries without methods count as misses?
8118 ++Reader.NumMethodPoolEntriesRead;
8119 ASTSelectorLookupTrait::data_type Data = *Pos;
8120 if (Reader.DeserializationListener)
8121 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8123 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8124 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8125 InstanceBits = Data.InstanceBits;
8126 FactoryBits = Data.FactoryBits;
8127 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8128 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8132 /// Retrieve the instance methods found by this visitor.
8133 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8134 return InstanceMethods;
8137 /// Retrieve the instance methods found by this visitor.
8138 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8139 return FactoryMethods;
8142 unsigned getInstanceBits() const { return InstanceBits; }
8143 unsigned getFactoryBits() const { return FactoryBits; }
8145 bool instanceHasMoreThanOneDecl() const {
8146 return InstanceHasMoreThanOneDecl;
8149 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8152 } // namespace serialization
8153 } // namespace clang
8155 /// Add the given set of methods to the method list.
8156 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8157 ObjCMethodList &List) {
8158 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8159 S.addMethodToGlobalList(&List, Methods[I]);
8163 void ASTReader::ReadMethodPool(Selector Sel) {
8164 // Get the selector generation and update it to the current generation.
8165 unsigned &Generation = SelectorGeneration[Sel];
8166 unsigned PriorGeneration = Generation;
8167 Generation = getGeneration();
8168 SelectorOutOfDate[Sel] = false;
8170 // Search for methods defined with this selector.
8171 ++NumMethodPoolLookups;
8172 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8173 ModuleMgr.visit(Visitor);
8175 if (Visitor.getInstanceMethods().empty() &&
8176 Visitor.getFactoryMethods().empty())
8179 ++NumMethodPoolHits;
8184 Sema &S = *getSema();
8185 Sema::GlobalMethodPool::iterator Pos
8186 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8188 Pos->second.first.setBits(Visitor.getInstanceBits());
8189 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8190 Pos->second.second.setBits(Visitor.getFactoryBits());
8191 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8193 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8194 // when building a module we keep every method individually and may need to
8195 // update hasMoreThanOneDecl as we add the methods.
8196 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8197 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8200 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8201 if (SelectorOutOfDate[Sel])
8202 ReadMethodPool(Sel);
8205 void ASTReader::ReadKnownNamespaces(
8206 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8209 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8210 if (NamespaceDecl *Namespace
8211 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8212 Namespaces.push_back(Namespace);
8216 void ASTReader::ReadUndefinedButUsed(
8217 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8218 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8219 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8220 SourceLocation Loc =
8221 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8222 Undefined.insert(std::make_pair(D, Loc));
8226 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8227 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8229 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8230 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8231 uint64_t Count = DelayedDeleteExprs[Idx++];
8232 for (uint64_t C = 0; C < Count; ++C) {
8233 SourceLocation DeleteLoc =
8234 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8235 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8236 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8241 void ASTReader::ReadTentativeDefinitions(
8242 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8243 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8244 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8246 TentativeDefs.push_back(Var);
8248 TentativeDefinitions.clear();
8251 void ASTReader::ReadUnusedFileScopedDecls(
8252 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8253 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8255 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8259 UnusedFileScopedDecls.clear();
8262 void ASTReader::ReadDelegatingConstructors(
8263 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8264 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8265 CXXConstructorDecl *D
8266 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8270 DelegatingCtorDecls.clear();
8273 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8274 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8276 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8280 ExtVectorDecls.clear();
8283 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8284 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8285 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8287 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8288 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8292 UnusedLocalTypedefNameCandidates.clear();
8295 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8296 llvm::SmallVector<Decl *, 4> &Decls) {
8297 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8299 auto *D = dyn_cast_or_null<Decl>(
8300 GetDecl(DeclsToCheckForDeferredDiags[I]));
8304 DeclsToCheckForDeferredDiags.clear();
8308 void ASTReader::ReadReferencedSelectors(
8309 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8310 if (ReferencedSelectorsData.empty())
8313 // If there are @selector references added them to its pool. This is for
8314 // implementation of -Wselector.
8315 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8317 while (I < DataSize) {
8318 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8319 SourceLocation SelLoc
8320 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8321 Sels.push_back(std::make_pair(Sel, SelLoc));
8323 ReferencedSelectorsData.clear();
8326 void ASTReader::ReadWeakUndeclaredIdentifiers(
8327 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8328 if (WeakUndeclaredIdentifiers.empty())
8331 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8332 IdentifierInfo *WeakId
8333 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8334 IdentifierInfo *AliasId
8335 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8337 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8338 bool Used = WeakUndeclaredIdentifiers[I++];
8339 WeakInfo WI(AliasId, Loc);
8341 WeakIDs.push_back(std::make_pair(WeakId, WI));
8343 WeakUndeclaredIdentifiers.clear();
8346 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8347 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8348 ExternalVTableUse VT;
8349 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8350 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8351 VT.DefinitionRequired = VTableUses[Idx++];
8352 VTables.push_back(VT);
8358 void ASTReader::ReadPendingInstantiations(
8359 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8360 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8361 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8363 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8365 Pending.push_back(std::make_pair(D, Loc));
8367 PendingInstantiations.clear();
8370 void ASTReader::ReadLateParsedTemplates(
8371 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8373 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8375 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8377 auto LT = std::make_unique<LateParsedTemplate>();
8378 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8380 ModuleFile *F = getOwningModuleFile(LT->D);
8381 assert(F && "No module");
8383 unsigned TokN = LateParsedTemplates[Idx++];
8384 LT->Toks.reserve(TokN);
8385 for (unsigned T = 0; T < TokN; ++T)
8386 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8388 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8391 LateParsedTemplates.clear();
8394 void ASTReader::LoadSelector(Selector Sel) {
8395 // It would be complicated to avoid reading the methods anyway. So don't.
8396 ReadMethodPool(Sel);
8399 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8400 assert(ID && "Non-zero identifier ID required");
8401 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8402 IdentifiersLoaded[ID - 1] = II;
8403 if (DeserializationListener)
8404 DeserializationListener->IdentifierRead(ID, II);
8407 /// Set the globally-visible declarations associated with the given
8410 /// If the AST reader is currently in a state where the given declaration IDs
8411 /// cannot safely be resolved, they are queued until it is safe to resolve
8414 /// \param II an IdentifierInfo that refers to one or more globally-visible
8417 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8418 /// visible at global scope.
8420 /// \param Decls if non-null, this vector will be populated with the set of
8421 /// deserialized declarations. These declarations will not be pushed into
8424 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8425 const SmallVectorImpl<uint32_t> &DeclIDs,
8426 SmallVectorImpl<Decl *> *Decls) {
8427 if (NumCurrentElementsDeserializing && !Decls) {
8428 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8432 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8434 // Queue this declaration so that it will be added to the
8435 // translation unit scope and identifier's declaration chain
8436 // once a Sema object is known.
8437 PreloadedDeclIDs.push_back(DeclIDs[I]);
8441 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8443 // If we're simply supposed to record the declarations, do so now.
8445 Decls->push_back(D);
8449 // Introduce this declaration into the translation-unit scope
8450 // and add it to the declaration chain for this identifier, so
8451 // that (unqualified) name lookup will find it.
8452 pushExternalDeclIntoScope(D, II);
8456 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8460 if (IdentifiersLoaded.empty()) {
8461 Error("no identifier table in AST file");
8466 if (!IdentifiersLoaded[ID]) {
8467 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8468 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8469 ModuleFile *M = I->second;
8470 unsigned Index = ID - M->BaseIdentifierID;
8471 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8473 // All of the strings in the AST file are preceded by a 16-bit length.
8474 // Extract that 16-bit length to avoid having to execute strlen().
8475 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8476 // unsigned integers. This is important to avoid integer overflow when
8477 // we cast them to 'unsigned'.
8478 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8479 unsigned StrLen = (((unsigned) StrLenPtr[0])
8480 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8481 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8482 IdentifiersLoaded[ID] = &II;
8483 markIdentifierFromAST(*this, II);
8484 if (DeserializationListener)
8485 DeserializationListener->IdentifierRead(ID + 1, &II);
8488 return IdentifiersLoaded[ID];
8491 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8492 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8495 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8496 if (LocalID < NUM_PREDEF_IDENT_IDS)
8499 if (!M.ModuleOffsetMap.empty())
8500 ReadModuleOffsetMap(M);
8502 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8503 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8504 assert(I != M.IdentifierRemap.end()
8505 && "Invalid index into identifier index remap");
8507 return LocalID + I->second;
8510 MacroInfo *ASTReader::getMacro(MacroID ID) {
8514 if (MacrosLoaded.empty()) {
8515 Error("no macro table in AST file");
8519 ID -= NUM_PREDEF_MACRO_IDS;
8520 if (!MacrosLoaded[ID]) {
8521 GlobalMacroMapType::iterator I
8522 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8523 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8524 ModuleFile *M = I->second;
8525 unsigned Index = ID - M->BaseMacroID;
8527 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8529 if (DeserializationListener)
8530 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8534 return MacrosLoaded[ID];
8537 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8538 if (LocalID < NUM_PREDEF_MACRO_IDS)
8541 if (!M.ModuleOffsetMap.empty())
8542 ReadModuleOffsetMap(M);
8544 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8545 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8546 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8548 return LocalID + I->second;
8551 serialization::SubmoduleID
8552 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8553 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8556 if (!M.ModuleOffsetMap.empty())
8557 ReadModuleOffsetMap(M);
8559 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8560 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8561 assert(I != M.SubmoduleRemap.end()
8562 && "Invalid index into submodule index remap");
8564 return LocalID + I->second;
8567 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8568 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8569 assert(GlobalID == 0 && "Unhandled global submodule ID");
8573 if (GlobalID > SubmodulesLoaded.size()) {
8574 Error("submodule ID out of range in AST file");
8578 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8581 Module *ASTReader::getModule(unsigned ID) {
8582 return getSubmodule(ID);
8585 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8586 ModuleFile *MF = getOwningModuleFile(D);
8587 return MF && MF->PCHHasObjectFile;
8590 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8592 // It's a module, look it up by submodule ID.
8593 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8594 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8596 // It's a prefix (preamble, PCH, ...). Look it up by index.
8597 unsigned IndexFromEnd = ID >> 1;
8598 assert(IndexFromEnd && "got reference to unknown module file");
8599 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8603 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8607 // For a file representing a module, use the submodule ID of the top-level
8608 // module as the file ID. For any other kind of file, the number of such
8609 // files loaded beforehand will be the same on reload.
8610 // FIXME: Is this true even if we have an explicit module file and a PCH?
8612 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8614 auto PCHModules = getModuleManager().pch_modules();
8615 auto I = llvm::find(PCHModules, F);
8616 assert(I != PCHModules.end() && "emitting reference to unknown file");
8617 return (I - PCHModules.end()) << 1;
8620 llvm::Optional<ASTSourceDescriptor>
8621 ASTReader::getSourceDescriptor(unsigned ID) {
8622 if (Module *M = getSubmodule(ID))
8623 return ASTSourceDescriptor(*M);
8625 // If there is only a single PCH, return it instead.
8626 // Chained PCH are not supported.
8627 const auto &PCHChain = ModuleMgr.pch_modules();
8628 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8629 ModuleFile &MF = ModuleMgr.getPrimaryModule();
8630 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8631 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8632 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8638 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8639 auto I = DefinitionSource.find(FD);
8640 if (I == DefinitionSource.end())
8641 return EK_ReplyHazy;
8642 return I->second ? EK_Never : EK_Always;
8645 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8646 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8649 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8653 if (ID > SelectorsLoaded.size()) {
8654 Error("selector ID out of range in AST file");
8658 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8659 // Load this selector from the selector table.
8660 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8661 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8662 ModuleFile &M = *I->second;
8663 ASTSelectorLookupTrait Trait(*this, M);
8664 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8665 SelectorsLoaded[ID - 1] =
8666 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8667 if (DeserializationListener)
8668 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8671 return SelectorsLoaded[ID - 1];
8674 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8675 return DecodeSelector(ID);
8678 uint32_t ASTReader::GetNumExternalSelectors() {
8679 // ID 0 (the null selector) is considered an external selector.
8680 return getTotalNumSelectors() + 1;
8683 serialization::SelectorID
8684 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8685 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8688 if (!M.ModuleOffsetMap.empty())
8689 ReadModuleOffsetMap(M);
8691 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8692 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8693 assert(I != M.SelectorRemap.end()
8694 && "Invalid index into selector index remap");
8696 return LocalID + I->second;
8700 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8701 DeclarationNameLoc DNLoc;
8702 switch (Name.getNameKind()) {
8703 case DeclarationName::CXXConstructorName:
8704 case DeclarationName::CXXDestructorName:
8705 case DeclarationName::CXXConversionFunctionName:
8706 DNLoc.NamedType.TInfo = readTypeSourceInfo();
8709 case DeclarationName::CXXOperatorName:
8710 DNLoc.CXXOperatorName.BeginOpNameLoc
8711 = readSourceLocation().getRawEncoding();
8712 DNLoc.CXXOperatorName.EndOpNameLoc
8713 = readSourceLocation().getRawEncoding();
8716 case DeclarationName::CXXLiteralOperatorName:
8717 DNLoc.CXXLiteralOperatorName.OpNameLoc
8718 = readSourceLocation().getRawEncoding();
8721 case DeclarationName::Identifier:
8722 case DeclarationName::ObjCZeroArgSelector:
8723 case DeclarationName::ObjCOneArgSelector:
8724 case DeclarationName::ObjCMultiArgSelector:
8725 case DeclarationName::CXXUsingDirective:
8726 case DeclarationName::CXXDeductionGuideName:
8732 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8733 DeclarationNameInfo NameInfo;
8734 NameInfo.setName(readDeclarationName());
8735 NameInfo.setLoc(readSourceLocation());
8736 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8740 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8741 Info.QualifierLoc = readNestedNameSpecifierLoc();
8742 unsigned NumTPLists = readInt();
8743 Info.NumTemplParamLists = NumTPLists;
8745 Info.TemplParamLists =
8746 new (getContext()) TemplateParameterList *[NumTPLists];
8747 for (unsigned i = 0; i != NumTPLists; ++i)
8748 Info.TemplParamLists[i] = readTemplateParameterList();
8752 TemplateParameterList *
8753 ASTRecordReader::readTemplateParameterList() {
8754 SourceLocation TemplateLoc = readSourceLocation();
8755 SourceLocation LAngleLoc = readSourceLocation();
8756 SourceLocation RAngleLoc = readSourceLocation();
8758 unsigned NumParams = readInt();
8759 SmallVector<NamedDecl *, 16> Params;
8760 Params.reserve(NumParams);
8762 Params.push_back(readDeclAs<NamedDecl>());
8764 bool HasRequiresClause = readBool();
8765 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8767 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8768 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8769 return TemplateParams;
8772 void ASTRecordReader::readTemplateArgumentList(
8773 SmallVectorImpl<TemplateArgument> &TemplArgs,
8774 bool Canonicalize) {
8775 unsigned NumTemplateArgs = readInt();
8776 TemplArgs.reserve(NumTemplateArgs);
8777 while (NumTemplateArgs--)
8778 TemplArgs.push_back(readTemplateArgument(Canonicalize));
8781 /// Read a UnresolvedSet structure.
8782 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8783 unsigned NumDecls = readInt();
8784 Set.reserve(getContext(), NumDecls);
8785 while (NumDecls--) {
8786 DeclID ID = readDeclID();
8787 AccessSpecifier AS = (AccessSpecifier) readInt();
8788 Set.addLazyDecl(getContext(), ID, AS);
8793 ASTRecordReader::readCXXBaseSpecifier() {
8794 bool isVirtual = readBool();
8795 bool isBaseOfClass = readBool();
8796 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8797 bool inheritConstructors = readBool();
8798 TypeSourceInfo *TInfo = readTypeSourceInfo();
8799 SourceRange Range = readSourceRange();
8800 SourceLocation EllipsisLoc = readSourceLocation();
8801 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8803 Result.setInheritConstructors(inheritConstructors);
8807 CXXCtorInitializer **
8808 ASTRecordReader::readCXXCtorInitializers() {
8809 ASTContext &Context = getContext();
8810 unsigned NumInitializers = readInt();
8811 assert(NumInitializers && "wrote ctor initializers but have no inits");
8812 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8813 for (unsigned i = 0; i != NumInitializers; ++i) {
8814 TypeSourceInfo *TInfo = nullptr;
8815 bool IsBaseVirtual = false;
8816 FieldDecl *Member = nullptr;
8817 IndirectFieldDecl *IndirectMember = nullptr;
8819 CtorInitializerType Type = (CtorInitializerType) readInt();
8821 case CTOR_INITIALIZER_BASE:
8822 TInfo = readTypeSourceInfo();
8823 IsBaseVirtual = readBool();
8826 case CTOR_INITIALIZER_DELEGATING:
8827 TInfo = readTypeSourceInfo();
8830 case CTOR_INITIALIZER_MEMBER:
8831 Member = readDeclAs<FieldDecl>();
8834 case CTOR_INITIALIZER_INDIRECT_MEMBER:
8835 IndirectMember = readDeclAs<IndirectFieldDecl>();
8839 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8840 Expr *Init = readExpr();
8841 SourceLocation LParenLoc = readSourceLocation();
8842 SourceLocation RParenLoc = readSourceLocation();
8844 CXXCtorInitializer *BOMInit;
8845 if (Type == CTOR_INITIALIZER_BASE)
8846 BOMInit = new (Context)
8847 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8848 RParenLoc, MemberOrEllipsisLoc);
8849 else if (Type == CTOR_INITIALIZER_DELEGATING)
8850 BOMInit = new (Context)
8851 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8853 BOMInit = new (Context)
8854 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8857 BOMInit = new (Context)
8858 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8859 LParenLoc, Init, RParenLoc);
8861 if (/*IsWritten*/readBool()) {
8862 unsigned SourceOrder = readInt();
8863 BOMInit->setSourceOrder(SourceOrder);
8866 CtorInitializers[i] = BOMInit;
8869 return CtorInitializers;
8872 NestedNameSpecifierLoc
8873 ASTRecordReader::readNestedNameSpecifierLoc() {
8874 ASTContext &Context = getContext();
8875 unsigned N = readInt();
8876 NestedNameSpecifierLocBuilder Builder;
8877 for (unsigned I = 0; I != N; ++I) {
8878 auto Kind = readNestedNameSpecifierKind();
8880 case NestedNameSpecifier::Identifier: {
8881 IdentifierInfo *II = readIdentifier();
8882 SourceRange Range = readSourceRange();
8883 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8887 case NestedNameSpecifier::Namespace: {
8888 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8889 SourceRange Range = readSourceRange();
8890 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8894 case NestedNameSpecifier::NamespaceAlias: {
8895 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8896 SourceRange Range = readSourceRange();
8897 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8901 case NestedNameSpecifier::TypeSpec:
8902 case NestedNameSpecifier::TypeSpecWithTemplate: {
8903 bool Template = readBool();
8904 TypeSourceInfo *T = readTypeSourceInfo();
8906 return NestedNameSpecifierLoc();
8907 SourceLocation ColonColonLoc = readSourceLocation();
8909 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8910 Builder.Extend(Context,
8911 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8912 T->getTypeLoc(), ColonColonLoc);
8916 case NestedNameSpecifier::Global: {
8917 SourceLocation ColonColonLoc = readSourceLocation();
8918 Builder.MakeGlobal(Context, ColonColonLoc);
8922 case NestedNameSpecifier::Super: {
8923 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8924 SourceRange Range = readSourceRange();
8925 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8931 return Builder.getWithLocInContext(Context);
8935 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8937 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8938 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8939 return SourceRange(beg, end);
8942 static FixedPointSemantics
8943 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8945 unsigned Width = Record[Idx++];
8946 unsigned Scale = Record[Idx++];
8947 uint64_t Tmp = Record[Idx++];
8948 bool IsSigned = Tmp & 0x1;
8949 bool IsSaturated = Tmp & 0x2;
8950 bool HasUnsignedPadding = Tmp & 0x4;
8951 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8952 HasUnsignedPadding);
8955 static const llvm::fltSemantics &
8956 readAPFloatSemantics(ASTRecordReader &reader) {
8957 return llvm::APFloatBase::EnumToSemantics(
8958 static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8961 APValue ASTRecordReader::readAPValue() {
8962 unsigned Kind = readInt();
8963 switch ((APValue::ValueKind) Kind) {
8966 case APValue::Indeterminate:
8967 return APValue::IndeterminateValue();
8969 return APValue(readAPSInt());
8970 case APValue::Float: {
8971 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8972 return APValue(readAPFloat(FloatSema));
8974 case APValue::FixedPoint: {
8975 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8976 return APValue(APFixedPoint(readAPInt(), FPSema));
8978 case APValue::ComplexInt: {
8979 llvm::APSInt First = readAPSInt();
8980 return APValue(std::move(First), readAPSInt());
8982 case APValue::ComplexFloat: {
8983 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8984 llvm::APFloat First = readAPFloat(FloatSema1);
8985 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8986 return APValue(std::move(First), readAPFloat(FloatSema2));
8988 case APValue::LValue:
8989 case APValue::Vector:
8990 case APValue::Array:
8991 case APValue::Struct:
8992 case APValue::Union:
8993 case APValue::MemberPointer:
8994 case APValue::AddrLabelDiff:
8995 // TODO : Handle all these APValue::ValueKind.
8998 llvm_unreachable("Invalid APValue::ValueKind");
9001 /// Read a floating-point value
9002 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9003 return llvm::APFloat(Sem, readAPInt());
9007 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9008 unsigned Len = Record[Idx++];
9009 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9014 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9016 std::string Filename = ReadString(Record, Idx);
9017 ResolveImportedPath(F, Filename);
9021 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9022 const RecordData &Record, unsigned &Idx) {
9023 std::string Filename = ReadString(Record, Idx);
9024 if (!BaseDirectory.empty())
9025 ResolveImportedPath(Filename, BaseDirectory);
9029 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9031 unsigned Major = Record[Idx++];
9032 unsigned Minor = Record[Idx++];
9033 unsigned Subminor = Record[Idx++];
9035 return VersionTuple(Major);
9037 return VersionTuple(Major, Minor - 1);
9038 return VersionTuple(Major, Minor - 1, Subminor - 1);
9041 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9042 const RecordData &Record,
9044 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9045 return CXXTemporary::Create(getContext(), Decl);
9048 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9049 return Diag(CurrentImportLoc, DiagID);
9052 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9053 return Diags.Report(Loc, DiagID);
9056 /// Retrieve the identifier table associated with the
9058 IdentifierTable &ASTReader::getIdentifierTable() {
9059 return PP.getIdentifierTable();
9062 /// Record that the given ID maps to the given switch-case
9064 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9065 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9066 "Already have a SwitchCase with this ID");
9067 (*CurrSwitchCaseStmts)[ID] = SC;
9070 /// Retrieve the switch-case statement with the given ID.
9071 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9072 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9073 return (*CurrSwitchCaseStmts)[ID];
9076 void ASTReader::ClearSwitchCaseIDs() {
9077 CurrSwitchCaseStmts->clear();
9080 void ASTReader::ReadComments() {
9081 ASTContext &Context = getContext();
9082 std::vector<RawComment *> Comments;
9083 for (SmallVectorImpl<std::pair<BitstreamCursor,
9084 serialization::ModuleFile *>>::iterator
9085 I = CommentsCursors.begin(),
9086 E = CommentsCursors.end();
9089 BitstreamCursor &Cursor = I->first;
9090 serialization::ModuleFile &F = *I->second;
9091 SavedStreamPosition SavedPosition(Cursor);
9095 Expected<llvm::BitstreamEntry> MaybeEntry =
9096 Cursor.advanceSkippingSubblocks(
9097 BitstreamCursor::AF_DontPopBlockAtEnd);
9099 Error(MaybeEntry.takeError());
9102 llvm::BitstreamEntry Entry = MaybeEntry.get();
9104 switch (Entry.Kind) {
9105 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9106 case llvm::BitstreamEntry::Error:
9107 Error("malformed block record in AST file");
9109 case llvm::BitstreamEntry::EndBlock:
9111 case llvm::BitstreamEntry::Record:
9112 // The interesting case.
9118 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9119 if (!MaybeComment) {
9120 Error(MaybeComment.takeError());
9123 switch ((CommentRecordTypes)MaybeComment.get()) {
9124 case COMMENTS_RAW_COMMENT: {
9126 SourceRange SR = ReadSourceRange(F, Record, Idx);
9127 RawComment::CommentKind Kind =
9128 (RawComment::CommentKind) Record[Idx++];
9129 bool IsTrailingComment = Record[Idx++];
9130 bool IsAlmostTrailingComment = Record[Idx++];
9131 Comments.push_back(new (Context) RawComment(
9132 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9138 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9139 FileToOffsetToComment;
9140 for (RawComment *C : Comments) {
9141 SourceLocation CommentLoc = C->getBeginLoc();
9142 if (CommentLoc.isValid()) {
9143 std::pair<FileID, unsigned> Loc =
9144 SourceMgr.getDecomposedLoc(CommentLoc);
9145 if (Loc.first.isValid())
9146 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9152 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9153 bool IncludeSystem, bool Complain,
9154 llvm::function_ref<void(const serialization::InputFile &IF,
9155 bool isSystem)> Visitor) {
9156 unsigned NumUserInputs = MF.NumUserInputFiles;
9157 unsigned NumInputs = MF.InputFilesLoaded.size();
9158 assert(NumUserInputs <= NumInputs);
9159 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9160 for (unsigned I = 0; I < N; ++I) {
9161 bool IsSystem = I >= NumUserInputs;
9162 InputFile IF = getInputFile(MF, I+1, Complain);
9163 Visitor(IF, IsSystem);
9167 void ASTReader::visitTopLevelModuleMaps(
9168 serialization::ModuleFile &MF,
9169 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9170 unsigned NumInputs = MF.InputFilesLoaded.size();
9171 for (unsigned I = 0; I < NumInputs; ++I) {
9172 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9173 if (IFI.TopLevelModuleMap)
9174 // FIXME: This unnecessarily re-reads the InputFileInfo.
9175 if (auto *FE = getInputFile(MF, I + 1).getFile())
9180 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9181 // If we know the owning module, use it.
9182 if (Module *M = D->getImportedOwningModule())
9183 return M->getFullModuleName();
9185 // Otherwise, use the name of the top-level module the decl is within.
9186 if (ModuleFile *M = getOwningModuleFile(D))
9187 return M->ModuleName;
9189 // Not from a module.
9193 void ASTReader::finishPendingActions() {
9194 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9195 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9196 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9197 !PendingUpdateRecords.empty()) {
9198 // If any identifiers with corresponding top-level declarations have
9199 // been loaded, load those declarations now.
9200 using TopLevelDeclsMap =
9201 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9202 TopLevelDeclsMap TopLevelDecls;
9204 while (!PendingIdentifierInfos.empty()) {
9205 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9206 SmallVector<uint32_t, 4> DeclIDs =
9207 std::move(PendingIdentifierInfos.back().second);
9208 PendingIdentifierInfos.pop_back();
9210 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9213 // Load each function type that we deferred loading because it was a
9214 // deduced type that might refer to a local type declared within itself.
9215 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9216 auto *FD = PendingFunctionTypes[I].first;
9217 FD->setType(GetType(PendingFunctionTypes[I].second));
9219 // If we gave a function a deduced return type, remember that we need to
9220 // propagate that along the redeclaration chain.
9221 auto *DT = FD->getReturnType()->getContainedDeducedType();
9222 if (DT && DT->isDeduced())
9223 PendingDeducedTypeUpdates.insert(
9224 {FD->getCanonicalDecl(), FD->getReturnType()});
9226 PendingFunctionTypes.clear();
9228 // For each decl chain that we wanted to complete while deserializing, mark
9229 // it as "still needs to be completed".
9230 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9231 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9233 PendingIncompleteDeclChains.clear();
9235 // Load pending declaration chains.
9236 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9237 loadPendingDeclChain(PendingDeclChains[I].first,
9238 PendingDeclChains[I].second);
9239 PendingDeclChains.clear();
9241 // Make the most recent of the top-level declarations visible.
9242 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9243 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9244 IdentifierInfo *II = TLD->first;
9245 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9246 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9250 // Load any pending macro definitions.
9251 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9252 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9253 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9254 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9255 // Initialize the macro history from chained-PCHs ahead of module imports.
9256 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9258 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9259 if (!Info.M->isModule())
9260 resolvePendingMacro(II, Info);
9262 // Handle module imports.
9263 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9265 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9266 if (Info.M->isModule())
9267 resolvePendingMacro(II, Info);
9270 PendingMacroIDs.clear();
9272 // Wire up the DeclContexts for Decls that we delayed setting until
9273 // recursive loading is completed.
9274 while (!PendingDeclContextInfos.empty()) {
9275 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9276 PendingDeclContextInfos.pop_front();
9277 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9278 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9279 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9282 // Perform any pending declaration updates.
9283 while (!PendingUpdateRecords.empty()) {
9284 auto Update = PendingUpdateRecords.pop_back_val();
9285 ReadingKindTracker ReadingKind(Read_Decl, *this);
9286 loadDeclUpdateRecords(Update);
9290 // At this point, all update records for loaded decls are in place, so any
9291 // fake class definitions should have become real.
9292 assert(PendingFakeDefinitionData.empty() &&
9293 "faked up a class definition but never saw the real one");
9295 // If we deserialized any C++ or Objective-C class definitions, any
9296 // Objective-C protocol definitions, or any redeclarable templates, make sure
9297 // that all redeclarations point to the definitions. Note that this can only
9298 // happen now, after the redeclaration chains have been fully wired.
9299 for (Decl *D : PendingDefinitions) {
9300 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9301 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9302 // Make sure that the TagType points at the definition.
9303 const_cast<TagType*>(TagT)->decl = TD;
9306 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9307 for (auto *R = getMostRecentExistingDecl(RD); R;
9308 R = R->getPreviousDecl()) {
9310 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9311 "declaration thinks it's the definition but it isn't");
9312 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9319 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9320 // Make sure that the ObjCInterfaceType points at the definition.
9321 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9324 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9325 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9330 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9331 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9332 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9337 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9338 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9339 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9341 PendingDefinitions.clear();
9343 // Load the bodies of any functions or methods we've encountered. We do
9344 // this now (delayed) so that we can be sure that the declaration chains
9345 // have been fully wired up (hasBody relies on this).
9346 // FIXME: We shouldn't require complete redeclaration chains here.
9347 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9348 PBEnd = PendingBodies.end();
9349 PB != PBEnd; ++PB) {
9350 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9351 // For a function defined inline within a class template, force the
9352 // canonical definition to be the one inside the canonical definition of
9353 // the template. This ensures that we instantiate from a correct view
9356 // Sadly we can't do this more generally: we can't be sure that all
9357 // copies of an arbitrary class definition will have the same members
9358 // defined (eg, some member functions may not be instantiated, and some
9359 // special members may or may not have been implicitly defined).
9360 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9361 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9364 // FIXME: Check for =delete/=default?
9365 // FIXME: Complain about ODR violations here?
9366 const FunctionDecl *Defn = nullptr;
9367 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9368 FD->setLazyBody(PB->second);
9370 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9371 mergeDefinitionVisibility(NonConstDefn, FD);
9373 if (!FD->isLateTemplateParsed() &&
9374 !NonConstDefn->isLateTemplateParsed() &&
9375 FD->getODRHash() != NonConstDefn->getODRHash()) {
9376 if (!isa<CXXMethodDecl>(FD)) {
9377 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9378 } else if (FD->getLexicalParent()->isFileContext() &&
9379 NonConstDefn->getLexicalParent()->isFileContext()) {
9380 // Only diagnose out-of-line method definitions. If they are
9381 // in class definitions, then an error will be generated when
9382 // processing the class bodies.
9383 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9390 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9391 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9392 MD->setLazyBody(PB->second);
9394 PendingBodies.clear();
9397 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9398 getContext().deduplicateMergedDefinitonsFor(ND);
9399 PendingMergedDefinitionsToDeduplicate.clear();
9402 void ASTReader::diagnoseOdrViolations() {
9403 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9404 PendingFunctionOdrMergeFailures.empty() &&
9405 PendingEnumOdrMergeFailures.empty())
9408 // Trigger the import of the full definition of each class that had any
9409 // odr-merging problems, so we can produce better diagnostics for them.
9410 // These updates may in turn find and diagnose some ODR failures, so take
9411 // ownership of the set first.
9412 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9413 PendingOdrMergeFailures.clear();
9414 for (auto &Merge : OdrMergeFailures) {
9415 Merge.first->buildLookup();
9416 Merge.first->decls_begin();
9417 Merge.first->bases_begin();
9418 Merge.first->vbases_begin();
9419 for (auto &RecordPair : Merge.second) {
9420 auto *RD = RecordPair.first;
9427 // Trigger the import of functions.
9428 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9429 PendingFunctionOdrMergeFailures.clear();
9430 for (auto &Merge : FunctionOdrMergeFailures) {
9431 Merge.first->buildLookup();
9432 Merge.first->decls_begin();
9433 Merge.first->getBody();
9434 for (auto &FD : Merge.second) {
9441 // Trigger the import of enums.
9442 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9443 PendingEnumOdrMergeFailures.clear();
9444 for (auto &Merge : EnumOdrMergeFailures) {
9445 Merge.first->decls_begin();
9446 for (auto &Enum : Merge.second) {
9447 Enum->decls_begin();
9451 // For each declaration from a merged context, check that the canonical
9452 // definition of that context also contains a declaration of the same
9455 // Caution: this loop does things that might invalidate iterators into
9456 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9457 while (!PendingOdrMergeChecks.empty()) {
9458 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9460 // FIXME: Skip over implicit declarations for now. This matters for things
9461 // like implicitly-declared special member functions. This isn't entirely
9462 // correct; we can end up with multiple unmerged declarations of the same
9464 if (D->isImplicit())
9467 DeclContext *CanonDef = D->getDeclContext();
9470 const Decl *DCanon = D->getCanonicalDecl();
9472 for (auto RI : D->redecls()) {
9473 if (RI->getLexicalDeclContext() == CanonDef) {
9481 // Quick check failed, time to do the slow thing. Note, we can't just
9482 // look up the name of D in CanonDef here, because the member that is
9483 // in CanonDef might not be found by name lookup (it might have been
9484 // replaced by a more recent declaration in the lookup table), and we
9485 // can't necessarily find it in the redeclaration chain because it might
9486 // be merely mergeable, not redeclarable.
9487 llvm::SmallVector<const NamedDecl*, 4> Candidates;
9488 for (auto *CanonMember : CanonDef->decls()) {
9489 if (CanonMember->getCanonicalDecl() == DCanon) {
9490 // This can happen if the declaration is merely mergeable and not
9491 // actually redeclarable (we looked for redeclarations earlier).
9493 // FIXME: We should be able to detect this more efficiently, without
9494 // pulling in all of the members of CanonDef.
9498 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9499 if (ND->getDeclName() == D->getDeclName())
9500 Candidates.push_back(ND);
9504 // The AST doesn't like TagDecls becoming invalid after they've been
9505 // completed. We only really need to mark FieldDecls as invalid here.
9506 if (!isa<TagDecl>(D))
9507 D->setInvalidDecl();
9509 // Ensure we don't accidentally recursively enter deserialization while
9510 // we're producing our diagnostic.
9511 Deserializing RecursionGuard(this);
9513 std::string CanonDefModule =
9514 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9515 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9516 << D << getOwningModuleNameForDiagnostic(D)
9517 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9519 if (Candidates.empty())
9520 Diag(cast<Decl>(CanonDef)->getLocation(),
9521 diag::note_module_odr_violation_no_possible_decls) << D;
9523 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9524 Diag(Candidates[I]->getLocation(),
9525 diag::note_module_odr_violation_possible_decl)
9529 DiagnosedOdrMergeFailures.insert(CanonDef);
9533 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9534 EnumOdrMergeFailures.empty())
9537 // Ensure we don't accidentally recursively enter deserialization while
9538 // we're producing our diagnostics.
9539 Deserializing RecursionGuard(this);
9541 // Common code for hashing helpers.
9543 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9545 Hash.AddQualType(Ty);
9546 return Hash.CalculateHash();
9549 auto ComputeODRHash = [&Hash](const Stmt *S) {
9553 return Hash.CalculateHash();
9556 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9560 return Hash.CalculateHash();
9563 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9565 Hash.AddTemplateArgument(TA);
9566 return Hash.CalculateHash();
9569 auto ComputeTemplateParameterListODRHash =
9570 [&Hash](const TemplateParameterList *TPL) {
9573 Hash.AddTemplateParameterList(TPL);
9574 return Hash.CalculateHash();
9577 // Used with err_module_odr_violation_mismatch_decl and
9578 // note_module_odr_violation_mismatch_decl
9579 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9580 enum ODRMismatchDecl {
9596 // Used with err_module_odr_violation_mismatch_decl_diff and
9597 // note_module_odr_violation_mismatch_decl_diff
9598 enum ODRMismatchDeclDifference {
9599 StaticAssertCondition,
9600 StaticAssertMessage,
9601 StaticAssertOnlyMessage,
9604 FieldSingleBitField,
9605 FieldDifferentWidthBitField,
9607 FieldSingleInitializer,
9608 FieldDifferentInitializers,
9617 MethodNumberParameters,
9618 MethodParameterType,
9619 MethodParameterName,
9620 MethodParameterSingleDefaultArgument,
9621 MethodParameterDifferentDefaultArgument,
9622 MethodNoTemplateArguments,
9623 MethodDifferentNumberTemplateArguments,
9624 MethodDifferentTemplateArgument,
9626 MethodDifferentBody,
9631 VarSingleInitializer,
9632 VarDifferentInitializer,
9637 FunctionTemplateDifferentNumberParameters,
9638 FunctionTemplateParameterDifferentKind,
9639 FunctionTemplateParameterName,
9640 FunctionTemplateParameterSingleDefaultArgument,
9641 FunctionTemplateParameterDifferentDefaultArgument,
9642 FunctionTemplateParameterDifferentType,
9643 FunctionTemplatePackParameter,
9646 // These lambdas have the common portions of the ODR diagnostics. This
9647 // has the same return as Diag(), so addition parameters can be passed
9648 // in with operator<<
9649 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9650 SourceLocation Loc, SourceRange Range,
9651 ODRMismatchDeclDifference DiffType) {
9652 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9653 << FirstRecord << FirstModule.empty() << FirstModule << Range
9656 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9657 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9658 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9659 << SecondModule << Range << DiffType;
9662 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9663 &ComputeQualTypeODRHash, &ComputeODRHash](
9664 NamedDecl *FirstRecord, StringRef FirstModule,
9665 StringRef SecondModule, FieldDecl *FirstField,
9666 FieldDecl *SecondField) {
9667 IdentifierInfo *FirstII = FirstField->getIdentifier();
9668 IdentifierInfo *SecondII = SecondField->getIdentifier();
9669 if (FirstII->getName() != SecondII->getName()) {
9670 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9671 FirstField->getSourceRange(), FieldName)
9673 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9674 SecondField->getSourceRange(), FieldName)
9680 assert(getContext().hasSameType(FirstField->getType(),
9681 SecondField->getType()));
9683 QualType FirstType = FirstField->getType();
9684 QualType SecondType = SecondField->getType();
9685 if (ComputeQualTypeODRHash(FirstType) !=
9686 ComputeQualTypeODRHash(SecondType)) {
9687 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9688 FirstField->getSourceRange(), FieldTypeName)
9689 << FirstII << FirstType;
9690 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9691 SecondField->getSourceRange(), FieldTypeName)
9692 << SecondII << SecondType;
9697 const bool IsFirstBitField = FirstField->isBitField();
9698 const bool IsSecondBitField = SecondField->isBitField();
9699 if (IsFirstBitField != IsSecondBitField) {
9700 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9701 FirstField->getSourceRange(), FieldSingleBitField)
9702 << FirstII << IsFirstBitField;
9703 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9704 SecondField->getSourceRange(), FieldSingleBitField)
9705 << SecondII << IsSecondBitField;
9709 if (IsFirstBitField && IsSecondBitField) {
9710 unsigned FirstBitWidthHash =
9711 ComputeODRHash(FirstField->getBitWidth());
9712 unsigned SecondBitWidthHash =
9713 ComputeODRHash(SecondField->getBitWidth());
9714 if (FirstBitWidthHash != SecondBitWidthHash) {
9715 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9716 FirstField->getSourceRange(),
9717 FieldDifferentWidthBitField)
9718 << FirstII << FirstField->getBitWidth()->getSourceRange();
9719 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9720 SecondField->getSourceRange(),
9721 FieldDifferentWidthBitField)
9722 << SecondII << SecondField->getBitWidth()->getSourceRange();
9727 if (!PP.getLangOpts().CPlusPlus)
9730 const bool IsFirstMutable = FirstField->isMutable();
9731 const bool IsSecondMutable = SecondField->isMutable();
9732 if (IsFirstMutable != IsSecondMutable) {
9733 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9734 FirstField->getSourceRange(), FieldSingleMutable)
9735 << FirstII << IsFirstMutable;
9736 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9737 SecondField->getSourceRange(), FieldSingleMutable)
9738 << SecondII << IsSecondMutable;
9742 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9743 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9744 if ((!FirstInitializer && SecondInitializer) ||
9745 (FirstInitializer && !SecondInitializer)) {
9746 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9747 FirstField->getSourceRange(), FieldSingleInitializer)
9748 << FirstII << (FirstInitializer != nullptr);
9749 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9750 SecondField->getSourceRange(), FieldSingleInitializer)
9751 << SecondII << (SecondInitializer != nullptr);
9755 if (FirstInitializer && SecondInitializer) {
9756 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9757 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9758 if (FirstInitHash != SecondInitHash) {
9759 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9760 FirstField->getSourceRange(),
9761 FieldDifferentInitializers)
9762 << FirstII << FirstInitializer->getSourceRange();
9763 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9764 SecondField->getSourceRange(),
9765 FieldDifferentInitializers)
9766 << SecondII << SecondInitializer->getSourceRange();
9774 auto ODRDiagTypeDefOrAlias =
9775 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9776 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9777 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9779 auto FirstName = FirstTD->getDeclName();
9780 auto SecondName = SecondTD->getDeclName();
9781 if (FirstName != SecondName) {
9782 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9783 FirstTD->getSourceRange(), TypedefName)
9784 << IsTypeAlias << FirstName;
9785 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9786 SecondTD->getSourceRange(), TypedefName)
9787 << IsTypeAlias << SecondName;
9791 QualType FirstType = FirstTD->getUnderlyingType();
9792 QualType SecondType = SecondTD->getUnderlyingType();
9793 if (ComputeQualTypeODRHash(FirstType) !=
9794 ComputeQualTypeODRHash(SecondType)) {
9795 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9796 FirstTD->getSourceRange(), TypedefType)
9797 << IsTypeAlias << FirstName << FirstType;
9798 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9799 SecondTD->getSourceRange(), TypedefType)
9800 << IsTypeAlias << SecondName << SecondType;
9807 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9808 &ComputeQualTypeODRHash, &ComputeODRHash,
9809 this](NamedDecl *FirstRecord, StringRef FirstModule,
9810 StringRef SecondModule, VarDecl *FirstVD,
9811 VarDecl *SecondVD) {
9812 auto FirstName = FirstVD->getDeclName();
9813 auto SecondName = SecondVD->getDeclName();
9814 if (FirstName != SecondName) {
9815 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9816 FirstVD->getSourceRange(), VarName)
9818 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9819 SecondVD->getSourceRange(), VarName)
9824 QualType FirstType = FirstVD->getType();
9825 QualType SecondType = SecondVD->getType();
9826 if (ComputeQualTypeODRHash(FirstType) !=
9827 ComputeQualTypeODRHash(SecondType)) {
9828 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9829 FirstVD->getSourceRange(), VarType)
9830 << FirstName << FirstType;
9831 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9832 SecondVD->getSourceRange(), VarType)
9833 << SecondName << SecondType;
9837 if (!PP.getLangOpts().CPlusPlus)
9840 const Expr *FirstInit = FirstVD->getInit();
9841 const Expr *SecondInit = SecondVD->getInit();
9842 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9843 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9844 FirstVD->getSourceRange(), VarSingleInitializer)
9845 << FirstName << (FirstInit == nullptr)
9846 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9847 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9848 SecondVD->getSourceRange(), VarSingleInitializer)
9849 << SecondName << (SecondInit == nullptr)
9850 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9854 if (FirstInit && SecondInit &&
9855 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9856 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9857 FirstVD->getSourceRange(), VarDifferentInitializer)
9858 << FirstName << FirstInit->getSourceRange();
9859 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9860 SecondVD->getSourceRange(), VarDifferentInitializer)
9861 << SecondName << SecondInit->getSourceRange();
9865 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9866 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9867 if (FirstIsConstexpr != SecondIsConstexpr) {
9868 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9869 FirstVD->getSourceRange(), VarConstexpr)
9870 << FirstName << FirstIsConstexpr;
9871 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9872 SecondVD->getSourceRange(), VarConstexpr)
9873 << SecondName << SecondIsConstexpr;
9879 auto DifferenceSelector = [](Decl *D) {
9880 assert(D && "valid Decl required");
9881 switch (D->getKind()) {
9884 case Decl::AccessSpec:
9885 switch (D->getAccess()) {
9887 return PublicSpecifer;
9889 return PrivateSpecifer;
9891 return ProtectedSpecifer;
9895 llvm_unreachable("Invalid access specifier");
9896 case Decl::StaticAssert:
9897 return StaticAssert;
9900 case Decl::CXXMethod:
9901 case Decl::CXXConstructor:
9902 case Decl::CXXDestructor:
9904 case Decl::TypeAlias:
9912 case Decl::FunctionTemplate:
9913 return FunctionTemplate;
9917 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9918 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9920 const DeclContext *DC) {
9921 for (auto *D : Record->decls()) {
9922 if (!ODRHash::isDeclToBeProcessed(D, DC))
9924 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9929 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9930 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9933 // If there is a diagnoseable difference, FirstDiffType and
9934 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9935 // filled in if not EndOfClass.
9936 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9937 DeclHashes &SecondHashes) {
9939 auto FirstIt = FirstHashes.begin();
9940 auto SecondIt = SecondHashes.begin();
9941 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9942 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9943 FirstIt->second == SecondIt->second) {
9949 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9951 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9954 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9956 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9962 // Use this to diagnose that an unexpected Decl was encountered
9963 // or no difference was detected. This causes a generic error
9964 // message to be emitted.
9965 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9966 StringRef FirstModule,
9967 NamedDecl *SecondRecord,
9968 StringRef SecondModule) {
9969 Diag(FirstRecord->getLocation(),
9970 diag::err_module_odr_violation_different_definitions)
9971 << FirstRecord << FirstModule.empty() << FirstModule;
9974 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9975 << FirstRecord << DR.FirstDecl->getSourceRange();
9978 Diag(SecondRecord->getLocation(),
9979 diag::note_module_odr_violation_different_definitions)
9982 if (DR.SecondDecl) {
9983 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9984 << DR.SecondDecl->getSourceRange();
9988 auto DiagnoseODRMismatch =
9989 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9990 NamedDecl *SecondRecord, StringRef SecondModule) {
9991 SourceLocation FirstLoc;
9992 SourceRange FirstRange;
9993 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9994 if (DR.FirstDiffType == EndOfClass && FirstTag) {
9995 FirstLoc = FirstTag->getBraceRange().getEnd();
9997 FirstLoc = DR.FirstDecl->getLocation();
9998 FirstRange = DR.FirstDecl->getSourceRange();
10000 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10001 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10002 << DR.FirstDiffType;
10004 SourceLocation SecondLoc;
10005 SourceRange SecondRange;
10006 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10007 if (DR.SecondDiffType == EndOfClass && SecondTag) {
10008 SecondLoc = SecondTag->getBraceRange().getEnd();
10010 SecondLoc = DR.SecondDecl->getLocation();
10011 SecondRange = DR.SecondDecl->getSourceRange();
10013 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10014 << SecondModule << SecondRange << DR.SecondDiffType;
10017 // Issue any pending ODR-failure diagnostics.
10018 for (auto &Merge : OdrMergeFailures) {
10019 // If we've already pointed out a specific problem with this class, don't
10020 // bother issuing a general "something's different" diagnostic.
10021 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10024 bool Diagnosed = false;
10025 CXXRecordDecl *FirstRecord = Merge.first;
10026 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10027 for (auto &RecordPair : Merge.second) {
10028 CXXRecordDecl *SecondRecord = RecordPair.first;
10029 // Multiple different declarations got merged together; tell the user
10030 // where they came from.
10031 if (FirstRecord == SecondRecord)
10034 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10036 auto *FirstDD = FirstRecord->DefinitionData;
10037 auto *SecondDD = RecordPair.second;
10039 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10041 // Diagnostics from DefinitionData are emitted here.
10042 if (FirstDD != SecondDD) {
10043 enum ODRDefinitionDataDifference {
10050 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10051 this](SourceLocation Loc, SourceRange Range,
10052 ODRDefinitionDataDifference DiffType) {
10053 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10054 << FirstRecord << FirstModule.empty() << FirstModule << Range
10057 auto ODRDiagBaseNote = [&SecondModule,
10058 this](SourceLocation Loc, SourceRange Range,
10059 ODRDefinitionDataDifference DiffType) {
10060 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10061 << SecondModule << Range << DiffType;
10064 unsigned FirstNumBases = FirstDD->NumBases;
10065 unsigned FirstNumVBases = FirstDD->NumVBases;
10066 unsigned SecondNumBases = SecondDD->NumBases;
10067 unsigned SecondNumVBases = SecondDD->NumVBases;
10069 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10070 unsigned NumBases = DD->NumBases;
10071 if (NumBases == 0) return SourceRange();
10072 auto bases = DD->bases();
10073 return SourceRange(bases[0].getBeginLoc(),
10074 bases[NumBases - 1].getEndLoc());
10077 if (FirstNumBases != SecondNumBases) {
10078 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10081 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10088 if (FirstNumVBases != SecondNumVBases) {
10089 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10092 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10094 << SecondNumVBases;
10099 auto FirstBases = FirstDD->bases();
10100 auto SecondBases = SecondDD->bases();
10102 for (i = 0; i < FirstNumBases; ++i) {
10103 auto FirstBase = FirstBases[i];
10104 auto SecondBase = SecondBases[i];
10105 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10106 ComputeQualTypeODRHash(SecondBase.getType())) {
10107 ODRDiagBaseError(FirstRecord->getLocation(),
10108 FirstBase.getSourceRange(), BaseType)
10109 << (i + 1) << FirstBase.getType();
10110 ODRDiagBaseNote(SecondRecord->getLocation(),
10111 SecondBase.getSourceRange(), BaseType)
10112 << (i + 1) << SecondBase.getType();
10116 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10117 ODRDiagBaseError(FirstRecord->getLocation(),
10118 FirstBase.getSourceRange(), BaseVirtual)
10119 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10120 ODRDiagBaseNote(SecondRecord->getLocation(),
10121 SecondBase.getSourceRange(), BaseVirtual)
10122 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10126 if (FirstBase.getAccessSpecifierAsWritten() !=
10127 SecondBase.getAccessSpecifierAsWritten()) {
10128 ODRDiagBaseError(FirstRecord->getLocation(),
10129 FirstBase.getSourceRange(), BaseAccess)
10130 << (i + 1) << FirstBase.getType()
10131 << (int)FirstBase.getAccessSpecifierAsWritten();
10132 ODRDiagBaseNote(SecondRecord->getLocation(),
10133 SecondBase.getSourceRange(), BaseAccess)
10134 << (i + 1) << SecondBase.getType()
10135 << (int)SecondBase.getAccessSpecifierAsWritten();
10140 if (i != FirstNumBases) {
10146 const ClassTemplateDecl *FirstTemplate =
10147 FirstRecord->getDescribedClassTemplate();
10148 const ClassTemplateDecl *SecondTemplate =
10149 SecondRecord->getDescribedClassTemplate();
10151 assert(!FirstTemplate == !SecondTemplate &&
10152 "Both pointers should be null or non-null");
10154 enum ODRTemplateDifference {
10157 ParamSingleDefaultArgument,
10158 ParamDifferentDefaultArgument,
10161 if (FirstTemplate && SecondTemplate) {
10162 DeclHashes FirstTemplateHashes;
10163 DeclHashes SecondTemplateHashes;
10165 auto PopulateTemplateParameterHashs =
10166 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10167 const ClassTemplateDecl *TD) {
10168 for (auto *D : TD->getTemplateParameters()->asArray()) {
10169 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10173 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10174 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10176 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10177 "Number of template parameters should be equal.");
10179 auto FirstIt = FirstTemplateHashes.begin();
10180 auto FirstEnd = FirstTemplateHashes.end();
10181 auto SecondIt = SecondTemplateHashes.begin();
10182 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10183 if (FirstIt->second == SecondIt->second)
10186 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10187 SourceLocation Loc, SourceRange Range,
10188 ODRTemplateDifference DiffType) {
10189 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10190 << FirstRecord << FirstModule.empty() << FirstModule << Range
10193 auto ODRDiagTemplateNote = [&SecondModule, this](
10194 SourceLocation Loc, SourceRange Range,
10195 ODRTemplateDifference DiffType) {
10196 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10197 << SecondModule << Range << DiffType;
10200 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10201 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10203 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10204 "Parameter Decl's should be the same kind.");
10206 DeclarationName FirstName = FirstDecl->getDeclName();
10207 DeclarationName SecondName = SecondDecl->getDeclName();
10209 if (FirstName != SecondName) {
10210 const bool FirstNameEmpty =
10211 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10212 const bool SecondNameEmpty =
10213 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10214 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10215 "Both template parameters cannot be unnamed.");
10216 ODRDiagTemplateError(FirstDecl->getLocation(),
10217 FirstDecl->getSourceRange(),
10218 FirstNameEmpty ? ParamEmptyName : ParamName)
10220 ODRDiagTemplateNote(SecondDecl->getLocation(),
10221 SecondDecl->getSourceRange(),
10222 SecondNameEmpty ? ParamEmptyName : ParamName)
10227 switch (FirstDecl->getKind()) {
10229 llvm_unreachable("Invalid template parameter type.");
10230 case Decl::TemplateTypeParm: {
10231 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10232 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10233 const bool HasFirstDefaultArgument =
10234 FirstParam->hasDefaultArgument() &&
10235 !FirstParam->defaultArgumentWasInherited();
10236 const bool HasSecondDefaultArgument =
10237 SecondParam->hasDefaultArgument() &&
10238 !SecondParam->defaultArgumentWasInherited();
10240 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10241 ODRDiagTemplateError(FirstDecl->getLocation(),
10242 FirstDecl->getSourceRange(),
10243 ParamSingleDefaultArgument)
10244 << HasFirstDefaultArgument;
10245 ODRDiagTemplateNote(SecondDecl->getLocation(),
10246 SecondDecl->getSourceRange(),
10247 ParamSingleDefaultArgument)
10248 << HasSecondDefaultArgument;
10252 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10253 "Expecting default arguments.");
10255 ODRDiagTemplateError(FirstDecl->getLocation(),
10256 FirstDecl->getSourceRange(),
10257 ParamDifferentDefaultArgument);
10258 ODRDiagTemplateNote(SecondDecl->getLocation(),
10259 SecondDecl->getSourceRange(),
10260 ParamDifferentDefaultArgument);
10264 case Decl::NonTypeTemplateParm: {
10265 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10266 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10267 const bool HasFirstDefaultArgument =
10268 FirstParam->hasDefaultArgument() &&
10269 !FirstParam->defaultArgumentWasInherited();
10270 const bool HasSecondDefaultArgument =
10271 SecondParam->hasDefaultArgument() &&
10272 !SecondParam->defaultArgumentWasInherited();
10274 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10275 ODRDiagTemplateError(FirstDecl->getLocation(),
10276 FirstDecl->getSourceRange(),
10277 ParamSingleDefaultArgument)
10278 << HasFirstDefaultArgument;
10279 ODRDiagTemplateNote(SecondDecl->getLocation(),
10280 SecondDecl->getSourceRange(),
10281 ParamSingleDefaultArgument)
10282 << HasSecondDefaultArgument;
10286 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10287 "Expecting default arguments.");
10289 ODRDiagTemplateError(FirstDecl->getLocation(),
10290 FirstDecl->getSourceRange(),
10291 ParamDifferentDefaultArgument);
10292 ODRDiagTemplateNote(SecondDecl->getLocation(),
10293 SecondDecl->getSourceRange(),
10294 ParamDifferentDefaultArgument);
10298 case Decl::TemplateTemplateParm: {
10299 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10300 const auto *SecondParam =
10301 cast<TemplateTemplateParmDecl>(SecondDecl);
10302 const bool HasFirstDefaultArgument =
10303 FirstParam->hasDefaultArgument() &&
10304 !FirstParam->defaultArgumentWasInherited();
10305 const bool HasSecondDefaultArgument =
10306 SecondParam->hasDefaultArgument() &&
10307 !SecondParam->defaultArgumentWasInherited();
10309 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10310 ODRDiagTemplateError(FirstDecl->getLocation(),
10311 FirstDecl->getSourceRange(),
10312 ParamSingleDefaultArgument)
10313 << HasFirstDefaultArgument;
10314 ODRDiagTemplateNote(SecondDecl->getLocation(),
10315 SecondDecl->getSourceRange(),
10316 ParamSingleDefaultArgument)
10317 << HasSecondDefaultArgument;
10321 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10322 "Expecting default arguments.");
10324 ODRDiagTemplateError(FirstDecl->getLocation(),
10325 FirstDecl->getSourceRange(),
10326 ParamDifferentDefaultArgument);
10327 ODRDiagTemplateNote(SecondDecl->getLocation(),
10328 SecondDecl->getSourceRange(),
10329 ParamDifferentDefaultArgument);
10338 if (FirstIt != FirstEnd) {
10344 DeclHashes FirstHashes;
10345 DeclHashes SecondHashes;
10346 const DeclContext *DC = FirstRecord;
10347 PopulateHashes(FirstHashes, FirstRecord, DC);
10348 PopulateHashes(SecondHashes, SecondRecord, DC);
10350 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10351 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10352 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10353 Decl *FirstDecl = DR.FirstDecl;
10354 Decl *SecondDecl = DR.SecondDecl;
10356 if (FirstDiffType == Other || SecondDiffType == Other) {
10357 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10363 if (FirstDiffType != SecondDiffType) {
10364 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10370 assert(FirstDiffType == SecondDiffType);
10372 switch (FirstDiffType) {
10375 case PublicSpecifer:
10376 case PrivateSpecifer:
10377 case ProtectedSpecifer:
10378 llvm_unreachable("Invalid diff type");
10380 case StaticAssert: {
10381 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10382 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10384 Expr *FirstExpr = FirstSA->getAssertExpr();
10385 Expr *SecondExpr = SecondSA->getAssertExpr();
10386 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10387 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10388 if (FirstODRHash != SecondODRHash) {
10389 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10390 FirstExpr->getSourceRange(), StaticAssertCondition);
10391 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10392 SecondExpr->getSourceRange(), StaticAssertCondition);
10397 StringLiteral *FirstStr = FirstSA->getMessage();
10398 StringLiteral *SecondStr = SecondSA->getMessage();
10399 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10400 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10401 SourceLocation FirstLoc, SecondLoc;
10402 SourceRange FirstRange, SecondRange;
10404 FirstLoc = FirstStr->getBeginLoc();
10405 FirstRange = FirstStr->getSourceRange();
10407 FirstLoc = FirstSA->getBeginLoc();
10408 FirstRange = FirstSA->getSourceRange();
10411 SecondLoc = SecondStr->getBeginLoc();
10412 SecondRange = SecondStr->getSourceRange();
10414 SecondLoc = SecondSA->getBeginLoc();
10415 SecondRange = SecondSA->getSourceRange();
10417 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10418 StaticAssertOnlyMessage)
10419 << (FirstStr == nullptr);
10420 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10421 StaticAssertOnlyMessage)
10422 << (SecondStr == nullptr);
10427 if (FirstStr && SecondStr &&
10428 FirstStr->getString() != SecondStr->getString()) {
10429 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10430 FirstStr->getSourceRange(), StaticAssertMessage);
10431 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10432 SecondStr->getSourceRange(), StaticAssertMessage);
10439 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10440 cast<FieldDecl>(FirstDecl),
10441 cast<FieldDecl>(SecondDecl));
10451 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10452 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10453 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10456 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10457 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10458 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10459 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10460 auto FirstName = FirstMethod->getDeclName();
10461 auto SecondName = SecondMethod->getDeclName();
10462 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10463 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10464 FirstMethod->getSourceRange(), MethodName)
10465 << FirstMethodType << FirstName;
10466 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10467 SecondMethod->getSourceRange(), MethodName)
10468 << SecondMethodType << SecondName;
10474 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10475 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10476 if (FirstDeleted != SecondDeleted) {
10477 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10478 FirstMethod->getSourceRange(), MethodDeleted)
10479 << FirstMethodType << FirstName << FirstDeleted;
10481 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10482 SecondMethod->getSourceRange(), MethodDeleted)
10483 << SecondMethodType << SecondName << SecondDeleted;
10488 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10489 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10490 if (FirstDefaulted != SecondDefaulted) {
10491 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10492 FirstMethod->getSourceRange(), MethodDefaulted)
10493 << FirstMethodType << FirstName << FirstDefaulted;
10495 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10496 SecondMethod->getSourceRange(), MethodDefaulted)
10497 << SecondMethodType << SecondName << SecondDefaulted;
10502 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10503 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10504 const bool FirstPure = FirstMethod->isPure();
10505 const bool SecondPure = SecondMethod->isPure();
10506 if ((FirstVirtual || SecondVirtual) &&
10507 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10508 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10509 FirstMethod->getSourceRange(), MethodVirtual)
10510 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10511 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10512 SecondMethod->getSourceRange(), MethodVirtual)
10513 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10518 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10519 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10520 // class needs to be checked instead.
10521 const auto FirstStorage = FirstMethod->getStorageClass();
10522 const auto SecondStorage = SecondMethod->getStorageClass();
10523 const bool FirstStatic = FirstStorage == SC_Static;
10524 const bool SecondStatic = SecondStorage == SC_Static;
10525 if (FirstStatic != SecondStatic) {
10526 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10527 FirstMethod->getSourceRange(), MethodStatic)
10528 << FirstMethodType << FirstName << FirstStatic;
10529 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10530 SecondMethod->getSourceRange(), MethodStatic)
10531 << SecondMethodType << SecondName << SecondStatic;
10536 const bool FirstVolatile = FirstMethod->isVolatile();
10537 const bool SecondVolatile = SecondMethod->isVolatile();
10538 if (FirstVolatile != SecondVolatile) {
10539 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10540 FirstMethod->getSourceRange(), MethodVolatile)
10541 << FirstMethodType << FirstName << FirstVolatile;
10542 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10543 SecondMethod->getSourceRange(), MethodVolatile)
10544 << SecondMethodType << SecondName << SecondVolatile;
10549 const bool FirstConst = FirstMethod->isConst();
10550 const bool SecondConst = SecondMethod->isConst();
10551 if (FirstConst != SecondConst) {
10552 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10553 FirstMethod->getSourceRange(), MethodConst)
10554 << FirstMethodType << FirstName << FirstConst;
10555 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10556 SecondMethod->getSourceRange(), MethodConst)
10557 << SecondMethodType << SecondName << SecondConst;
10562 const bool FirstInline = FirstMethod->isInlineSpecified();
10563 const bool SecondInline = SecondMethod->isInlineSpecified();
10564 if (FirstInline != SecondInline) {
10565 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10566 FirstMethod->getSourceRange(), MethodInline)
10567 << FirstMethodType << FirstName << FirstInline;
10568 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10569 SecondMethod->getSourceRange(), MethodInline)
10570 << SecondMethodType << SecondName << SecondInline;
10575 const unsigned FirstNumParameters = FirstMethod->param_size();
10576 const unsigned SecondNumParameters = SecondMethod->param_size();
10577 if (FirstNumParameters != SecondNumParameters) {
10578 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10579 FirstMethod->getSourceRange(),
10580 MethodNumberParameters)
10581 << FirstMethodType << FirstName << FirstNumParameters;
10582 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10583 SecondMethod->getSourceRange(),
10584 MethodNumberParameters)
10585 << SecondMethodType << SecondName << SecondNumParameters;
10590 // Need this status boolean to know when break out of the switch.
10591 bool ParameterMismatch = false;
10592 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10593 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10594 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10596 QualType FirstParamType = FirstParam->getType();
10597 QualType SecondParamType = SecondParam->getType();
10598 if (FirstParamType != SecondParamType &&
10599 ComputeQualTypeODRHash(FirstParamType) !=
10600 ComputeQualTypeODRHash(SecondParamType)) {
10601 if (const DecayedType *ParamDecayedType =
10602 FirstParamType->getAs<DecayedType>()) {
10604 FirstRecord, FirstModule, FirstMethod->getLocation(),
10605 FirstMethod->getSourceRange(), MethodParameterType)
10606 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10607 << true << ParamDecayedType->getOriginalType();
10610 FirstRecord, FirstModule, FirstMethod->getLocation(),
10611 FirstMethod->getSourceRange(), MethodParameterType)
10612 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10616 if (const DecayedType *ParamDecayedType =
10617 SecondParamType->getAs<DecayedType>()) {
10618 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10619 SecondMethod->getSourceRange(),
10620 MethodParameterType)
10621 << SecondMethodType << SecondName << (I + 1)
10622 << SecondParamType << true
10623 << ParamDecayedType->getOriginalType();
10625 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10626 SecondMethod->getSourceRange(),
10627 MethodParameterType)
10628 << SecondMethodType << SecondName << (I + 1)
10629 << SecondParamType << false;
10631 ParameterMismatch = true;
10635 DeclarationName FirstParamName = FirstParam->getDeclName();
10636 DeclarationName SecondParamName = SecondParam->getDeclName();
10637 if (FirstParamName != SecondParamName) {
10638 ODRDiagDeclError(FirstRecord, FirstModule,
10639 FirstMethod->getLocation(),
10640 FirstMethod->getSourceRange(), MethodParameterName)
10641 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10642 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10643 SecondMethod->getSourceRange(), MethodParameterName)
10644 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10645 ParameterMismatch = true;
10649 const Expr *FirstInit = FirstParam->getInit();
10650 const Expr *SecondInit = SecondParam->getInit();
10651 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10652 ODRDiagDeclError(FirstRecord, FirstModule,
10653 FirstMethod->getLocation(),
10654 FirstMethod->getSourceRange(),
10655 MethodParameterSingleDefaultArgument)
10656 << FirstMethodType << FirstName << (I + 1)
10657 << (FirstInit == nullptr)
10658 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10659 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10660 SecondMethod->getSourceRange(),
10661 MethodParameterSingleDefaultArgument)
10662 << SecondMethodType << SecondName << (I + 1)
10663 << (SecondInit == nullptr)
10664 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10665 ParameterMismatch = true;
10669 if (FirstInit && SecondInit &&
10670 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10671 ODRDiagDeclError(FirstRecord, FirstModule,
10672 FirstMethod->getLocation(),
10673 FirstMethod->getSourceRange(),
10674 MethodParameterDifferentDefaultArgument)
10675 << FirstMethodType << FirstName << (I + 1)
10676 << FirstInit->getSourceRange();
10677 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10678 SecondMethod->getSourceRange(),
10679 MethodParameterDifferentDefaultArgument)
10680 << SecondMethodType << SecondName << (I + 1)
10681 << SecondInit->getSourceRange();
10682 ParameterMismatch = true;
10688 if (ParameterMismatch) {
10693 const auto *FirstTemplateArgs =
10694 FirstMethod->getTemplateSpecializationArgs();
10695 const auto *SecondTemplateArgs =
10696 SecondMethod->getTemplateSpecializationArgs();
10698 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10699 (!FirstTemplateArgs && SecondTemplateArgs)) {
10700 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10701 FirstMethod->getSourceRange(),
10702 MethodNoTemplateArguments)
10703 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10704 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10705 SecondMethod->getSourceRange(),
10706 MethodNoTemplateArguments)
10707 << SecondMethodType << SecondName
10708 << (SecondTemplateArgs != nullptr);
10714 if (FirstTemplateArgs && SecondTemplateArgs) {
10715 // Remove pack expansions from argument list.
10716 auto ExpandTemplateArgumentList =
10717 [](const TemplateArgumentList *TAL) {
10718 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10719 for (const TemplateArgument &TA : TAL->asArray()) {
10720 if (TA.getKind() != TemplateArgument::Pack) {
10721 ExpandedList.push_back(&TA);
10724 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10725 ExpandedList.push_back(&PackTA);
10728 return ExpandedList;
10730 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10731 ExpandTemplateArgumentList(FirstTemplateArgs);
10732 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10733 ExpandTemplateArgumentList(SecondTemplateArgs);
10735 if (FirstExpandedList.size() != SecondExpandedList.size()) {
10736 ODRDiagDeclError(FirstRecord, FirstModule,
10737 FirstMethod->getLocation(),
10738 FirstMethod->getSourceRange(),
10739 MethodDifferentNumberTemplateArguments)
10740 << FirstMethodType << FirstName
10741 << (unsigned)FirstExpandedList.size();
10742 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10743 SecondMethod->getSourceRange(),
10744 MethodDifferentNumberTemplateArguments)
10745 << SecondMethodType << SecondName
10746 << (unsigned)SecondExpandedList.size();
10752 bool TemplateArgumentMismatch = false;
10753 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10754 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10755 &SecondTA = *SecondExpandedList[i];
10756 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10757 ComputeTemplateArgumentODRHash(SecondTA)) {
10762 FirstRecord, FirstModule, FirstMethod->getLocation(),
10763 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10764 << FirstMethodType << FirstName << FirstTA << i + 1;
10765 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10766 SecondMethod->getSourceRange(),
10767 MethodDifferentTemplateArgument)
10768 << SecondMethodType << SecondName << SecondTA << i + 1;
10770 TemplateArgumentMismatch = true;
10774 if (TemplateArgumentMismatch) {
10780 // Compute the hash of the method as if it has no body.
10781 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10783 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10784 return Hash.CalculateHash();
10787 // Compare the hash generated to the hash stored. A difference means
10788 // that a body was present in the original source. Due to merging,
10789 // the stardard way of detecting a body will not work.
10790 const bool HasFirstBody =
10791 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10792 const bool HasSecondBody =
10793 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10795 if (HasFirstBody != HasSecondBody) {
10796 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10797 FirstMethod->getSourceRange(), MethodSingleBody)
10798 << FirstMethodType << FirstName << HasFirstBody;
10799 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10800 SecondMethod->getSourceRange(), MethodSingleBody)
10801 << SecondMethodType << SecondName << HasSecondBody;
10806 if (HasFirstBody && HasSecondBody) {
10807 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10808 FirstMethod->getSourceRange(), MethodDifferentBody)
10809 << FirstMethodType << FirstName;
10810 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10811 SecondMethod->getSourceRange(), MethodDifferentBody)
10812 << SecondMethodType << SecondName;
10821 Diagnosed = ODRDiagTypeDefOrAlias(
10822 FirstRecord, FirstModule, SecondModule,
10823 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10824 FirstDiffType == TypeAlias);
10829 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10830 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10834 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10835 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10837 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10838 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10840 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10841 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10843 if (FirstND && SecondND) {
10844 ODRDiagDeclError(FirstRecord, FirstModule,
10845 FirstFriend->getFriendLoc(),
10846 FirstFriend->getSourceRange(), FriendFunction)
10848 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10849 SecondFriend->getSourceRange(), FriendFunction)
10856 if (FirstTSI && SecondTSI) {
10857 QualType FirstFriendType = FirstTSI->getType();
10858 QualType SecondFriendType = SecondTSI->getType();
10859 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10860 ComputeQualTypeODRHash(SecondFriendType));
10861 ODRDiagDeclError(FirstRecord, FirstModule,
10862 FirstFriend->getFriendLoc(),
10863 FirstFriend->getSourceRange(), FriendType)
10864 << FirstFriendType;
10865 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10866 SecondFriend->getSourceRange(), FriendType)
10867 << SecondFriendType;
10872 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10873 FirstFriend->getSourceRange(), FriendTypeFunction)
10874 << (FirstTSI == nullptr);
10875 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10876 SecondFriend->getSourceRange(), FriendTypeFunction)
10877 << (SecondTSI == nullptr);
10882 case FunctionTemplate: {
10883 FunctionTemplateDecl *FirstTemplate =
10884 cast<FunctionTemplateDecl>(FirstDecl);
10885 FunctionTemplateDecl *SecondTemplate =
10886 cast<FunctionTemplateDecl>(SecondDecl);
10888 TemplateParameterList *FirstTPL =
10889 FirstTemplate->getTemplateParameters();
10890 TemplateParameterList *SecondTPL =
10891 SecondTemplate->getTemplateParameters();
10893 if (FirstTPL->size() != SecondTPL->size()) {
10894 ODRDiagDeclError(FirstRecord, FirstModule,
10895 FirstTemplate->getLocation(),
10896 FirstTemplate->getSourceRange(),
10897 FunctionTemplateDifferentNumberParameters)
10898 << FirstTemplate << FirstTPL->size();
10899 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10900 SecondTemplate->getSourceRange(),
10901 FunctionTemplateDifferentNumberParameters)
10902 << SecondTemplate << SecondTPL->size();
10908 bool ParameterMismatch = false;
10909 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10910 NamedDecl *FirstParam = FirstTPL->getParam(i);
10911 NamedDecl *SecondParam = SecondTPL->getParam(i);
10913 if (FirstParam->getKind() != SecondParam->getKind()) {
10915 TemplateTypeParameter,
10916 NonTypeTemplateParameter,
10917 TemplateTemplateParameter,
10919 auto GetParamType = [](NamedDecl *D) {
10920 switch (D->getKind()) {
10922 llvm_unreachable("Unexpected template parameter type");
10923 case Decl::TemplateTypeParm:
10924 return TemplateTypeParameter;
10925 case Decl::NonTypeTemplateParm:
10926 return NonTypeTemplateParameter;
10927 case Decl::TemplateTemplateParm:
10928 return TemplateTemplateParameter;
10932 ODRDiagDeclError(FirstRecord, FirstModule,
10933 FirstTemplate->getLocation(),
10934 FirstTemplate->getSourceRange(),
10935 FunctionTemplateParameterDifferentKind)
10936 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10937 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10938 SecondTemplate->getSourceRange(),
10939 FunctionTemplateParameterDifferentKind)
10940 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10942 ParameterMismatch = true;
10946 if (FirstParam->getName() != SecondParam->getName()) {
10948 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10949 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10950 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10952 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10953 SecondTemplate->getSourceRange(),
10954 FunctionTemplateParameterName)
10955 << SecondTemplate << (i + 1)
10956 << (bool)SecondParam->getIdentifier() << SecondParam;
10957 ParameterMismatch = true;
10961 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10962 isa<TemplateTypeParmDecl>(SecondParam)) {
10963 TemplateTypeParmDecl *FirstTTPD =
10964 cast<TemplateTypeParmDecl>(FirstParam);
10965 TemplateTypeParmDecl *SecondTTPD =
10966 cast<TemplateTypeParmDecl>(SecondParam);
10967 bool HasFirstDefaultArgument =
10968 FirstTTPD->hasDefaultArgument() &&
10969 !FirstTTPD->defaultArgumentWasInherited();
10970 bool HasSecondDefaultArgument =
10971 SecondTTPD->hasDefaultArgument() &&
10972 !SecondTTPD->defaultArgumentWasInherited();
10973 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10974 ODRDiagDeclError(FirstRecord, FirstModule,
10975 FirstTemplate->getLocation(),
10976 FirstTemplate->getSourceRange(),
10977 FunctionTemplateParameterSingleDefaultArgument)
10978 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10979 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10980 SecondTemplate->getSourceRange(),
10981 FunctionTemplateParameterSingleDefaultArgument)
10982 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10983 ParameterMismatch = true;
10987 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10988 QualType FirstType = FirstTTPD->getDefaultArgument();
10989 QualType SecondType = SecondTTPD->getDefaultArgument();
10990 if (ComputeQualTypeODRHash(FirstType) !=
10991 ComputeQualTypeODRHash(SecondType)) {
10993 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10994 FirstTemplate->getSourceRange(),
10995 FunctionTemplateParameterDifferentDefaultArgument)
10996 << FirstTemplate << (i + 1) << FirstType;
10998 SecondModule, SecondTemplate->getLocation(),
10999 SecondTemplate->getSourceRange(),
11000 FunctionTemplateParameterDifferentDefaultArgument)
11001 << SecondTemplate << (i + 1) << SecondType;
11002 ParameterMismatch = true;
11007 if (FirstTTPD->isParameterPack() !=
11008 SecondTTPD->isParameterPack()) {
11009 ODRDiagDeclError(FirstRecord, FirstModule,
11010 FirstTemplate->getLocation(),
11011 FirstTemplate->getSourceRange(),
11012 FunctionTemplatePackParameter)
11013 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11014 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11015 SecondTemplate->getSourceRange(),
11016 FunctionTemplatePackParameter)
11017 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11018 ParameterMismatch = true;
11023 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11024 isa<TemplateTemplateParmDecl>(SecondParam)) {
11025 TemplateTemplateParmDecl *FirstTTPD =
11026 cast<TemplateTemplateParmDecl>(FirstParam);
11027 TemplateTemplateParmDecl *SecondTTPD =
11028 cast<TemplateTemplateParmDecl>(SecondParam);
11030 TemplateParameterList *FirstTPL =
11031 FirstTTPD->getTemplateParameters();
11032 TemplateParameterList *SecondTPL =
11033 SecondTTPD->getTemplateParameters();
11035 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11036 ComputeTemplateParameterListODRHash(SecondTPL)) {
11037 ODRDiagDeclError(FirstRecord, FirstModule,
11038 FirstTemplate->getLocation(),
11039 FirstTemplate->getSourceRange(),
11040 FunctionTemplateParameterDifferentType)
11041 << FirstTemplate << (i + 1);
11042 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11043 SecondTemplate->getSourceRange(),
11044 FunctionTemplateParameterDifferentType)
11045 << SecondTemplate << (i + 1);
11046 ParameterMismatch = true;
11050 bool HasFirstDefaultArgument =
11051 FirstTTPD->hasDefaultArgument() &&
11052 !FirstTTPD->defaultArgumentWasInherited();
11053 bool HasSecondDefaultArgument =
11054 SecondTTPD->hasDefaultArgument() &&
11055 !SecondTTPD->defaultArgumentWasInherited();
11056 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11057 ODRDiagDeclError(FirstRecord, FirstModule,
11058 FirstTemplate->getLocation(),
11059 FirstTemplate->getSourceRange(),
11060 FunctionTemplateParameterSingleDefaultArgument)
11061 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11062 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11063 SecondTemplate->getSourceRange(),
11064 FunctionTemplateParameterSingleDefaultArgument)
11065 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11066 ParameterMismatch = true;
11070 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11071 TemplateArgument FirstTA =
11072 FirstTTPD->getDefaultArgument().getArgument();
11073 TemplateArgument SecondTA =
11074 SecondTTPD->getDefaultArgument().getArgument();
11075 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11076 ComputeTemplateArgumentODRHash(SecondTA)) {
11078 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11079 FirstTemplate->getSourceRange(),
11080 FunctionTemplateParameterDifferentDefaultArgument)
11081 << FirstTemplate << (i + 1) << FirstTA;
11083 SecondModule, SecondTemplate->getLocation(),
11084 SecondTemplate->getSourceRange(),
11085 FunctionTemplateParameterDifferentDefaultArgument)
11086 << SecondTemplate << (i + 1) << SecondTA;
11087 ParameterMismatch = true;
11092 if (FirstTTPD->isParameterPack() !=
11093 SecondTTPD->isParameterPack()) {
11094 ODRDiagDeclError(FirstRecord, FirstModule,
11095 FirstTemplate->getLocation(),
11096 FirstTemplate->getSourceRange(),
11097 FunctionTemplatePackParameter)
11098 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11099 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11100 SecondTemplate->getSourceRange(),
11101 FunctionTemplatePackParameter)
11102 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11103 ParameterMismatch = true;
11108 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11109 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11110 NonTypeTemplateParmDecl *FirstNTTPD =
11111 cast<NonTypeTemplateParmDecl>(FirstParam);
11112 NonTypeTemplateParmDecl *SecondNTTPD =
11113 cast<NonTypeTemplateParmDecl>(SecondParam);
11115 QualType FirstType = FirstNTTPD->getType();
11116 QualType SecondType = SecondNTTPD->getType();
11117 if (ComputeQualTypeODRHash(FirstType) !=
11118 ComputeQualTypeODRHash(SecondType)) {
11119 ODRDiagDeclError(FirstRecord, FirstModule,
11120 FirstTemplate->getLocation(),
11121 FirstTemplate->getSourceRange(),
11122 FunctionTemplateParameterDifferentType)
11123 << FirstTemplate << (i + 1);
11124 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11125 SecondTemplate->getSourceRange(),
11126 FunctionTemplateParameterDifferentType)
11127 << SecondTemplate << (i + 1);
11128 ParameterMismatch = true;
11132 bool HasFirstDefaultArgument =
11133 FirstNTTPD->hasDefaultArgument() &&
11134 !FirstNTTPD->defaultArgumentWasInherited();
11135 bool HasSecondDefaultArgument =
11136 SecondNTTPD->hasDefaultArgument() &&
11137 !SecondNTTPD->defaultArgumentWasInherited();
11138 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11139 ODRDiagDeclError(FirstRecord, FirstModule,
11140 FirstTemplate->getLocation(),
11141 FirstTemplate->getSourceRange(),
11142 FunctionTemplateParameterSingleDefaultArgument)
11143 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11144 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11145 SecondTemplate->getSourceRange(),
11146 FunctionTemplateParameterSingleDefaultArgument)
11147 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11148 ParameterMismatch = true;
11152 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11153 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11154 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11155 if (ComputeODRHash(FirstDefaultArgument) !=
11156 ComputeODRHash(SecondDefaultArgument)) {
11158 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11159 FirstTemplate->getSourceRange(),
11160 FunctionTemplateParameterDifferentDefaultArgument)
11161 << FirstTemplate << (i + 1) << FirstDefaultArgument;
11163 SecondModule, SecondTemplate->getLocation(),
11164 SecondTemplate->getSourceRange(),
11165 FunctionTemplateParameterDifferentDefaultArgument)
11166 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11167 ParameterMismatch = true;
11172 if (FirstNTTPD->isParameterPack() !=
11173 SecondNTTPD->isParameterPack()) {
11174 ODRDiagDeclError(FirstRecord, FirstModule,
11175 FirstTemplate->getLocation(),
11176 FirstTemplate->getSourceRange(),
11177 FunctionTemplatePackParameter)
11178 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11179 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11180 SecondTemplate->getSourceRange(),
11181 FunctionTemplatePackParameter)
11182 << SecondTemplate << (i + 1)
11183 << SecondNTTPD->isParameterPack();
11184 ParameterMismatch = true;
11190 if (ParameterMismatch) {
11202 Diag(FirstDecl->getLocation(),
11203 diag::err_module_odr_violation_mismatch_decl_unknown)
11204 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11205 << FirstDecl->getSourceRange();
11206 Diag(SecondDecl->getLocation(),
11207 diag::note_module_odr_violation_mismatch_decl_unknown)
11208 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11213 // All definitions are updates to the same declaration. This happens if a
11214 // module instantiates the declaration of a class template specialization
11215 // and two or more other modules instantiate its definition.
11217 // FIXME: Indicate which modules had instantiations of this definition.
11218 // FIXME: How can this even happen?
11219 Diag(Merge.first->getLocation(),
11220 diag::err_module_odr_violation_different_instantiations)
11225 // Issue ODR failures diagnostics for functions.
11226 for (auto &Merge : FunctionOdrMergeFailures) {
11227 enum ODRFunctionDifference {
11231 ParameterSingleDefaultArgument,
11232 ParameterDifferentDefaultArgument,
11236 FunctionDecl *FirstFunction = Merge.first;
11237 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11239 bool Diagnosed = false;
11240 for (auto &SecondFunction : Merge.second) {
11242 if (FirstFunction == SecondFunction)
11245 std::string SecondModule =
11246 getOwningModuleNameForDiagnostic(SecondFunction);
11248 auto ODRDiagError = [FirstFunction, &FirstModule,
11249 this](SourceLocation Loc, SourceRange Range,
11250 ODRFunctionDifference DiffType) {
11251 return Diag(Loc, diag::err_module_odr_violation_function)
11252 << FirstFunction << FirstModule.empty() << FirstModule << Range
11255 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11257 ODRFunctionDifference DiffType) {
11258 return Diag(Loc, diag::note_module_odr_violation_function)
11259 << SecondModule << Range << DiffType;
11262 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11263 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11264 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11265 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11266 << FirstFunction->getReturnType();
11267 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11268 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11269 << SecondFunction->getReturnType();
11274 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11275 "Merged functions with different number of parameters");
11277 auto ParamSize = FirstFunction->param_size();
11278 bool ParameterMismatch = false;
11279 for (unsigned I = 0; I < ParamSize; ++I) {
11280 auto *FirstParam = FirstFunction->getParamDecl(I);
11281 auto *SecondParam = SecondFunction->getParamDecl(I);
11283 assert(getContext().hasSameType(FirstParam->getType(),
11284 SecondParam->getType()) &&
11285 "Merged function has different parameter types.");
11287 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11288 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11290 << I + 1 << FirstParam->getDeclName();
11291 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11293 << I + 1 << SecondParam->getDeclName();
11294 ParameterMismatch = true;
11298 QualType FirstParamType = FirstParam->getType();
11299 QualType SecondParamType = SecondParam->getType();
11300 if (FirstParamType != SecondParamType &&
11301 ComputeQualTypeODRHash(FirstParamType) !=
11302 ComputeQualTypeODRHash(SecondParamType)) {
11303 if (const DecayedType *ParamDecayedType =
11304 FirstParamType->getAs<DecayedType>()) {
11305 ODRDiagError(FirstParam->getLocation(),
11306 FirstParam->getSourceRange(), ParameterType)
11307 << (I + 1) << FirstParamType << true
11308 << ParamDecayedType->getOriginalType();
11310 ODRDiagError(FirstParam->getLocation(),
11311 FirstParam->getSourceRange(), ParameterType)
11312 << (I + 1) << FirstParamType << false;
11315 if (const DecayedType *ParamDecayedType =
11316 SecondParamType->getAs<DecayedType>()) {
11317 ODRDiagNote(SecondParam->getLocation(),
11318 SecondParam->getSourceRange(), ParameterType)
11319 << (I + 1) << SecondParamType << true
11320 << ParamDecayedType->getOriginalType();
11322 ODRDiagNote(SecondParam->getLocation(),
11323 SecondParam->getSourceRange(), ParameterType)
11324 << (I + 1) << SecondParamType << false;
11326 ParameterMismatch = true;
11330 const Expr *FirstInit = FirstParam->getInit();
11331 const Expr *SecondInit = SecondParam->getInit();
11332 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11333 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11334 ParameterSingleDefaultArgument)
11335 << (I + 1) << (FirstInit == nullptr)
11336 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11337 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11338 ParameterSingleDefaultArgument)
11339 << (I + 1) << (SecondInit == nullptr)
11340 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11341 ParameterMismatch = true;
11345 if (FirstInit && SecondInit &&
11346 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11347 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11348 ParameterDifferentDefaultArgument)
11349 << (I + 1) << FirstInit->getSourceRange();
11350 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11351 ParameterDifferentDefaultArgument)
11352 << (I + 1) << SecondInit->getSourceRange();
11353 ParameterMismatch = true;
11357 assert(ComputeSubDeclODRHash(FirstParam) ==
11358 ComputeSubDeclODRHash(SecondParam) &&
11359 "Undiagnosed parameter difference.");
11362 if (ParameterMismatch) {
11367 // If no error has been generated before now, assume the problem is in
11368 // the body and generate a message.
11369 ODRDiagError(FirstFunction->getLocation(),
11370 FirstFunction->getSourceRange(), FunctionBody);
11371 ODRDiagNote(SecondFunction->getLocation(),
11372 SecondFunction->getSourceRange(), FunctionBody);
11377 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11380 // Issue ODR failures diagnostics for enums.
11381 for (auto &Merge : EnumOdrMergeFailures) {
11382 enum ODREnumDifference {
11384 EnumTagKeywordMismatch,
11385 SingleSpecifiedType,
11386 DifferentSpecifiedTypes,
11387 DifferentNumberEnumConstants,
11389 EnumConstantSingleInitilizer,
11390 EnumConstantDifferentInitilizer,
11393 // If we've already pointed out a specific problem with this enum, don't
11394 // bother issuing a general "something's different" diagnostic.
11395 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11398 EnumDecl *FirstEnum = Merge.first;
11399 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11402 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11403 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11404 DeclHashes &Hashes, EnumDecl *Enum) {
11405 for (auto *D : Enum->decls()) {
11406 // Due to decl merging, the first EnumDecl is the parent of
11407 // Decls in both records.
11408 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11410 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11411 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11412 ComputeSubDeclODRHash(D));
11415 DeclHashes FirstHashes;
11416 PopulateHashes(FirstHashes, FirstEnum);
11417 bool Diagnosed = false;
11418 for (auto &SecondEnum : Merge.second) {
11420 if (FirstEnum == SecondEnum)
11423 std::string SecondModule =
11424 getOwningModuleNameForDiagnostic(SecondEnum);
11426 auto ODRDiagError = [FirstEnum, &FirstModule,
11427 this](SourceLocation Loc, SourceRange Range,
11428 ODREnumDifference DiffType) {
11429 return Diag(Loc, diag::err_module_odr_violation_enum)
11430 << FirstEnum << FirstModule.empty() << FirstModule << Range
11433 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11435 ODREnumDifference DiffType) {
11436 return Diag(Loc, diag::note_module_odr_violation_enum)
11437 << SecondModule << Range << DiffType;
11440 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11441 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11443 << FirstEnum->isScoped();
11444 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11446 << SecondEnum->isScoped();
11451 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11452 if (FirstEnum->isScopedUsingClassTag() !=
11453 SecondEnum->isScopedUsingClassTag()) {
11454 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11455 EnumTagKeywordMismatch)
11456 << FirstEnum->isScopedUsingClassTag();
11457 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11458 EnumTagKeywordMismatch)
11459 << SecondEnum->isScopedUsingClassTag();
11465 QualType FirstUnderlyingType =
11466 FirstEnum->getIntegerTypeSourceInfo()
11467 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11469 QualType SecondUnderlyingType =
11470 SecondEnum->getIntegerTypeSourceInfo()
11471 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11473 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11474 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11475 SingleSpecifiedType)
11476 << !FirstUnderlyingType.isNull();
11477 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11478 SingleSpecifiedType)
11479 << !SecondUnderlyingType.isNull();
11484 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11485 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11486 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11487 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11488 DifferentSpecifiedTypes)
11489 << FirstUnderlyingType;
11490 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11491 DifferentSpecifiedTypes)
11492 << SecondUnderlyingType;
11498 DeclHashes SecondHashes;
11499 PopulateHashes(SecondHashes, SecondEnum);
11501 if (FirstHashes.size() != SecondHashes.size()) {
11502 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11503 DifferentNumberEnumConstants)
11504 << (int)FirstHashes.size();
11505 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11506 DifferentNumberEnumConstants)
11507 << (int)SecondHashes.size();
11512 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11513 if (FirstHashes[I].second == SecondHashes[I].second)
11515 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11516 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11518 if (FirstEnumConstant->getDeclName() !=
11519 SecondEnumConstant->getDeclName()) {
11521 ODRDiagError(FirstEnumConstant->getLocation(),
11522 FirstEnumConstant->getSourceRange(), EnumConstantName)
11523 << I + 1 << FirstEnumConstant;
11524 ODRDiagNote(SecondEnumConstant->getLocation(),
11525 SecondEnumConstant->getSourceRange(), EnumConstantName)
11526 << I + 1 << SecondEnumConstant;
11531 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11532 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11533 if (!FirstInit && !SecondInit)
11536 if (!FirstInit || !SecondInit) {
11537 ODRDiagError(FirstEnumConstant->getLocation(),
11538 FirstEnumConstant->getSourceRange(),
11539 EnumConstantSingleInitilizer)
11540 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11541 ODRDiagNote(SecondEnumConstant->getLocation(),
11542 SecondEnumConstant->getSourceRange(),
11543 EnumConstantSingleInitilizer)
11544 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11549 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11550 ODRDiagError(FirstEnumConstant->getLocation(),
11551 FirstEnumConstant->getSourceRange(),
11552 EnumConstantDifferentInitilizer)
11553 << I + 1 << FirstEnumConstant;
11554 ODRDiagNote(SecondEnumConstant->getLocation(),
11555 SecondEnumConstant->getSourceRange(),
11556 EnumConstantDifferentInitilizer)
11557 << I + 1 << SecondEnumConstant;
11565 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11569 void ASTReader::StartedDeserializing() {
11570 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11571 ReadTimer->startTimer();
11574 void ASTReader::FinishedDeserializing() {
11575 assert(NumCurrentElementsDeserializing &&
11576 "FinishedDeserializing not paired with StartedDeserializing");
11577 if (NumCurrentElementsDeserializing == 1) {
11578 // We decrease NumCurrentElementsDeserializing only after pending actions
11579 // are finished, to avoid recursively re-calling finishPendingActions().
11580 finishPendingActions();
11582 --NumCurrentElementsDeserializing;
11584 if (NumCurrentElementsDeserializing == 0) {
11585 // Propagate exception specification and deduced type updates along
11586 // redeclaration chains.
11588 // We do this now rather than in finishPendingActions because we want to
11589 // be able to walk the complete redeclaration chains of the updated decls.
11590 while (!PendingExceptionSpecUpdates.empty() ||
11591 !PendingDeducedTypeUpdates.empty()) {
11592 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11593 PendingExceptionSpecUpdates.clear();
11594 for (auto Update : ESUpdates) {
11595 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11596 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11597 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11598 if (auto *Listener = getContext().getASTMutationListener())
11599 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11600 for (auto *Redecl : Update.second->redecls())
11601 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11604 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11605 PendingDeducedTypeUpdates.clear();
11606 for (auto Update : DTUpdates) {
11607 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11608 // FIXME: If the return type is already deduced, check that it matches.
11609 getContext().adjustDeducedFunctionResultType(Update.first,
11615 ReadTimer->stopTimer();
11617 diagnoseOdrViolations();
11619 // We are not in recursive loading, so it's safe to pass the "interesting"
11620 // decls to the consumer.
11622 PassInterestingDeclsToConsumer();
11626 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11627 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11628 // Remove any fake results before adding any real ones.
11629 auto It = PendingFakeLookupResults.find(II);
11630 if (It != PendingFakeLookupResults.end()) {
11631 for (auto *ND : It->second)
11632 SemaObj->IdResolver.RemoveDecl(ND);
11633 // FIXME: this works around module+PCH performance issue.
11634 // Rather than erase the result from the map, which is O(n), just clear
11635 // the vector of NamedDecls.
11636 It->second.clear();
11640 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11641 SemaObj->TUScope->AddDecl(D);
11642 } else if (SemaObj->TUScope) {
11643 // Adding the decl to IdResolver may have failed because it was already in
11644 // (even though it was not added in scope). If it is already in, make sure
11645 // it gets in the scope as well.
11646 if (std::find(SemaObj->IdResolver.begin(Name),
11647 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11648 SemaObj->TUScope->AddDecl(D);
11652 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11653 ASTContext *Context,
11654 const PCHContainerReader &PCHContainerRdr,
11655 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11656 StringRef isysroot, bool DisableValidation,
11657 bool AllowASTWithCompilerErrors,
11658 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11659 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11660 std::unique_ptr<llvm::Timer> ReadTimer)
11661 : Listener(DisableValidation
11662 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11663 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11664 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11665 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11666 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11667 PCHContainerRdr, PP.getHeaderSearchInfo()),
11668 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11669 DisableValidation(DisableValidation),
11670 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11671 AllowConfigurationMismatch(AllowConfigurationMismatch),
11672 ValidateSystemInputs(ValidateSystemInputs),
11673 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11674 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11675 SourceMgr.setExternalSLocEntrySource(this);
11677 for (const auto &Ext : Extensions) {
11678 auto BlockName = Ext->getExtensionMetadata().BlockName;
11679 auto Known = ModuleFileExtensions.find(BlockName);
11680 if (Known != ModuleFileExtensions.end()) {
11681 Diags.Report(diag::warn_duplicate_module_file_extension)
11686 ModuleFileExtensions.insert({BlockName, Ext});
11690 ASTReader::~ASTReader() {
11691 if (OwnsDeserializationListener)
11692 delete DeserializationListener;
11695 IdentifierResolver &ASTReader::getIdResolver() {
11696 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11699 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11700 unsigned AbbrevID) {
11703 return Cursor.readRecord(AbbrevID, Record);
11705 //===----------------------------------------------------------------------===//
11706 //// OMPClauseReader implementation
11707 ////===----------------------------------------------------------------------===//
11709 // This has to be in namespace clang because it's friended by all
11710 // of the OMP clauses.
11713 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11714 ASTRecordReader &Record;
11715 ASTContext &Context;
11718 OMPClauseReader(ASTRecordReader &Record)
11719 : Record(Record), Context(Record.getContext()) {}
11721 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11722 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11723 OMPClause *readClause();
11724 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11725 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11728 } // end namespace clang
11730 OMPClause *ASTRecordReader::readOMPClause() {
11731 return OMPClauseReader(*this).readClause();
11734 OMPClause *OMPClauseReader::readClause() {
11735 OMPClause *C = nullptr;
11736 switch (llvm::omp::Clause(Record.readInt())) {
11737 case llvm::omp::OMPC_if:
11738 C = new (Context) OMPIfClause();
11740 case llvm::omp::OMPC_final:
11741 C = new (Context) OMPFinalClause();
11743 case llvm::omp::OMPC_num_threads:
11744 C = new (Context) OMPNumThreadsClause();
11746 case llvm::omp::OMPC_safelen:
11747 C = new (Context) OMPSafelenClause();
11749 case llvm::omp::OMPC_simdlen:
11750 C = new (Context) OMPSimdlenClause();
11752 case llvm::omp::OMPC_allocator:
11753 C = new (Context) OMPAllocatorClause();
11755 case llvm::omp::OMPC_collapse:
11756 C = new (Context) OMPCollapseClause();
11758 case llvm::omp::OMPC_default:
11759 C = new (Context) OMPDefaultClause();
11761 case llvm::omp::OMPC_proc_bind:
11762 C = new (Context) OMPProcBindClause();
11764 case llvm::omp::OMPC_schedule:
11765 C = new (Context) OMPScheduleClause();
11767 case llvm::omp::OMPC_ordered:
11768 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11770 case llvm::omp::OMPC_nowait:
11771 C = new (Context) OMPNowaitClause();
11773 case llvm::omp::OMPC_untied:
11774 C = new (Context) OMPUntiedClause();
11776 case llvm::omp::OMPC_mergeable:
11777 C = new (Context) OMPMergeableClause();
11779 case llvm::omp::OMPC_read:
11780 C = new (Context) OMPReadClause();
11782 case llvm::omp::OMPC_write:
11783 C = new (Context) OMPWriteClause();
11785 case llvm::omp::OMPC_update:
11786 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11788 case llvm::omp::OMPC_capture:
11789 C = new (Context) OMPCaptureClause();
11791 case llvm::omp::OMPC_seq_cst:
11792 C = new (Context) OMPSeqCstClause();
11794 case llvm::omp::OMPC_acq_rel:
11795 C = new (Context) OMPAcqRelClause();
11797 case llvm::omp::OMPC_acquire:
11798 C = new (Context) OMPAcquireClause();
11800 case llvm::omp::OMPC_release:
11801 C = new (Context) OMPReleaseClause();
11803 case llvm::omp::OMPC_relaxed:
11804 C = new (Context) OMPRelaxedClause();
11806 case llvm::omp::OMPC_threads:
11807 C = new (Context) OMPThreadsClause();
11809 case llvm::omp::OMPC_simd:
11810 C = new (Context) OMPSIMDClause();
11812 case llvm::omp::OMPC_nogroup:
11813 C = new (Context) OMPNogroupClause();
11815 case llvm::omp::OMPC_unified_address:
11816 C = new (Context) OMPUnifiedAddressClause();
11818 case llvm::omp::OMPC_unified_shared_memory:
11819 C = new (Context) OMPUnifiedSharedMemoryClause();
11821 case llvm::omp::OMPC_reverse_offload:
11822 C = new (Context) OMPReverseOffloadClause();
11824 case llvm::omp::OMPC_dynamic_allocators:
11825 C = new (Context) OMPDynamicAllocatorsClause();
11827 case llvm::omp::OMPC_atomic_default_mem_order:
11828 C = new (Context) OMPAtomicDefaultMemOrderClause();
11830 case llvm::omp::OMPC_private:
11831 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11833 case llvm::omp::OMPC_firstprivate:
11834 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11836 case llvm::omp::OMPC_lastprivate:
11837 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11839 case llvm::omp::OMPC_shared:
11840 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11842 case llvm::omp::OMPC_reduction: {
11843 unsigned N = Record.readInt();
11844 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11845 C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11848 case llvm::omp::OMPC_task_reduction:
11849 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11851 case llvm::omp::OMPC_in_reduction:
11852 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11854 case llvm::omp::OMPC_linear:
11855 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11857 case llvm::omp::OMPC_aligned:
11858 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11860 case llvm::omp::OMPC_copyin:
11861 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11863 case llvm::omp::OMPC_copyprivate:
11864 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11866 case llvm::omp::OMPC_flush:
11867 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11869 case llvm::omp::OMPC_depobj:
11870 C = OMPDepobjClause::CreateEmpty(Context);
11872 case llvm::omp::OMPC_depend: {
11873 unsigned NumVars = Record.readInt();
11874 unsigned NumLoops = Record.readInt();
11875 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11878 case llvm::omp::OMPC_device:
11879 C = new (Context) OMPDeviceClause();
11881 case llvm::omp::OMPC_map: {
11882 OMPMappableExprListSizeTy Sizes;
11883 Sizes.NumVars = Record.readInt();
11884 Sizes.NumUniqueDeclarations = Record.readInt();
11885 Sizes.NumComponentLists = Record.readInt();
11886 Sizes.NumComponents = Record.readInt();
11887 C = OMPMapClause::CreateEmpty(Context, Sizes);
11890 case llvm::omp::OMPC_num_teams:
11891 C = new (Context) OMPNumTeamsClause();
11893 case llvm::omp::OMPC_thread_limit:
11894 C = new (Context) OMPThreadLimitClause();
11896 case llvm::omp::OMPC_priority:
11897 C = new (Context) OMPPriorityClause();
11899 case llvm::omp::OMPC_grainsize:
11900 C = new (Context) OMPGrainsizeClause();
11902 case llvm::omp::OMPC_num_tasks:
11903 C = new (Context) OMPNumTasksClause();
11905 case llvm::omp::OMPC_hint:
11906 C = new (Context) OMPHintClause();
11908 case llvm::omp::OMPC_dist_schedule:
11909 C = new (Context) OMPDistScheduleClause();
11911 case llvm::omp::OMPC_defaultmap:
11912 C = new (Context) OMPDefaultmapClause();
11914 case llvm::omp::OMPC_to: {
11915 OMPMappableExprListSizeTy Sizes;
11916 Sizes.NumVars = Record.readInt();
11917 Sizes.NumUniqueDeclarations = Record.readInt();
11918 Sizes.NumComponentLists = Record.readInt();
11919 Sizes.NumComponents = Record.readInt();
11920 C = OMPToClause::CreateEmpty(Context, Sizes);
11923 case llvm::omp::OMPC_from: {
11924 OMPMappableExprListSizeTy Sizes;
11925 Sizes.NumVars = Record.readInt();
11926 Sizes.NumUniqueDeclarations = Record.readInt();
11927 Sizes.NumComponentLists = Record.readInt();
11928 Sizes.NumComponents = Record.readInt();
11929 C = OMPFromClause::CreateEmpty(Context, Sizes);
11932 case llvm::omp::OMPC_use_device_ptr: {
11933 OMPMappableExprListSizeTy Sizes;
11934 Sizes.NumVars = Record.readInt();
11935 Sizes.NumUniqueDeclarations = Record.readInt();
11936 Sizes.NumComponentLists = Record.readInt();
11937 Sizes.NumComponents = Record.readInt();
11938 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11941 case llvm::omp::OMPC_use_device_addr: {
11942 OMPMappableExprListSizeTy Sizes;
11943 Sizes.NumVars = Record.readInt();
11944 Sizes.NumUniqueDeclarations = Record.readInt();
11945 Sizes.NumComponentLists = Record.readInt();
11946 Sizes.NumComponents = Record.readInt();
11947 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11950 case llvm::omp::OMPC_is_device_ptr: {
11951 OMPMappableExprListSizeTy Sizes;
11952 Sizes.NumVars = Record.readInt();
11953 Sizes.NumUniqueDeclarations = Record.readInt();
11954 Sizes.NumComponentLists = Record.readInt();
11955 Sizes.NumComponents = Record.readInt();
11956 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11959 case llvm::omp::OMPC_allocate:
11960 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11962 case llvm::omp::OMPC_nontemporal:
11963 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11965 case llvm::omp::OMPC_inclusive:
11966 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11968 case llvm::omp::OMPC_exclusive:
11969 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11971 case llvm::omp::OMPC_order:
11972 C = new (Context) OMPOrderClause();
11974 case llvm::omp::OMPC_destroy:
11975 C = new (Context) OMPDestroyClause();
11977 case llvm::omp::OMPC_detach:
11978 C = new (Context) OMPDetachClause();
11980 case llvm::omp::OMPC_uses_allocators:
11981 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11983 case llvm::omp::OMPC_affinity:
11984 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11986 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11987 case llvm::omp::Enum: \
11989 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11993 assert(C && "Unknown OMPClause type");
11996 C->setLocStart(Record.readSourceLocation());
11997 C->setLocEnd(Record.readSourceLocation());
12002 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12003 C->setPreInitStmt(Record.readSubStmt(),
12004 static_cast<OpenMPDirectiveKind>(Record.readInt()));
12007 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12008 VisitOMPClauseWithPreInit(C);
12009 C->setPostUpdateExpr(Record.readSubExpr());
12012 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12013 VisitOMPClauseWithPreInit(C);
12014 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12015 C->setNameModifierLoc(Record.readSourceLocation());
12016 C->setColonLoc(Record.readSourceLocation());
12017 C->setCondition(Record.readSubExpr());
12018 C->setLParenLoc(Record.readSourceLocation());
12021 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12022 VisitOMPClauseWithPreInit(C);
12023 C->setCondition(Record.readSubExpr());
12024 C->setLParenLoc(Record.readSourceLocation());
12027 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12028 VisitOMPClauseWithPreInit(C);
12029 C->setNumThreads(Record.readSubExpr());
12030 C->setLParenLoc(Record.readSourceLocation());
12033 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12034 C->setSafelen(Record.readSubExpr());
12035 C->setLParenLoc(Record.readSourceLocation());
12038 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12039 C->setSimdlen(Record.readSubExpr());
12040 C->setLParenLoc(Record.readSourceLocation());
12043 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12044 C->setAllocator(Record.readExpr());
12045 C->setLParenLoc(Record.readSourceLocation());
12048 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12049 C->setNumForLoops(Record.readSubExpr());
12050 C->setLParenLoc(Record.readSourceLocation());
12053 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12054 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12055 C->setLParenLoc(Record.readSourceLocation());
12056 C->setDefaultKindKwLoc(Record.readSourceLocation());
12059 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12060 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12061 C->setLParenLoc(Record.readSourceLocation());
12062 C->setProcBindKindKwLoc(Record.readSourceLocation());
12065 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12066 VisitOMPClauseWithPreInit(C);
12067 C->setScheduleKind(
12068 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12069 C->setFirstScheduleModifier(
12070 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12071 C->setSecondScheduleModifier(
12072 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12073 C->setChunkSize(Record.readSubExpr());
12074 C->setLParenLoc(Record.readSourceLocation());
12075 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12076 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12077 C->setScheduleKindLoc(Record.readSourceLocation());
12078 C->setCommaLoc(Record.readSourceLocation());
12081 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12082 C->setNumForLoops(Record.readSubExpr());
12083 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12084 C->setLoopNumIterations(I, Record.readSubExpr());
12085 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12086 C->setLoopCounter(I, Record.readSubExpr());
12087 C->setLParenLoc(Record.readSourceLocation());
12090 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12091 C->setEventHandler(Record.readSubExpr());
12092 C->setLParenLoc(Record.readSourceLocation());
12095 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12097 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12099 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12101 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12103 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12105 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12106 if (C->isExtended()) {
12107 C->setLParenLoc(Record.readSourceLocation());
12108 C->setArgumentLoc(Record.readSourceLocation());
12109 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12113 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12115 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12117 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12119 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12121 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12123 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12125 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12127 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12129 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12131 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12133 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12135 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12136 OMPUnifiedSharedMemoryClause *) {}
12138 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12141 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12144 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12145 OMPAtomicDefaultMemOrderClause *C) {
12146 C->setAtomicDefaultMemOrderKind(
12147 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12148 C->setLParenLoc(Record.readSourceLocation());
12149 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12152 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12153 C->setLParenLoc(Record.readSourceLocation());
12154 unsigned NumVars = C->varlist_size();
12155 SmallVector<Expr *, 16> Vars;
12156 Vars.reserve(NumVars);
12157 for (unsigned i = 0; i != NumVars; ++i)
12158 Vars.push_back(Record.readSubExpr());
12159 C->setVarRefs(Vars);
12161 for (unsigned i = 0; i != NumVars; ++i)
12162 Vars.push_back(Record.readSubExpr());
12163 C->setPrivateCopies(Vars);
12166 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12167 VisitOMPClauseWithPreInit(C);
12168 C->setLParenLoc(Record.readSourceLocation());
12169 unsigned NumVars = C->varlist_size();
12170 SmallVector<Expr *, 16> Vars;
12171 Vars.reserve(NumVars);
12172 for (unsigned i = 0; i != NumVars; ++i)
12173 Vars.push_back(Record.readSubExpr());
12174 C->setVarRefs(Vars);
12176 for (unsigned i = 0; i != NumVars; ++i)
12177 Vars.push_back(Record.readSubExpr());
12178 C->setPrivateCopies(Vars);
12180 for (unsigned i = 0; i != NumVars; ++i)
12181 Vars.push_back(Record.readSubExpr());
12185 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12186 VisitOMPClauseWithPostUpdate(C);
12187 C->setLParenLoc(Record.readSourceLocation());
12188 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12189 C->setKindLoc(Record.readSourceLocation());
12190 C->setColonLoc(Record.readSourceLocation());
12191 unsigned NumVars = C->varlist_size();
12192 SmallVector<Expr *, 16> Vars;
12193 Vars.reserve(NumVars);
12194 for (unsigned i = 0; i != NumVars; ++i)
12195 Vars.push_back(Record.readSubExpr());
12196 C->setVarRefs(Vars);
12198 for (unsigned i = 0; i != NumVars; ++i)
12199 Vars.push_back(Record.readSubExpr());
12200 C->setPrivateCopies(Vars);
12202 for (unsigned i = 0; i != NumVars; ++i)
12203 Vars.push_back(Record.readSubExpr());
12204 C->setSourceExprs(Vars);
12206 for (unsigned i = 0; i != NumVars; ++i)
12207 Vars.push_back(Record.readSubExpr());
12208 C->setDestinationExprs(Vars);
12210 for (unsigned i = 0; i != NumVars; ++i)
12211 Vars.push_back(Record.readSubExpr());
12212 C->setAssignmentOps(Vars);
12215 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12216 C->setLParenLoc(Record.readSourceLocation());
12217 unsigned NumVars = C->varlist_size();
12218 SmallVector<Expr *, 16> Vars;
12219 Vars.reserve(NumVars);
12220 for (unsigned i = 0; i != NumVars; ++i)
12221 Vars.push_back(Record.readSubExpr());
12222 C->setVarRefs(Vars);
12225 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12226 VisitOMPClauseWithPostUpdate(C);
12227 C->setLParenLoc(Record.readSourceLocation());
12228 C->setModifierLoc(Record.readSourceLocation());
12229 C->setColonLoc(Record.readSourceLocation());
12230 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12231 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12232 C->setQualifierLoc(NNSL);
12233 C->setNameInfo(DNI);
12235 unsigned NumVars = C->varlist_size();
12236 SmallVector<Expr *, 16> Vars;
12237 Vars.reserve(NumVars);
12238 for (unsigned i = 0; i != NumVars; ++i)
12239 Vars.push_back(Record.readSubExpr());
12240 C->setVarRefs(Vars);
12242 for (unsigned i = 0; i != NumVars; ++i)
12243 Vars.push_back(Record.readSubExpr());
12244 C->setPrivates(Vars);
12246 for (unsigned i = 0; i != NumVars; ++i)
12247 Vars.push_back(Record.readSubExpr());
12248 C->setLHSExprs(Vars);
12250 for (unsigned i = 0; i != NumVars; ++i)
12251 Vars.push_back(Record.readSubExpr());
12252 C->setRHSExprs(Vars);
12254 for (unsigned i = 0; i != NumVars; ++i)
12255 Vars.push_back(Record.readSubExpr());
12256 C->setReductionOps(Vars);
12257 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12259 for (unsigned i = 0; i != NumVars; ++i)
12260 Vars.push_back(Record.readSubExpr());
12261 C->setInscanCopyOps(Vars);
12263 for (unsigned i = 0; i != NumVars; ++i)
12264 Vars.push_back(Record.readSubExpr());
12265 C->setInscanCopyArrayTemps(Vars);
12267 for (unsigned i = 0; i != NumVars; ++i)
12268 Vars.push_back(Record.readSubExpr());
12269 C->setInscanCopyArrayElems(Vars);
12273 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12274 VisitOMPClauseWithPostUpdate(C);
12275 C->setLParenLoc(Record.readSourceLocation());
12276 C->setColonLoc(Record.readSourceLocation());
12277 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12278 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12279 C->setQualifierLoc(NNSL);
12280 C->setNameInfo(DNI);
12282 unsigned NumVars = C->varlist_size();
12283 SmallVector<Expr *, 16> Vars;
12284 Vars.reserve(NumVars);
12285 for (unsigned I = 0; I != NumVars; ++I)
12286 Vars.push_back(Record.readSubExpr());
12287 C->setVarRefs(Vars);
12289 for (unsigned I = 0; I != NumVars; ++I)
12290 Vars.push_back(Record.readSubExpr());
12291 C->setPrivates(Vars);
12293 for (unsigned I = 0; I != NumVars; ++I)
12294 Vars.push_back(Record.readSubExpr());
12295 C->setLHSExprs(Vars);
12297 for (unsigned I = 0; I != NumVars; ++I)
12298 Vars.push_back(Record.readSubExpr());
12299 C->setRHSExprs(Vars);
12301 for (unsigned I = 0; I != NumVars; ++I)
12302 Vars.push_back(Record.readSubExpr());
12303 C->setReductionOps(Vars);
12306 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12307 VisitOMPClauseWithPostUpdate(C);
12308 C->setLParenLoc(Record.readSourceLocation());
12309 C->setColonLoc(Record.readSourceLocation());
12310 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12311 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12312 C->setQualifierLoc(NNSL);
12313 C->setNameInfo(DNI);
12315 unsigned NumVars = C->varlist_size();
12316 SmallVector<Expr *, 16> Vars;
12317 Vars.reserve(NumVars);
12318 for (unsigned I = 0; I != NumVars; ++I)
12319 Vars.push_back(Record.readSubExpr());
12320 C->setVarRefs(Vars);
12322 for (unsigned I = 0; I != NumVars; ++I)
12323 Vars.push_back(Record.readSubExpr());
12324 C->setPrivates(Vars);
12326 for (unsigned I = 0; I != NumVars; ++I)
12327 Vars.push_back(Record.readSubExpr());
12328 C->setLHSExprs(Vars);
12330 for (unsigned I = 0; I != NumVars; ++I)
12331 Vars.push_back(Record.readSubExpr());
12332 C->setRHSExprs(Vars);
12334 for (unsigned I = 0; I != NumVars; ++I)
12335 Vars.push_back(Record.readSubExpr());
12336 C->setReductionOps(Vars);
12338 for (unsigned I = 0; I != NumVars; ++I)
12339 Vars.push_back(Record.readSubExpr());
12340 C->setTaskgroupDescriptors(Vars);
12343 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12344 VisitOMPClauseWithPostUpdate(C);
12345 C->setLParenLoc(Record.readSourceLocation());
12346 C->setColonLoc(Record.readSourceLocation());
12347 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12348 C->setModifierLoc(Record.readSourceLocation());
12349 unsigned NumVars = C->varlist_size();
12350 SmallVector<Expr *, 16> Vars;
12351 Vars.reserve(NumVars);
12352 for (unsigned i = 0; i != NumVars; ++i)
12353 Vars.push_back(Record.readSubExpr());
12354 C->setVarRefs(Vars);
12356 for (unsigned i = 0; i != NumVars; ++i)
12357 Vars.push_back(Record.readSubExpr());
12358 C->setPrivates(Vars);
12360 for (unsigned i = 0; i != NumVars; ++i)
12361 Vars.push_back(Record.readSubExpr());
12364 for (unsigned i = 0; i != NumVars; ++i)
12365 Vars.push_back(Record.readSubExpr());
12366 C->setUpdates(Vars);
12368 for (unsigned i = 0; i != NumVars; ++i)
12369 Vars.push_back(Record.readSubExpr());
12370 C->setFinals(Vars);
12371 C->setStep(Record.readSubExpr());
12372 C->setCalcStep(Record.readSubExpr());
12374 for (unsigned I = 0; I != NumVars + 1; ++I)
12375 Vars.push_back(Record.readSubExpr());
12376 C->setUsedExprs(Vars);
12379 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12380 C->setLParenLoc(Record.readSourceLocation());
12381 C->setColonLoc(Record.readSourceLocation());
12382 unsigned NumVars = C->varlist_size();
12383 SmallVector<Expr *, 16> Vars;
12384 Vars.reserve(NumVars);
12385 for (unsigned i = 0; i != NumVars; ++i)
12386 Vars.push_back(Record.readSubExpr());
12387 C->setVarRefs(Vars);
12388 C->setAlignment(Record.readSubExpr());
12391 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12392 C->setLParenLoc(Record.readSourceLocation());
12393 unsigned NumVars = C->varlist_size();
12394 SmallVector<Expr *, 16> Exprs;
12395 Exprs.reserve(NumVars);
12396 for (unsigned i = 0; i != NumVars; ++i)
12397 Exprs.push_back(Record.readSubExpr());
12398 C->setVarRefs(Exprs);
12400 for (unsigned i = 0; i != NumVars; ++i)
12401 Exprs.push_back(Record.readSubExpr());
12402 C->setSourceExprs(Exprs);
12404 for (unsigned i = 0; i != NumVars; ++i)
12405 Exprs.push_back(Record.readSubExpr());
12406 C->setDestinationExprs(Exprs);
12408 for (unsigned i = 0; i != NumVars; ++i)
12409 Exprs.push_back(Record.readSubExpr());
12410 C->setAssignmentOps(Exprs);
12413 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12414 C->setLParenLoc(Record.readSourceLocation());
12415 unsigned NumVars = C->varlist_size();
12416 SmallVector<Expr *, 16> Exprs;
12417 Exprs.reserve(NumVars);
12418 for (unsigned i = 0; i != NumVars; ++i)
12419 Exprs.push_back(Record.readSubExpr());
12420 C->setVarRefs(Exprs);
12422 for (unsigned i = 0; i != NumVars; ++i)
12423 Exprs.push_back(Record.readSubExpr());
12424 C->setSourceExprs(Exprs);
12426 for (unsigned i = 0; i != NumVars; ++i)
12427 Exprs.push_back(Record.readSubExpr());
12428 C->setDestinationExprs(Exprs);
12430 for (unsigned i = 0; i != NumVars; ++i)
12431 Exprs.push_back(Record.readSubExpr());
12432 C->setAssignmentOps(Exprs);
12435 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12436 C->setLParenLoc(Record.readSourceLocation());
12437 unsigned NumVars = C->varlist_size();
12438 SmallVector<Expr *, 16> Vars;
12439 Vars.reserve(NumVars);
12440 for (unsigned i = 0; i != NumVars; ++i)
12441 Vars.push_back(Record.readSubExpr());
12442 C->setVarRefs(Vars);
12445 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12446 C->setDepobj(Record.readSubExpr());
12447 C->setLParenLoc(Record.readSourceLocation());
12450 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12451 C->setLParenLoc(Record.readSourceLocation());
12452 C->setModifier(Record.readSubExpr());
12453 C->setDependencyKind(
12454 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12455 C->setDependencyLoc(Record.readSourceLocation());
12456 C->setColonLoc(Record.readSourceLocation());
12457 unsigned NumVars = C->varlist_size();
12458 SmallVector<Expr *, 16> Vars;
12459 Vars.reserve(NumVars);
12460 for (unsigned I = 0; I != NumVars; ++I)
12461 Vars.push_back(Record.readSubExpr());
12462 C->setVarRefs(Vars);
12463 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12464 C->setLoopData(I, Record.readSubExpr());
12467 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12468 VisitOMPClauseWithPreInit(C);
12469 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12470 C->setDevice(Record.readSubExpr());
12471 C->setModifierLoc(Record.readSourceLocation());
12472 C->setLParenLoc(Record.readSourceLocation());
12475 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12476 C->setLParenLoc(Record.readSourceLocation());
12477 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12478 C->setMapTypeModifier(
12479 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12480 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12482 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12483 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12485 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12486 C->setMapLoc(Record.readSourceLocation());
12487 C->setColonLoc(Record.readSourceLocation());
12488 auto NumVars = C->varlist_size();
12489 auto UniqueDecls = C->getUniqueDeclarationsNum();
12490 auto TotalLists = C->getTotalComponentListNum();
12491 auto TotalComponents = C->getTotalComponentsNum();
12493 SmallVector<Expr *, 16> Vars;
12494 Vars.reserve(NumVars);
12495 for (unsigned i = 0; i != NumVars; ++i)
12496 Vars.push_back(Record.readExpr());
12497 C->setVarRefs(Vars);
12499 SmallVector<Expr *, 16> UDMappers;
12500 UDMappers.reserve(NumVars);
12501 for (unsigned I = 0; I < NumVars; ++I)
12502 UDMappers.push_back(Record.readExpr());
12503 C->setUDMapperRefs(UDMappers);
12505 SmallVector<ValueDecl *, 16> Decls;
12506 Decls.reserve(UniqueDecls);
12507 for (unsigned i = 0; i < UniqueDecls; ++i)
12508 Decls.push_back(Record.readDeclAs<ValueDecl>());
12509 C->setUniqueDecls(Decls);
12511 SmallVector<unsigned, 16> ListsPerDecl;
12512 ListsPerDecl.reserve(UniqueDecls);
12513 for (unsigned i = 0; i < UniqueDecls; ++i)
12514 ListsPerDecl.push_back(Record.readInt());
12515 C->setDeclNumLists(ListsPerDecl);
12517 SmallVector<unsigned, 32> ListSizes;
12518 ListSizes.reserve(TotalLists);
12519 for (unsigned i = 0; i < TotalLists; ++i)
12520 ListSizes.push_back(Record.readInt());
12521 C->setComponentListSizes(ListSizes);
12523 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12524 Components.reserve(TotalComponents);
12525 for (unsigned i = 0; i < TotalComponents; ++i) {
12526 Expr *AssociatedExpr = Record.readExpr();
12527 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12528 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12529 AssociatedExpr, AssociatedDecl));
12531 C->setComponents(Components, ListSizes);
12534 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12535 C->setLParenLoc(Record.readSourceLocation());
12536 C->setColonLoc(Record.readSourceLocation());
12537 C->setAllocator(Record.readSubExpr());
12538 unsigned NumVars = C->varlist_size();
12539 SmallVector<Expr *, 16> Vars;
12540 Vars.reserve(NumVars);
12541 for (unsigned i = 0; i != NumVars; ++i)
12542 Vars.push_back(Record.readSubExpr());
12543 C->setVarRefs(Vars);
12546 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12547 VisitOMPClauseWithPreInit(C);
12548 C->setNumTeams(Record.readSubExpr());
12549 C->setLParenLoc(Record.readSourceLocation());
12552 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12553 VisitOMPClauseWithPreInit(C);
12554 C->setThreadLimit(Record.readSubExpr());
12555 C->setLParenLoc(Record.readSourceLocation());
12558 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12559 VisitOMPClauseWithPreInit(C);
12560 C->setPriority(Record.readSubExpr());
12561 C->setLParenLoc(Record.readSourceLocation());
12564 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12565 VisitOMPClauseWithPreInit(C);
12566 C->setGrainsize(Record.readSubExpr());
12567 C->setLParenLoc(Record.readSourceLocation());
12570 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12571 VisitOMPClauseWithPreInit(C);
12572 C->setNumTasks(Record.readSubExpr());
12573 C->setLParenLoc(Record.readSourceLocation());
12576 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12577 C->setHint(Record.readSubExpr());
12578 C->setLParenLoc(Record.readSourceLocation());
12581 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12582 VisitOMPClauseWithPreInit(C);
12583 C->setDistScheduleKind(
12584 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12585 C->setChunkSize(Record.readSubExpr());
12586 C->setLParenLoc(Record.readSourceLocation());
12587 C->setDistScheduleKindLoc(Record.readSourceLocation());
12588 C->setCommaLoc(Record.readSourceLocation());
12591 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12592 C->setDefaultmapKind(
12593 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12594 C->setDefaultmapModifier(
12595 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12596 C->setLParenLoc(Record.readSourceLocation());
12597 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12598 C->setDefaultmapKindLoc(Record.readSourceLocation());
12601 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12602 C->setLParenLoc(Record.readSourceLocation());
12603 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12604 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12605 auto NumVars = C->varlist_size();
12606 auto UniqueDecls = C->getUniqueDeclarationsNum();
12607 auto TotalLists = C->getTotalComponentListNum();
12608 auto TotalComponents = C->getTotalComponentsNum();
12610 SmallVector<Expr *, 16> Vars;
12611 Vars.reserve(NumVars);
12612 for (unsigned i = 0; i != NumVars; ++i)
12613 Vars.push_back(Record.readSubExpr());
12614 C->setVarRefs(Vars);
12616 SmallVector<Expr *, 16> UDMappers;
12617 UDMappers.reserve(NumVars);
12618 for (unsigned I = 0; I < NumVars; ++I)
12619 UDMappers.push_back(Record.readSubExpr());
12620 C->setUDMapperRefs(UDMappers);
12622 SmallVector<ValueDecl *, 16> Decls;
12623 Decls.reserve(UniqueDecls);
12624 for (unsigned i = 0; i < UniqueDecls; ++i)
12625 Decls.push_back(Record.readDeclAs<ValueDecl>());
12626 C->setUniqueDecls(Decls);
12628 SmallVector<unsigned, 16> ListsPerDecl;
12629 ListsPerDecl.reserve(UniqueDecls);
12630 for (unsigned i = 0; i < UniqueDecls; ++i)
12631 ListsPerDecl.push_back(Record.readInt());
12632 C->setDeclNumLists(ListsPerDecl);
12634 SmallVector<unsigned, 32> ListSizes;
12635 ListSizes.reserve(TotalLists);
12636 for (unsigned i = 0; i < TotalLists; ++i)
12637 ListSizes.push_back(Record.readInt());
12638 C->setComponentListSizes(ListSizes);
12640 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12641 Components.reserve(TotalComponents);
12642 for (unsigned i = 0; i < TotalComponents; ++i) {
12643 Expr *AssociatedExpr = Record.readSubExpr();
12644 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12645 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12646 AssociatedExpr, AssociatedDecl));
12648 C->setComponents(Components, ListSizes);
12651 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12652 C->setLParenLoc(Record.readSourceLocation());
12653 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12654 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12655 auto NumVars = C->varlist_size();
12656 auto UniqueDecls = C->getUniqueDeclarationsNum();
12657 auto TotalLists = C->getTotalComponentListNum();
12658 auto TotalComponents = C->getTotalComponentsNum();
12660 SmallVector<Expr *, 16> Vars;
12661 Vars.reserve(NumVars);
12662 for (unsigned i = 0; i != NumVars; ++i)
12663 Vars.push_back(Record.readSubExpr());
12664 C->setVarRefs(Vars);
12666 SmallVector<Expr *, 16> UDMappers;
12667 UDMappers.reserve(NumVars);
12668 for (unsigned I = 0; I < NumVars; ++I)
12669 UDMappers.push_back(Record.readSubExpr());
12670 C->setUDMapperRefs(UDMappers);
12672 SmallVector<ValueDecl *, 16> Decls;
12673 Decls.reserve(UniqueDecls);
12674 for (unsigned i = 0; i < UniqueDecls; ++i)
12675 Decls.push_back(Record.readDeclAs<ValueDecl>());
12676 C->setUniqueDecls(Decls);
12678 SmallVector<unsigned, 16> ListsPerDecl;
12679 ListsPerDecl.reserve(UniqueDecls);
12680 for (unsigned i = 0; i < UniqueDecls; ++i)
12681 ListsPerDecl.push_back(Record.readInt());
12682 C->setDeclNumLists(ListsPerDecl);
12684 SmallVector<unsigned, 32> ListSizes;
12685 ListSizes.reserve(TotalLists);
12686 for (unsigned i = 0; i < TotalLists; ++i)
12687 ListSizes.push_back(Record.readInt());
12688 C->setComponentListSizes(ListSizes);
12690 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12691 Components.reserve(TotalComponents);
12692 for (unsigned i = 0; i < TotalComponents; ++i) {
12693 Expr *AssociatedExpr = Record.readSubExpr();
12694 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12695 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12696 AssociatedExpr, AssociatedDecl));
12698 C->setComponents(Components, ListSizes);
12701 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12702 C->setLParenLoc(Record.readSourceLocation());
12703 auto NumVars = C->varlist_size();
12704 auto UniqueDecls = C->getUniqueDeclarationsNum();
12705 auto TotalLists = C->getTotalComponentListNum();
12706 auto TotalComponents = C->getTotalComponentsNum();
12708 SmallVector<Expr *, 16> Vars;
12709 Vars.reserve(NumVars);
12710 for (unsigned i = 0; i != NumVars; ++i)
12711 Vars.push_back(Record.readSubExpr());
12712 C->setVarRefs(Vars);
12714 for (unsigned i = 0; i != NumVars; ++i)
12715 Vars.push_back(Record.readSubExpr());
12716 C->setPrivateCopies(Vars);
12718 for (unsigned i = 0; i != NumVars; ++i)
12719 Vars.push_back(Record.readSubExpr());
12722 SmallVector<ValueDecl *, 16> Decls;
12723 Decls.reserve(UniqueDecls);
12724 for (unsigned i = 0; i < UniqueDecls; ++i)
12725 Decls.push_back(Record.readDeclAs<ValueDecl>());
12726 C->setUniqueDecls(Decls);
12728 SmallVector<unsigned, 16> ListsPerDecl;
12729 ListsPerDecl.reserve(UniqueDecls);
12730 for (unsigned i = 0; i < UniqueDecls; ++i)
12731 ListsPerDecl.push_back(Record.readInt());
12732 C->setDeclNumLists(ListsPerDecl);
12734 SmallVector<unsigned, 32> ListSizes;
12735 ListSizes.reserve(TotalLists);
12736 for (unsigned i = 0; i < TotalLists; ++i)
12737 ListSizes.push_back(Record.readInt());
12738 C->setComponentListSizes(ListSizes);
12740 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12741 Components.reserve(TotalComponents);
12742 for (unsigned i = 0; i < TotalComponents; ++i) {
12743 Expr *AssociatedExpr = Record.readSubExpr();
12744 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12745 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12746 AssociatedExpr, AssociatedDecl));
12748 C->setComponents(Components, ListSizes);
12751 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12752 C->setLParenLoc(Record.readSourceLocation());
12753 auto NumVars = C->varlist_size();
12754 auto UniqueDecls = C->getUniqueDeclarationsNum();
12755 auto TotalLists = C->getTotalComponentListNum();
12756 auto TotalComponents = C->getTotalComponentsNum();
12758 SmallVector<Expr *, 16> Vars;
12759 Vars.reserve(NumVars);
12760 for (unsigned i = 0; i != NumVars; ++i)
12761 Vars.push_back(Record.readSubExpr());
12762 C->setVarRefs(Vars);
12764 SmallVector<ValueDecl *, 16> Decls;
12765 Decls.reserve(UniqueDecls);
12766 for (unsigned i = 0; i < UniqueDecls; ++i)
12767 Decls.push_back(Record.readDeclAs<ValueDecl>());
12768 C->setUniqueDecls(Decls);
12770 SmallVector<unsigned, 16> ListsPerDecl;
12771 ListsPerDecl.reserve(UniqueDecls);
12772 for (unsigned i = 0; i < UniqueDecls; ++i)
12773 ListsPerDecl.push_back(Record.readInt());
12774 C->setDeclNumLists(ListsPerDecl);
12776 SmallVector<unsigned, 32> ListSizes;
12777 ListSizes.reserve(TotalLists);
12778 for (unsigned i = 0; i < TotalLists; ++i)
12779 ListSizes.push_back(Record.readInt());
12780 C->setComponentListSizes(ListSizes);
12782 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12783 Components.reserve(TotalComponents);
12784 for (unsigned i = 0; i < TotalComponents; ++i) {
12785 Expr *AssociatedExpr = Record.readSubExpr();
12786 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12787 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12788 AssociatedExpr, AssociatedDecl));
12790 C->setComponents(Components, ListSizes);
12793 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12794 C->setLParenLoc(Record.readSourceLocation());
12795 auto NumVars = C->varlist_size();
12796 auto UniqueDecls = C->getUniqueDeclarationsNum();
12797 auto TotalLists = C->getTotalComponentListNum();
12798 auto TotalComponents = C->getTotalComponentsNum();
12800 SmallVector<Expr *, 16> Vars;
12801 Vars.reserve(NumVars);
12802 for (unsigned i = 0; i != NumVars; ++i)
12803 Vars.push_back(Record.readSubExpr());
12804 C->setVarRefs(Vars);
12807 SmallVector<ValueDecl *, 16> Decls;
12808 Decls.reserve(UniqueDecls);
12809 for (unsigned i = 0; i < UniqueDecls; ++i)
12810 Decls.push_back(Record.readDeclAs<ValueDecl>());
12811 C->setUniqueDecls(Decls);
12813 SmallVector<unsigned, 16> ListsPerDecl;
12814 ListsPerDecl.reserve(UniqueDecls);
12815 for (unsigned i = 0; i < UniqueDecls; ++i)
12816 ListsPerDecl.push_back(Record.readInt());
12817 C->setDeclNumLists(ListsPerDecl);
12819 SmallVector<unsigned, 32> ListSizes;
12820 ListSizes.reserve(TotalLists);
12821 for (unsigned i = 0; i < TotalLists; ++i)
12822 ListSizes.push_back(Record.readInt());
12823 C->setComponentListSizes(ListSizes);
12825 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12826 Components.reserve(TotalComponents);
12827 for (unsigned i = 0; i < TotalComponents; ++i) {
12828 Expr *AssociatedExpr = Record.readSubExpr();
12829 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12830 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12831 AssociatedExpr, AssociatedDecl));
12833 C->setComponents(Components, ListSizes);
12836 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12837 C->setLParenLoc(Record.readSourceLocation());
12838 unsigned NumVars = C->varlist_size();
12839 SmallVector<Expr *, 16> Vars;
12840 Vars.reserve(NumVars);
12841 for (unsigned i = 0; i != NumVars; ++i)
12842 Vars.push_back(Record.readSubExpr());
12843 C->setVarRefs(Vars);
12845 Vars.reserve(NumVars);
12846 for (unsigned i = 0; i != NumVars; ++i)
12847 Vars.push_back(Record.readSubExpr());
12848 C->setPrivateRefs(Vars);
12851 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12852 C->setLParenLoc(Record.readSourceLocation());
12853 unsigned NumVars = C->varlist_size();
12854 SmallVector<Expr *, 16> Vars;
12855 Vars.reserve(NumVars);
12856 for (unsigned i = 0; i != NumVars; ++i)
12857 Vars.push_back(Record.readSubExpr());
12858 C->setVarRefs(Vars);
12861 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12862 C->setLParenLoc(Record.readSourceLocation());
12863 unsigned NumVars = C->varlist_size();
12864 SmallVector<Expr *, 16> Vars;
12865 Vars.reserve(NumVars);
12866 for (unsigned i = 0; i != NumVars; ++i)
12867 Vars.push_back(Record.readSubExpr());
12868 C->setVarRefs(Vars);
12871 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12872 C->setLParenLoc(Record.readSourceLocation());
12873 unsigned NumOfAllocators = C->getNumberOfAllocators();
12874 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12875 Data.reserve(NumOfAllocators);
12876 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12877 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12878 D.Allocator = Record.readSubExpr();
12879 D.AllocatorTraits = Record.readSubExpr();
12880 D.LParenLoc = Record.readSourceLocation();
12881 D.RParenLoc = Record.readSourceLocation();
12883 C->setAllocatorsData(Data);
12886 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12887 C->setLParenLoc(Record.readSourceLocation());
12888 C->setModifier(Record.readSubExpr());
12889 C->setColonLoc(Record.readSourceLocation());
12890 unsigned NumOfLocators = C->varlist_size();
12891 SmallVector<Expr *, 4> Locators;
12892 Locators.reserve(NumOfLocators);
12893 for (unsigned I = 0; I != NumOfLocators; ++I)
12894 Locators.push_back(Record.readSubExpr());
12895 C->setVarRefs(Locators);
12898 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12899 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12900 C->setLParenLoc(Record.readSourceLocation());
12901 C->setKindKwLoc(Record.readSourceLocation());
12904 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12905 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12906 TI.Sets.resize(readUInt32());
12907 for (auto &Set : TI.Sets) {
12908 Set.Kind = readEnum<llvm::omp::TraitSet>();
12909 Set.Selectors.resize(readUInt32());
12910 for (auto &Selector : Set.Selectors) {
12911 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12912 Selector.ScoreOrCondition = nullptr;
12914 Selector.ScoreOrCondition = readExprRef();
12915 Selector.Properties.resize(readUInt32());
12916 for (auto &Property : Selector.Properties)
12917 Property.Kind = readEnum<llvm::omp::TraitProperty>();