1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the ASTReader class, which reads AST files.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Serialization/ASTReader.h"
14 #include "ASTCommon.h"
15 #include "ASTReaderInternals.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ASTMutationListener.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/TypeLocVisitor.h"
39 #include "clang/AST/UnresolvedSet.h"
40 #include "clang/Basic/CommentOptions.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/DiagnosticOptions.h"
43 #include "clang/Basic/ExceptionSpecificationType.h"
44 #include "clang/Basic/FileManager.h"
45 #include "clang/Basic/FileSystemOptions.h"
46 #include "clang/Basic/IdentifierTable.h"
47 #include "clang/Basic/LLVM.h"
48 #include "clang/Basic/LangOptions.h"
49 #include "clang/Basic/Module.h"
50 #include "clang/Basic/ObjCRuntime.h"
51 #include "clang/Basic/OperatorKinds.h"
52 #include "clang/Basic/PragmaKinds.h"
53 #include "clang/Basic/Sanitizers.h"
54 #include "clang/Basic/SourceLocation.h"
55 #include "clang/Basic/SourceManager.h"
56 #include "clang/Basic/SourceManagerInternals.h"
57 #include "clang/Basic/Specifiers.h"
58 #include "clang/Basic/TargetInfo.h"
59 #include "clang/Basic/TargetOptions.h"
60 #include "clang/Basic/TokenKinds.h"
61 #include "clang/Basic/Version.h"
62 #include "clang/Lex/HeaderSearch.h"
63 #include "clang/Lex/HeaderSearchOptions.h"
64 #include "clang/Lex/MacroInfo.h"
65 #include "clang/Lex/ModuleMap.h"
66 #include "clang/Lex/PreprocessingRecord.h"
67 #include "clang/Lex/Preprocessor.h"
68 #include "clang/Lex/PreprocessorOptions.h"
69 #include "clang/Lex/Token.h"
70 #include "clang/Sema/ObjCMethodList.h"
71 #include "clang/Sema/Scope.h"
72 #include "clang/Sema/Sema.h"
73 #include "clang/Sema/Weak.h"
74 #include "clang/Serialization/ASTBitCodes.h"
75 #include "clang/Serialization/ASTDeserializationListener.h"
76 #include "clang/Serialization/ContinuousRangeMap.h"
77 #include "clang/Serialization/GlobalModuleIndex.h"
78 #include "clang/Serialization/InMemoryModuleCache.h"
79 #include "clang/Serialization/Module.h"
80 #include "clang/Serialization/ModuleFileExtension.h"
81 #include "clang/Serialization/ModuleManager.h"
82 #include "clang/Serialization/PCHContainerOperations.h"
83 #include "clang/Serialization/SerializationDiagnostic.h"
84 #include "llvm/ADT/APFloat.h"
85 #include "llvm/ADT/APInt.h"
86 #include "llvm/ADT/APSInt.h"
87 #include "llvm/ADT/ArrayRef.h"
88 #include "llvm/ADT/DenseMap.h"
89 #include "llvm/ADT/FoldingSet.h"
90 #include "llvm/ADT/Hashing.h"
91 #include "llvm/ADT/IntrusiveRefCntPtr.h"
92 #include "llvm/ADT/None.h"
93 #include "llvm/ADT/Optional.h"
94 #include "llvm/ADT/STLExtras.h"
95 #include "llvm/ADT/ScopeExit.h"
96 #include "llvm/ADT/SmallPtrSet.h"
97 #include "llvm/ADT/SmallString.h"
98 #include "llvm/ADT/SmallVector.h"
99 #include "llvm/ADT/StringExtras.h"
100 #include "llvm/ADT/StringMap.h"
101 #include "llvm/ADT/StringRef.h"
102 #include "llvm/ADT/Triple.h"
103 #include "llvm/ADT/iterator_range.h"
104 #include "llvm/Bitstream/BitstreamReader.h"
105 #include "llvm/Support/Casting.h"
106 #include "llvm/Support/Compiler.h"
107 #include "llvm/Support/Compression.h"
108 #include "llvm/Support/DJB.h"
109 #include "llvm/Support/Endian.h"
110 #include "llvm/Support/Error.h"
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FileSystem.h"
113 #include "llvm/Support/MemoryBuffer.h"
114 #include "llvm/Support/Path.h"
115 #include "llvm/Support/SaveAndRestore.h"
116 #include "llvm/Support/Timer.h"
117 #include "llvm/Support/VersionTuple.h"
118 #include "llvm/Support/raw_ostream.h"
130 #include <system_error>
135 using namespace clang;
136 using namespace clang::serialization;
137 using namespace clang::serialization::reader;
138 using llvm::BitstreamCursor;
140 //===----------------------------------------------------------------------===//
141 // ChainedASTReaderListener implementation
142 //===----------------------------------------------------------------------===//
145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146 return First->ReadFullVersionInformation(FullVersion) ||
147 Second->ReadFullVersionInformation(FullVersion);
150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151 First->ReadModuleName(ModuleName);
152 Second->ReadModuleName(ModuleName);
155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156 First->ReadModuleMapFile(ModuleMapPath);
157 Second->ReadModuleMapFile(ModuleMapPath);
161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
163 bool AllowCompatibleDifferences) {
164 return First->ReadLanguageOptions(LangOpts, Complain,
165 AllowCompatibleDifferences) ||
166 Second->ReadLanguageOptions(LangOpts, Complain,
167 AllowCompatibleDifferences);
170 bool ChainedASTReaderListener::ReadTargetOptions(
171 const TargetOptions &TargetOpts, bool Complain,
172 bool AllowCompatibleDifferences) {
173 return First->ReadTargetOptions(TargetOpts, Complain,
174 AllowCompatibleDifferences) ||
175 Second->ReadTargetOptions(TargetOpts, Complain,
176 AllowCompatibleDifferences);
179 bool ChainedASTReaderListener::ReadDiagnosticOptions(
180 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
181 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182 Second->ReadDiagnosticOptions(DiagOpts, Complain);
186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
188 return First->ReadFileSystemOptions(FSOpts, Complain) ||
189 Second->ReadFileSystemOptions(FSOpts, Complain);
192 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
193 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
195 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
197 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201 bool ChainedASTReaderListener::ReadPreprocessorOptions(
202 const PreprocessorOptions &PPOpts, bool Complain,
203 std::string &SuggestedPredefines) {
204 return First->ReadPreprocessorOptions(PPOpts, Complain,
205 SuggestedPredefines) ||
206 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
211 First->ReadCounter(M, Value);
212 Second->ReadCounter(M, Value);
215 bool ChainedASTReaderListener::needsInputFileVisitation() {
216 return First->needsInputFileVisitation() ||
217 Second->needsInputFileVisitation();
220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221 return First->needsSystemInputFileVisitation() ||
222 Second->needsSystemInputFileVisitation();
225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
227 First->visitModuleFile(Filename, Kind);
228 Second->visitModuleFile(Filename, Kind);
231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
234 bool isExplicitModule) {
235 bool Continue = false;
236 if (First->needsInputFileVisitation() &&
237 (!isSystem || First->needsSystemInputFileVisitation()))
238 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
240 if (Second->needsInputFileVisitation() &&
241 (!isSystem || Second->needsSystemInputFileVisitation()))
242 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
247 void ChainedASTReaderListener::readModuleFileExtension(
248 const ModuleFileExtensionMetadata &Metadata) {
249 First->readModuleFileExtension(Metadata);
250 Second->readModuleFileExtension(Metadata);
253 //===----------------------------------------------------------------------===//
254 // PCH validator implementation
255 //===----------------------------------------------------------------------===//
257 ASTReaderListener::~ASTReaderListener() = default;
259 /// Compare the given set of language options against an existing set of
260 /// language options.
262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
263 /// \param AllowCompatibleDifferences If true, differences between compatible
264 /// language options will be permitted.
266 /// \returns true if the languagae options mis-match, false otherwise.
267 static bool checkLanguageOptions(const LangOptions &LangOpts,
268 const LangOptions &ExistingLangOpts,
269 DiagnosticsEngine *Diags,
270 bool AllowCompatibleDifferences = true) {
271 #define LANGOPT(Name, Bits, Default, Description) \
272 if (ExistingLangOpts.Name != LangOpts.Name) { \
274 Diags->Report(diag::err_pch_langopt_mismatch) \
275 << Description << LangOpts.Name << ExistingLangOpts.Name; \
279 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
280 if (ExistingLangOpts.Name != LangOpts.Name) { \
282 Diags->Report(diag::err_pch_langopt_value_mismatch) \
287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
288 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
290 Diags->Report(diag::err_pch_langopt_value_mismatch) \
295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
296 if (!AllowCompatibleDifferences) \
297 LANGOPT(Name, Bits, Default, Description)
299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
300 if (!AllowCompatibleDifferences) \
301 ENUM_LANGOPT(Name, Bits, Default, Description)
303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304 if (!AllowCompatibleDifferences) \
305 VALUE_LANGOPT(Name, Bits, Default, Description)
307 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
310 #include "clang/Basic/LangOptions.def"
312 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
314 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
318 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
320 Diags->Report(diag::err_pch_langopt_value_mismatch)
321 << "target Objective-C runtime";
325 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326 LangOpts.CommentOpts.BlockCommandNames) {
328 Diags->Report(diag::err_pch_langopt_value_mismatch)
329 << "block command names";
333 // Sanitizer feature mismatches are treated as compatible differences. If
334 // compatible differences aren't allowed, we still only want to check for
335 // mismatches of non-modular sanitizers (the only ones which can affect AST
337 if (!AllowCompatibleDifferences) {
338 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341 ExistingSanitizers.clear(ModularSanitizers);
342 ImportedSanitizers.clear(ModularSanitizers);
343 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344 const std::string Flag = "-fsanitize=";
346 #define SANITIZER(NAME, ID) \
348 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
349 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
350 if (InExistingModule != InImportedModule) \
351 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
352 << InExistingModule << (Flag + NAME); \
354 #include "clang/Basic/Sanitizers.def"
363 /// Compare the given set of target options against an existing set of
366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
368 /// \returns true if the target options mis-match, false otherwise.
369 static bool checkTargetOptions(const TargetOptions &TargetOpts,
370 const TargetOptions &ExistingTargetOpts,
371 DiagnosticsEngine *Diags,
372 bool AllowCompatibleDifferences = true) {
373 #define CHECK_TARGET_OPT(Field, Name) \
374 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
376 Diags->Report(diag::err_pch_targetopt_mismatch) \
377 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
381 // The triple and ABI must match exactly.
382 CHECK_TARGET_OPT(Triple, "target");
383 CHECK_TARGET_OPT(ABI, "target ABI");
385 // We can tolerate different CPUs in many cases, notably when one CPU
386 // supports a strict superset of another. When allowing compatible
387 // differences skip this check.
388 if (!AllowCompatibleDifferences)
389 CHECK_TARGET_OPT(CPU, "target CPU");
391 #undef CHECK_TARGET_OPT
393 // Compare feature sets.
394 SmallVector<StringRef, 4> ExistingFeatures(
395 ExistingTargetOpts.FeaturesAsWritten.begin(),
396 ExistingTargetOpts.FeaturesAsWritten.end());
397 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398 TargetOpts.FeaturesAsWritten.end());
399 llvm::sort(ExistingFeatures);
400 llvm::sort(ReadFeatures);
402 // We compute the set difference in both directions explicitly so that we can
403 // diagnose the differences differently.
404 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
406 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409 ExistingFeatures.begin(), ExistingFeatures.end(),
410 std::back_inserter(UnmatchedReadFeatures));
412 // If we are allowing compatible differences and the read feature set is
413 // a strict subset of the existing feature set, there is nothing to diagnose.
414 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
418 for (StringRef Feature : UnmatchedReadFeatures)
419 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
420 << /* is-existing-feature */ false << Feature;
421 for (StringRef Feature : UnmatchedExistingFeatures)
422 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423 << /* is-existing-feature */ true << Feature;
426 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
432 bool AllowCompatibleDifferences) {
433 const LangOptions &ExistingLangOpts = PP.getLangOpts();
434 return checkLanguageOptions(LangOpts, ExistingLangOpts,
435 Complain ? &Reader.Diags : nullptr,
436 AllowCompatibleDifferences);
439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
441 bool AllowCompatibleDifferences) {
442 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
444 Complain ? &Reader.Diags : nullptr,
445 AllowCompatibleDifferences);
450 using MacroDefinitionsMap =
451 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457 DiagnosticsEngine &Diags,
459 using Level = DiagnosticsEngine::Level;
461 // Check current mappings for new -Werror mappings, and the stored mappings
462 // for cases that were explicitly mapped to *not* be errors that are now
463 // errors because of options like -Werror.
464 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
466 for (DiagnosticsEngine *MappingSource : MappingSources) {
467 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468 diag::kind DiagID = DiagIDMappingPair.first;
469 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470 if (CurLevel < DiagnosticsEngine::Error)
471 continue; // not significant
473 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474 if (StoredLevel < DiagnosticsEngine::Error) {
476 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
490 return Ext >= diag::Severity::Error;
493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494 DiagnosticsEngine &Diags,
495 bool IsSystem, bool Complain) {
498 if (Diags.getSuppressSystemWarnings())
500 // If -Wsystem-headers was not enabled before, be conservative
501 if (StoredDiags.getSuppressSystemWarnings()) {
503 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
508 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
514 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515 !StoredDiags.getEnableAllWarnings()) {
517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
521 if (isExtHandlingFromDiagsError(Diags) &&
522 !isExtHandlingFromDiagsError(StoredDiags)) {
524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
528 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
531 /// Return the top import module if it is implicit, nullptr otherwise.
532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
534 // If the original import came from a file explicitly generated by the user,
535 // don't check the diagnostic mappings.
536 // FIXME: currently this is approximated by checking whether this is not a
537 // module import of an implicitly-loaded module file.
538 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539 // the transitive closure of its imports, since unrelated modules cannot be
540 // imported until after this module finishes validation.
541 ModuleFile *TopImport = &*ModuleMgr.rbegin();
542 while (!TopImport->ImportedBy.empty())
543 TopImport = TopImport->ImportedBy[0];
544 if (TopImport->Kind != MK_ImplicitModule)
547 StringRef ModuleName = TopImport->ModuleName;
548 assert(!ModuleName.empty() && "diagnostic options read before module name");
550 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551 assert(M && "missing module");
555 bool PCHValidator::ReadDiagnosticOptions(
556 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
560 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
561 // This should never fail, because we would have processed these options
562 // before writing them to an ASTFile.
563 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
565 ModuleManager &ModuleMgr = Reader.getModuleManager();
566 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
568 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
572 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573 // contains the union of their flags.
574 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
578 /// Collect the macro definitions provided by the given preprocessor
581 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582 MacroDefinitionsMap &Macros,
583 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
584 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585 StringRef Macro = PPOpts.Macros[I].first;
586 bool IsUndef = PPOpts.Macros[I].second;
588 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589 StringRef MacroName = MacroPair.first;
590 StringRef MacroBody = MacroPair.second;
592 // For an #undef'd macro, we only care about the name.
594 if (MacroNames && !Macros.count(MacroName))
595 MacroNames->push_back(MacroName);
597 Macros[MacroName] = std::make_pair("", true);
601 // For a #define'd macro, figure out the actual definition.
602 if (MacroName.size() == Macro.size())
605 // Note: GCC drops anything following an end-of-line character.
606 StringRef::size_type End = MacroBody.find_first_of("\n\r");
607 MacroBody = MacroBody.substr(0, End);
610 if (MacroNames && !Macros.count(MacroName))
611 MacroNames->push_back(MacroName);
612 Macros[MacroName] = std::make_pair(MacroBody, false);
616 /// Check the preprocessor options deserialized from the control block
617 /// against the preprocessor options in an existing preprocessor.
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 /// \param Validate If true, validate preprocessor options. If false, allow
621 /// macros defined by \p ExistingPPOpts to override those defined by
622 /// \p PPOpts in SuggestedPredefines.
623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624 const PreprocessorOptions &ExistingPPOpts,
625 DiagnosticsEngine *Diags,
626 FileManager &FileMgr,
627 std::string &SuggestedPredefines,
628 const LangOptions &LangOpts,
629 bool Validate = true) {
630 // Check macro definitions.
631 MacroDefinitionsMap ASTFileMacros;
632 collectMacroDefinitions(PPOpts, ASTFileMacros);
633 MacroDefinitionsMap ExistingMacros;
634 SmallVector<StringRef, 4> ExistingMacroNames;
635 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
637 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
638 // Dig out the macro definition in the existing preprocessor options.
639 StringRef MacroName = ExistingMacroNames[I];
640 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
642 // Check whether we know anything about this macro name or not.
643 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644 ASTFileMacros.find(MacroName);
645 if (!Validate || Known == ASTFileMacros.end()) {
646 // FIXME: Check whether this identifier was referenced anywhere in the
647 // AST file. If so, we should reject the AST file. Unfortunately, this
648 // information isn't in the control block. What shall we do about it?
650 if (Existing.second) {
651 SuggestedPredefines += "#undef ";
652 SuggestedPredefines += MacroName.str();
653 SuggestedPredefines += '\n';
655 SuggestedPredefines += "#define ";
656 SuggestedPredefines += MacroName.str();
657 SuggestedPredefines += ' ';
658 SuggestedPredefines += Existing.first.str();
659 SuggestedPredefines += '\n';
664 // If the macro was defined in one but undef'd in the other, we have a
666 if (Existing.second != Known->second.second) {
668 Diags->Report(diag::err_pch_macro_def_undef)
669 << MacroName << Known->second.second;
674 // If the macro was #undef'd in both, or if the macro bodies are identical,
676 if (Existing.second || Existing.first == Known->second.first)
679 // The macro bodies differ; complain.
681 Diags->Report(diag::err_pch_macro_def_conflict)
682 << MacroName << Known->second.first << Existing.first;
687 // Check whether we're using predefines.
688 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
690 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
695 // Detailed record is important since it is used for the module cache hash.
696 if (LangOpts.Modules &&
697 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
699 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
704 // Compute the #include and #include_macros lines we need.
705 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706 StringRef File = ExistingPPOpts.Includes[I];
708 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
709 !ExistingPPOpts.PCHThroughHeader.empty()) {
710 // In case the through header is an include, we must add all the includes
711 // to the predefines so the start point can be determined.
712 SuggestedPredefines += "#include \"";
713 SuggestedPredefines += File;
714 SuggestedPredefines += "\"\n";
718 if (File == ExistingPPOpts.ImplicitPCHInclude)
721 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
722 != PPOpts.Includes.end())
725 SuggestedPredefines += "#include \"";
726 SuggestedPredefines += File;
727 SuggestedPredefines += "\"\n";
730 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
731 StringRef File = ExistingPPOpts.MacroIncludes[I];
732 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
734 != PPOpts.MacroIncludes.end())
737 SuggestedPredefines += "#__include_macros \"";
738 SuggestedPredefines += File;
739 SuggestedPredefines += "\"\n##\n";
745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
747 std::string &SuggestedPredefines) {
748 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
750 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
751 Complain? &Reader.Diags : nullptr,
757 bool SimpleASTReaderListener::ReadPreprocessorOptions(
758 const PreprocessorOptions &PPOpts,
760 std::string &SuggestedPredefines) {
761 return checkPreprocessorOptions(PPOpts,
762 PP.getPreprocessorOpts(),
770 /// Check the header search options deserialized from the control block
771 /// against the header search options in an existing preprocessor.
773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
775 StringRef SpecificModuleCachePath,
776 StringRef ExistingModuleCachePath,
777 DiagnosticsEngine *Diags,
778 const LangOptions &LangOpts) {
779 if (LangOpts.Modules) {
780 if (SpecificModuleCachePath != ExistingModuleCachePath) {
782 Diags->Report(diag::err_pch_modulecache_mismatch)
783 << SpecificModuleCachePath << ExistingModuleCachePath;
791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792 StringRef SpecificModuleCachePath,
794 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
795 PP.getHeaderSearchInfo().getModuleCachePath(),
796 Complain ? &Reader.Diags : nullptr,
800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
801 PP.setCounterValue(Value);
804 //===----------------------------------------------------------------------===//
805 // AST reader implementation
806 //===----------------------------------------------------------------------===//
808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
809 bool TakeOwnership) {
810 DeserializationListener = Listener;
811 OwnsDeserializationListener = TakeOwnership;
814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
815 return serialization::ComputeHash(Sel);
818 std::pair<unsigned, unsigned>
819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
820 using namespace llvm::support;
822 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
823 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
824 return std::make_pair(KeyLen, DataLen);
827 ASTSelectorLookupTrait::internal_key_type
828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
829 using namespace llvm::support;
831 SelectorTable &SelTable = Reader.getContext().Selectors;
832 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
833 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
834 F, endian::readNext<uint32_t, little, unaligned>(d));
836 return SelTable.getNullarySelector(FirstII);
838 return SelTable.getUnarySelector(FirstII);
840 SmallVector<IdentifierInfo *, 16> Args;
841 Args.push_back(FirstII);
842 for (unsigned I = 1; I != N; ++I)
843 Args.push_back(Reader.getLocalIdentifier(
844 F, endian::readNext<uint32_t, little, unaligned>(d)));
846 return SelTable.getSelector(N, Args.data());
849 ASTSelectorLookupTrait::data_type
850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
852 using namespace llvm::support;
856 Result.ID = Reader.getGlobalSelectorID(
857 F, endian::readNext<uint32_t, little, unaligned>(d));
858 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
859 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
860 Result.InstanceBits = FullInstanceBits & 0x3;
861 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
862 Result.FactoryBits = FullFactoryBits & 0x3;
863 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
864 unsigned NumInstanceMethods = FullInstanceBits >> 3;
865 unsigned NumFactoryMethods = FullFactoryBits >> 3;
867 // Load instance methods
868 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
869 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
870 F, endian::readNext<uint32_t, little, unaligned>(d)))
871 Result.Instance.push_back(Method);
874 // Load factory methods
875 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877 F, endian::readNext<uint32_t, little, unaligned>(d)))
878 Result.Factory.push_back(Method);
884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
885 return llvm::djbHash(a);
888 std::pair<unsigned, unsigned>
889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
890 using namespace llvm::support;
892 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
893 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
894 return std::make_pair(KeyLen, DataLen);
897 ASTIdentifierLookupTraitBase::internal_key_type
898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
899 assert(n >= 2 && d[n-1] == '\0');
900 return StringRef((const char*) d, n-1);
903 /// Whether the given identifier is "interesting".
904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
906 return II.hadMacroDefinition() ||
908 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
909 II.hasRevertedTokenIDToIdentifier() ||
910 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
911 II.getFETokenInfo());
914 static bool readBit(unsigned &Bits) {
915 bool Value = Bits & 0x1;
920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
921 using namespace llvm::support;
923 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
924 return Reader.getGlobalIdentifierID(F, RawID >> 1);
927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
928 if (!II.isFromAST()) {
930 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
931 if (isInterestingIdentifier(Reader, II, IsModule))
932 II.setChangedSinceDeserialization();
936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
937 const unsigned char* d,
939 using namespace llvm::support;
941 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
942 bool IsInteresting = RawID & 0x01;
944 // Wipe out the "is interesting" bit.
947 // Build the IdentifierInfo and link the identifier ID with it.
948 IdentifierInfo *II = KnownII;
950 II = &Reader.getIdentifierTable().getOwn(k);
953 markIdentifierFromAST(Reader, *II);
954 Reader.markIdentifierUpToDate(II);
956 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
957 if (!IsInteresting) {
958 // For uninteresting identifiers, there's nothing else to do. Just notify
959 // the reader that we've finished loading this identifier.
960 Reader.SetIdentifierInfo(ID, II);
964 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
965 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
966 bool CPlusPlusOperatorKeyword = readBit(Bits);
967 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
968 bool HasRevertedBuiltin = readBit(Bits);
969 bool Poisoned = readBit(Bits);
970 bool ExtensionToken = readBit(Bits);
971 bool HadMacroDefinition = readBit(Bits);
973 assert(Bits == 0 && "Extra bits in the identifier?");
976 // Set or check the various bits in the IdentifierInfo structure.
977 // Token IDs are read-only.
978 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
979 II->revertTokenIDToIdentifier();
981 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
982 else if (HasRevertedBuiltin && II->getBuiltinID()) {
984 assert((II->hasRevertedBuiltin() ||
985 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
986 "Incorrect ObjC keyword or builtin ID");
988 assert(II->isExtensionToken() == ExtensionToken &&
989 "Incorrect extension token flag");
990 (void)ExtensionToken;
992 II->setIsPoisoned(true);
993 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
994 "Incorrect C++ operator keyword flag");
995 (void)CPlusPlusOperatorKeyword;
997 // If this identifier is a macro, deserialize the macro
999 if (HadMacroDefinition) {
1000 uint32_t MacroDirectivesOffset =
1001 endian::readNext<uint32_t, little, unaligned>(d);
1004 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1007 Reader.SetIdentifierInfo(ID, II);
1009 // Read all of the declarations visible at global scope with this
1012 SmallVector<uint32_t, 4> DeclIDs;
1013 for (; DataLen > 0; DataLen -= 4)
1014 DeclIDs.push_back(Reader.getGlobalDeclID(
1015 F, endian::readNext<uint32_t, little, unaligned>(d)));
1016 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1023 : Kind(Name.getNameKind()) {
1025 case DeclarationName::Identifier:
1026 Data = (uint64_t)Name.getAsIdentifierInfo();
1028 case DeclarationName::ObjCZeroArgSelector:
1029 case DeclarationName::ObjCOneArgSelector:
1030 case DeclarationName::ObjCMultiArgSelector:
1031 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1033 case DeclarationName::CXXOperatorName:
1034 Data = Name.getCXXOverloadedOperator();
1036 case DeclarationName::CXXLiteralOperatorName:
1037 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1039 case DeclarationName::CXXDeductionGuideName:
1040 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1041 ->getDeclName().getAsIdentifierInfo();
1043 case DeclarationName::CXXConstructorName:
1044 case DeclarationName::CXXDestructorName:
1045 case DeclarationName::CXXConversionFunctionName:
1046 case DeclarationName::CXXUsingDirective:
1052 unsigned DeclarationNameKey::getHash() const {
1053 llvm::FoldingSetNodeID ID;
1054 ID.AddInteger(Kind);
1057 case DeclarationName::Identifier:
1058 case DeclarationName::CXXLiteralOperatorName:
1059 case DeclarationName::CXXDeductionGuideName:
1060 ID.AddString(((IdentifierInfo*)Data)->getName());
1062 case DeclarationName::ObjCZeroArgSelector:
1063 case DeclarationName::ObjCOneArgSelector:
1064 case DeclarationName::ObjCMultiArgSelector:
1065 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1067 case DeclarationName::CXXOperatorName:
1068 ID.AddInteger((OverloadedOperatorKind)Data);
1070 case DeclarationName::CXXConstructorName:
1071 case DeclarationName::CXXDestructorName:
1072 case DeclarationName::CXXConversionFunctionName:
1073 case DeclarationName::CXXUsingDirective:
1077 return ID.ComputeHash();
1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1082 using namespace llvm::support;
1084 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1085 return Reader.getLocalModuleFile(F, ModuleFileID);
1088 std::pair<unsigned, unsigned>
1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1090 using namespace llvm::support;
1092 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1093 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1094 return std::make_pair(KeyLen, DataLen);
1097 ASTDeclContextNameLookupTrait::internal_key_type
1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1099 using namespace llvm::support;
1101 auto Kind = (DeclarationName::NameKind)*d++;
1104 case DeclarationName::Identifier:
1105 case DeclarationName::CXXLiteralOperatorName:
1106 case DeclarationName::CXXDeductionGuideName:
1107 Data = (uint64_t)Reader.getLocalIdentifier(
1108 F, endian::readNext<uint32_t, little, unaligned>(d));
1110 case DeclarationName::ObjCZeroArgSelector:
1111 case DeclarationName::ObjCOneArgSelector:
1112 case DeclarationName::ObjCMultiArgSelector:
1114 (uint64_t)Reader.getLocalSelector(
1115 F, endian::readNext<uint32_t, little, unaligned>(
1116 d)).getAsOpaquePtr();
1118 case DeclarationName::CXXOperatorName:
1119 Data = *d++; // OverloadedOperatorKind
1121 case DeclarationName::CXXConstructorName:
1122 case DeclarationName::CXXDestructorName:
1123 case DeclarationName::CXXConversionFunctionName:
1124 case DeclarationName::CXXUsingDirective:
1129 return DeclarationNameKey(Kind, Data);
1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1133 const unsigned char *d,
1135 data_type_builder &Val) {
1136 using namespace llvm::support;
1138 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1139 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1140 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1145 BitstreamCursor &Cursor,
1148 assert(Offset != 0);
1150 SavedStreamPosition SavedPosition(Cursor);
1151 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1152 Error(std::move(Err));
1158 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1160 Error(MaybeCode.takeError());
1163 unsigned Code = MaybeCode.get();
1165 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1166 if (!MaybeRecCode) {
1167 Error(MaybeRecCode.takeError());
1170 unsigned RecCode = MaybeRecCode.get();
1171 if (RecCode != DECL_CONTEXT_LEXICAL) {
1172 Error("Expected lexical block");
1176 assert(!isa<TranslationUnitDecl>(DC) &&
1177 "expected a TU_UPDATE_LEXICAL record for TU");
1178 // If we are handling a C++ class template instantiation, we can see multiple
1179 // lexical updates for the same record. It's important that we select only one
1180 // of them, so that field numbering works properly. Just pick the first one we
1182 auto &Lex = LexicalDecls[DC];
1184 Lex = std::make_pair(
1185 &M, llvm::makeArrayRef(
1186 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1190 DC->setHasExternalLexicalStorage(true);
1194 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1195 BitstreamCursor &Cursor,
1198 assert(Offset != 0);
1200 SavedStreamPosition SavedPosition(Cursor);
1201 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1202 Error(std::move(Err));
1208 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1210 Error(MaybeCode.takeError());
1213 unsigned Code = MaybeCode.get();
1215 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1216 if (!MaybeRecCode) {
1217 Error(MaybeRecCode.takeError());
1220 unsigned RecCode = MaybeRecCode.get();
1221 if (RecCode != DECL_CONTEXT_VISIBLE) {
1222 Error("Expected visible lookup table block");
1226 // We can't safely determine the primary context yet, so delay attaching the
1227 // lookup table until we're done with recursive deserialization.
1228 auto *Data = (const unsigned char*)Blob.data();
1229 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1233 void ASTReader::Error(StringRef Msg) const {
1234 Error(diag::err_fe_pch_malformed, Msg);
1235 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1236 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1237 Diag(diag::note_module_cache_path)
1238 << PP.getHeaderSearchInfo().getModuleCachePath();
1242 void ASTReader::Error(unsigned DiagID,
1243 StringRef Arg1, StringRef Arg2) const {
1244 if (Diags.isDiagnosticInFlight())
1245 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1247 Diag(DiagID) << Arg1 << Arg2;
1250 void ASTReader::Error(llvm::Error &&Err) const {
1251 Error(toString(std::move(Err)));
1254 //===----------------------------------------------------------------------===//
1255 // Source Manager Deserialization
1256 //===----------------------------------------------------------------------===//
1258 /// Read the line table in the source manager block.
1259 /// \returns true if there was an error.
1260 bool ASTReader::ParseLineTable(ModuleFile &F,
1261 const RecordData &Record) {
1263 LineTableInfo &LineTable = SourceMgr.getLineTable();
1265 // Parse the file names
1266 std::map<int, int> FileIDs;
1267 FileIDs[-1] = -1; // For unspecified filenames.
1268 for (unsigned I = 0; Record[Idx]; ++I) {
1269 // Extract the file name
1270 auto Filename = ReadPath(F, Record, Idx);
1271 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1275 // Parse the line entries
1276 std::vector<LineEntry> Entries;
1277 while (Idx < Record.size()) {
1278 int FID = Record[Idx++];
1279 assert(FID >= 0 && "Serialized line entries for non-local file.");
1280 // Remap FileID from 1-based old view.
1281 FID += F.SLocEntryBaseID - 1;
1283 // Extract the line entries
1284 unsigned NumEntries = Record[Idx++];
1285 assert(NumEntries && "no line entries for file ID");
1287 Entries.reserve(NumEntries);
1288 for (unsigned I = 0; I != NumEntries; ++I) {
1289 unsigned FileOffset = Record[Idx++];
1290 unsigned LineNo = Record[Idx++];
1291 int FilenameID = FileIDs[Record[Idx++]];
1292 SrcMgr::CharacteristicKind FileKind
1293 = (SrcMgr::CharacteristicKind)Record[Idx++];
1294 unsigned IncludeOffset = Record[Idx++];
1295 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1296 FileKind, IncludeOffset));
1298 LineTable.AddEntry(FileID::get(FID), Entries);
1304 /// Read a source manager block
1305 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1306 using namespace SrcMgr;
1308 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1310 // Set the source-location entry cursor to the current position in
1311 // the stream. This cursor will be used to read the contents of the
1312 // source manager block initially, and then lazily read
1313 // source-location entries as needed.
1314 SLocEntryCursor = F.Stream;
1316 // The stream itself is going to skip over the source manager block.
1317 if (llvm::Error Err = F.Stream.SkipBlock()) {
1318 Error(std::move(Err));
1322 // Enter the source manager block.
1323 if (llvm::Error Err =
1324 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1325 Error(std::move(Err));
1331 Expected<llvm::BitstreamEntry> MaybeE =
1332 SLocEntryCursor.advanceSkippingSubblocks();
1334 Error(MaybeE.takeError());
1337 llvm::BitstreamEntry E = MaybeE.get();
1340 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1341 case llvm::BitstreamEntry::Error:
1342 Error("malformed block record in AST file");
1344 case llvm::BitstreamEntry::EndBlock:
1346 case llvm::BitstreamEntry::Record:
1347 // The interesting case.
1354 Expected<unsigned> MaybeRecord =
1355 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1357 Error(MaybeRecord.takeError());
1360 switch (MaybeRecord.get()) {
1361 default: // Default behavior: ignore.
1364 case SM_SLOC_FILE_ENTRY:
1365 case SM_SLOC_BUFFER_ENTRY:
1366 case SM_SLOC_EXPANSION_ENTRY:
1367 // Once we hit one of the source location entries, we're done.
1373 /// If a header file is not found at the path that we expect it to be
1374 /// and the PCH file was moved from its original location, try to resolve the
1375 /// file by assuming that header+PCH were moved together and the header is in
1376 /// the same place relative to the PCH.
1378 resolveFileRelativeToOriginalDir(const std::string &Filename,
1379 const std::string &OriginalDir,
1380 const std::string &CurrDir) {
1381 assert(OriginalDir != CurrDir &&
1382 "No point trying to resolve the file if the PCH dir didn't change");
1384 using namespace llvm::sys;
1386 SmallString<128> filePath(Filename);
1387 fs::make_absolute(filePath);
1388 assert(path::is_absolute(OriginalDir));
1389 SmallString<128> currPCHPath(CurrDir);
1391 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1392 fileDirE = path::end(path::parent_path(filePath));
1393 path::const_iterator origDirI = path::begin(OriginalDir),
1394 origDirE = path::end(OriginalDir);
1395 // Skip the common path components from filePath and OriginalDir.
1396 while (fileDirI != fileDirE && origDirI != origDirE &&
1397 *fileDirI == *origDirI) {
1401 for (; origDirI != origDirE; ++origDirI)
1402 path::append(currPCHPath, "..");
1403 path::append(currPCHPath, fileDirI, fileDirE);
1404 path::append(currPCHPath, path::filename(Filename));
1405 return currPCHPath.str();
1408 bool ASTReader::ReadSLocEntry(int ID) {
1412 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1413 Error("source location entry ID out-of-range for AST file");
1417 // Local helper to read the (possibly-compressed) buffer data following the
1419 auto ReadBuffer = [this](
1420 BitstreamCursor &SLocEntryCursor,
1421 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1424 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1426 Error(MaybeCode.takeError());
1429 unsigned Code = MaybeCode.get();
1431 Expected<unsigned> MaybeRecCode =
1432 SLocEntryCursor.readRecord(Code, Record, &Blob);
1433 if (!MaybeRecCode) {
1434 Error(MaybeRecCode.takeError());
1437 unsigned RecCode = MaybeRecCode.get();
1439 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1440 if (!llvm::zlib::isAvailable()) {
1441 Error("zlib is not available");
1444 SmallString<0> Uncompressed;
1446 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1447 Error("could not decompress embedded file contents: " +
1448 llvm::toString(std::move(E)));
1451 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1452 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1453 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1455 Error("AST record has invalid code");
1460 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1461 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1462 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1463 Error(std::move(Err));
1467 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1468 unsigned BaseOffset = F->SLocEntryBaseOffset;
1470 ++NumSLocEntriesRead;
1471 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1473 Error(MaybeEntry.takeError());
1476 llvm::BitstreamEntry Entry = MaybeEntry.get();
1478 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1479 Error("incorrectly-formatted source location entry in AST file");
1485 Expected<unsigned> MaybeSLOC =
1486 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1488 Error(MaybeSLOC.takeError());
1491 switch (MaybeSLOC.get()) {
1493 Error("incorrectly-formatted source location entry in AST file");
1496 case SM_SLOC_FILE_ENTRY: {
1497 // We will detect whether a file changed and return 'Failure' for it, but
1498 // we will also try to fail gracefully by setting up the SLocEntry.
1499 unsigned InputID = Record[4];
1500 InputFile IF = getInputFile(*F, InputID);
1501 const FileEntry *File = IF.getFile();
1502 bool OverriddenBuffer = IF.isOverridden();
1504 // Note that we only check if a File was returned. If it was out-of-date
1505 // we have complained but we will continue creating a FileID to recover
1510 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1511 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1512 // This is the module's main file.
1513 IncludeLoc = getImportLocation(F);
1515 SrcMgr::CharacteristicKind
1516 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1517 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1518 ID, BaseOffset + Record[0]);
1519 SrcMgr::FileInfo &FileInfo =
1520 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1521 FileInfo.NumCreatedFIDs = Record[5];
1523 FileInfo.setHasLineDirectives();
1525 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1526 unsigned NumFileDecls = Record[7];
1527 if (NumFileDecls && ContextObj) {
1528 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1529 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1533 const SrcMgr::ContentCache *ContentCache
1534 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1535 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1536 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1537 !ContentCache->getRawBuffer()) {
1538 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1541 SourceMgr.overrideFileContents(File, std::move(Buffer));
1547 case SM_SLOC_BUFFER_ENTRY: {
1548 const char *Name = Blob.data();
1549 unsigned Offset = Record[0];
1550 SrcMgr::CharacteristicKind
1551 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1552 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1553 if (IncludeLoc.isInvalid() && F->isModule()) {
1554 IncludeLoc = getImportLocation(F);
1557 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1560 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1561 BaseOffset + Offset, IncludeLoc);
1565 case SM_SLOC_EXPANSION_ENTRY: {
1566 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1567 SourceMgr.createExpansionLoc(SpellingLoc,
1568 ReadSourceLocation(*F, Record[2]),
1569 ReadSourceLocation(*F, Record[3]),
1573 BaseOffset + Record[0]);
1581 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1583 return std::make_pair(SourceLocation(), "");
1585 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1586 Error("source location entry ID out-of-range for AST file");
1587 return std::make_pair(SourceLocation(), "");
1590 // Find which module file this entry lands in.
1591 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1593 return std::make_pair(SourceLocation(), "");
1595 // FIXME: Can we map this down to a particular submodule? That would be
1597 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1600 /// Find the location where the module F is imported.
1601 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1602 if (F->ImportLoc.isValid())
1603 return F->ImportLoc;
1605 // Otherwise we have a PCH. It's considered to be "imported" at the first
1606 // location of its includer.
1607 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1608 // Main file is the importer.
1609 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1610 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1612 return F->ImportedBy[0]->FirstLoc;
1615 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1616 /// the abbreviations that are at the top of the block and then leave the cursor
1617 /// pointing into the block.
1618 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1619 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1620 // FIXME this drops errors on the floor.
1621 consumeError(std::move(Err));
1626 uint64_t Offset = Cursor.GetCurrentBitNo();
1627 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1629 // FIXME this drops errors on the floor.
1630 consumeError(MaybeCode.takeError());
1633 unsigned Code = MaybeCode.get();
1635 // We expect all abbrevs to be at the start of the block.
1636 if (Code != llvm::bitc::DEFINE_ABBREV) {
1637 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1638 // FIXME this drops errors on the floor.
1639 consumeError(std::move(Err));
1644 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1645 // FIXME this drops errors on the floor.
1646 consumeError(std::move(Err));
1652 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1656 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1657 Tok.setLength(Record[Idx++]);
1658 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1659 Tok.setIdentifierInfo(II);
1660 Tok.setKind((tok::TokenKind)Record[Idx++]);
1661 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1665 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1666 BitstreamCursor &Stream = F.MacroCursor;
1668 // Keep track of where we are in the stream, then jump back there
1669 // after reading this macro.
1670 SavedStreamPosition SavedPosition(Stream);
1672 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1673 // FIXME this drops errors on the floor.
1674 consumeError(std::move(Err));
1678 SmallVector<IdentifierInfo*, 16> MacroParams;
1679 MacroInfo *Macro = nullptr;
1682 // Advance to the next record, but if we get to the end of the block, don't
1683 // pop it (removing all the abbreviations from the cursor) since we want to
1684 // be able to reseek within the block and read entries.
1685 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1686 Expected<llvm::BitstreamEntry> MaybeEntry =
1687 Stream.advanceSkippingSubblocks(Flags);
1689 Error(MaybeEntry.takeError());
1692 llvm::BitstreamEntry Entry = MaybeEntry.get();
1694 switch (Entry.Kind) {
1695 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1696 case llvm::BitstreamEntry::Error:
1697 Error("malformed block record in AST file");
1699 case llvm::BitstreamEntry::EndBlock:
1701 case llvm::BitstreamEntry::Record:
1702 // The interesting case.
1708 PreprocessorRecordTypes RecType;
1709 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1710 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1712 Error(MaybeRecType.takeError());
1716 case PP_MODULE_MACRO:
1717 case PP_MACRO_DIRECTIVE_HISTORY:
1720 case PP_MACRO_OBJECT_LIKE:
1721 case PP_MACRO_FUNCTION_LIKE: {
1722 // If we already have a macro, that means that we've hit the end
1723 // of the definition of the macro we were looking for. We're
1728 unsigned NextIndex = 1; // Skip identifier ID.
1729 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1730 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1731 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1732 MI->setIsUsed(Record[NextIndex++]);
1733 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1735 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1736 // Decode function-like macro info.
1737 bool isC99VarArgs = Record[NextIndex++];
1738 bool isGNUVarArgs = Record[NextIndex++];
1739 bool hasCommaPasting = Record[NextIndex++];
1740 MacroParams.clear();
1741 unsigned NumArgs = Record[NextIndex++];
1742 for (unsigned i = 0; i != NumArgs; ++i)
1743 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1745 // Install function-like macro info.
1746 MI->setIsFunctionLike();
1747 if (isC99VarArgs) MI->setIsC99Varargs();
1748 if (isGNUVarArgs) MI->setIsGNUVarargs();
1749 if (hasCommaPasting) MI->setHasCommaPasting();
1750 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1753 // Remember that we saw this macro last so that we add the tokens that
1754 // form its body to it.
1757 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1758 Record[NextIndex]) {
1759 // We have a macro definition. Register the association
1760 PreprocessedEntityID
1761 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1762 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1763 PreprocessingRecord::PPEntityID PPID =
1764 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1765 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1766 PPRec.getPreprocessedEntity(PPID));
1768 PPRec.RegisterMacroDefinition(Macro, PPDef);
1776 // If we see a TOKEN before a PP_MACRO_*, then the file is
1777 // erroneous, just pretend we didn't see this.
1781 Token Tok = ReadToken(F, Record, Idx);
1782 Macro->AddTokenToBody(Tok);
1789 PreprocessedEntityID
1790 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1791 unsigned LocalID) const {
1792 if (!M.ModuleOffsetMap.empty())
1793 ReadModuleOffsetMap(M);
1795 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1796 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1797 assert(I != M.PreprocessedEntityRemap.end()
1798 && "Invalid index into preprocessed entity index remap");
1800 return LocalID + I->second;
1803 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1804 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1807 HeaderFileInfoTrait::internal_key_type
1808 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1809 internal_key_type ikey = {FE->getSize(),
1810 M.HasTimestamps ? FE->getModificationTime() : 0,
1811 FE->getName(), /*Imported*/ false};
1815 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1816 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1819 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1822 // Determine whether the actual files are equivalent.
1823 FileManager &FileMgr = Reader.getFileManager();
1824 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1826 return FileMgr.getFile(Key.Filename);
1828 std::string Resolved = Key.Filename;
1829 Reader.ResolveImportedPath(M, Resolved);
1830 return FileMgr.getFile(Resolved);
1833 const FileEntry *FEA = GetFile(a);
1834 const FileEntry *FEB = GetFile(b);
1835 return FEA && FEA == FEB;
1838 std::pair<unsigned, unsigned>
1839 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1840 using namespace llvm::support;
1842 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1843 unsigned DataLen = (unsigned) *d++;
1844 return std::make_pair(KeyLen, DataLen);
1847 HeaderFileInfoTrait::internal_key_type
1848 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1849 using namespace llvm::support;
1851 internal_key_type ikey;
1852 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1853 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1854 ikey.Filename = (const char *)d;
1855 ikey.Imported = true;
1859 HeaderFileInfoTrait::data_type
1860 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1862 using namespace llvm::support;
1864 const unsigned char *End = d + DataLen;
1866 unsigned Flags = *d++;
1867 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1868 HFI.isImport |= (Flags >> 5) & 0x01;
1869 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1870 HFI.DirInfo = (Flags >> 1) & 0x07;
1871 HFI.IndexHeaderMapHeader = Flags & 0x01;
1872 // FIXME: Find a better way to handle this. Maybe just store a
1873 // "has been included" flag?
1874 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1876 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1877 M, endian::readNext<uint32_t, little, unaligned>(d));
1878 if (unsigned FrameworkOffset =
1879 endian::readNext<uint32_t, little, unaligned>(d)) {
1880 // The framework offset is 1 greater than the actual offset,
1881 // since 0 is used as an indicator for "no framework name".
1882 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1883 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1886 assert((End - d) % 4 == 0 &&
1887 "Wrong data length in HeaderFileInfo deserialization");
1889 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1890 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1893 // This header is part of a module. Associate it with the module to enable
1894 // implicit module import.
1895 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1896 Module *Mod = Reader.getSubmodule(GlobalSMID);
1897 FileManager &FileMgr = Reader.getFileManager();
1899 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1901 std::string Filename = key.Filename;
1903 Reader.ResolveImportedPath(M, Filename);
1904 // FIXME: This is not always the right filename-as-written, but we're not
1905 // going to use this information to rebuild the module, so it doesn't make
1906 // a lot of difference.
1907 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1908 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1909 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1912 // This HeaderFileInfo was externally loaded.
1913 HFI.External = true;
1918 void ASTReader::addPendingMacro(IdentifierInfo *II,
1920 uint64_t MacroDirectivesOffset) {
1921 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1922 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1925 void ASTReader::ReadDefinedMacros() {
1926 // Note that we are loading defined macros.
1927 Deserializing Macros(this);
1929 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1930 BitstreamCursor &MacroCursor = I.MacroCursor;
1932 // If there was no preprocessor block, skip this file.
1933 if (MacroCursor.getBitcodeBytes().empty())
1936 BitstreamCursor Cursor = MacroCursor;
1937 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1938 Error(std::move(Err));
1944 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1946 Error(MaybeE.takeError());
1949 llvm::BitstreamEntry E = MaybeE.get();
1952 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1953 case llvm::BitstreamEntry::Error:
1954 Error("malformed block record in AST file");
1956 case llvm::BitstreamEntry::EndBlock:
1959 case llvm::BitstreamEntry::Record: {
1961 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1963 Error(MaybeRecord.takeError());
1966 switch (MaybeRecord.get()) {
1967 default: // Default behavior: ignore.
1970 case PP_MACRO_OBJECT_LIKE:
1971 case PP_MACRO_FUNCTION_LIKE: {
1972 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1973 if (II->isOutOfDate())
1974 updateOutOfDateIdentifier(*II);
1992 /// Visitor class used to look up identifirs in an AST file.
1993 class IdentifierLookupVisitor {
1996 unsigned PriorGeneration;
1997 unsigned &NumIdentifierLookups;
1998 unsigned &NumIdentifierLookupHits;
1999 IdentifierInfo *Found = nullptr;
2002 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2003 unsigned &NumIdentifierLookups,
2004 unsigned &NumIdentifierLookupHits)
2005 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2006 PriorGeneration(PriorGeneration),
2007 NumIdentifierLookups(NumIdentifierLookups),
2008 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2010 bool operator()(ModuleFile &M) {
2011 // If we've already searched this module file, skip it now.
2012 if (M.Generation <= PriorGeneration)
2015 ASTIdentifierLookupTable *IdTable
2016 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2020 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2022 ++NumIdentifierLookups;
2023 ASTIdentifierLookupTable::iterator Pos =
2024 IdTable->find_hashed(Name, NameHash, &Trait);
2025 if (Pos == IdTable->end())
2028 // Dereferencing the iterator has the effect of building the
2029 // IdentifierInfo node and populating it with the various
2030 // declarations it needs.
2031 ++NumIdentifierLookupHits;
2036 // Retrieve the identifier info found within the module
2038 IdentifierInfo *getIdentifierInfo() const { return Found; }
2043 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2044 // Note that we are loading an identifier.
2045 Deserializing AnIdentifier(this);
2047 unsigned PriorGeneration = 0;
2048 if (getContext().getLangOpts().Modules)
2049 PriorGeneration = IdentifierGeneration[&II];
2051 // If there is a global index, look there first to determine which modules
2052 // provably do not have any results for this identifier.
2053 GlobalModuleIndex::HitSet Hits;
2054 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2055 if (!loadGlobalIndex()) {
2056 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2061 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2062 NumIdentifierLookups,
2063 NumIdentifierLookupHits);
2064 ModuleMgr.visit(Visitor, HitsPtr);
2065 markIdentifierUpToDate(&II);
2068 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2072 II->setOutOfDate(false);
2074 // Update the generation for this identifier.
2075 if (getContext().getLangOpts().Modules)
2076 IdentifierGeneration[II] = getGeneration();
2079 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2080 const PendingMacroInfo &PMInfo) {
2081 ModuleFile &M = *PMInfo.M;
2083 BitstreamCursor &Cursor = M.MacroCursor;
2084 SavedStreamPosition SavedPosition(Cursor);
2085 if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2086 Error(std::move(Err));
2090 struct ModuleMacroRecord {
2091 SubmoduleID SubModID;
2093 SmallVector<SubmoduleID, 8> Overrides;
2095 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2097 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2098 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2102 Expected<llvm::BitstreamEntry> MaybeEntry =
2103 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2105 Error(MaybeEntry.takeError());
2108 llvm::BitstreamEntry Entry = MaybeEntry.get();
2110 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2111 Error("malformed block record in AST file");
2116 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2118 Error(MaybePP.takeError());
2121 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2122 case PP_MACRO_DIRECTIVE_HISTORY:
2125 case PP_MODULE_MACRO: {
2126 ModuleMacros.push_back(ModuleMacroRecord());
2127 auto &Info = ModuleMacros.back();
2128 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2129 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2130 for (int I = 2, N = Record.size(); I != N; ++I)
2131 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2136 Error("malformed block record in AST file");
2140 // We found the macro directive history; that's the last record
2145 // Module macros are listed in reverse dependency order.
2147 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2148 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2149 for (auto &MMR : ModuleMacros) {
2151 for (unsigned ModID : MMR.Overrides) {
2152 Module *Mod = getSubmodule(ModID);
2153 auto *Macro = PP.getModuleMacro(Mod, II);
2154 assert(Macro && "missing definition for overridden macro");
2155 Overrides.push_back(Macro);
2158 bool Inserted = false;
2159 Module *Owner = getSubmodule(MMR.SubModID);
2160 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2164 // Don't read the directive history for a module; we don't have anywhere
2169 // Deserialize the macro directives history in reverse source-order.
2170 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2171 unsigned Idx = 0, N = Record.size();
2173 MacroDirective *MD = nullptr;
2174 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2175 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2177 case MacroDirective::MD_Define: {
2178 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2179 MD = PP.AllocateDefMacroDirective(MI, Loc);
2182 case MacroDirective::MD_Undefine:
2183 MD = PP.AllocateUndefMacroDirective(Loc);
2185 case MacroDirective::MD_Visibility:
2186 bool isPublic = Record[Idx++];
2187 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2194 Earliest->setPrevious(MD);
2199 PP.setLoadedMacroDirective(II, Earliest, Latest);
2202 ASTReader::InputFileInfo
2203 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2204 // Go find this input file.
2205 BitstreamCursor &Cursor = F.InputFilesCursor;
2206 SavedStreamPosition SavedPosition(Cursor);
2207 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2208 // FIXME this drops errors on the floor.
2209 consumeError(std::move(Err));
2212 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2214 // FIXME this drops errors on the floor.
2215 consumeError(MaybeCode.takeError());
2217 unsigned Code = MaybeCode.get();
2221 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2222 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2223 "invalid record type for input file");
2225 // FIXME this drops errors on the floor.
2226 consumeError(Maybe.takeError());
2229 assert(Record[0] == ID && "Bogus stored ID or offset");
2231 R.StoredSize = static_cast<off_t>(Record[1]);
2232 R.StoredTime = static_cast<time_t>(Record[2]);
2233 R.Overridden = static_cast<bool>(Record[3]);
2234 R.Transient = static_cast<bool>(Record[4]);
2235 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2237 ResolveImportedPath(F, R.Filename);
2241 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2242 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2243 // If this ID is bogus, just return an empty input file.
2244 if (ID == 0 || ID > F.InputFilesLoaded.size())
2247 // If we've already loaded this input file, return it.
2248 if (F.InputFilesLoaded[ID-1].getFile())
2249 return F.InputFilesLoaded[ID-1];
2251 if (F.InputFilesLoaded[ID-1].isNotFound())
2254 // Go find this input file.
2255 BitstreamCursor &Cursor = F.InputFilesCursor;
2256 SavedStreamPosition SavedPosition(Cursor);
2257 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2258 // FIXME this drops errors on the floor.
2259 consumeError(std::move(Err));
2262 InputFileInfo FI = readInputFileInfo(F, ID);
2263 off_t StoredSize = FI.StoredSize;
2264 time_t StoredTime = FI.StoredTime;
2265 bool Overridden = FI.Overridden;
2266 bool Transient = FI.Transient;
2267 StringRef Filename = FI.Filename;
2269 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
2270 // If we didn't find the file, resolve it relative to the
2271 // original directory from which this AST file was created.
2272 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2273 F.OriginalDir != F.BaseDirectory) {
2274 std::string Resolved = resolveFileRelativeToOriginalDir(
2275 Filename, F.OriginalDir, F.BaseDirectory);
2276 if (!Resolved.empty())
2277 File = FileMgr.getFile(Resolved);
2280 // For an overridden file, create a virtual file with the stored
2282 if ((Overridden || Transient) && File == nullptr)
2283 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2285 if (File == nullptr) {
2287 std::string ErrorStr = "could not find file '";
2288 ErrorStr += Filename;
2289 ErrorStr += "' referenced by AST file '";
2290 ErrorStr += F.FileName;
2294 // Record that we didn't find the file.
2295 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2299 // Check if there was a request to override the contents of the file
2300 // that was part of the precompiled header. Overriding such a file
2301 // can lead to problems when lexing using the source locations from the
2303 SourceManager &SM = getSourceManager();
2304 // FIXME: Reject if the overrides are different.
2305 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2307 Error(diag::err_fe_pch_file_overridden, Filename);
2308 // After emitting the diagnostic, recover by disabling the override so
2309 // that the original file will be used.
2311 // FIXME: This recovery is just as broken as the original state; there may
2312 // be another precompiled module that's using the overridden contents, or
2313 // we might be half way through parsing it. Instead, we should treat the
2314 // overridden contents as belonging to a separate FileEntry.
2315 SM.disableFileContentsOverride(File);
2316 // The FileEntry is a virtual file entry with the size of the contents
2317 // that would override the original contents. Set it to the original's
2319 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2320 StoredSize, StoredTime);
2323 bool IsOutOfDate = false;
2325 // For an overridden file, there is nothing to validate.
2326 if (!Overridden && //
2327 (StoredSize != File->getSize() ||
2328 (StoredTime && StoredTime != File->getModificationTime() &&
2332 // Build a list of the PCH imports that got us here (in reverse).
2333 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2334 while (!ImportStack.back()->ImportedBy.empty())
2335 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2337 // The top-level PCH is stale.
2338 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2339 unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2340 if (DiagnosticKind == 0)
2341 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2342 else if (DiagnosticKind == 1)
2343 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2345 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2347 // Print the import stack.
2348 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2349 Diag(diag::note_pch_required_by)
2350 << Filename << ImportStack[0]->FileName;
2351 for (unsigned I = 1; I < ImportStack.size(); ++I)
2352 Diag(diag::note_pch_required_by)
2353 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2356 if (!Diags.isDiagnosticInFlight())
2357 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2362 // FIXME: If the file is overridden and we've already opened it,
2363 // issue an error (or split it into a separate FileEntry).
2365 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2367 // Note that we've loaded this input file.
2368 F.InputFilesLoaded[ID-1] = IF;
2372 /// If we are loading a relocatable PCH or module file, and the filename
2373 /// is not an absolute path, add the system or module root to the beginning of
2375 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2376 // Resolve relative to the base directory, if we have one.
2377 if (!M.BaseDirectory.empty())
2378 return ResolveImportedPath(Filename, M.BaseDirectory);
2381 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2382 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2385 SmallString<128> Buffer;
2386 llvm::sys::path::append(Buffer, Prefix, Filename);
2387 Filename.assign(Buffer.begin(), Buffer.end());
2390 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2392 case ASTReader::Failure: return true;
2393 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2394 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2395 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2396 case ASTReader::ConfigurationMismatch:
2397 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2398 case ASTReader::HadErrors: return true;
2399 case ASTReader::Success: return false;
2402 llvm_unreachable("unknown ASTReadResult");
2405 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2406 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2407 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2408 std::string &SuggestedPredefines) {
2409 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2410 // FIXME this drops errors on the floor.
2411 consumeError(std::move(Err));
2415 // Read all of the records in the options block.
2417 ASTReadResult Result = Success;
2419 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2421 // FIXME this drops errors on the floor.
2422 consumeError(MaybeEntry.takeError());
2425 llvm::BitstreamEntry Entry = MaybeEntry.get();
2427 switch (Entry.Kind) {
2428 case llvm::BitstreamEntry::Error:
2429 case llvm::BitstreamEntry::SubBlock:
2432 case llvm::BitstreamEntry::EndBlock:
2435 case llvm::BitstreamEntry::Record:
2436 // The interesting case.
2440 // Read and process a record.
2442 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2443 if (!MaybeRecordType) {
2444 // FIXME this drops errors on the floor.
2445 consumeError(MaybeRecordType.takeError());
2448 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2449 case LANGUAGE_OPTIONS: {
2450 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2451 if (ParseLanguageOptions(Record, Complain, Listener,
2452 AllowCompatibleConfigurationMismatch))
2453 Result = ConfigurationMismatch;
2457 case TARGET_OPTIONS: {
2458 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2459 if (ParseTargetOptions(Record, Complain, Listener,
2460 AllowCompatibleConfigurationMismatch))
2461 Result = ConfigurationMismatch;
2465 case FILE_SYSTEM_OPTIONS: {
2466 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2467 if (!AllowCompatibleConfigurationMismatch &&
2468 ParseFileSystemOptions(Record, Complain, Listener))
2469 Result = ConfigurationMismatch;
2473 case HEADER_SEARCH_OPTIONS: {
2474 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2475 if (!AllowCompatibleConfigurationMismatch &&
2476 ParseHeaderSearchOptions(Record, Complain, Listener))
2477 Result = ConfigurationMismatch;
2481 case PREPROCESSOR_OPTIONS:
2482 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2483 if (!AllowCompatibleConfigurationMismatch &&
2484 ParsePreprocessorOptions(Record, Complain, Listener,
2485 SuggestedPredefines))
2486 Result = ConfigurationMismatch;
2492 ASTReader::ASTReadResult
2493 ASTReader::ReadControlBlock(ModuleFile &F,
2494 SmallVectorImpl<ImportedModule> &Loaded,
2495 const ModuleFile *ImportedBy,
2496 unsigned ClientLoadCapabilities) {
2497 BitstreamCursor &Stream = F.Stream;
2498 ASTReadResult Result = Success;
2500 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2501 Error(std::move(Err));
2505 // Lambda to read the unhashed control block the first time it's called.
2507 // For PCM files, the unhashed control block cannot be read until after the
2508 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2509 // need to look ahead before reading the IMPORTS record. For consistency,
2510 // this block is always read somehow (see BitstreamEntry::EndBlock).
2511 bool HasReadUnhashedControlBlock = false;
2512 auto readUnhashedControlBlockOnce = [&]() {
2513 if (!HasReadUnhashedControlBlock) {
2514 HasReadUnhashedControlBlock = true;
2515 if (ASTReadResult Result =
2516 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2522 // Read all of the records and blocks in the control block.
2524 unsigned NumInputs = 0;
2525 unsigned NumUserInputs = 0;
2526 StringRef BaseDirectoryAsWritten;
2528 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2530 Error(MaybeEntry.takeError());
2533 llvm::BitstreamEntry Entry = MaybeEntry.get();
2535 switch (Entry.Kind) {
2536 case llvm::BitstreamEntry::Error:
2537 Error("malformed block record in AST file");
2539 case llvm::BitstreamEntry::EndBlock: {
2540 // Validate the module before returning. This call catches an AST with
2541 // no module name and no imports.
2542 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2545 // Validate input files.
2546 const HeaderSearchOptions &HSOpts =
2547 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2549 // All user input files reside at the index range [0, NumUserInputs), and
2550 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2551 // loaded module files, ignore missing inputs.
2552 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2553 F.Kind != MK_PrebuiltModule) {
2554 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2556 // If we are reading a module, we will create a verification timestamp,
2557 // so we verify all input files. Otherwise, verify only user input
2560 unsigned N = NumUserInputs;
2561 if (ValidateSystemInputs ||
2562 (HSOpts.ModulesValidateOncePerBuildSession &&
2563 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2564 F.Kind == MK_ImplicitModule))
2567 for (unsigned I = 0; I < N; ++I) {
2568 InputFile IF = getInputFile(F, I+1, Complain);
2569 if (!IF.getFile() || IF.isOutOfDate())
2575 Listener->visitModuleFile(F.FileName, F.Kind);
2577 if (Listener && Listener->needsInputFileVisitation()) {
2578 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2580 for (unsigned I = 0; I < N; ++I) {
2581 bool IsSystem = I >= NumUserInputs;
2582 InputFileInfo FI = readInputFileInfo(F, I+1);
2583 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2584 F.Kind == MK_ExplicitModule ||
2585 F.Kind == MK_PrebuiltModule);
2592 case llvm::BitstreamEntry::SubBlock:
2594 case INPUT_FILES_BLOCK_ID:
2595 F.InputFilesCursor = Stream;
2596 if (llvm::Error Err = Stream.SkipBlock()) {
2597 Error(std::move(Err));
2600 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2601 Error("malformed block record in AST file");
2606 case OPTIONS_BLOCK_ID:
2607 // If we're reading the first module for this group, check its options
2608 // are compatible with ours. For modules it imports, no further checking
2609 // is required, because we checked them when we built it.
2610 if (Listener && !ImportedBy) {
2611 // Should we allow the configuration of the module file to differ from
2612 // the configuration of the current translation unit in a compatible
2615 // FIXME: Allow this for files explicitly specified with -include-pch.
2616 bool AllowCompatibleConfigurationMismatch =
2617 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2619 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2620 AllowCompatibleConfigurationMismatch,
2621 *Listener, SuggestedPredefines);
2622 if (Result == Failure) {
2623 Error("malformed block record in AST file");
2627 if (DisableValidation ||
2628 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2631 // If we can't load the module, exit early since we likely
2632 // will rebuild the module anyway. The stream may be in the
2633 // middle of a block.
2634 if (Result != Success)
2636 } else if (llvm::Error Err = Stream.SkipBlock()) {
2637 Error(std::move(Err));
2643 if (llvm::Error Err = Stream.SkipBlock()) {
2644 Error(std::move(Err));
2650 case llvm::BitstreamEntry::Record:
2651 // The interesting case.
2655 // Read and process a record.
2658 Expected<unsigned> MaybeRecordType =
2659 Stream.readRecord(Entry.ID, Record, &Blob);
2660 if (!MaybeRecordType) {
2661 Error(MaybeRecordType.takeError());
2664 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2666 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2667 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2668 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2669 : diag::err_pch_version_too_new);
2670 return VersionMismatch;
2673 bool hasErrors = Record[7];
2674 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2675 Diag(diag::err_pch_with_compiler_errors);
2679 Diags.ErrorOccurred = true;
2680 Diags.UncompilableErrorOccurred = true;
2681 Diags.UnrecoverableErrorOccurred = true;
2684 F.RelocatablePCH = Record[4];
2685 // Relative paths in a relocatable PCH are relative to our sysroot.
2686 if (F.RelocatablePCH)
2687 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2689 F.HasTimestamps = Record[5];
2691 F.PCHHasObjectFile = Record[6];
2693 const std::string &CurBranch = getClangFullRepositoryVersion();
2694 StringRef ASTBranch = Blob;
2695 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2696 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2697 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2698 return VersionMismatch;
2704 // Validate the AST before processing any imports (otherwise, untangling
2705 // them can be error-prone and expensive). A module will have a name and
2706 // will already have been validated, but this catches the PCH case.
2707 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2710 // Load each of the imported PCH files.
2711 unsigned Idx = 0, N = Record.size();
2713 // Read information about the AST file.
2714 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2715 // The import location will be the local one for now; we will adjust
2716 // all import locations of module imports after the global source
2717 // location info are setup, in ReadAST.
2718 SourceLocation ImportLoc =
2719 ReadUntranslatedSourceLocation(Record[Idx++]);
2720 off_t StoredSize = (off_t)Record[Idx++];
2721 time_t StoredModTime = (time_t)Record[Idx++];
2722 ASTFileSignature StoredSignature = {
2723 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2724 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2725 (uint32_t)Record[Idx++]}}};
2727 std::string ImportedName = ReadString(Record, Idx);
2728 std::string ImportedFile;
2730 // For prebuilt and explicit modules first consult the file map for
2731 // an override. Note that here we don't search prebuilt module
2732 // directories, only the explicit name to file mappings. Also, we will
2733 // still verify the size/signature making sure it is essentially the
2734 // same file but perhaps in a different location.
2735 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2736 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2737 ImportedName, /*FileMapOnly*/ true);
2739 if (ImportedFile.empty())
2740 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2741 // ModuleCache as when writing.
2742 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2744 SkipPath(Record, Idx);
2746 // If our client can't cope with us being out of date, we can't cope with
2747 // our dependency being missing.
2748 unsigned Capabilities = ClientLoadCapabilities;
2749 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2750 Capabilities &= ~ARR_Missing;
2752 // Load the AST file.
2753 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2754 Loaded, StoredSize, StoredModTime,
2755 StoredSignature, Capabilities);
2757 // If we diagnosed a problem, produce a backtrace.
2758 if (isDiagnosedResult(Result, Capabilities))
2759 Diag(diag::note_module_file_imported_by)
2760 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2763 case Failure: return Failure;
2764 // If we have to ignore the dependency, we'll have to ignore this too.
2766 case OutOfDate: return OutOfDate;
2767 case VersionMismatch: return VersionMismatch;
2768 case ConfigurationMismatch: return ConfigurationMismatch;
2769 case HadErrors: return HadErrors;
2770 case Success: break;
2777 F.OriginalSourceFileID = FileID::get(Record[0]);
2778 F.ActualOriginalSourceFileName = Blob;
2779 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2780 ResolveImportedPath(F, F.OriginalSourceFileName);
2783 case ORIGINAL_FILE_ID:
2784 F.OriginalSourceFileID = FileID::get(Record[0]);
2787 case ORIGINAL_PCH_DIR:
2788 F.OriginalDir = Blob;
2792 F.ModuleName = Blob;
2793 Diag(diag::remark_module_import)
2794 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2795 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2797 Listener->ReadModuleName(F.ModuleName);
2799 // Validate the AST as soon as we have a name so we can exit early on
2801 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2806 case MODULE_DIRECTORY: {
2807 // Save the BaseDirectory as written in the PCM for computing the module
2808 // filename for the ModuleCache.
2809 BaseDirectoryAsWritten = Blob;
2810 assert(!F.ModuleName.empty() &&
2811 "MODULE_DIRECTORY found before MODULE_NAME");
2812 // If we've already loaded a module map file covering this module, we may
2813 // have a better path for it (relative to the current build).
2814 Module *M = PP.getHeaderSearchInfo().lookupModule(
2815 F.ModuleName, /*AllowSearch*/ true,
2816 /*AllowExtraModuleMapSearch*/ true);
2817 if (M && M->Directory) {
2818 // If we're implicitly loading a module, the base directory can't
2819 // change between the build and use.
2820 // Don't emit module relocation error if we have -fno-validate-pch
2821 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2822 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2823 const DirectoryEntry *BuildDir =
2824 PP.getFileManager().getDirectory(Blob);
2825 if (!BuildDir || BuildDir != M->Directory) {
2826 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2827 Diag(diag::err_imported_module_relocated)
2828 << F.ModuleName << Blob << M->Directory->getName();
2832 F.BaseDirectory = M->Directory->getName();
2834 F.BaseDirectory = Blob;
2839 case MODULE_MAP_FILE:
2840 if (ASTReadResult Result =
2841 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2845 case INPUT_FILE_OFFSETS:
2846 NumInputs = Record[0];
2847 NumUserInputs = Record[1];
2848 F.InputFileOffsets =
2849 (const llvm::support::unaligned_uint64_t *)Blob.data();
2850 F.InputFilesLoaded.resize(NumInputs);
2851 F.NumUserInputFiles = NumUserInputs;
2857 ASTReader::ASTReadResult
2858 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2859 BitstreamCursor &Stream = F.Stream;
2861 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2862 Error(std::move(Err));
2866 // Read all of the records and blocks for the AST file.
2869 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2871 Error(MaybeEntry.takeError());
2874 llvm::BitstreamEntry Entry = MaybeEntry.get();
2876 switch (Entry.Kind) {
2877 case llvm::BitstreamEntry::Error:
2878 Error("error at end of module block in AST file");
2880 case llvm::BitstreamEntry::EndBlock:
2881 // Outside of C++, we do not store a lookup map for the translation unit.
2882 // Instead, mark it as needing a lookup map to be built if this module
2883 // contains any declarations lexically within it (which it always does!).
2884 // This usually has no cost, since we very rarely need the lookup map for
2885 // the translation unit outside C++.
2886 if (ASTContext *Ctx = ContextObj) {
2887 DeclContext *DC = Ctx->getTranslationUnitDecl();
2888 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2889 DC->setMustBuildLookupTable();
2893 case llvm::BitstreamEntry::SubBlock:
2895 case DECLTYPES_BLOCK_ID:
2896 // We lazily load the decls block, but we want to set up the
2897 // DeclsCursor cursor to point into it. Clone our current bitcode
2898 // cursor to it, enter the block and read the abbrevs in that block.
2899 // With the main cursor, we just skip over it.
2900 F.DeclsCursor = Stream;
2901 if (llvm::Error Err = Stream.SkipBlock()) {
2902 Error(std::move(Err));
2905 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2906 Error("malformed block record in AST file");
2911 case PREPROCESSOR_BLOCK_ID:
2912 F.MacroCursor = Stream;
2913 if (!PP.getExternalSource())
2914 PP.setExternalSource(this);
2916 if (llvm::Error Err = Stream.SkipBlock()) {
2917 Error(std::move(Err));
2920 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2921 Error("malformed block record in AST file");
2924 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2927 case PREPROCESSOR_DETAIL_BLOCK_ID:
2928 F.PreprocessorDetailCursor = Stream;
2930 if (llvm::Error Err = Stream.SkipBlock()) {
2931 Error(std::move(Err));
2934 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2935 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2936 Error("malformed preprocessor detail record in AST file");
2939 F.PreprocessorDetailStartOffset
2940 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2942 if (!PP.getPreprocessingRecord())
2943 PP.createPreprocessingRecord();
2944 if (!PP.getPreprocessingRecord()->getExternalSource())
2945 PP.getPreprocessingRecord()->SetExternalSource(*this);
2948 case SOURCE_MANAGER_BLOCK_ID:
2949 if (ReadSourceManagerBlock(F))
2953 case SUBMODULE_BLOCK_ID:
2954 if (ASTReadResult Result =
2955 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2959 case COMMENTS_BLOCK_ID: {
2960 BitstreamCursor C = Stream;
2962 if (llvm::Error Err = Stream.SkipBlock()) {
2963 Error(std::move(Err));
2966 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2967 Error("malformed comments block in AST file");
2970 CommentsCursors.push_back(std::make_pair(C, &F));
2975 if (llvm::Error Err = Stream.SkipBlock()) {
2976 Error(std::move(Err));
2983 case llvm::BitstreamEntry::Record:
2984 // The interesting case.
2988 // Read and process a record.
2991 Expected<unsigned> MaybeRecordType =
2992 Stream.readRecord(Entry.ID, Record, &Blob);
2993 if (!MaybeRecordType) {
2994 Error(MaybeRecordType.takeError());
2997 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
2999 // If we're not loading an AST context, we don't care about most records.
3001 switch (RecordType) {
3002 case IDENTIFIER_TABLE:
3003 case IDENTIFIER_OFFSET:
3004 case INTERESTING_IDENTIFIERS:
3006 case PP_CONDITIONAL_STACK:
3007 case PP_COUNTER_VALUE:
3008 case SOURCE_LOCATION_OFFSETS:
3009 case MODULE_OFFSET_MAP:
3010 case SOURCE_MANAGER_LINE_TABLE:
3011 case SOURCE_LOCATION_PRELOADS:
3012 case PPD_ENTITIES_OFFSETS:
3013 case HEADER_SEARCH_TABLE:
3014 case IMPORTED_MODULES:
3022 switch (RecordType) {
3023 default: // Default behavior: ignore.
3027 if (F.LocalNumTypes != 0) {
3028 Error("duplicate TYPE_OFFSET record in AST file");
3031 F.TypeOffsets = (const uint32_t *)Blob.data();
3032 F.LocalNumTypes = Record[0];
3033 unsigned LocalBaseTypeIndex = Record[1];
3034 F.BaseTypeIndex = getTotalNumTypes();
3036 if (F.LocalNumTypes > 0) {
3037 // Introduce the global -> local mapping for types within this module.
3038 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3040 // Introduce the local -> global mapping for types within this module.
3041 F.TypeRemap.insertOrReplace(
3042 std::make_pair(LocalBaseTypeIndex,
3043 F.BaseTypeIndex - LocalBaseTypeIndex));
3045 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3051 if (F.LocalNumDecls != 0) {
3052 Error("duplicate DECL_OFFSET record in AST file");
3055 F.DeclOffsets = (const DeclOffset *)Blob.data();
3056 F.LocalNumDecls = Record[0];
3057 unsigned LocalBaseDeclID = Record[1];
3058 F.BaseDeclID = getTotalNumDecls();
3060 if (F.LocalNumDecls > 0) {
3061 // Introduce the global -> local mapping for declarations within this
3063 GlobalDeclMap.insert(
3064 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3066 // Introduce the local -> global mapping for declarations within this
3068 F.DeclRemap.insertOrReplace(
3069 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3071 // Introduce the global -> local mapping for declarations within this
3073 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3075 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3080 case TU_UPDATE_LEXICAL: {
3081 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3082 LexicalContents Contents(
3083 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3085 static_cast<unsigned int>(Blob.size() / 4));
3086 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3087 TU->setHasExternalLexicalStorage(true);
3091 case UPDATE_VISIBLE: {
3093 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3094 auto *Data = (const unsigned char*)Blob.data();
3095 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3096 // If we've already loaded the decl, perform the updates when we finish
3097 // loading this block.
3098 if (Decl *D = GetExistingDecl(ID))
3099 PendingUpdateRecords.push_back(
3100 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3104 case IDENTIFIER_TABLE:
3105 F.IdentifierTableData = Blob.data();
3107 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3108 (const unsigned char *)F.IdentifierTableData + Record[0],
3109 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3110 (const unsigned char *)F.IdentifierTableData,
3111 ASTIdentifierLookupTrait(*this, F));
3113 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3117 case IDENTIFIER_OFFSET: {
3118 if (F.LocalNumIdentifiers != 0) {
3119 Error("duplicate IDENTIFIER_OFFSET record in AST file");
3122 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3123 F.LocalNumIdentifiers = Record[0];
3124 unsigned LocalBaseIdentifierID = Record[1];
3125 F.BaseIdentifierID = getTotalNumIdentifiers();
3127 if (F.LocalNumIdentifiers > 0) {
3128 // Introduce the global -> local mapping for identifiers within this
3130 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3133 // Introduce the local -> global mapping for identifiers within this
3135 F.IdentifierRemap.insertOrReplace(
3136 std::make_pair(LocalBaseIdentifierID,
3137 F.BaseIdentifierID - LocalBaseIdentifierID));
3139 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3140 + F.LocalNumIdentifiers);
3145 case INTERESTING_IDENTIFIERS:
3146 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3149 case EAGERLY_DESERIALIZED_DECLS:
3150 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3151 // about "interesting" decls (for instance, if we're building a module).
3152 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3153 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3156 case MODULAR_CODEGEN_DECLS:
3157 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3158 // them (ie: if we're not codegenerating this module).
3159 if (F.Kind == MK_MainFile)
3160 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3161 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3165 if (SpecialTypes.empty()) {
3166 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3167 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3171 if (SpecialTypes.size() != Record.size()) {
3172 Error("invalid special-types record");
3176 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3177 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3178 if (!SpecialTypes[I])
3179 SpecialTypes[I] = ID;
3180 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3186 TotalNumStatements += Record[0];
3187 TotalNumMacros += Record[1];
3188 TotalLexicalDeclContexts += Record[2];
3189 TotalVisibleDeclContexts += Record[3];
3192 case UNUSED_FILESCOPED_DECLS:
3193 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3194 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3197 case DELEGATING_CTORS:
3198 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3199 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3202 case WEAK_UNDECLARED_IDENTIFIERS:
3203 if (Record.size() % 4 != 0) {
3204 Error("invalid weak identifiers record");
3208 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3209 // files. This isn't the way to do it :)
3210 WeakUndeclaredIdentifiers.clear();
3212 // Translate the weak, undeclared identifiers into global IDs.
3213 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3214 WeakUndeclaredIdentifiers.push_back(
3215 getGlobalIdentifierID(F, Record[I++]));
3216 WeakUndeclaredIdentifiers.push_back(
3217 getGlobalIdentifierID(F, Record[I++]));
3218 WeakUndeclaredIdentifiers.push_back(
3219 ReadSourceLocation(F, Record, I).getRawEncoding());
3220 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3224 case SELECTOR_OFFSETS: {
3225 F.SelectorOffsets = (const uint32_t *)Blob.data();
3226 F.LocalNumSelectors = Record[0];
3227 unsigned LocalBaseSelectorID = Record[1];
3228 F.BaseSelectorID = getTotalNumSelectors();
3230 if (F.LocalNumSelectors > 0) {
3231 // Introduce the global -> local mapping for selectors within this
3233 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3235 // Introduce the local -> global mapping for selectors within this
3237 F.SelectorRemap.insertOrReplace(
3238 std::make_pair(LocalBaseSelectorID,
3239 F.BaseSelectorID - LocalBaseSelectorID));
3241 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3247 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3249 F.SelectorLookupTable
3250 = ASTSelectorLookupTable::Create(
3251 F.SelectorLookupTableData + Record[0],
3252 F.SelectorLookupTableData,
3253 ASTSelectorLookupTrait(*this, F));
3254 TotalNumMethodPoolEntries += Record[1];
3257 case REFERENCED_SELECTOR_POOL:
3258 if (!Record.empty()) {
3259 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3260 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3262 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3268 case PP_CONDITIONAL_STACK:
3269 if (!Record.empty()) {
3270 unsigned Idx = 0, End = Record.size() - 1;
3271 bool ReachedEOFWhileSkipping = Record[Idx++];
3272 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3273 if (ReachedEOFWhileSkipping) {
3274 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3275 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3276 bool FoundNonSkipPortion = Record[Idx++];
3277 bool FoundElse = Record[Idx++];
3278 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3279 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3280 FoundElse, ElseLoc);
3282 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3284 auto Loc = ReadSourceLocation(F, Record, Idx);
3285 bool WasSkipping = Record[Idx++];
3286 bool FoundNonSkip = Record[Idx++];
3287 bool FoundElse = Record[Idx++];
3288 ConditionalStack.push_back(
3289 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3291 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3295 case PP_COUNTER_VALUE:
3296 if (!Record.empty() && Listener)
3297 Listener->ReadCounter(F, Record[0]);
3300 case FILE_SORTED_DECLS:
3301 F.FileSortedDecls = (const DeclID *)Blob.data();
3302 F.NumFileSortedDecls = Record[0];
3305 case SOURCE_LOCATION_OFFSETS: {
3306 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3307 F.LocalNumSLocEntries = Record[0];
3308 unsigned SLocSpaceSize = Record[1];
3309 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3310 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3312 if (!F.SLocEntryBaseID) {
3313 Error("ran out of source locations");
3316 // Make our entry in the range map. BaseID is negative and growing, so
3317 // we invert it. Because we invert it, though, we need the other end of
3319 unsigned RangeStart =
3320 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3321 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3322 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3324 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3325 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3326 GlobalSLocOffsetMap.insert(
3327 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3328 - SLocSpaceSize,&F));
3330 // Initialize the remapping table.
3331 // Invalid stays invalid.
3332 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3333 // This module. Base was 2 when being compiled.
3334 F.SLocRemap.insertOrReplace(std::make_pair(2U,
3335 static_cast<int>(F.SLocEntryBaseOffset - 2)));
3337 TotalNumSLocEntries += F.LocalNumSLocEntries;
3341 case MODULE_OFFSET_MAP:
3342 F.ModuleOffsetMap = Blob;
3345 case SOURCE_MANAGER_LINE_TABLE:
3346 if (ParseLineTable(F, Record))
3350 case SOURCE_LOCATION_PRELOADS: {
3351 // Need to transform from the local view (1-based IDs) to the global view,
3352 // which is based off F.SLocEntryBaseID.
3353 if (!F.PreloadSLocEntries.empty()) {
3354 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3358 F.PreloadSLocEntries.swap(Record);
3362 case EXT_VECTOR_DECLS:
3363 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3364 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3368 if (Record.size() % 3 != 0) {
3369 Error("Invalid VTABLE_USES record");
3373 // Later tables overwrite earlier ones.
3374 // FIXME: Modules will have some trouble with this. This is clearly not
3375 // the right way to do this.
3378 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3379 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3380 VTableUses.push_back(
3381 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3382 VTableUses.push_back(Record[Idx++]);
3386 case PENDING_IMPLICIT_INSTANTIATIONS:
3387 if (PendingInstantiations.size() % 2 != 0) {
3388 Error("Invalid existing PendingInstantiations");
3392 if (Record.size() % 2 != 0) {
3393 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3397 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3398 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3399 PendingInstantiations.push_back(
3400 ReadSourceLocation(F, Record, I).getRawEncoding());
3404 case SEMA_DECL_REFS:
3405 if (Record.size() != 3) {
3406 Error("Invalid SEMA_DECL_REFS block");
3409 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3410 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3413 case PPD_ENTITIES_OFFSETS: {
3414 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3415 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3416 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3418 unsigned LocalBasePreprocessedEntityID = Record[0];
3420 unsigned StartingID;
3421 if (!PP.getPreprocessingRecord())
3422 PP.createPreprocessingRecord();
3423 if (!PP.getPreprocessingRecord()->getExternalSource())
3424 PP.getPreprocessingRecord()->SetExternalSource(*this);
3426 = PP.getPreprocessingRecord()
3427 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3428 F.BasePreprocessedEntityID = StartingID;
3430 if (F.NumPreprocessedEntities > 0) {
3431 // Introduce the global -> local mapping for preprocessed entities in
3433 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3435 // Introduce the local -> global mapping for preprocessed entities in
3437 F.PreprocessedEntityRemap.insertOrReplace(
3438 std::make_pair(LocalBasePreprocessedEntityID,
3439 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3445 case PPD_SKIPPED_RANGES: {
3446 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3447 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3448 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3450 if (!PP.getPreprocessingRecord())
3451 PP.createPreprocessingRecord();
3452 if (!PP.getPreprocessingRecord()->getExternalSource())
3453 PP.getPreprocessingRecord()->SetExternalSource(*this);
3454 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3455 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3457 if (F.NumPreprocessedSkippedRanges > 0)
3458 GlobalSkippedRangeMap.insert(
3459 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3463 case DECL_UPDATE_OFFSETS:
3464 if (Record.size() % 2 != 0) {
3465 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3468 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3469 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3470 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3472 // If we've already loaded the decl, perform the updates when we finish
3473 // loading this block.
3474 if (Decl *D = GetExistingDecl(ID))
3475 PendingUpdateRecords.push_back(
3476 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3480 case OBJC_CATEGORIES_MAP:
3481 if (F.LocalNumObjCCategoriesInMap != 0) {
3482 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3486 F.LocalNumObjCCategoriesInMap = Record[0];
3487 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3490 case OBJC_CATEGORIES:
3491 F.ObjCCategories.swap(Record);
3494 case CUDA_SPECIAL_DECL_REFS:
3495 // Later tables overwrite earlier ones.
3496 // FIXME: Modules will have trouble with this.
3497 CUDASpecialDeclRefs.clear();
3498 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3499 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3502 case HEADER_SEARCH_TABLE:
3503 F.HeaderFileInfoTableData = Blob.data();
3504 F.LocalNumHeaderFileInfos = Record[1];
3506 F.HeaderFileInfoTable
3507 = HeaderFileInfoLookupTable::Create(
3508 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3509 (const unsigned char *)F.HeaderFileInfoTableData,
3510 HeaderFileInfoTrait(*this, F,
3511 &PP.getHeaderSearchInfo(),
3512 Blob.data() + Record[2]));
3514 PP.getHeaderSearchInfo().SetExternalSource(this);
3515 if (!PP.getHeaderSearchInfo().getExternalLookup())
3516 PP.getHeaderSearchInfo().SetExternalLookup(this);
3520 case FP_PRAGMA_OPTIONS:
3521 // Later tables overwrite earlier ones.
3522 FPPragmaOptions.swap(Record);
3525 case OPENCL_EXTENSIONS:
3526 for (unsigned I = 0, E = Record.size(); I != E; ) {
3527 auto Name = ReadString(Record, I);
3528 auto &Opt = OpenCLExtensions.OptMap[Name];
3529 Opt.Supported = Record[I++] != 0;
3530 Opt.Enabled = Record[I++] != 0;
3531 Opt.Avail = Record[I++];
3532 Opt.Core = Record[I++];
3536 case OPENCL_EXTENSION_TYPES:
3537 for (unsigned I = 0, E = Record.size(); I != E;) {
3538 auto TypeID = static_cast<::TypeID>(Record[I++]);
3539 auto *Type = GetType(TypeID).getTypePtr();
3540 auto NumExt = static_cast<unsigned>(Record[I++]);
3541 for (unsigned II = 0; II != NumExt; ++II) {
3542 auto Ext = ReadString(Record, I);
3543 OpenCLTypeExtMap[Type].insert(Ext);
3548 case OPENCL_EXTENSION_DECLS:
3549 for (unsigned I = 0, E = Record.size(); I != E;) {
3550 auto DeclID = static_cast<::DeclID>(Record[I++]);
3551 auto *Decl = GetDecl(DeclID);
3552 auto NumExt = static_cast<unsigned>(Record[I++]);
3553 for (unsigned II = 0; II != NumExt; ++II) {
3554 auto Ext = ReadString(Record, I);
3555 OpenCLDeclExtMap[Decl].insert(Ext);
3560 case TENTATIVE_DEFINITIONS:
3561 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3562 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3565 case KNOWN_NAMESPACES:
3566 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3567 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3570 case UNDEFINED_BUT_USED:
3571 if (UndefinedButUsed.size() % 2 != 0) {
3572 Error("Invalid existing UndefinedButUsed");
3576 if (Record.size() % 2 != 0) {
3577 Error("invalid undefined-but-used record");
3580 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3581 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3582 UndefinedButUsed.push_back(
3583 ReadSourceLocation(F, Record, I).getRawEncoding());
3587 case DELETE_EXPRS_TO_ANALYZE:
3588 for (unsigned I = 0, N = Record.size(); I != N;) {
3589 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3590 const uint64_t Count = Record[I++];
3591 DelayedDeleteExprs.push_back(Count);
3592 for (uint64_t C = 0; C < Count; ++C) {
3593 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3594 bool IsArrayForm = Record[I++] == 1;
3595 DelayedDeleteExprs.push_back(IsArrayForm);
3600 case IMPORTED_MODULES:
3601 if (!F.isModule()) {
3602 // If we aren't loading a module (which has its own exports), make
3603 // all of the imported modules visible.
3604 // FIXME: Deal with macros-only imports.
3605 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3606 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3607 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3609 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3610 if (DeserializationListener)
3611 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3617 case MACRO_OFFSET: {
3618 if (F.LocalNumMacros != 0) {
3619 Error("duplicate MACRO_OFFSET record in AST file");
3622 F.MacroOffsets = (const uint32_t *)Blob.data();
3623 F.LocalNumMacros = Record[0];
3624 unsigned LocalBaseMacroID = Record[1];
3625 F.BaseMacroID = getTotalNumMacros();
3627 if (F.LocalNumMacros > 0) {
3628 // Introduce the global -> local mapping for macros within this module.
3629 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3631 // Introduce the local -> global mapping for macros within this module.
3632 F.MacroRemap.insertOrReplace(
3633 std::make_pair(LocalBaseMacroID,
3634 F.BaseMacroID - LocalBaseMacroID));
3636 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3641 case LATE_PARSED_TEMPLATE:
3642 LateParsedTemplates.append(Record.begin(), Record.end());
3645 case OPTIMIZE_PRAGMA_OPTIONS:
3646 if (Record.size() != 1) {
3647 Error("invalid pragma optimize record");
3650 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3653 case MSSTRUCT_PRAGMA_OPTIONS:
3654 if (Record.size() != 1) {
3655 Error("invalid pragma ms_struct record");
3658 PragmaMSStructState = Record[0];
3661 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3662 if (Record.size() != 2) {
3663 Error("invalid pragma ms_struct record");
3666 PragmaMSPointersToMembersState = Record[0];
3667 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3670 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3671 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3672 UnusedLocalTypedefNameCandidates.push_back(
3673 getGlobalDeclID(F, Record[I]));
3676 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3677 if (Record.size() != 1) {
3678 Error("invalid cuda pragma options record");
3681 ForceCUDAHostDeviceDepth = Record[0];
3684 case PACK_PRAGMA_OPTIONS: {
3685 if (Record.size() < 3) {
3686 Error("invalid pragma pack record");
3689 PragmaPackCurrentValue = Record[0];
3690 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3691 unsigned NumStackEntries = Record[2];
3693 // Reset the stack when importing a new module.
3694 PragmaPackStack.clear();
3695 for (unsigned I = 0; I < NumStackEntries; ++I) {
3696 PragmaPackStackEntry Entry;
3697 Entry.Value = Record[Idx++];
3698 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3699 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3700 PragmaPackStrings.push_back(ReadString(Record, Idx));
3701 Entry.SlotLabel = PragmaPackStrings.back();
3702 PragmaPackStack.push_back(Entry);
3710 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3711 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3713 // Additional remapping information.
3714 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3715 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3716 F.ModuleOffsetMap = StringRef();
3718 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3719 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3720 F.SLocRemap.insert(std::make_pair(0U, 0));
3721 F.SLocRemap.insert(std::make_pair(2U, 1));
3724 // Continuous range maps we may be updating in our module.
3725 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3726 RemapBuilder SLocRemap(F.SLocRemap);
3727 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3728 RemapBuilder MacroRemap(F.MacroRemap);
3729 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3730 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3731 RemapBuilder SelectorRemap(F.SelectorRemap);
3732 RemapBuilder DeclRemap(F.DeclRemap);
3733 RemapBuilder TypeRemap(F.TypeRemap);
3735 while (Data < DataEnd) {
3736 // FIXME: Looking up dependency modules by filename is horrible. Let's
3737 // start fixing this with prebuilt and explicit modules and see how it
3739 using namespace llvm::support;
3740 ModuleKind Kind = static_cast<ModuleKind>(
3741 endian::readNext<uint8_t, little, unaligned>(Data));
3742 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3743 StringRef Name = StringRef((const char*)Data, Len);
3745 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3746 ? ModuleMgr.lookupByModuleName(Name)
3747 : ModuleMgr.lookupByFileName(Name));
3750 "SourceLocation remap refers to unknown module, cannot find ";
3756 uint32_t SLocOffset =
3757 endian::readNext<uint32_t, little, unaligned>(Data);
3758 uint32_t IdentifierIDOffset =
3759 endian::readNext<uint32_t, little, unaligned>(Data);
3760 uint32_t MacroIDOffset =
3761 endian::readNext<uint32_t, little, unaligned>(Data);
3762 uint32_t PreprocessedEntityIDOffset =
3763 endian::readNext<uint32_t, little, unaligned>(Data);
3764 uint32_t SubmoduleIDOffset =
3765 endian::readNext<uint32_t, little, unaligned>(Data);
3766 uint32_t SelectorIDOffset =
3767 endian::readNext<uint32_t, little, unaligned>(Data);
3768 uint32_t DeclIDOffset =
3769 endian::readNext<uint32_t, little, unaligned>(Data);
3770 uint32_t TypeIndexOffset =
3771 endian::readNext<uint32_t, little, unaligned>(Data);
3773 uint32_t None = std::numeric_limits<uint32_t>::max();
3775 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3776 RemapBuilder &Remap) {
3778 Remap.insert(std::make_pair(Offset,
3779 static_cast<int>(BaseOffset - Offset)));
3781 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3782 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3783 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3784 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3785 PreprocessedEntityRemap);
3786 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3787 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3788 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3789 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3791 // Global -> local mappings.
3792 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3796 ASTReader::ASTReadResult
3797 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3798 const ModuleFile *ImportedBy,
3799 unsigned ClientLoadCapabilities) {
3801 F.ModuleMapPath = ReadPath(F, Record, Idx);
3803 // Try to resolve ModuleName in the current header search context and
3804 // verify that it is found in the same module map file as we saved. If the
3805 // top-level AST file is a main file, skip this check because there is no
3806 // usable header search context.
3807 assert(!F.ModuleName.empty() &&
3808 "MODULE_NAME should come before MODULE_MAP_FILE");
3809 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3810 // An implicitly-loaded module file should have its module listed in some
3811 // module map file that we've already loaded.
3812 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3813 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3814 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3815 // Don't emit module relocation error if we have -fno-validate-pch
3816 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3817 assert(ImportedBy && "top-level import should be verified");
3818 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3819 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3820 // This module was defined by an imported (explicit) module.
3821 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3822 << ASTFE->getName();
3824 // This module was built with a different module map.
3825 Diag(diag::err_imported_module_not_found)
3826 << F.ModuleName << F.FileName << ImportedBy->FileName
3828 // In case it was imported by a PCH, there's a chance the user is
3829 // just missing to include the search path to the directory containing
3831 if (ImportedBy->Kind == MK_PCH)
3832 Diag(diag::note_imported_by_pch_module_not_found)
3833 << llvm::sys::path::parent_path(F.ModuleMapPath);
3839 assert(M->Name == F.ModuleName && "found module with different name");
3841 // Check the primary module map file.
3842 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3843 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3844 assert(ModMap && "found module is missing module map file");
3845 assert(ImportedBy && "top-level import should be verified");
3846 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3847 Diag(diag::err_imported_module_modmap_changed)
3848 << F.ModuleName << ImportedBy->FileName
3849 << ModMap->getName() << F.ModuleMapPath;
3853 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3854 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3855 // FIXME: we should use input files rather than storing names.
3856 std::string Filename = ReadPath(F, Record, Idx);
3857 const FileEntry *F =
3858 FileMgr.getFile(Filename, false, false);
3860 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3861 Error("could not find file '" + Filename +"' referenced by AST file");
3864 AdditionalStoredMaps.insert(F);
3867 // Check any additional module map files (e.g. module.private.modulemap)
3868 // that are not in the pcm.
3869 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3870 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3871 // Remove files that match
3872 // Note: SmallPtrSet::erase is really remove
3873 if (!AdditionalStoredMaps.erase(ModMap)) {
3874 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3875 Diag(diag::err_module_different_modmap)
3876 << F.ModuleName << /*new*/0 << ModMap->getName();
3882 // Check any additional module map files that are in the pcm, but not
3883 // found in header search. Cases that match are already removed.
3884 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3885 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3886 Diag(diag::err_module_different_modmap)
3887 << F.ModuleName << /*not new*/1 << ModMap->getName();
3893 Listener->ReadModuleMapFile(F.ModuleMapPath);
3897 /// Move the given method to the back of the global list of methods.
3898 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3899 // Find the entry for this selector in the method pool.
3900 Sema::GlobalMethodPool::iterator Known
3901 = S.MethodPool.find(Method->getSelector());
3902 if (Known == S.MethodPool.end())
3905 // Retrieve the appropriate method list.
3906 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3907 : Known->second.second;
3909 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3911 if (List->getMethod() == Method) {
3919 if (List->getNext())
3920 List->setMethod(List->getNext()->getMethod());
3922 List->setMethod(Method);
3926 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3927 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3928 for (Decl *D : Names) {
3929 bool wasHidden = D->isHidden();
3930 D->setVisibleDespiteOwningModule();
3932 if (wasHidden && SemaObj) {
3933 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3934 moveMethodToBackOfGlobalList(*SemaObj, Method);
3940 void ASTReader::makeModuleVisible(Module *Mod,
3941 Module::NameVisibilityKind NameVisibility,
3942 SourceLocation ImportLoc) {
3943 llvm::SmallPtrSet<Module *, 4> Visited;
3944 SmallVector<Module *, 4> Stack;
3945 Stack.push_back(Mod);
3946 while (!Stack.empty()) {
3947 Mod = Stack.pop_back_val();
3949 if (NameVisibility <= Mod->NameVisibility) {
3950 // This module already has this level of visibility (or greater), so
3951 // there is nothing more to do.
3955 if (!Mod->isAvailable()) {
3956 // Modules that aren't available cannot be made visible.
3960 // Update the module's name visibility.
3961 Mod->NameVisibility = NameVisibility;
3963 // If we've already deserialized any names from this module,
3964 // mark them as visible.
3965 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3966 if (Hidden != HiddenNamesMap.end()) {
3967 auto HiddenNames = std::move(*Hidden);
3968 HiddenNamesMap.erase(Hidden);
3969 makeNamesVisible(HiddenNames.second, HiddenNames.first);
3970 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3971 "making names visible added hidden names");
3974 // Push any exported modules onto the stack to be marked as visible.
3975 SmallVector<Module *, 16> Exports;
3976 Mod->getExportedModules(Exports);
3977 for (SmallVectorImpl<Module *>::iterator
3978 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3979 Module *Exported = *I;
3980 if (Visited.insert(Exported).second)
3981 Stack.push_back(Exported);
3986 /// We've merged the definition \p MergedDef into the existing definition
3987 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3989 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3990 NamedDecl *MergedDef) {
3991 if (Def->isHidden()) {
3992 // If MergedDef is visible or becomes visible, make the definition visible.
3993 if (!MergedDef->isHidden())
3994 Def->setVisibleDespiteOwningModule();
3996 getContext().mergeDefinitionIntoModule(
3997 Def, MergedDef->getImportedOwningModule(),
3998 /*NotifyListeners*/ false);
3999 PendingMergedDefinitionsToDeduplicate.insert(Def);
4004 bool ASTReader::loadGlobalIndex() {
4008 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4009 !PP.getLangOpts().Modules)
4012 // Try to load the global index.
4013 TriedLoadingGlobalIndex = true;
4014 StringRef ModuleCachePath
4015 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4016 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4017 GlobalModuleIndex::readIndex(ModuleCachePath);
4018 if (llvm::Error Err = std::move(Result.second)) {
4019 assert(!Result.first);
4020 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4024 GlobalIndex.reset(Result.first);
4025 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4029 bool ASTReader::isGlobalIndexUnavailable() const {
4030 return PP.getLangOpts().Modules && UseGlobalIndex &&
4031 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4034 static void updateModuleTimestamp(ModuleFile &MF) {
4035 // Overwrite the timestamp file contents so that file's mtime changes.
4036 std::string TimestampFilename = MF.getTimestampFilename();
4038 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
4041 OS << "Timestamp file\n";
4043 OS.clear_error(); // Avoid triggering a fatal error.
4046 /// Given a cursor at the start of an AST file, scan ahead and drop the
4047 /// cursor into the start of the given block ID, returning false on success and
4048 /// true on failure.
4049 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4051 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4053 // FIXME this drops errors on the floor.
4054 consumeError(MaybeEntry.takeError());
4057 llvm::BitstreamEntry Entry = MaybeEntry.get();
4059 switch (Entry.Kind) {
4060 case llvm::BitstreamEntry::Error:
4061 case llvm::BitstreamEntry::EndBlock:
4064 case llvm::BitstreamEntry::Record:
4065 // Ignore top-level records.
4066 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4069 // FIXME this drops errors on the floor.
4070 consumeError(Skipped.takeError());
4074 case llvm::BitstreamEntry::SubBlock:
4075 if (Entry.ID == BlockID) {
4076 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4077 // FIXME this drops the error on the floor.
4078 consumeError(std::move(Err));
4085 if (llvm::Error Err = Cursor.SkipBlock()) {
4086 // FIXME this drops the error on the floor.
4087 consumeError(std::move(Err));
4094 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4096 SourceLocation ImportLoc,
4097 unsigned ClientLoadCapabilities,
4098 SmallVectorImpl<ImportedSubmodule> *Imported) {
4099 llvm::SaveAndRestore<SourceLocation>
4100 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4102 // Defer any pending actions until we get to the end of reading the AST file.
4103 Deserializing AnASTFile(this);
4105 // Bump the generation number.
4106 unsigned PreviousGeneration = 0;
4108 PreviousGeneration = incrementGeneration(*ContextObj);
4110 unsigned NumModules = ModuleMgr.size();
4111 SmallVector<ImportedModule, 4> Loaded;
4112 switch (ASTReadResult ReadResult =
4113 ReadASTCore(FileName, Type, ImportLoc,
4114 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4115 ASTFileSignature(), ClientLoadCapabilities)) {
4119 case VersionMismatch:
4120 case ConfigurationMismatch:
4122 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
4123 for (const ImportedModule &IM : Loaded)
4124 LoadedSet.insert(IM.Mod);
4126 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
4127 PP.getLangOpts().Modules
4128 ? &PP.getHeaderSearchInfo().getModuleMap()
4131 // If we find that any modules are unusable, the global index is going
4132 // to be out-of-date. Just remove it.
4133 GlobalIndex.reset();
4134 ModuleMgr.setGlobalIndex(nullptr);
4141 // Here comes stuff that we only do once the entire chain is loaded.
4143 // Load the AST blocks of all of the modules that we loaded.
4144 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4145 MEnd = Loaded.end();
4147 ModuleFile &F = *M->Mod;
4149 // Read the AST block.
4150 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4153 // Read the extension blocks.
4154 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4155 if (ASTReadResult Result = ReadExtensionBlock(F))
4159 // Once read, set the ModuleFile bit base offset and update the size in
4160 // bits of all files we've seen.
4161 F.GlobalBitOffset = TotalModulesSizeInBits;
4162 TotalModulesSizeInBits += F.SizeInBits;
4163 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4165 // Preload SLocEntries.
4166 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4167 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4168 // Load it through the SourceManager and don't call ReadSLocEntry()
4169 // directly because the entry may have already been loaded in which case
4170 // calling ReadSLocEntry() directly would trigger an assertion in
4172 SourceMgr.getLoadedSLocEntryByID(Index);
4175 // Map the original source file ID into the ID space of the current
4177 if (F.OriginalSourceFileID.isValid()) {
4178 F.OriginalSourceFileID = FileID::get(
4179 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4182 // Preload all the pending interesting identifiers by marking them out of
4184 for (auto Offset : F.PreloadIdentifierOffsets) {
4185 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4186 F.IdentifierTableData + Offset);
4188 ASTIdentifierLookupTrait Trait(*this, F);
4189 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4190 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4191 auto &II = PP.getIdentifierTable().getOwn(Key);
4192 II.setOutOfDate(true);
4194 // Mark this identifier as being from an AST file so that we can track
4195 // whether we need to serialize it.
4196 markIdentifierFromAST(*this, II);
4198 // Associate the ID with the identifier so that the writer can reuse it.
4199 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4200 SetIdentifierInfo(ID, &II);
4204 // Setup the import locations and notify the module manager that we've
4205 // committed to these module files.
4206 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4207 MEnd = Loaded.end();
4209 ModuleFile &F = *M->Mod;
4211 ModuleMgr.moduleFileAccepted(&F);
4213 // Set the import location.
4214 F.DirectImportLoc = ImportLoc;
4215 // FIXME: We assume that locations from PCH / preamble do not need
4218 F.ImportLoc = M->ImportLoc;
4220 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4223 if (!PP.getLangOpts().CPlusPlus ||
4224 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4225 Type != MK_PrebuiltModule)) {
4226 // Mark all of the identifiers in the identifier table as being out of date,
4227 // so that various accessors know to check the loaded modules when the
4228 // identifier is used.
4230 // For C++ modules, we don't need information on many identifiers (just
4231 // those that provide macros or are poisoned), so we mark all of
4232 // the interesting ones via PreloadIdentifierOffsets.
4233 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4234 IdEnd = PP.getIdentifierTable().end();
4236 Id->second->setOutOfDate(true);
4238 // Mark selectors as out of date.
4239 for (auto Sel : SelectorGeneration)
4240 SelectorOutOfDate[Sel.first] = true;
4242 // Resolve any unresolved module exports.
4243 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4244 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4245 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4246 Module *ResolvedMod = getSubmodule(GlobalID);
4248 switch (Unresolved.Kind) {
4249 case UnresolvedModuleRef::Conflict:
4251 Module::Conflict Conflict;
4252 Conflict.Other = ResolvedMod;
4253 Conflict.Message = Unresolved.String.str();
4254 Unresolved.Mod->Conflicts.push_back(Conflict);
4258 case UnresolvedModuleRef::Import:
4260 Unresolved.Mod->Imports.insert(ResolvedMod);
4263 case UnresolvedModuleRef::Export:
4264 if (ResolvedMod || Unresolved.IsWildcard)
4265 Unresolved.Mod->Exports.push_back(
4266 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4270 UnresolvedModuleRefs.clear();
4273 Imported->append(ImportedModules.begin(),
4274 ImportedModules.end());
4276 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4277 // Might be unnecessary as use declarations are only used to build the
4281 InitializeContext();
4286 if (DeserializationListener)
4287 DeserializationListener->ReaderInitialized(this);
4289 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4290 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4291 // If this AST file is a precompiled preamble, then set the
4292 // preamble file ID of the source manager to the file source file
4293 // from which the preamble was built.
4294 if (Type == MK_Preamble) {
4295 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4296 } else if (Type == MK_MainFile) {
4297 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4301 // For any Objective-C class definitions we have already loaded, make sure
4302 // that we load any additional categories.
4304 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4305 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4306 ObjCClassesLoaded[I],
4307 PreviousGeneration);
4311 if (PP.getHeaderSearchInfo()
4312 .getHeaderSearchOpts()
4313 .ModulesValidateOncePerBuildSession) {
4314 // Now we are certain that the module and all modules it depends on are
4315 // up to date. Create or update timestamp files for modules that are
4316 // located in the module cache (not for PCH files that could be anywhere
4317 // in the filesystem).
4318 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4319 ImportedModule &M = Loaded[I];
4320 if (M.Mod->Kind == MK_ImplicitModule) {
4321 updateModuleTimestamp(*M.Mod);
4329 static ASTFileSignature readASTFileSignature(StringRef PCH);
4331 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4332 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4333 // FIXME checking magic headers is done in other places such as
4334 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4335 // always done the same. Unify it all with a helper.
4336 if (!Stream.canSkipToPos(4))
4337 return llvm::createStringError(std::errc::illegal_byte_sequence,
4338 "file too small to contain AST file magic");
4339 for (unsigned C : {'C', 'P', 'C', 'H'})
4340 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4342 return llvm::createStringError(
4343 std::errc::illegal_byte_sequence,
4344 "file doesn't start with AST file magic");
4346 return Res.takeError();
4347 return llvm::Error::success();
4350 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4354 case MK_ImplicitModule:
4355 case MK_ExplicitModule:
4356 case MK_PrebuiltModule:
4360 return 2; // main source file
4362 llvm_unreachable("unknown module kind");
4365 ASTReader::ASTReadResult
4366 ASTReader::ReadASTCore(StringRef FileName,
4368 SourceLocation ImportLoc,
4369 ModuleFile *ImportedBy,
4370 SmallVectorImpl<ImportedModule> &Loaded,
4371 off_t ExpectedSize, time_t ExpectedModTime,
4372 ASTFileSignature ExpectedSignature,
4373 unsigned ClientLoadCapabilities) {
4375 std::string ErrorStr;
4376 ModuleManager::AddModuleResult AddResult
4377 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4378 getGeneration(), ExpectedSize, ExpectedModTime,
4379 ExpectedSignature, readASTFileSignature,
4382 switch (AddResult) {
4383 case ModuleManager::AlreadyLoaded:
4384 Diag(diag::remark_module_import)
4385 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4386 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4389 case ModuleManager::NewlyLoaded:
4390 // Load module file below.
4393 case ModuleManager::Missing:
4394 // The module file was missing; if the client can handle that, return
4396 if (ClientLoadCapabilities & ARR_Missing)
4399 // Otherwise, return an error.
4400 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4401 << FileName << !ErrorStr.empty()
4405 case ModuleManager::OutOfDate:
4406 // We couldn't load the module file because it is out-of-date. If the
4407 // client can handle out-of-date, return it.
4408 if (ClientLoadCapabilities & ARR_OutOfDate)
4411 // Otherwise, return an error.
4412 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4413 << FileName << !ErrorStr.empty()
4418 assert(M && "Missing module file");
4420 bool ShouldFinalizePCM = false;
4421 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4422 auto &MC = getModuleManager().getModuleCache();
4423 if (ShouldFinalizePCM)
4424 MC.finalizePCM(FileName);
4426 MC.tryToDropPCM(FileName);
4429 BitstreamCursor &Stream = F.Stream;
4430 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4431 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4433 // Sniff for the signature.
4434 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4435 Diag(diag::err_module_file_invalid)
4436 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4440 // This is used for compatibility with older PCH formats.
4441 bool HaveReadControlBlock = false;
4443 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4445 Error(MaybeEntry.takeError());
4448 llvm::BitstreamEntry Entry = MaybeEntry.get();
4450 switch (Entry.Kind) {
4451 case llvm::BitstreamEntry::Error:
4452 case llvm::BitstreamEntry::Record:
4453 case llvm::BitstreamEntry::EndBlock:
4454 Error("invalid record at top-level of AST file");
4457 case llvm::BitstreamEntry::SubBlock:
4462 case CONTROL_BLOCK_ID:
4463 HaveReadControlBlock = true;
4464 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4466 // Check that we didn't try to load a non-module AST file as a module.
4468 // FIXME: Should we also perform the converse check? Loading a module as
4469 // a PCH file sort of works, but it's a bit wonky.
4470 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4471 Type == MK_PrebuiltModule) &&
4472 F.ModuleName.empty()) {
4473 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4474 if (Result != OutOfDate ||
4475 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4476 Diag(diag::err_module_file_not_module) << FileName;
4481 case Failure: return Failure;
4482 case Missing: return Missing;
4483 case OutOfDate: return OutOfDate;
4484 case VersionMismatch: return VersionMismatch;
4485 case ConfigurationMismatch: return ConfigurationMismatch;
4486 case HadErrors: return HadErrors;
4491 if (!HaveReadControlBlock) {
4492 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4493 Diag(diag::err_pch_version_too_old);
4494 return VersionMismatch;
4497 // Record that we've loaded this module.
4498 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4499 ShouldFinalizePCM = true;
4502 case UNHASHED_CONTROL_BLOCK_ID:
4503 // This block is handled using look-ahead during ReadControlBlock. We
4504 // shouldn't get here!
4505 Error("malformed block record in AST file");
4509 if (llvm::Error Err = Stream.SkipBlock()) {
4510 Error(std::move(Err));
4517 llvm_unreachable("unexpected break; expected return");
4520 ASTReader::ASTReadResult
4521 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4522 unsigned ClientLoadCapabilities) {
4523 const HeaderSearchOptions &HSOpts =
4524 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4525 bool AllowCompatibleConfigurationMismatch =
4526 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4528 ASTReadResult Result = readUnhashedControlBlockImpl(
4529 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4531 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4533 // If F was directly imported by another module, it's implicitly validated by
4534 // the importing module.
4535 if (DisableValidation || WasImportedBy ||
4536 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4539 if (Result == Failure) {
4540 Error("malformed block record in AST file");
4544 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4545 // If this module has already been finalized in the ModuleCache, we're stuck
4546 // with it; we can only load a single version of each module.
4548 // This can happen when a module is imported in two contexts: in one, as a
4549 // user module; in another, as a system module (due to an import from
4550 // another module marked with the [system] flag). It usually indicates a
4551 // bug in the module map: this module should also be marked with [system].
4553 // If -Wno-system-headers (the default), and the first import is as a
4554 // system module, then validation will fail during the as-user import,
4555 // since -Werror flags won't have been validated. However, it's reasonable
4556 // to treat this consistently as a system module.
4558 // If -Wsystem-headers, the PCM on disk was built with
4559 // -Wno-system-headers, and the first import is as a user module, then
4560 // validation will fail during the as-system import since the PCM on disk
4561 // doesn't guarantee that -Werror was respected. However, the -Werror
4562 // flags were checked during the initial as-user import.
4563 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4564 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4572 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4573 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4574 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4575 bool ValidateDiagnosticOptions) {
4576 // Initialize a stream.
4577 BitstreamCursor Stream(StreamData);
4579 // Sniff for the signature.
4580 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4581 // FIXME this drops the error on the floor.
4582 consumeError(std::move(Err));
4586 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4587 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4590 // Read all of the records in the options block.
4592 ASTReadResult Result = Success;
4594 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4596 // FIXME this drops the error on the floor.
4597 consumeError(MaybeEntry.takeError());
4600 llvm::BitstreamEntry Entry = MaybeEntry.get();
4602 switch (Entry.Kind) {
4603 case llvm::BitstreamEntry::Error:
4604 case llvm::BitstreamEntry::SubBlock:
4607 case llvm::BitstreamEntry::EndBlock:
4610 case llvm::BitstreamEntry::Record:
4611 // The interesting case.
4615 // Read and process a record.
4617 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4618 if (!MaybeRecordType) {
4619 // FIXME this drops the error.
4622 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4625 std::copy(Record.begin(), Record.end(), F->Signature.data());
4627 case DIAGNOSTIC_OPTIONS: {
4628 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4629 if (Listener && ValidateDiagnosticOptions &&
4630 !AllowCompatibleConfigurationMismatch &&
4631 ParseDiagnosticOptions(Record, Complain, *Listener))
4632 Result = OutOfDate; // Don't return early. Read the signature.
4635 case DIAG_PRAGMA_MAPPINGS:
4638 if (F->PragmaDiagMappings.empty())
4639 F->PragmaDiagMappings.swap(Record);
4641 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4642 Record.begin(), Record.end());
4648 /// Parse a record and blob containing module file extension metadata.
4649 static bool parseModuleFileExtensionMetadata(
4650 const SmallVectorImpl<uint64_t> &Record,
4652 ModuleFileExtensionMetadata &Metadata) {
4653 if (Record.size() < 4) return true;
4655 Metadata.MajorVersion = Record[0];
4656 Metadata.MinorVersion = Record[1];
4658 unsigned BlockNameLen = Record[2];
4659 unsigned UserInfoLen = Record[3];
4661 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4663 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4664 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4665 Blob.data() + BlockNameLen + UserInfoLen);
4669 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4670 BitstreamCursor &Stream = F.Stream;
4674 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4676 Error(MaybeEntry.takeError());
4679 llvm::BitstreamEntry Entry = MaybeEntry.get();
4681 switch (Entry.Kind) {
4682 case llvm::BitstreamEntry::SubBlock:
4683 if (llvm::Error Err = Stream.SkipBlock()) {
4684 Error(std::move(Err));
4689 case llvm::BitstreamEntry::EndBlock:
4692 case llvm::BitstreamEntry::Error:
4695 case llvm::BitstreamEntry::Record:
4701 Expected<unsigned> MaybeRecCode =
4702 Stream.readRecord(Entry.ID, Record, &Blob);
4703 if (!MaybeRecCode) {
4704 Error(MaybeRecCode.takeError());
4707 switch (MaybeRecCode.get()) {
4708 case EXTENSION_METADATA: {
4709 ModuleFileExtensionMetadata Metadata;
4710 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4713 // Find a module file extension with this block name.
4714 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4715 if (Known == ModuleFileExtensions.end()) break;
4718 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4720 F.ExtensionReaders.push_back(std::move(Reader));
4731 void ASTReader::InitializeContext() {
4732 assert(ContextObj && "no context to initialize");
4733 ASTContext &Context = *ContextObj;
4735 // If there's a listener, notify them that we "read" the translation unit.
4736 if (DeserializationListener)
4737 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4738 Context.getTranslationUnitDecl());
4740 // FIXME: Find a better way to deal with collisions between these
4741 // built-in types. Right now, we just ignore the problem.
4743 // Load the special types.
4744 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4745 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4746 if (!Context.CFConstantStringTypeDecl)
4747 Context.setCFConstantStringType(GetType(String));
4750 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4751 QualType FileType = GetType(File);
4752 if (FileType.isNull()) {
4753 Error("FILE type is NULL");
4757 if (!Context.FILEDecl) {
4758 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4759 Context.setFILEDecl(Typedef->getDecl());
4761 const TagType *Tag = FileType->getAs<TagType>();
4763 Error("Invalid FILE type in AST file");
4766 Context.setFILEDecl(Tag->getDecl());
4771 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4772 QualType Jmp_bufType = GetType(Jmp_buf);
4773 if (Jmp_bufType.isNull()) {
4774 Error("jmp_buf type is NULL");
4778 if (!Context.jmp_bufDecl) {
4779 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4780 Context.setjmp_bufDecl(Typedef->getDecl());
4782 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4784 Error("Invalid jmp_buf type in AST file");
4787 Context.setjmp_bufDecl(Tag->getDecl());
4792 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4793 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4794 if (Sigjmp_bufType.isNull()) {
4795 Error("sigjmp_buf type is NULL");
4799 if (!Context.sigjmp_bufDecl) {
4800 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4801 Context.setsigjmp_bufDecl(Typedef->getDecl());
4803 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4804 assert(Tag && "Invalid sigjmp_buf type in AST file");
4805 Context.setsigjmp_bufDecl(Tag->getDecl());
4810 if (unsigned ObjCIdRedef
4811 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4812 if (Context.ObjCIdRedefinitionType.isNull())
4813 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4816 if (unsigned ObjCClassRedef
4817 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4818 if (Context.ObjCClassRedefinitionType.isNull())
4819 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4822 if (unsigned ObjCSelRedef
4823 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4824 if (Context.ObjCSelRedefinitionType.isNull())
4825 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4828 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4829 QualType Ucontext_tType = GetType(Ucontext_t);
4830 if (Ucontext_tType.isNull()) {
4831 Error("ucontext_t type is NULL");
4835 if (!Context.ucontext_tDecl) {
4836 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4837 Context.setucontext_tDecl(Typedef->getDecl());
4839 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4840 assert(Tag && "Invalid ucontext_t type in AST file");
4841 Context.setucontext_tDecl(Tag->getDecl());
4847 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4849 // If there were any CUDA special declarations, deserialize them.
4850 if (!CUDASpecialDeclRefs.empty()) {
4851 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4852 Context.setcudaConfigureCallDecl(
4853 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4856 // Re-export any modules that were imported by a non-module AST file.
4857 // FIXME: This does not make macro-only imports visible again.
4858 for (auto &Import : ImportedModules) {
4859 if (Module *Imported = getSubmodule(Import.ID)) {
4860 makeModuleVisible(Imported, Module::AllVisible,
4861 /*ImportLoc=*/Import.ImportLoc);
4862 if (Import.ImportLoc.isValid())
4863 PP.makeModuleVisible(Imported, Import.ImportLoc);
4864 // FIXME: should we tell Sema to make the module visible too?
4867 ImportedModules.clear();
4870 void ASTReader::finalizeForWriting() {
4871 // Nothing to do for now.
4874 /// Reads and return the signature record from \p PCH's control block, or
4876 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4877 BitstreamCursor Stream(PCH);
4878 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4879 // FIXME this drops the error on the floor.
4880 consumeError(std::move(Err));
4881 return ASTFileSignature();
4884 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4885 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4886 return ASTFileSignature();
4888 // Scan for SIGNATURE inside the diagnostic options block.
4889 ASTReader::RecordData Record;
4891 Expected<llvm::BitstreamEntry> MaybeEntry =
4892 Stream.advanceSkippingSubblocks();
4894 // FIXME this drops the error on the floor.
4895 consumeError(MaybeEntry.takeError());
4896 return ASTFileSignature();
4898 llvm::BitstreamEntry Entry = MaybeEntry.get();
4900 if (Entry.Kind != llvm::BitstreamEntry::Record)
4901 return ASTFileSignature();
4905 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4907 // FIXME this drops the error on the floor.
4908 consumeError(MaybeRecord.takeError());
4909 return ASTFileSignature();
4911 if (SIGNATURE == MaybeRecord.get())
4912 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4913 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4917 /// Retrieve the name of the original source file name
4918 /// directly from the AST file, without actually loading the AST
4920 std::string ASTReader::getOriginalSourceFile(
4921 const std::string &ASTFileName, FileManager &FileMgr,
4922 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4923 // Open the AST file.
4924 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4926 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4927 << ASTFileName << Buffer.getError().message();
4928 return std::string();
4931 // Initialize the stream
4932 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4934 // Sniff for the signature.
4935 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4936 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
4937 return std::string();
4940 // Scan for the CONTROL_BLOCK_ID block.
4941 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4942 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4943 return std::string();
4946 // Scan for ORIGINAL_FILE inside the control block.
4949 Expected<llvm::BitstreamEntry> MaybeEntry =
4950 Stream.advanceSkippingSubblocks();
4952 // FIXME this drops errors on the floor.
4953 consumeError(MaybeEntry.takeError());
4954 return std::string();
4956 llvm::BitstreamEntry Entry = MaybeEntry.get();
4958 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4959 return std::string();
4961 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4962 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4963 return std::string();
4968 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4970 // FIXME this drops the errors on the floor.
4971 consumeError(MaybeRecord.takeError());
4972 return std::string();
4974 if (ORIGINAL_FILE == MaybeRecord.get())
4981 class SimplePCHValidator : public ASTReaderListener {
4982 const LangOptions &ExistingLangOpts;
4983 const TargetOptions &ExistingTargetOpts;
4984 const PreprocessorOptions &ExistingPPOpts;
4985 std::string ExistingModuleCachePath;
4986 FileManager &FileMgr;
4989 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4990 const TargetOptions &ExistingTargetOpts,
4991 const PreprocessorOptions &ExistingPPOpts,
4992 StringRef ExistingModuleCachePath,
4993 FileManager &FileMgr)
4994 : ExistingLangOpts(ExistingLangOpts),
4995 ExistingTargetOpts(ExistingTargetOpts),
4996 ExistingPPOpts(ExistingPPOpts),
4997 ExistingModuleCachePath(ExistingModuleCachePath),
5000 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5001 bool AllowCompatibleDifferences) override {
5002 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5003 AllowCompatibleDifferences);
5006 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5007 bool AllowCompatibleDifferences) override {
5008 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5009 AllowCompatibleDifferences);
5012 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5013 StringRef SpecificModuleCachePath,
5014 bool Complain) override {
5015 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5016 ExistingModuleCachePath,
5017 nullptr, ExistingLangOpts);
5020 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5022 std::string &SuggestedPredefines) override {
5023 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5024 SuggestedPredefines, ExistingLangOpts);
5030 bool ASTReader::readASTFileControlBlock(
5031 StringRef Filename, FileManager &FileMgr,
5032 const PCHContainerReader &PCHContainerRdr,
5033 bool FindModuleFileExtensions,
5034 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5035 // Open the AST file.
5036 // FIXME: This allows use of the VFS; we do not allow use of the
5037 // VFS when actually loading a module.
5038 auto Buffer = FileMgr.getBufferForFile(Filename);
5043 // Initialize the stream
5044 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5045 BitstreamCursor Stream(Bytes);
5047 // Sniff for the signature.
5048 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5049 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5053 // Scan for the CONTROL_BLOCK_ID block.
5054 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5057 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5058 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5059 bool NeedsImports = Listener.needsImportVisitation();
5060 BitstreamCursor InputFilesCursor;
5063 std::string ModuleDir;
5064 bool DoneWithControlBlock = false;
5065 while (!DoneWithControlBlock) {
5066 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5068 // FIXME this drops the error on the floor.
5069 consumeError(MaybeEntry.takeError());
5072 llvm::BitstreamEntry Entry = MaybeEntry.get();
5074 switch (Entry.Kind) {
5075 case llvm::BitstreamEntry::SubBlock: {
5077 case OPTIONS_BLOCK_ID: {
5078 std::string IgnoredSuggestedPredefines;
5079 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5080 /*AllowCompatibleConfigurationMismatch*/ false,
5081 Listener, IgnoredSuggestedPredefines) != Success)
5086 case INPUT_FILES_BLOCK_ID:
5087 InputFilesCursor = Stream;
5088 if (llvm::Error Err = Stream.SkipBlock()) {
5089 // FIXME this drops the error on the floor.
5090 consumeError(std::move(Err));
5093 if (NeedsInputFiles &&
5094 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5099 if (llvm::Error Err = Stream.SkipBlock()) {
5100 // FIXME this drops the error on the floor.
5101 consumeError(std::move(Err));
5110 case llvm::BitstreamEntry::EndBlock:
5111 DoneWithControlBlock = true;
5114 case llvm::BitstreamEntry::Error:
5117 case llvm::BitstreamEntry::Record:
5121 if (DoneWithControlBlock) break;
5125 Expected<unsigned> MaybeRecCode =
5126 Stream.readRecord(Entry.ID, Record, &Blob);
5127 if (!MaybeRecCode) {
5128 // FIXME this drops the error.
5131 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5133 if (Record[0] != VERSION_MAJOR)
5135 if (Listener.ReadFullVersionInformation(Blob))
5139 Listener.ReadModuleName(Blob);
5141 case MODULE_DIRECTORY:
5144 case MODULE_MAP_FILE: {
5146 auto Path = ReadString(Record, Idx);
5147 ResolveImportedPath(Path, ModuleDir);
5148 Listener.ReadModuleMapFile(Path);
5151 case INPUT_FILE_OFFSETS: {
5152 if (!NeedsInputFiles)
5155 unsigned NumInputFiles = Record[0];
5156 unsigned NumUserFiles = Record[1];
5157 const llvm::support::unaligned_uint64_t *InputFileOffs =
5158 (const llvm::support::unaligned_uint64_t *)Blob.data();
5159 for (unsigned I = 0; I != NumInputFiles; ++I) {
5160 // Go find this input file.
5161 bool isSystemFile = I >= NumUserFiles;
5163 if (isSystemFile && !NeedsSystemInputFiles)
5164 break; // the rest are system input files
5166 BitstreamCursor &Cursor = InputFilesCursor;
5167 SavedStreamPosition SavedPosition(Cursor);
5168 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5169 // FIXME this drops errors on the floor.
5170 consumeError(std::move(Err));
5173 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5175 // FIXME this drops errors on the floor.
5176 consumeError(MaybeCode.takeError());
5178 unsigned Code = MaybeCode.get();
5182 bool shouldContinue = false;
5183 Expected<unsigned> MaybeRecordType =
5184 Cursor.readRecord(Code, Record, &Blob);
5185 if (!MaybeRecordType) {
5186 // FIXME this drops errors on the floor.
5187 consumeError(MaybeRecordType.takeError());
5189 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5191 bool Overridden = static_cast<bool>(Record[3]);
5192 std::string Filename = Blob;
5193 ResolveImportedPath(Filename, ModuleDir);
5194 shouldContinue = Listener.visitInputFile(
5195 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5198 if (!shouldContinue)
5208 unsigned Idx = 0, N = Record.size();
5210 // Read information about the AST file.
5211 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5212 std::string ModuleName = ReadString(Record, Idx);
5213 std::string Filename = ReadString(Record, Idx);
5214 ResolveImportedPath(Filename, ModuleDir);
5215 Listener.visitImport(ModuleName, Filename);
5221 // No other validation to perform.
5226 // Look for module file extension blocks, if requested.
5227 if (FindModuleFileExtensions) {
5228 BitstreamCursor SavedStream = Stream;
5229 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5230 bool DoneWithExtensionBlock = false;
5231 while (!DoneWithExtensionBlock) {
5232 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5234 // FIXME this drops the error.
5237 llvm::BitstreamEntry Entry = MaybeEntry.get();
5239 switch (Entry.Kind) {
5240 case llvm::BitstreamEntry::SubBlock:
5241 if (llvm::Error Err = Stream.SkipBlock()) {
5242 // FIXME this drops the error on the floor.
5243 consumeError(std::move(Err));
5248 case llvm::BitstreamEntry::EndBlock:
5249 DoneWithExtensionBlock = true;
5252 case llvm::BitstreamEntry::Error:
5255 case llvm::BitstreamEntry::Record:
5261 Expected<unsigned> MaybeRecCode =
5262 Stream.readRecord(Entry.ID, Record, &Blob);
5263 if (!MaybeRecCode) {
5264 // FIXME this drops the error.
5267 switch (MaybeRecCode.get()) {
5268 case EXTENSION_METADATA: {
5269 ModuleFileExtensionMetadata Metadata;
5270 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5273 Listener.readModuleFileExtension(Metadata);
5279 Stream = SavedStream;
5282 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5283 if (readUnhashedControlBlockImpl(
5284 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5285 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5286 ValidateDiagnosticOptions) != Success)
5292 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5293 const PCHContainerReader &PCHContainerRdr,
5294 const LangOptions &LangOpts,
5295 const TargetOptions &TargetOpts,
5296 const PreprocessorOptions &PPOpts,
5297 StringRef ExistingModuleCachePath) {
5298 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5299 ExistingModuleCachePath, FileMgr);
5300 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5301 /*FindModuleFileExtensions=*/false,
5303 /*ValidateDiagnosticOptions=*/true);
5306 ASTReader::ASTReadResult
5307 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5308 // Enter the submodule block.
5309 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5310 Error(std::move(Err));
5314 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5316 Module *CurrentModule = nullptr;
5319 Expected<llvm::BitstreamEntry> MaybeEntry =
5320 F.Stream.advanceSkippingSubblocks();
5322 Error(MaybeEntry.takeError());
5325 llvm::BitstreamEntry Entry = MaybeEntry.get();
5327 switch (Entry.Kind) {
5328 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5329 case llvm::BitstreamEntry::Error:
5330 Error("malformed block record in AST file");
5332 case llvm::BitstreamEntry::EndBlock:
5334 case llvm::BitstreamEntry::Record:
5335 // The interesting case.
5342 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5344 Error(MaybeKind.takeError());
5347 unsigned Kind = MaybeKind.get();
5349 if ((Kind == SUBMODULE_METADATA) != First) {
5350 Error("submodule metadata record should be at beginning of block");
5355 // Submodule information is only valid if we have a current module.
5356 // FIXME: Should we error on these cases?
5357 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5358 Kind != SUBMODULE_DEFINITION)
5362 default: // Default behavior: ignore.
5365 case SUBMODULE_DEFINITION: {
5366 if (Record.size() < 12) {
5367 Error("malformed module definition");
5371 StringRef Name = Blob;
5373 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5374 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5375 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5376 bool IsFramework = Record[Idx++];
5377 bool IsExplicit = Record[Idx++];
5378 bool IsSystem = Record[Idx++];
5379 bool IsExternC = Record[Idx++];
5380 bool InferSubmodules = Record[Idx++];
5381 bool InferExplicitSubmodules = Record[Idx++];
5382 bool InferExportWildcard = Record[Idx++];
5383 bool ConfigMacrosExhaustive = Record[Idx++];
5384 bool ModuleMapIsPrivate = Record[Idx++];
5386 Module *ParentModule = nullptr;
5388 ParentModule = getSubmodule(Parent);
5390 // Retrieve this (sub)module from the module map, creating it if
5393 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5396 // FIXME: set the definition loc for CurrentModule, or call
5397 // ModMap.setInferredModuleAllowedBy()
5399 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5400 if (GlobalIndex >= SubmodulesLoaded.size() ||
5401 SubmodulesLoaded[GlobalIndex]) {
5402 Error("too many submodules");
5406 if (!ParentModule) {
5407 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5408 // Don't emit module relocation error if we have -fno-validate-pch
5409 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5410 CurFile != F.File) {
5411 if (!Diags.isDiagnosticInFlight()) {
5412 Diag(diag::err_module_file_conflict)
5413 << CurrentModule->getTopLevelModuleName()
5414 << CurFile->getName()
5415 << F.File->getName();
5421 CurrentModule->setASTFile(F.File);
5422 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5425 CurrentModule->Kind = Kind;
5426 CurrentModule->Signature = F.Signature;
5427 CurrentModule->IsFromModuleFile = true;
5428 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5429 CurrentModule->IsExternC = IsExternC;
5430 CurrentModule->InferSubmodules = InferSubmodules;
5431 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5432 CurrentModule->InferExportWildcard = InferExportWildcard;
5433 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5434 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5435 if (DeserializationListener)
5436 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5438 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5440 // Clear out data that will be replaced by what is in the module file.
5441 CurrentModule->LinkLibraries.clear();
5442 CurrentModule->ConfigMacros.clear();
5443 CurrentModule->UnresolvedConflicts.clear();
5444 CurrentModule->Conflicts.clear();
5446 // The module is available unless it's missing a requirement; relevant
5447 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5448 // Missing headers that were present when the module was built do not
5449 // make it unavailable -- if we got this far, this must be an explicitly
5450 // imported module file.
5451 CurrentModule->Requirements.clear();
5452 CurrentModule->MissingHeaders.clear();
5453 CurrentModule->IsMissingRequirement =
5454 ParentModule && ParentModule->IsMissingRequirement;
5455 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5459 case SUBMODULE_UMBRELLA_HEADER: {
5460 std::string Filename = Blob;
5461 ResolveImportedPath(F, Filename);
5462 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
5463 if (!CurrentModule->getUmbrellaHeader())
5464 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
5465 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
5466 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5467 Error("mismatched umbrella headers in submodule");
5474 case SUBMODULE_HEADER:
5475 case SUBMODULE_EXCLUDED_HEADER:
5476 case SUBMODULE_PRIVATE_HEADER:
5477 // We lazily associate headers with their modules via the HeaderInfo table.
5478 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5479 // of complete filenames or remove it entirely.
5482 case SUBMODULE_TEXTUAL_HEADER:
5483 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5484 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5488 case SUBMODULE_TOPHEADER:
5489 CurrentModule->addTopHeaderFilename(Blob);
5492 case SUBMODULE_UMBRELLA_DIR: {
5493 std::string Dirname = Blob;
5494 ResolveImportedPath(F, Dirname);
5495 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5496 if (!CurrentModule->getUmbrellaDir())
5497 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
5498 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
5499 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5500 Error("mismatched umbrella directories in submodule");
5507 case SUBMODULE_METADATA: {
5508 F.BaseSubmoduleID = getTotalNumSubmodules();
5509 F.LocalNumSubmodules = Record[0];
5510 unsigned LocalBaseSubmoduleID = Record[1];
5511 if (F.LocalNumSubmodules > 0) {
5512 // Introduce the global -> local mapping for submodules within this
5514 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5516 // Introduce the local -> global mapping for submodules within this
5518 F.SubmoduleRemap.insertOrReplace(
5519 std::make_pair(LocalBaseSubmoduleID,
5520 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5522 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5527 case SUBMODULE_IMPORTS:
5528 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5529 UnresolvedModuleRef Unresolved;
5530 Unresolved.File = &F;
5531 Unresolved.Mod = CurrentModule;
5532 Unresolved.ID = Record[Idx];
5533 Unresolved.Kind = UnresolvedModuleRef::Import;
5534 Unresolved.IsWildcard = false;
5535 UnresolvedModuleRefs.push_back(Unresolved);
5539 case SUBMODULE_EXPORTS:
5540 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5541 UnresolvedModuleRef Unresolved;
5542 Unresolved.File = &F;
5543 Unresolved.Mod = CurrentModule;
5544 Unresolved.ID = Record[Idx];
5545 Unresolved.Kind = UnresolvedModuleRef::Export;
5546 Unresolved.IsWildcard = Record[Idx + 1];
5547 UnresolvedModuleRefs.push_back(Unresolved);
5550 // Once we've loaded the set of exports, there's no reason to keep
5551 // the parsed, unresolved exports around.
5552 CurrentModule->UnresolvedExports.clear();
5555 case SUBMODULE_REQUIRES:
5556 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5557 PP.getTargetInfo());
5560 case SUBMODULE_LINK_LIBRARY:
5561 ModMap.resolveLinkAsDependencies(CurrentModule);
5562 CurrentModule->LinkLibraries.push_back(
5563 Module::LinkLibrary(Blob, Record[0]));
5566 case SUBMODULE_CONFIG_MACRO:
5567 CurrentModule->ConfigMacros.push_back(Blob.str());
5570 case SUBMODULE_CONFLICT: {
5571 UnresolvedModuleRef Unresolved;
5572 Unresolved.File = &F;
5573 Unresolved.Mod = CurrentModule;
5574 Unresolved.ID = Record[0];
5575 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5576 Unresolved.IsWildcard = false;
5577 Unresolved.String = Blob;
5578 UnresolvedModuleRefs.push_back(Unresolved);
5582 case SUBMODULE_INITIALIZERS: {
5585 SmallVector<uint32_t, 16> Inits;
5586 for (auto &ID : Record)
5587 Inits.push_back(getGlobalDeclID(F, ID));
5588 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5592 case SUBMODULE_EXPORT_AS:
5593 CurrentModule->ExportAsModule = Blob.str();
5594 ModMap.addLinkAsDependency(CurrentModule);
5600 /// Parse the record that corresponds to a LangOptions data
5603 /// This routine parses the language options from the AST file and then gives
5604 /// them to the AST listener if one is set.
5606 /// \returns true if the listener deems the file unacceptable, false otherwise.
5607 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5609 ASTReaderListener &Listener,
5610 bool AllowCompatibleDifferences) {
5611 LangOptions LangOpts;
5613 #define LANGOPT(Name, Bits, Default, Description) \
5614 LangOpts.Name = Record[Idx++];
5615 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5616 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5617 #include "clang/Basic/LangOptions.def"
5618 #define SANITIZER(NAME, ID) \
5619 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5620 #include "clang/Basic/Sanitizers.def"
5622 for (unsigned N = Record[Idx++]; N; --N)
5623 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5625 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5626 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5627 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5629 LangOpts.CurrentModule = ReadString(Record, Idx);
5632 for (unsigned N = Record[Idx++]; N; --N) {
5633 LangOpts.CommentOpts.BlockCommandNames.push_back(
5634 ReadString(Record, Idx));
5636 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5638 // OpenMP offloading options.
5639 for (unsigned N = Record[Idx++]; N; --N) {
5640 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5643 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5645 return Listener.ReadLanguageOptions(LangOpts, Complain,
5646 AllowCompatibleDifferences);
5649 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5650 ASTReaderListener &Listener,
5651 bool AllowCompatibleDifferences) {
5653 TargetOptions TargetOpts;
5654 TargetOpts.Triple = ReadString(Record, Idx);
5655 TargetOpts.CPU = ReadString(Record, Idx);
5656 TargetOpts.ABI = ReadString(Record, Idx);
5657 for (unsigned N = Record[Idx++]; N; --N) {
5658 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5660 for (unsigned N = Record[Idx++]; N; --N) {
5661 TargetOpts.Features.push_back(ReadString(Record, Idx));
5664 return Listener.ReadTargetOptions(TargetOpts, Complain,
5665 AllowCompatibleDifferences);
5668 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5669 ASTReaderListener &Listener) {
5670 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5672 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5673 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5674 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5675 #include "clang/Basic/DiagnosticOptions.def"
5677 for (unsigned N = Record[Idx++]; N; --N)
5678 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5679 for (unsigned N = Record[Idx++]; N; --N)
5680 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5682 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5685 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5686 ASTReaderListener &Listener) {
5687 FileSystemOptions FSOpts;
5689 FSOpts.WorkingDir = ReadString(Record, Idx);
5690 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5693 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5695 ASTReaderListener &Listener) {
5696 HeaderSearchOptions HSOpts;
5698 HSOpts.Sysroot = ReadString(Record, Idx);
5701 for (unsigned N = Record[Idx++]; N; --N) {
5702 std::string Path = ReadString(Record, Idx);
5703 frontend::IncludeDirGroup Group
5704 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5705 bool IsFramework = Record[Idx++];
5706 bool IgnoreSysRoot = Record[Idx++];
5707 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5711 // System header prefixes.
5712 for (unsigned N = Record[Idx++]; N; --N) {
5713 std::string Prefix = ReadString(Record, Idx);
5714 bool IsSystemHeader = Record[Idx++];
5715 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5718 HSOpts.ResourceDir = ReadString(Record, Idx);
5719 HSOpts.ModuleCachePath = ReadString(Record, Idx);
5720 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5721 HSOpts.DisableModuleHash = Record[Idx++];
5722 HSOpts.ImplicitModuleMaps = Record[Idx++];
5723 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5724 HSOpts.UseBuiltinIncludes = Record[Idx++];
5725 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5726 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5727 HSOpts.UseLibcxx = Record[Idx++];
5728 std::string SpecificModuleCachePath = ReadString(Record, Idx);
5730 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5734 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5736 ASTReaderListener &Listener,
5737 std::string &SuggestedPredefines) {
5738 PreprocessorOptions PPOpts;
5741 // Macro definitions/undefs
5742 for (unsigned N = Record[Idx++]; N; --N) {
5743 std::string Macro = ReadString(Record, Idx);
5744 bool IsUndef = Record[Idx++];
5745 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5749 for (unsigned N = Record[Idx++]; N; --N) {
5750 PPOpts.Includes.push_back(ReadString(Record, Idx));
5754 for (unsigned N = Record[Idx++]; N; --N) {
5755 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5758 PPOpts.UsePredefines = Record[Idx++];
5759 PPOpts.DetailedRecord = Record[Idx++];
5760 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5761 PPOpts.ObjCXXARCStandardLibrary =
5762 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5763 SuggestedPredefines.clear();
5764 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5765 SuggestedPredefines);
5768 std::pair<ModuleFile *, unsigned>
5769 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5770 GlobalPreprocessedEntityMapType::iterator
5771 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5772 assert(I != GlobalPreprocessedEntityMap.end() &&
5773 "Corrupted global preprocessed entity map");
5774 ModuleFile *M = I->second;
5775 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5776 return std::make_pair(M, LocalIndex);
5779 llvm::iterator_range<PreprocessingRecord::iterator>
5780 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5781 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5782 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5783 Mod.NumPreprocessedEntities);
5785 return llvm::make_range(PreprocessingRecord::iterator(),
5786 PreprocessingRecord::iterator());
5789 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5790 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5791 return llvm::make_range(
5792 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5793 ModuleDeclIterator(this, &Mod,
5794 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5797 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5798 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5799 assert(I != GlobalSkippedRangeMap.end() &&
5800 "Corrupted global skipped range map");
5801 ModuleFile *M = I->second;
5802 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5803 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5804 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5805 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5806 TranslateSourceLocation(*M, RawRange.getEnd()));
5807 assert(Range.isValid());
5811 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5812 PreprocessedEntityID PPID = Index+1;
5813 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5814 ModuleFile &M = *PPInfo.first;
5815 unsigned LocalIndex = PPInfo.second;
5816 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5818 if (!PP.getPreprocessingRecord()) {
5819 Error("no preprocessing record");
5823 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5824 if (llvm::Error Err =
5825 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5826 Error(std::move(Err));
5830 Expected<llvm::BitstreamEntry> MaybeEntry =
5831 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5833 Error(MaybeEntry.takeError());
5836 llvm::BitstreamEntry Entry = MaybeEntry.get();
5838 if (Entry.Kind != llvm::BitstreamEntry::Record)
5842 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5843 TranslateSourceLocation(M, PPOffs.getEnd()));
5844 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5847 Expected<unsigned> MaybeRecType =
5848 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5849 if (!MaybeRecType) {
5850 Error(MaybeRecType.takeError());
5853 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5854 case PPD_MACRO_EXPANSION: {
5855 bool isBuiltin = Record[0];
5856 IdentifierInfo *Name = nullptr;
5857 MacroDefinitionRecord *Def = nullptr;
5859 Name = getLocalIdentifier(M, Record[1]);
5861 PreprocessedEntityID GlobalID =
5862 getGlobalPreprocessedEntityID(M, Record[1]);
5863 Def = cast<MacroDefinitionRecord>(
5864 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5869 ME = new (PPRec) MacroExpansion(Name, Range);
5871 ME = new (PPRec) MacroExpansion(Def, Range);
5876 case PPD_MACRO_DEFINITION: {
5877 // Decode the identifier info and then check again; if the macro is
5878 // still defined and associated with the identifier,
5879 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5880 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5882 if (DeserializationListener)
5883 DeserializationListener->MacroDefinitionRead(PPID, MD);
5888 case PPD_INCLUSION_DIRECTIVE: {
5889 const char *FullFileNameStart = Blob.data() + Record[0];
5890 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5891 const FileEntry *File = nullptr;
5892 if (!FullFileName.empty())
5893 File = PP.getFileManager().getFile(FullFileName);
5895 // FIXME: Stable encoding
5896 InclusionDirective::InclusionKind Kind
5897 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5898 InclusionDirective *ID
5899 = new (PPRec) InclusionDirective(PPRec, Kind,
5900 StringRef(Blob.data(), Record[0]),
5901 Record[1], Record[3],
5908 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5911 /// Find the next module that contains entities and return the ID
5912 /// of the first entry.
5914 /// \param SLocMapI points at a chunk of a module that contains no
5915 /// preprocessed entities or the entities it contains are not the ones we are
5917 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5918 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5920 for (GlobalSLocOffsetMapType::const_iterator
5921 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5922 ModuleFile &M = *SLocMapI->second;
5923 if (M.NumPreprocessedEntities)
5924 return M.BasePreprocessedEntityID;
5927 return getTotalNumPreprocessedEntities();
5932 struct PPEntityComp {
5933 const ASTReader &Reader;
5936 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5938 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5939 SourceLocation LHS = getLoc(L);
5940 SourceLocation RHS = getLoc(R);
5941 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5944 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5945 SourceLocation LHS = getLoc(L);
5946 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5949 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5950 SourceLocation RHS = getLoc(R);
5951 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5954 SourceLocation getLoc(const PPEntityOffset &PPE) const {
5955 return Reader.TranslateSourceLocation(M, PPE.getBegin());
5961 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5962 bool EndsAfter) const {
5963 if (SourceMgr.isLocalSourceLocation(Loc))
5964 return getTotalNumPreprocessedEntities();
5966 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5967 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5968 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5969 "Corrupted global sloc offset map");
5971 if (SLocMapI->second->NumPreprocessedEntities == 0)
5972 return findNextPreprocessedEntity(SLocMapI);
5974 ModuleFile &M = *SLocMapI->second;
5976 using pp_iterator = const PPEntityOffset *;
5978 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5979 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5981 size_t Count = M.NumPreprocessedEntities;
5983 pp_iterator First = pp_begin;
5987 PPI = std::upper_bound(pp_begin, pp_end, Loc,
5988 PPEntityComp(*this, M));
5990 // Do a binary search manually instead of using std::lower_bound because
5991 // The end locations of entities may be unordered (when a macro expansion
5992 // is inside another macro argument), but for this case it is not important
5993 // whether we get the first macro expansion or its containing macro.
5997 std::advance(PPI, Half);
5998 if (SourceMgr.isBeforeInTranslationUnit(
5999 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6002 Count = Count - Half - 1;
6009 return findNextPreprocessedEntity(SLocMapI);
6011 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6014 /// Returns a pair of [Begin, End) indices of preallocated
6015 /// preprocessed entities that \arg Range encompasses.
6016 std::pair<unsigned, unsigned>
6017 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6018 if (Range.isInvalid())
6019 return std::make_pair(0,0);
6020 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6022 PreprocessedEntityID BeginID =
6023 findPreprocessedEntity(Range.getBegin(), false);
6024 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6025 return std::make_pair(BeginID, EndID);
6028 /// Optionally returns true or false if the preallocated preprocessed
6029 /// entity with index \arg Index came from file \arg FID.
6030 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6032 if (FID.isInvalid())
6035 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6036 ModuleFile &M = *PPInfo.first;
6037 unsigned LocalIndex = PPInfo.second;
6038 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6040 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6041 if (Loc.isInvalid())
6044 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6052 /// Visitor used to search for information about a header file.
6053 class HeaderFileInfoVisitor {
6054 const FileEntry *FE;
6055 Optional<HeaderFileInfo> HFI;
6058 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6060 bool operator()(ModuleFile &M) {
6061 HeaderFileInfoLookupTable *Table
6062 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6066 // Look in the on-disk hash table for an entry for this file name.
6067 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6068 if (Pos == Table->end())
6075 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6080 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6081 HeaderFileInfoVisitor Visitor(FE);
6082 ModuleMgr.visit(Visitor);
6083 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6086 return HeaderFileInfo();
6089 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6090 using DiagState = DiagnosticsEngine::DiagState;
6091 SmallVector<DiagState *, 32> DiagStates;
6093 for (ModuleFile &F : ModuleMgr) {
6095 auto &Record = F.PragmaDiagMappings;
6101 auto ReadDiagState =
6102 [&](const DiagState &BasedOn, SourceLocation Loc,
6103 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6104 unsigned BackrefID = Record[Idx++];
6106 return DiagStates[BackrefID - 1];
6108 // A new DiagState was created here.
6109 Diag.DiagStates.push_back(BasedOn);
6110 DiagState *NewState = &Diag.DiagStates.back();
6111 DiagStates.push_back(NewState);
6112 unsigned Size = Record[Idx++];
6113 assert(Idx + Size * 2 <= Record.size() &&
6114 "Invalid data, not enough diag/map pairs");
6116 unsigned DiagID = Record[Idx++];
6117 DiagnosticMapping NewMapping =
6118 DiagnosticMapping::deserialize(Record[Idx++]);
6119 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6122 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6124 // If this mapping was specified as a warning but the severity was
6125 // upgraded due to diagnostic settings, simulate the current diagnostic
6126 // settings (and use a warning).
6127 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6128 NewMapping.setSeverity(diag::Severity::Warning);
6129 NewMapping.setUpgradedFromWarning(false);
6132 Mapping = NewMapping;
6137 // Read the first state.
6138 DiagState *FirstState;
6139 if (F.Kind == MK_ImplicitModule) {
6140 // Implicitly-built modules are reused with different diagnostic
6141 // settings. Use the initial diagnostic state from Diag to simulate this
6142 // compilation's diagnostic settings.
6143 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6144 DiagStates.push_back(FirstState);
6146 // Skip the initial diagnostic state from the serialized module.
6147 assert(Record[1] == 0 &&
6148 "Invalid data, unexpected backref in initial state");
6149 Idx = 3 + Record[2] * 2;
6150 assert(Idx < Record.size() &&
6151 "Invalid data, not enough state change pairs in initial state");
6152 } else if (F.isModule()) {
6153 // For an explicit module, preserve the flags from the module build
6154 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6156 unsigned Flags = Record[Idx++];
6158 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6159 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6160 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6161 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6162 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6163 Initial.ExtBehavior = (diag::Severity)Flags;
6164 FirstState = ReadDiagState(Initial, SourceLocation(), true);
6166 assert(F.OriginalSourceFileID.isValid());
6168 // Set up the root buffer of the module to start with the initial
6169 // diagnostic state of the module itself, to cover files that contain no
6170 // explicit transitions (for which we did not serialize anything).
6171 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6172 .StateTransitions.push_back({FirstState, 0});
6174 // For prefix ASTs, start with whatever the user configured on the
6176 Idx++; // Skip flags.
6177 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6178 SourceLocation(), false);
6181 // Read the state transitions.
6182 unsigned NumLocations = Record[Idx++];
6183 while (NumLocations--) {
6184 assert(Idx < Record.size() &&
6185 "Invalid data, missing pragma diagnostic states");
6186 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6187 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6188 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6189 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6190 unsigned Transitions = Record[Idx++];
6192 // Note that we don't need to set up Parent/ParentOffset here, because
6193 // we won't be changing the diagnostic state within imported FileIDs
6194 // (other than perhaps appending to the main source file, which has no
6196 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6197 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6198 for (unsigned I = 0; I != Transitions; ++I) {
6199 unsigned Offset = Record[Idx++];
6201 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6202 F.StateTransitions.push_back({State, Offset});
6206 // Read the final state.
6207 assert(Idx < Record.size() &&
6208 "Invalid data, missing final pragma diagnostic state");
6209 SourceLocation CurStateLoc =
6210 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6211 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6213 if (!F.isModule()) {
6214 Diag.DiagStatesByLoc.CurDiagState = CurState;
6215 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6217 // Preserve the property that the imaginary root file describes the
6220 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6222 T.push_back({CurState, 0});
6224 T[0].State = CurState;
6227 // Don't try to read these mappings again.
6232 /// Get the correct cursor and offset for loading a type.
6233 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6234 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6235 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6236 ModuleFile *M = I->second;
6237 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6240 /// Read and return the type with the given index..
6242 /// The index is the type ID, shifted and minus the number of predefs. This
6243 /// routine actually reads the record corresponding to the type at the given
6244 /// location. It is a helper routine for GetType, which deals with reading type
6246 QualType ASTReader::readTypeRecord(unsigned Index) {
6247 assert(ContextObj && "reading type with no AST context");
6248 ASTContext &Context = *ContextObj;
6249 RecordLocation Loc = TypeCursorForIndex(Index);
6250 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6252 // Keep track of where we are in the stream, then jump back there
6253 // after reading this type.
6254 SavedStreamPosition SavedPosition(DeclsCursor);
6256 ReadingKindTracker ReadingKind(Read_Type, *this);
6258 // Note that we are loading a type record.
6259 Deserializing AType(this);
6262 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6263 Error(std::move(Err));
6267 Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
6269 Error(MaybeCode.takeError());
6272 unsigned Code = MaybeCode.get();
6274 Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
6275 if (!MaybeTypeCode) {
6276 Error(MaybeTypeCode.takeError());
6279 switch ((TypeCode)MaybeTypeCode.get()) {
6280 case TYPE_EXT_QUAL: {
6281 if (Record.size() != 2) {
6282 Error("Incorrect encoding of extended qualifier type");
6285 QualType Base = readType(*Loc.F, Record, Idx);
6286 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6287 return Context.getQualifiedType(Base, Quals);
6290 case TYPE_COMPLEX: {
6291 if (Record.size() != 1) {
6292 Error("Incorrect encoding of complex type");
6295 QualType ElemType = readType(*Loc.F, Record, Idx);
6296 return Context.getComplexType(ElemType);
6299 case TYPE_POINTER: {
6300 if (Record.size() != 1) {
6301 Error("Incorrect encoding of pointer type");
6304 QualType PointeeType = readType(*Loc.F, Record, Idx);
6305 return Context.getPointerType(PointeeType);
6308 case TYPE_DECAYED: {
6309 if (Record.size() != 1) {
6310 Error("Incorrect encoding of decayed type");
6313 QualType OriginalType = readType(*Loc.F, Record, Idx);
6314 QualType DT = Context.getAdjustedParameterType(OriginalType);
6315 if (!isa<DecayedType>(DT))
6316 Error("Decayed type does not decay");
6320 case TYPE_ADJUSTED: {
6321 if (Record.size() != 2) {
6322 Error("Incorrect encoding of adjusted type");
6325 QualType OriginalTy = readType(*Loc.F, Record, Idx);
6326 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
6327 return Context.getAdjustedType(OriginalTy, AdjustedTy);
6330 case TYPE_BLOCK_POINTER: {
6331 if (Record.size() != 1) {
6332 Error("Incorrect encoding of block pointer type");
6335 QualType PointeeType = readType(*Loc.F, Record, Idx);
6336 return Context.getBlockPointerType(PointeeType);
6339 case TYPE_LVALUE_REFERENCE: {
6340 if (Record.size() != 2) {
6341 Error("Incorrect encoding of lvalue reference type");
6344 QualType PointeeType = readType(*Loc.F, Record, Idx);
6345 return Context.getLValueReferenceType(PointeeType, Record[1]);
6348 case TYPE_RVALUE_REFERENCE: {
6349 if (Record.size() != 1) {
6350 Error("Incorrect encoding of rvalue reference type");
6353 QualType PointeeType = readType(*Loc.F, Record, Idx);
6354 return Context.getRValueReferenceType(PointeeType);
6357 case TYPE_MEMBER_POINTER: {
6358 if (Record.size() != 2) {
6359 Error("Incorrect encoding of member pointer type");
6362 QualType PointeeType = readType(*Loc.F, Record, Idx);
6363 QualType ClassType = readType(*Loc.F, Record, Idx);
6364 if (PointeeType.isNull() || ClassType.isNull())
6367 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
6370 case TYPE_CONSTANT_ARRAY: {
6371 QualType ElementType = readType(*Loc.F, Record, Idx);
6372 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6373 unsigned IndexTypeQuals = Record[2];
6375 llvm::APInt Size = ReadAPInt(Record, Idx);
6376 return Context.getConstantArrayType(ElementType, Size,
6377 ASM, IndexTypeQuals);
6380 case TYPE_INCOMPLETE_ARRAY: {
6381 QualType ElementType = readType(*Loc.F, Record, Idx);
6382 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6383 unsigned IndexTypeQuals = Record[2];
6384 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6387 case TYPE_VARIABLE_ARRAY: {
6388 QualType ElementType = readType(*Loc.F, Record, Idx);
6389 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6390 unsigned IndexTypeQuals = Record[2];
6391 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6392 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6393 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6394 ASM, IndexTypeQuals,
6395 SourceRange(LBLoc, RBLoc));
6399 if (Record.size() != 3) {
6400 Error("incorrect encoding of vector type in AST file");
6404 QualType ElementType = readType(*Loc.F, Record, Idx);
6405 unsigned NumElements = Record[1];
6406 unsigned VecKind = Record[2];
6407 return Context.getVectorType(ElementType, NumElements,
6408 (VectorType::VectorKind)VecKind);
6411 case TYPE_EXT_VECTOR: {
6412 if (Record.size() != 3) {
6413 Error("incorrect encoding of extended vector type in AST file");
6417 QualType ElementType = readType(*Loc.F, Record, Idx);
6418 unsigned NumElements = Record[1];
6419 return Context.getExtVectorType(ElementType, NumElements);
6422 case TYPE_FUNCTION_NO_PROTO: {
6423 if (Record.size() != 8) {
6424 Error("incorrect encoding of no-proto function type");
6427 QualType ResultType = readType(*Loc.F, Record, Idx);
6428 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6429 (CallingConv)Record[4], Record[5], Record[6],
6431 return Context.getFunctionNoProtoType(ResultType, Info);
6434 case TYPE_FUNCTION_PROTO: {
6435 QualType ResultType = readType(*Loc.F, Record, Idx);
6437 FunctionProtoType::ExtProtoInfo EPI;
6438 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6439 /*hasregparm*/ Record[2],
6440 /*regparm*/ Record[3],
6441 static_cast<CallingConv>(Record[4]),
6442 /*produces*/ Record[5],
6443 /*nocallersavedregs*/ Record[6],
6444 /*nocfcheck*/ Record[7]);
6448 EPI.Variadic = Record[Idx++];
6449 EPI.HasTrailingReturn = Record[Idx++];
6450 EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6451 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6452 SmallVector<QualType, 8> ExceptionStorage;
6453 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6455 unsigned NumParams = Record[Idx++];
6456 SmallVector<QualType, 16> ParamTypes;
6457 for (unsigned I = 0; I != NumParams; ++I)
6458 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6460 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6461 if (Idx != Record.size()) {
6462 for (unsigned I = 0; I != NumParams; ++I)
6463 ExtParameterInfos.push_back(
6464 FunctionProtoType::ExtParameterInfo
6465 ::getFromOpaqueValue(Record[Idx++]));
6466 EPI.ExtParameterInfos = ExtParameterInfos.data();
6469 assert(Idx == Record.size());
6471 return Context.getFunctionType(ResultType, ParamTypes, EPI);
6474 case TYPE_UNRESOLVED_USING: {
6476 return Context.getTypeDeclType(
6477 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6480 case TYPE_TYPEDEF: {
6481 if (Record.size() != 2) {
6482 Error("incorrect encoding of typedef type");
6486 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6487 QualType Canonical = readType(*Loc.F, Record, Idx);
6488 if (!Canonical.isNull())
6489 Canonical = Context.getCanonicalType(Canonical);
6490 return Context.getTypedefType(Decl, Canonical);
6493 case TYPE_TYPEOF_EXPR:
6494 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6497 if (Record.size() != 1) {
6498 Error("incorrect encoding of typeof(type) in AST file");
6501 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6502 return Context.getTypeOfType(UnderlyingType);
6505 case TYPE_DECLTYPE: {
6506 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6507 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6510 case TYPE_UNARY_TRANSFORM: {
6511 QualType BaseType = readType(*Loc.F, Record, Idx);
6512 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6513 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6514 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6518 QualType Deduced = readType(*Loc.F, Record, Idx);
6519 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6520 bool IsDependent = false, IsPack = false;
6521 if (Deduced.isNull()) {
6522 IsDependent = Record[Idx] > 0;
6523 IsPack = Record[Idx] > 1;
6526 return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
6529 case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6530 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6531 QualType Deduced = readType(*Loc.F, Record, Idx);
6532 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6533 return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6538 if (Record.size() != 2) {
6539 Error("incorrect encoding of record type");
6543 bool IsDependent = Record[Idx++];
6544 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6545 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6546 QualType T = Context.getRecordType(RD);
6547 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6552 if (Record.size() != 2) {
6553 Error("incorrect encoding of enum type");
6557 bool IsDependent = Record[Idx++];
6559 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6560 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6564 case TYPE_ATTRIBUTED: {
6565 if (Record.size() != 3) {
6566 Error("incorrect encoding of attributed type");
6569 QualType modifiedType = readType(*Loc.F, Record, Idx);
6570 QualType equivalentType = readType(*Loc.F, Record, Idx);
6571 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6572 return Context.getAttributedType(kind, modifiedType, equivalentType);
6576 if (Record.size() != 1) {
6577 Error("incorrect encoding of paren type");
6580 QualType InnerType = readType(*Loc.F, Record, Idx);
6581 return Context.getParenType(InnerType);
6584 case TYPE_MACRO_QUALIFIED: {
6585 if (Record.size() != 2) {
6586 Error("incorrect encoding of macro defined type");
6589 QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6590 IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6591 return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6594 case TYPE_PACK_EXPANSION: {
6595 if (Record.size() != 2) {
6596 Error("incorrect encoding of pack expansion type");
6599 QualType Pattern = readType(*Loc.F, Record, Idx);
6600 if (Pattern.isNull())
6602 Optional<unsigned> NumExpansions;
6604 NumExpansions = Record[1] - 1;
6605 return Context.getPackExpansionType(Pattern, NumExpansions);
6608 case TYPE_ELABORATED: {
6610 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6611 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6612 QualType NamedType = readType(*Loc.F, Record, Idx);
6613 TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6614 return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6617 case TYPE_OBJC_INTERFACE: {
6619 ObjCInterfaceDecl *ItfD
6620 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6621 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6624 case TYPE_OBJC_TYPE_PARAM: {
6626 ObjCTypeParamDecl *Decl
6627 = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6628 unsigned NumProtos = Record[Idx++];
6629 SmallVector<ObjCProtocolDecl*, 4> Protos;
6630 for (unsigned I = 0; I != NumProtos; ++I)
6631 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6632 return Context.getObjCTypeParamType(Decl, Protos);
6635 case TYPE_OBJC_OBJECT: {
6637 QualType Base = readType(*Loc.F, Record, Idx);
6638 unsigned NumTypeArgs = Record[Idx++];
6639 SmallVector<QualType, 4> TypeArgs;
6640 for (unsigned I = 0; I != NumTypeArgs; ++I)
6641 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6642 unsigned NumProtos = Record[Idx++];
6643 SmallVector<ObjCProtocolDecl*, 4> Protos;
6644 for (unsigned I = 0; I != NumProtos; ++I)
6645 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6646 bool IsKindOf = Record[Idx++];
6647 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6650 case TYPE_OBJC_OBJECT_POINTER: {
6652 QualType Pointee = readType(*Loc.F, Record, Idx);
6653 return Context.getObjCObjectPointerType(Pointee);
6656 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6658 QualType Parm = readType(*Loc.F, Record, Idx);
6659 QualType Replacement = readType(*Loc.F, Record, Idx);
6660 return Context.getSubstTemplateTypeParmType(
6661 cast<TemplateTypeParmType>(Parm),
6662 Context.getCanonicalType(Replacement));
6665 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6667 QualType Parm = readType(*Loc.F, Record, Idx);
6668 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6669 return Context.getSubstTemplateTypeParmPackType(
6670 cast<TemplateTypeParmType>(Parm),
6674 case TYPE_INJECTED_CLASS_NAME: {
6675 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6676 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6677 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6678 // for AST reading, too much interdependencies.
6679 const Type *T = nullptr;
6680 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6681 if (const Type *Existing = DI->getTypeForDecl()) {
6687 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6688 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6689 DI->setTypeForDecl(T);
6691 return QualType(T, 0);
6694 case TYPE_TEMPLATE_TYPE_PARM: {
6696 unsigned Depth = Record[Idx++];
6697 unsigned Index = Record[Idx++];
6698 bool Pack = Record[Idx++];
6699 TemplateTypeParmDecl *D
6700 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6701 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6704 case TYPE_DEPENDENT_NAME: {
6706 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6707 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6708 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6709 QualType Canon = readType(*Loc.F, Record, Idx);
6710 if (!Canon.isNull())
6711 Canon = Context.getCanonicalType(Canon);
6712 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6715 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6717 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6718 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6719 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6720 unsigned NumArgs = Record[Idx++];
6721 SmallVector<TemplateArgument, 8> Args;
6722 Args.reserve(NumArgs);
6724 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6725 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6729 case TYPE_DEPENDENT_SIZED_ARRAY: {
6733 QualType ElementType = readType(*Loc.F, Record, Idx);
6734 ArrayType::ArraySizeModifier ASM
6735 = (ArrayType::ArraySizeModifier)Record[Idx++];
6736 unsigned IndexTypeQuals = Record[Idx++];
6738 // DependentSizedArrayType
6739 Expr *NumElts = ReadExpr(*Loc.F);
6740 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6742 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6743 IndexTypeQuals, Brackets);
6746 case TYPE_TEMPLATE_SPECIALIZATION: {
6748 bool IsDependent = Record[Idx++];
6749 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6750 SmallVector<TemplateArgument, 8> Args;
6751 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6752 QualType Underlying = readType(*Loc.F, Record, Idx);
6754 if (Underlying.isNull())
6755 T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6757 T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6758 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6763 if (Record.size() != 1) {
6764 Error("Incorrect encoding of atomic type");
6767 QualType ValueType = readType(*Loc.F, Record, Idx);
6768 return Context.getAtomicType(ValueType);
6772 if (Record.size() != 2) {
6773 Error("Incorrect encoding of pipe type");
6777 // Reading the pipe element type.
6778 QualType ElementType = readType(*Loc.F, Record, Idx);
6779 unsigned ReadOnly = Record[1];
6780 return Context.getPipeType(ElementType, ReadOnly);
6783 case TYPE_DEPENDENT_SIZED_VECTOR: {
6785 QualType ElementType = readType(*Loc.F, Record, Idx);
6786 Expr *SizeExpr = ReadExpr(*Loc.F);
6787 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6788 unsigned VecKind = Record[Idx];
6790 return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6791 (VectorType::VectorKind)VecKind);
6794 case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6797 // DependentSizedExtVectorType
6798 QualType ElementType = readType(*Loc.F, Record, Idx);
6799 Expr *SizeExpr = ReadExpr(*Loc.F);
6800 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6802 return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6806 case TYPE_DEPENDENT_ADDRESS_SPACE: {
6809 // DependentAddressSpaceType
6810 QualType PointeeType = readType(*Loc.F, Record, Idx);
6811 Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6812 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6814 return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6818 llvm_unreachable("Invalid TypeCode!");
6821 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6822 SmallVectorImpl<QualType> &Exceptions,
6823 FunctionProtoType::ExceptionSpecInfo &ESI,
6824 const RecordData &Record, unsigned &Idx) {
6825 ExceptionSpecificationType EST =
6826 static_cast<ExceptionSpecificationType>(Record[Idx++]);
6828 if (EST == EST_Dynamic) {
6829 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6830 Exceptions.push_back(readType(ModuleFile, Record, Idx));
6831 ESI.Exceptions = Exceptions;
6832 } else if (isComputedNoexcept(EST)) {
6833 ESI.NoexceptExpr = ReadExpr(ModuleFile);
6834 } else if (EST == EST_Uninstantiated) {
6835 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6836 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6837 } else if (EST == EST_Unevaluated) {
6838 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6844 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6847 const ASTReader::RecordData &Record;
6850 SourceLocation ReadSourceLocation() {
6851 return Reader->ReadSourceLocation(*F, Record, Idx);
6854 TypeSourceInfo *GetTypeSourceInfo() {
6855 return Reader->GetTypeSourceInfo(*F, Record, Idx);
6858 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6859 return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6863 return Reader->ReadAttr(*F, Record, Idx);
6867 TypeLocReader(ModuleFile &F, ASTReader &Reader,
6868 const ASTReader::RecordData &Record, unsigned &Idx)
6869 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6871 // We want compile-time assurance that we've enumerated all of
6872 // these, so unfortunately we have to declare them first, then
6873 // define them out-of-line.
6874 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6875 #define TYPELOC(CLASS, PARENT) \
6876 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6877 #include "clang/AST/TypeLocNodes.def"
6879 void VisitFunctionTypeLoc(FunctionTypeLoc);
6880 void VisitArrayTypeLoc(ArrayTypeLoc);
6883 } // namespace clang
6885 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6889 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6890 TL.setBuiltinLoc(ReadSourceLocation());
6891 if (TL.needsExtraLocalData()) {
6892 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6893 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6894 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6895 TL.setModeAttr(Record[Idx++]);
6899 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6900 TL.setNameLoc(ReadSourceLocation());
6903 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6904 TL.setStarLoc(ReadSourceLocation());
6907 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6911 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6915 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6916 TL.setExpansionLoc(ReadSourceLocation());
6919 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6920 TL.setCaretLoc(ReadSourceLocation());
6923 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6924 TL.setAmpLoc(ReadSourceLocation());
6927 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6928 TL.setAmpAmpLoc(ReadSourceLocation());
6931 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6932 TL.setStarLoc(ReadSourceLocation());
6933 TL.setClassTInfo(GetTypeSourceInfo());
6936 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6937 TL.setLBracketLoc(ReadSourceLocation());
6938 TL.setRBracketLoc(ReadSourceLocation());
6940 TL.setSizeExpr(Reader->ReadExpr(*F));
6942 TL.setSizeExpr(nullptr);
6945 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6946 VisitArrayTypeLoc(TL);
6949 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6950 VisitArrayTypeLoc(TL);
6953 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6954 VisitArrayTypeLoc(TL);
6957 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6958 DependentSizedArrayTypeLoc TL) {
6959 VisitArrayTypeLoc(TL);
6962 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6963 DependentAddressSpaceTypeLoc TL) {
6965 TL.setAttrNameLoc(ReadSourceLocation());
6967 range.setBegin(ReadSourceLocation());
6968 range.setEnd(ReadSourceLocation());
6969 TL.setAttrOperandParensRange(range);
6970 TL.setAttrExprOperand(Reader->ReadExpr(*F));
6973 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6974 DependentSizedExtVectorTypeLoc TL) {
6975 TL.setNameLoc(ReadSourceLocation());
6978 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6979 TL.setNameLoc(ReadSourceLocation());
6982 void TypeLocReader::VisitDependentVectorTypeLoc(
6983 DependentVectorTypeLoc TL) {
6984 TL.setNameLoc(ReadSourceLocation());
6987 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6988 TL.setNameLoc(ReadSourceLocation());
6991 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6992 TL.setLocalRangeBegin(ReadSourceLocation());
6993 TL.setLParenLoc(ReadSourceLocation());
6994 TL.setRParenLoc(ReadSourceLocation());
6995 TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
6996 Reader->ReadSourceLocation(*F, Record, Idx)));
6997 TL.setLocalRangeEnd(ReadSourceLocation());
6998 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6999 TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
7003 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7004 VisitFunctionTypeLoc(TL);
7007 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7008 VisitFunctionTypeLoc(TL);
7011 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7012 TL.setNameLoc(ReadSourceLocation());
7015 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7016 TL.setNameLoc(ReadSourceLocation());
7019 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7020 TL.setTypeofLoc(ReadSourceLocation());
7021 TL.setLParenLoc(ReadSourceLocation());
7022 TL.setRParenLoc(ReadSourceLocation());
7025 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7026 TL.setTypeofLoc(ReadSourceLocation());
7027 TL.setLParenLoc(ReadSourceLocation());
7028 TL.setRParenLoc(ReadSourceLocation());
7029 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7032 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7033 TL.setNameLoc(ReadSourceLocation());
7036 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7037 TL.setKWLoc(ReadSourceLocation());
7038 TL.setLParenLoc(ReadSourceLocation());
7039 TL.setRParenLoc(ReadSourceLocation());
7040 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7043 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7044 TL.setNameLoc(ReadSourceLocation());
7047 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7048 DeducedTemplateSpecializationTypeLoc TL) {
7049 TL.setTemplateNameLoc(ReadSourceLocation());
7052 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7053 TL.setNameLoc(ReadSourceLocation());
7056 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7057 TL.setNameLoc(ReadSourceLocation());
7060 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7061 TL.setAttr(ReadAttr());
7064 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7065 TL.setNameLoc(ReadSourceLocation());
7068 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7069 SubstTemplateTypeParmTypeLoc TL) {
7070 TL.setNameLoc(ReadSourceLocation());
7073 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7074 SubstTemplateTypeParmPackTypeLoc TL) {
7075 TL.setNameLoc(ReadSourceLocation());
7078 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7079 TemplateSpecializationTypeLoc TL) {
7080 TL.setTemplateKeywordLoc(ReadSourceLocation());
7081 TL.setTemplateNameLoc(ReadSourceLocation());
7082 TL.setLAngleLoc(ReadSourceLocation());
7083 TL.setRAngleLoc(ReadSourceLocation());
7084 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7087 Reader->GetTemplateArgumentLocInfo(
7088 *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
7091 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7092 TL.setLParenLoc(ReadSourceLocation());
7093 TL.setRParenLoc(ReadSourceLocation());
7096 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7097 TL.setElaboratedKeywordLoc(ReadSourceLocation());
7098 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7101 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7102 TL.setNameLoc(ReadSourceLocation());
7105 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7106 TL.setElaboratedKeywordLoc(ReadSourceLocation());
7107 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7108 TL.setNameLoc(ReadSourceLocation());
7111 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7112 DependentTemplateSpecializationTypeLoc TL) {
7113 TL.setElaboratedKeywordLoc(ReadSourceLocation());
7114 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7115 TL.setTemplateKeywordLoc(ReadSourceLocation());
7116 TL.setTemplateNameLoc(ReadSourceLocation());
7117 TL.setLAngleLoc(ReadSourceLocation());
7118 TL.setRAngleLoc(ReadSourceLocation());
7119 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7122 Reader->GetTemplateArgumentLocInfo(
7123 *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
7126 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7127 TL.setEllipsisLoc(ReadSourceLocation());
7130 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7131 TL.setNameLoc(ReadSourceLocation());
7134 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7135 if (TL.getNumProtocols()) {
7136 TL.setProtocolLAngleLoc(ReadSourceLocation());
7137 TL.setProtocolRAngleLoc(ReadSourceLocation());
7139 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7140 TL.setProtocolLoc(i, ReadSourceLocation());
7143 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7144 TL.setHasBaseTypeAsWritten(Record[Idx++]);
7145 TL.setTypeArgsLAngleLoc(ReadSourceLocation());
7146 TL.setTypeArgsRAngleLoc(ReadSourceLocation());
7147 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7148 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7149 TL.setProtocolLAngleLoc(ReadSourceLocation());
7150 TL.setProtocolRAngleLoc(ReadSourceLocation());
7151 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7152 TL.setProtocolLoc(i, ReadSourceLocation());
7155 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7156 TL.setStarLoc(ReadSourceLocation());
7159 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7160 TL.setKWLoc(ReadSourceLocation());
7161 TL.setLParenLoc(ReadSourceLocation());
7162 TL.setRParenLoc(ReadSourceLocation());
7165 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7166 TL.setKWLoc(ReadSourceLocation());
7169 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
7170 unsigned &Idx, TypeLoc TL) {
7171 TypeLocReader TLR(F, *this, Record, Idx);
7172 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7177 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
7179 QualType InfoTy = readType(F, Record, Idx);
7180 if (InfoTy.isNull())
7183 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7184 ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
7188 QualType ASTReader::GetType(TypeID ID) {
7189 assert(ContextObj && "reading type with no AST context");
7190 ASTContext &Context = *ContextObj;
7192 unsigned FastQuals = ID & Qualifiers::FastMask;
7193 unsigned Index = ID >> Qualifiers::FastWidth;
7195 if (Index < NUM_PREDEF_TYPE_IDS) {
7197 switch ((PredefinedTypeIDs)Index) {
7198 case PREDEF_TYPE_NULL_ID:
7200 case PREDEF_TYPE_VOID_ID:
7203 case PREDEF_TYPE_BOOL_ID:
7206 case PREDEF_TYPE_CHAR_U_ID:
7207 case PREDEF_TYPE_CHAR_S_ID:
7208 // FIXME: Check that the signedness of CharTy is correct!
7211 case PREDEF_TYPE_UCHAR_ID:
7212 T = Context.UnsignedCharTy;
7214 case PREDEF_TYPE_USHORT_ID:
7215 T = Context.UnsignedShortTy;
7217 case PREDEF_TYPE_UINT_ID:
7218 T = Context.UnsignedIntTy;
7220 case PREDEF_TYPE_ULONG_ID:
7221 T = Context.UnsignedLongTy;
7223 case PREDEF_TYPE_ULONGLONG_ID:
7224 T = Context.UnsignedLongLongTy;
7226 case PREDEF_TYPE_UINT128_ID:
7227 T = Context.UnsignedInt128Ty;
7229 case PREDEF_TYPE_SCHAR_ID:
7230 T = Context.SignedCharTy;
7232 case PREDEF_TYPE_WCHAR_ID:
7233 T = Context.WCharTy;
7235 case PREDEF_TYPE_SHORT_ID:
7236 T = Context.ShortTy;
7238 case PREDEF_TYPE_INT_ID:
7241 case PREDEF_TYPE_LONG_ID:
7244 case PREDEF_TYPE_LONGLONG_ID:
7245 T = Context.LongLongTy;
7247 case PREDEF_TYPE_INT128_ID:
7248 T = Context.Int128Ty;
7250 case PREDEF_TYPE_HALF_ID:
7253 case PREDEF_TYPE_FLOAT_ID:
7254 T = Context.FloatTy;
7256 case PREDEF_TYPE_DOUBLE_ID:
7257 T = Context.DoubleTy;
7259 case PREDEF_TYPE_LONGDOUBLE_ID:
7260 T = Context.LongDoubleTy;
7262 case PREDEF_TYPE_SHORT_ACCUM_ID:
7263 T = Context.ShortAccumTy;
7265 case PREDEF_TYPE_ACCUM_ID:
7266 T = Context.AccumTy;
7268 case PREDEF_TYPE_LONG_ACCUM_ID:
7269 T = Context.LongAccumTy;
7271 case PREDEF_TYPE_USHORT_ACCUM_ID:
7272 T = Context.UnsignedShortAccumTy;
7274 case PREDEF_TYPE_UACCUM_ID:
7275 T = Context.UnsignedAccumTy;
7277 case PREDEF_TYPE_ULONG_ACCUM_ID:
7278 T = Context.UnsignedLongAccumTy;
7280 case PREDEF_TYPE_SHORT_FRACT_ID:
7281 T = Context.ShortFractTy;
7283 case PREDEF_TYPE_FRACT_ID:
7284 T = Context.FractTy;
7286 case PREDEF_TYPE_LONG_FRACT_ID:
7287 T = Context.LongFractTy;
7289 case PREDEF_TYPE_USHORT_FRACT_ID:
7290 T = Context.UnsignedShortFractTy;
7292 case PREDEF_TYPE_UFRACT_ID:
7293 T = Context.UnsignedFractTy;
7295 case PREDEF_TYPE_ULONG_FRACT_ID:
7296 T = Context.UnsignedLongFractTy;
7298 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7299 T = Context.SatShortAccumTy;
7301 case PREDEF_TYPE_SAT_ACCUM_ID:
7302 T = Context.SatAccumTy;
7304 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7305 T = Context.SatLongAccumTy;
7307 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7308 T = Context.SatUnsignedShortAccumTy;
7310 case PREDEF_TYPE_SAT_UACCUM_ID:
7311 T = Context.SatUnsignedAccumTy;
7313 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7314 T = Context.SatUnsignedLongAccumTy;
7316 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7317 T = Context.SatShortFractTy;
7319 case PREDEF_TYPE_SAT_FRACT_ID:
7320 T = Context.SatFractTy;
7322 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7323 T = Context.SatLongFractTy;
7325 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7326 T = Context.SatUnsignedShortFractTy;
7328 case PREDEF_TYPE_SAT_UFRACT_ID:
7329 T = Context.SatUnsignedFractTy;
7331 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7332 T = Context.SatUnsignedLongFractTy;
7334 case PREDEF_TYPE_FLOAT16_ID:
7335 T = Context.Float16Ty;
7337 case PREDEF_TYPE_FLOAT128_ID:
7338 T = Context.Float128Ty;
7340 case PREDEF_TYPE_OVERLOAD_ID:
7341 T = Context.OverloadTy;
7343 case PREDEF_TYPE_BOUND_MEMBER:
7344 T = Context.BoundMemberTy;
7346 case PREDEF_TYPE_PSEUDO_OBJECT:
7347 T = Context.PseudoObjectTy;
7349 case PREDEF_TYPE_DEPENDENT_ID:
7350 T = Context.DependentTy;
7352 case PREDEF_TYPE_UNKNOWN_ANY:
7353 T = Context.UnknownAnyTy;
7355 case PREDEF_TYPE_NULLPTR_ID:
7356 T = Context.NullPtrTy;
7358 case PREDEF_TYPE_CHAR8_ID:
7359 T = Context.Char8Ty;
7361 case PREDEF_TYPE_CHAR16_ID:
7362 T = Context.Char16Ty;
7364 case PREDEF_TYPE_CHAR32_ID:
7365 T = Context.Char32Ty;
7367 case PREDEF_TYPE_OBJC_ID:
7368 T = Context.ObjCBuiltinIdTy;
7370 case PREDEF_TYPE_OBJC_CLASS:
7371 T = Context.ObjCBuiltinClassTy;
7373 case PREDEF_TYPE_OBJC_SEL:
7374 T = Context.ObjCBuiltinSelTy;
7376 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7377 case PREDEF_TYPE_##Id##_ID: \
7378 T = Context.SingletonId; \
7380 #include "clang/Basic/OpenCLImageTypes.def"
7381 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7382 case PREDEF_TYPE_##Id##_ID: \
7383 T = Context.Id##Ty; \
7385 #include "clang/Basic/OpenCLExtensionTypes.def"
7386 case PREDEF_TYPE_SAMPLER_ID:
7387 T = Context.OCLSamplerTy;
7389 case PREDEF_TYPE_EVENT_ID:
7390 T = Context.OCLEventTy;
7392 case PREDEF_TYPE_CLK_EVENT_ID:
7393 T = Context.OCLClkEventTy;
7395 case PREDEF_TYPE_QUEUE_ID:
7396 T = Context.OCLQueueTy;
7398 case PREDEF_TYPE_RESERVE_ID_ID:
7399 T = Context.OCLReserveIDTy;
7401 case PREDEF_TYPE_AUTO_DEDUCT:
7402 T = Context.getAutoDeductType();
7404 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7405 T = Context.getAutoRRefDeductType();
7407 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7408 T = Context.ARCUnbridgedCastTy;
7410 case PREDEF_TYPE_BUILTIN_FN:
7411 T = Context.BuiltinFnTy;
7413 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7414 T = Context.OMPArraySectionTy;
7418 assert(!T.isNull() && "Unknown predefined type");
7419 return T.withFastQualifiers(FastQuals);
7422 Index -= NUM_PREDEF_TYPE_IDS;
7423 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7424 if (TypesLoaded[Index].isNull()) {
7425 TypesLoaded[Index] = readTypeRecord(Index);
7426 if (TypesLoaded[Index].isNull())
7429 TypesLoaded[Index]->setFromAST();
7430 if (DeserializationListener)
7431 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7432 TypesLoaded[Index]);
7435 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7438 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7439 return GetType(getGlobalTypeID(F, LocalID));
7442 serialization::TypeID
7443 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7444 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7445 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7447 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7450 if (!F.ModuleOffsetMap.empty())
7451 ReadModuleOffsetMap(F);
7453 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7454 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7455 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7457 unsigned GlobalIndex = LocalIndex + I->second;
7458 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7461 TemplateArgumentLocInfo
7462 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7463 TemplateArgument::ArgKind Kind,
7464 const RecordData &Record,
7467 case TemplateArgument::Expression:
7469 case TemplateArgument::Type:
7470 return GetTypeSourceInfo(F, Record, Index);
7471 case TemplateArgument::Template: {
7472 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7474 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7475 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7478 case TemplateArgument::TemplateExpansion: {
7479 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7481 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7482 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7483 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7486 case TemplateArgument::Null:
7487 case TemplateArgument::Integral:
7488 case TemplateArgument::Declaration:
7489 case TemplateArgument::NullPtr:
7490 case TemplateArgument::Pack:
7491 // FIXME: Is this right?
7492 return TemplateArgumentLocInfo();
7494 llvm_unreachable("unexpected template argument loc");
7498 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7499 const RecordData &Record, unsigned &Index) {
7500 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7502 if (Arg.getKind() == TemplateArgument::Expression) {
7503 if (Record[Index++]) // bool InfoHasSameExpr.
7504 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7506 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7510 const ASTTemplateArgumentListInfo*
7511 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7512 const RecordData &Record,
7514 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7515 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7516 unsigned NumArgsAsWritten = Record[Index++];
7517 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7518 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7519 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7520 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7523 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7527 void ASTReader::CompleteRedeclChain(const Decl *D) {
7528 if (NumCurrentElementsDeserializing) {
7529 // We arrange to not care about the complete redeclaration chain while we're
7530 // deserializing. Just remember that the AST has marked this one as complete
7531 // but that it's not actually complete yet, so we know we still need to
7532 // complete it later.
7533 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7537 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7539 // If this is a named declaration, complete it by looking it up
7540 // within its context.
7542 // FIXME: Merging a function definition should merge
7543 // all mergeable entities within it.
7544 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7545 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7546 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7547 if (!getContext().getLangOpts().CPlusPlus &&
7548 isa<TranslationUnitDecl>(DC)) {
7549 // Outside of C++, we don't have a lookup table for the TU, so update
7550 // the identifier instead. (For C++ modules, we don't store decls
7551 // in the serialized identifier table, so we do the lookup in the TU.)
7552 auto *II = Name.getAsIdentifierInfo();
7553 assert(II && "non-identifier name in C?");
7554 if (II->isOutOfDate())
7555 updateOutOfDateIdentifier(*II);
7558 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7559 // Find all declarations of this kind from the relevant context.
7560 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7561 auto *DC = cast<DeclContext>(DCDecl);
7562 SmallVector<Decl*, 8> Decls;
7563 FindExternalLexicalDecls(
7564 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7569 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7570 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7571 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7572 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7573 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7574 if (auto *Template = FD->getPrimaryTemplate())
7575 Template->LoadLazySpecializations();
7579 CXXCtorInitializer **
7580 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7581 RecordLocation Loc = getLocalBitOffset(Offset);
7582 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7583 SavedStreamPosition SavedPosition(Cursor);
7584 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7585 Error(std::move(Err));
7588 ReadingKindTracker ReadingKind(Read_Decl, *this);
7591 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7593 Error(MaybeCode.takeError());
7596 unsigned Code = MaybeCode.get();
7598 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7599 if (!MaybeRecCode) {
7600 Error(MaybeRecCode.takeError());
7603 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7604 Error("malformed AST file: missing C++ ctor initializers");
7609 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7612 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7613 assert(ContextObj && "reading base specifiers with no AST context");
7614 ASTContext &Context = *ContextObj;
7616 RecordLocation Loc = getLocalBitOffset(Offset);
7617 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7618 SavedStreamPosition SavedPosition(Cursor);
7619 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7620 Error(std::move(Err));
7623 ReadingKindTracker ReadingKind(Read_Decl, *this);
7626 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7628 Error(MaybeCode.takeError());
7631 unsigned Code = MaybeCode.get();
7633 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7634 if (!MaybeRecCode) {
7635 Error(MaybeCode.takeError());
7638 unsigned RecCode = MaybeRecCode.get();
7640 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7641 Error("malformed AST file: missing C++ base specifiers");
7646 unsigned NumBases = Record[Idx++];
7647 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7648 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7649 for (unsigned I = 0; I != NumBases; ++I)
7650 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7654 serialization::DeclID
7655 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7656 if (LocalID < NUM_PREDEF_DECL_IDS)
7659 if (!F.ModuleOffsetMap.empty())
7660 ReadModuleOffsetMap(F);
7662 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7663 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7664 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7666 return LocalID + I->second;
7669 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7670 ModuleFile &M) const {
7671 // Predefined decls aren't from any module.
7672 if (ID < NUM_PREDEF_DECL_IDS)
7675 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7676 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7679 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7680 if (!D->isFromASTFile())
7682 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7683 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7687 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7688 if (ID < NUM_PREDEF_DECL_IDS)
7689 return SourceLocation();
7691 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7693 if (Index > DeclsLoaded.size()) {
7694 Error("declaration ID out-of-range for AST file");
7695 return SourceLocation();
7698 if (Decl *D = DeclsLoaded[Index])
7699 return D->getLocation();
7702 DeclCursorForID(ID, Loc);
7706 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7708 case PREDEF_DECL_NULL_ID:
7711 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7712 return Context.getTranslationUnitDecl();
7714 case PREDEF_DECL_OBJC_ID_ID:
7715 return Context.getObjCIdDecl();
7717 case PREDEF_DECL_OBJC_SEL_ID:
7718 return Context.getObjCSelDecl();
7720 case PREDEF_DECL_OBJC_CLASS_ID:
7721 return Context.getObjCClassDecl();
7723 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7724 return Context.getObjCProtocolDecl();
7726 case PREDEF_DECL_INT_128_ID:
7727 return Context.getInt128Decl();
7729 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7730 return Context.getUInt128Decl();
7732 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7733 return Context.getObjCInstanceTypeDecl();
7735 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7736 return Context.getBuiltinVaListDecl();
7738 case PREDEF_DECL_VA_LIST_TAG:
7739 return Context.getVaListTagDecl();
7741 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7742 return Context.getBuiltinMSVaListDecl();
7744 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7745 return Context.getExternCContextDecl();
7747 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7748 return Context.getMakeIntegerSeqDecl();
7750 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7751 return Context.getCFConstantStringDecl();
7753 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7754 return Context.getCFConstantStringTagDecl();
7756 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7757 return Context.getTypePackElementDecl();
7759 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7762 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7763 assert(ContextObj && "reading decl with no AST context");
7764 if (ID < NUM_PREDEF_DECL_IDS) {
7765 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7767 // Track that we have merged the declaration with ID \p ID into the
7768 // pre-existing predefined declaration \p D.
7769 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7771 Merged.push_back(ID);
7776 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7778 if (Index >= DeclsLoaded.size()) {
7779 assert(0 && "declaration ID out-of-range for AST file");
7780 Error("declaration ID out-of-range for AST file");
7784 return DeclsLoaded[Index];
7787 Decl *ASTReader::GetDecl(DeclID ID) {
7788 if (ID < NUM_PREDEF_DECL_IDS)
7789 return GetExistingDecl(ID);
7791 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7793 if (Index >= DeclsLoaded.size()) {
7794 assert(0 && "declaration ID out-of-range for AST file");
7795 Error("declaration ID out-of-range for AST file");
7799 if (!DeclsLoaded[Index]) {
7801 if (DeserializationListener)
7802 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7805 return DeclsLoaded[Index];
7808 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7810 if (GlobalID < NUM_PREDEF_DECL_IDS)
7813 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7814 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7815 ModuleFile *Owner = I->second;
7817 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7818 = M.GlobalToLocalDeclIDs.find(Owner);
7819 if (Pos == M.GlobalToLocalDeclIDs.end())
7822 return GlobalID - Owner->BaseDeclID + Pos->second;
7825 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7826 const RecordData &Record,
7828 if (Idx >= Record.size()) {
7829 Error("Corrupted AST file");
7833 return getGlobalDeclID(F, Record[Idx++]);
7836 /// Resolve the offset of a statement into a statement.
7838 /// This operation will read a new statement from the external
7839 /// source each time it is called, and is meant to be used via a
7840 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7841 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7842 // Switch case IDs are per Decl.
7843 ClearSwitchCaseIDs();
7845 // Offset here is a global offset across the entire chain.
7846 RecordLocation Loc = getLocalBitOffset(Offset);
7847 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7848 Error(std::move(Err));
7851 assert(NumCurrentElementsDeserializing == 0 &&
7852 "should not be called while already deserializing");
7853 Deserializing D(this);
7854 return ReadStmtFromStream(*Loc.F);
7857 void ASTReader::FindExternalLexicalDecls(
7858 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7859 SmallVectorImpl<Decl *> &Decls) {
7860 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7862 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7863 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7864 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7865 auto K = (Decl::Kind)+LexicalDecls[I];
7866 if (!IsKindWeWant(K))
7869 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7871 // Don't add predefined declarations to the lexical context more
7873 if (ID < NUM_PREDEF_DECL_IDS) {
7874 if (PredefsVisited[ID])
7877 PredefsVisited[ID] = true;
7880 if (Decl *D = GetLocalDecl(*M, ID)) {
7881 assert(D->getKind() == K && "wrong kind for lexical decl");
7882 if (!DC->isDeclInLexicalTraversal(D))
7888 if (isa<TranslationUnitDecl>(DC)) {
7889 for (auto Lexical : TULexicalDecls)
7890 Visit(Lexical.first, Lexical.second);
7892 auto I = LexicalDecls.find(DC);
7893 if (I != LexicalDecls.end())
7894 Visit(I->second.first, I->second.second);
7897 ++NumLexicalDeclContextsRead;
7907 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7909 bool operator()(LocalDeclID L, LocalDeclID R) const {
7910 SourceLocation LHS = getLocation(L);
7911 SourceLocation RHS = getLocation(R);
7912 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7915 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7916 SourceLocation RHS = getLocation(R);
7917 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7920 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7921 SourceLocation LHS = getLocation(L);
7922 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7925 SourceLocation getLocation(LocalDeclID ID) const {
7926 return Reader.getSourceManager().getFileLoc(
7927 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7933 void ASTReader::FindFileRegionDecls(FileID File,
7934 unsigned Offset, unsigned Length,
7935 SmallVectorImpl<Decl *> &Decls) {
7936 SourceManager &SM = getSourceManager();
7938 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7939 if (I == FileDeclIDs.end())
7942 FileDeclsInfo &DInfo = I->second;
7943 if (DInfo.Decls.empty())
7947 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7948 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7950 DeclIDComp DIDComp(*this, *DInfo.Mod);
7951 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7952 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7953 if (BeginIt != DInfo.Decls.begin())
7956 // If we are pointing at a top-level decl inside an objc container, we need
7957 // to backtrack until we find it otherwise we will fail to report that the
7958 // region overlaps with an objc container.
7959 while (BeginIt != DInfo.Decls.begin() &&
7960 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7961 ->isTopLevelDeclInObjCContainer())
7964 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7965 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7966 if (EndIt != DInfo.Decls.end())
7969 for (ArrayRef<serialization::LocalDeclID>::iterator
7970 DIt = BeginIt; DIt != EndIt; ++DIt)
7971 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7975 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7976 DeclarationName Name) {
7977 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7978 "DeclContext has no visible decls in storage");
7982 auto It = Lookups.find(DC);
7983 if (It == Lookups.end())
7986 Deserializing LookupResults(this);
7988 // Load the list of declarations.
7989 SmallVector<NamedDecl *, 64> Decls;
7990 for (DeclID ID : It->second.Table.find(Name)) {
7991 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7992 if (ND->getDeclName() == Name)
7993 Decls.push_back(ND);
7996 ++NumVisibleDeclContextsRead;
7997 SetExternalVisibleDeclsForName(DC, Name, Decls);
7998 return !Decls.empty();
8001 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8002 if (!DC->hasExternalVisibleStorage())
8005 auto It = Lookups.find(DC);
8006 assert(It != Lookups.end() &&
8007 "have external visible storage but no lookup tables");
8011 for (DeclID ID : It->second.Table.findAll()) {
8012 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8013 Decls[ND->getDeclName()].push_back(ND);
8016 ++NumVisibleDeclContextsRead;
8018 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8019 SetExternalVisibleDeclsForName(DC, I->first, I->second);
8021 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8024 const serialization::reader::DeclContextLookupTable *
8025 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8026 auto I = Lookups.find(Primary);
8027 return I == Lookups.end() ? nullptr : &I->second;
8030 /// Under non-PCH compilation the consumer receives the objc methods
8031 /// before receiving the implementation, and codegen depends on this.
8032 /// We simulate this by deserializing and passing to consumer the methods of the
8033 /// implementation before passing the deserialized implementation decl.
8034 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8035 ASTConsumer *Consumer) {
8036 assert(ImplD && Consumer);
8038 for (auto *I : ImplD->methods())
8039 Consumer->HandleInterestingDecl(DeclGroupRef(I));
8041 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8044 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8045 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8046 PassObjCImplDeclToConsumer(ImplD, Consumer);
8048 Consumer->HandleInterestingDecl(DeclGroupRef(D));
8051 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8052 this->Consumer = Consumer;
8055 PassInterestingDeclsToConsumer();
8057 if (DeserializationListener)
8058 DeserializationListener->ReaderInitialized(this);
8061 void ASTReader::PrintStats() {
8062 std::fprintf(stderr, "*** AST File Statistics:\n");
8064 unsigned NumTypesLoaded
8065 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
8067 unsigned NumDeclsLoaded
8068 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
8070 unsigned NumIdentifiersLoaded
8071 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
8072 IdentifiersLoaded.end(),
8073 (IdentifierInfo *)nullptr);
8074 unsigned NumMacrosLoaded
8075 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
8077 (MacroInfo *)nullptr);
8078 unsigned NumSelectorsLoaded
8079 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
8080 SelectorsLoaded.end(),
8083 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8084 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
8085 NumSLocEntriesRead, TotalNumSLocEntries,
8086 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8087 if (!TypesLoaded.empty())
8088 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
8089 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8090 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8091 if (!DeclsLoaded.empty())
8092 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
8093 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8094 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8095 if (!IdentifiersLoaded.empty())
8096 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
8097 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8098 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8099 if (!MacrosLoaded.empty())
8100 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
8101 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8102 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8103 if (!SelectorsLoaded.empty())
8104 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
8105 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8106 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8107 if (TotalNumStatements)
8108 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
8109 NumStatementsRead, TotalNumStatements,
8110 ((float)NumStatementsRead/TotalNumStatements * 100));
8112 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
8113 NumMacrosRead, TotalNumMacros,
8114 ((float)NumMacrosRead/TotalNumMacros * 100));
8115 if (TotalLexicalDeclContexts)
8116 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
8117 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8118 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8120 if (TotalVisibleDeclContexts)
8121 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
8122 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8123 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8125 if (TotalNumMethodPoolEntries)
8126 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
8127 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8128 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8130 if (NumMethodPoolLookups)
8131 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
8132 NumMethodPoolHits, NumMethodPoolLookups,
8133 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8134 if (NumMethodPoolTableLookups)
8135 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
8136 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8137 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8139 if (NumIdentifierLookupHits)
8140 std::fprintf(stderr,
8141 " %u / %u identifier table lookups succeeded (%f%%)\n",
8142 NumIdentifierLookupHits, NumIdentifierLookups,
8143 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8146 std::fprintf(stderr, "\n");
8147 GlobalIndex->printStats();
8150 std::fprintf(stderr, "\n");
8152 std::fprintf(stderr, "\n");
8155 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8156 LLVM_DUMP_METHOD static void
8157 dumpModuleIDMap(StringRef Name,
8158 const ContinuousRangeMap<Key, ModuleFile *,
8159 InitialCapacity> &Map) {
8160 if (Map.begin() == Map.end())
8163 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8165 llvm::errs() << Name << ":\n";
8166 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8168 llvm::errs() << " " << I->first << " -> " << I->second->FileName
8173 LLVM_DUMP_METHOD void ASTReader::dump() {
8174 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8175 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8176 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8177 dumpModuleIDMap("Global type map", GlobalTypeMap);
8178 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8179 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8180 dumpModuleIDMap("Global macro map", GlobalMacroMap);
8181 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8182 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8183 dumpModuleIDMap("Global preprocessed entity map",
8184 GlobalPreprocessedEntityMap);
8186 llvm::errs() << "\n*** PCH/Modules Loaded:";
8187 for (ModuleFile &M : ModuleMgr)
8191 /// Return the amount of memory used by memory buffers, breaking down
8192 /// by heap-backed versus mmap'ed memory.
8193 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8194 for (ModuleFile &I : ModuleMgr) {
8195 if (llvm::MemoryBuffer *buf = I.Buffer) {
8196 size_t bytes = buf->getBufferSize();
8197 switch (buf->getBufferKind()) {
8198 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8199 sizes.malloc_bytes += bytes;
8201 case llvm::MemoryBuffer::MemoryBuffer_MMap:
8202 sizes.mmap_bytes += bytes;
8209 void ASTReader::InitializeSema(Sema &S) {
8211 S.addExternalSource(this);
8213 // Makes sure any declarations that were deserialized "too early"
8214 // still get added to the identifier's declaration chains.
8215 for (uint64_t ID : PreloadedDeclIDs) {
8216 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8217 pushExternalDeclIntoScope(D, D->getDeclName());
8219 PreloadedDeclIDs.clear();
8221 // FIXME: What happens if these are changed by a module import?
8222 if (!FPPragmaOptions.empty()) {
8223 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8224 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
8227 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
8228 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
8229 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
8234 void ASTReader::UpdateSema() {
8235 assert(SemaObj && "no Sema to update");
8237 // Load the offsets of the declarations that Sema references.
8238 // They will be lazily deserialized when needed.
8239 if (!SemaDeclRefs.empty()) {
8240 assert(SemaDeclRefs.size() % 3 == 0);
8241 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8242 if (!SemaObj->StdNamespace)
8243 SemaObj->StdNamespace = SemaDeclRefs[I];
8244 if (!SemaObj->StdBadAlloc)
8245 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8246 if (!SemaObj->StdAlignValT)
8247 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8249 SemaDeclRefs.clear();
8252 // Update the state of pragmas. Use the same API as if we had encountered the
8253 // pragma in the source.
8254 if(OptimizeOffPragmaLocation.isValid())
8255 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8256 if (PragmaMSStructState != -1)
8257 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8258 if (PointersToMembersPragmaLocation.isValid()) {
8259 SemaObj->ActOnPragmaMSPointersToMembers(
8260 (LangOptions::PragmaMSPointersToMembersKind)
8261 PragmaMSPointersToMembersState,
8262 PointersToMembersPragmaLocation);
8264 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8266 if (PragmaPackCurrentValue) {
8267 // The bottom of the stack might have a default value. It must be adjusted
8268 // to the current value to ensure that the packing state is preserved after
8269 // popping entries that were included/imported from a PCH/module.
8270 bool DropFirst = false;
8271 if (!PragmaPackStack.empty() &&
8272 PragmaPackStack.front().Location.isInvalid()) {
8273 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
8274 "Expected a default alignment value");
8275 SemaObj->PackStack.Stack.emplace_back(
8276 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
8277 SemaObj->PackStack.CurrentPragmaLocation,
8278 PragmaPackStack.front().PushLocation);
8281 for (const auto &Entry :
8282 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
8283 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
8284 Entry.Location, Entry.PushLocation);
8285 if (PragmaPackCurrentLocation.isInvalid()) {
8286 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
8287 "Expected a default alignment value");
8288 // Keep the current values.
8290 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
8291 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
8296 IdentifierInfo *ASTReader::get(StringRef Name) {
8297 // Note that we are loading an identifier.
8298 Deserializing AnIdentifier(this);
8300 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8301 NumIdentifierLookups,
8302 NumIdentifierLookupHits);
8304 // We don't need to do identifier table lookups in C++ modules (we preload
8305 // all interesting declarations, and don't need to use the scope for name
8306 // lookups). Perform the lookup in PCH files, though, since we don't build
8307 // a complete initial identifier table if we're carrying on from a PCH.
8308 if (PP.getLangOpts().CPlusPlus) {
8309 for (auto F : ModuleMgr.pch_modules())
8313 // If there is a global index, look there first to determine which modules
8314 // provably do not have any results for this identifier.
8315 GlobalModuleIndex::HitSet Hits;
8316 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8317 if (!loadGlobalIndex()) {
8318 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8323 ModuleMgr.visit(Visitor, HitsPtr);
8326 IdentifierInfo *II = Visitor.getIdentifierInfo();
8327 markIdentifierUpToDate(II);
8333 /// An identifier-lookup iterator that enumerates all of the
8334 /// identifiers stored within a set of AST files.
8335 class ASTIdentifierIterator : public IdentifierIterator {
8336 /// The AST reader whose identifiers are being enumerated.
8337 const ASTReader &Reader;
8339 /// The current index into the chain of AST files stored in
8343 /// The current position within the identifier lookup table
8344 /// of the current AST file.
8345 ASTIdentifierLookupTable::key_iterator Current;
8347 /// The end position within the identifier lookup table of
8348 /// the current AST file.
8349 ASTIdentifierLookupTable::key_iterator End;
8351 /// Whether to skip any modules in the ASTReader.
8355 explicit ASTIdentifierIterator(const ASTReader &Reader,
8356 bool SkipModules = false);
8358 StringRef Next() override;
8361 } // namespace clang
8363 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8365 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8368 StringRef ASTIdentifierIterator::Next() {
8369 while (Current == End) {
8370 // If we have exhausted all of our AST files, we're done.
8375 ModuleFile &F = Reader.ModuleMgr[Index];
8376 if (SkipModules && F.isModule())
8379 ASTIdentifierLookupTable *IdTable =
8380 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8381 Current = IdTable->key_begin();
8382 End = IdTable->key_end();
8385 // We have any identifiers remaining in the current AST file; return
8387 StringRef Result = *Current;
8394 /// A utility for appending two IdentifierIterators.
8395 class ChainedIdentifierIterator : public IdentifierIterator {
8396 std::unique_ptr<IdentifierIterator> Current;
8397 std::unique_ptr<IdentifierIterator> Queued;
8400 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8401 std::unique_ptr<IdentifierIterator> Second)
8402 : Current(std::move(First)), Queued(std::move(Second)) {}
8404 StringRef Next() override {
8408 StringRef result = Current->Next();
8409 if (!result.empty())
8412 // Try the queued iterator, which may itself be empty.
8414 std::swap(Current, Queued);
8421 IdentifierIterator *ASTReader::getIdentifiers() {
8422 if (!loadGlobalIndex()) {
8423 std::unique_ptr<IdentifierIterator> ReaderIter(
8424 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8425 std::unique_ptr<IdentifierIterator> ModulesIter(
8426 GlobalIndex->createIdentifierIterator());
8427 return new ChainedIdentifierIterator(std::move(ReaderIter),
8428 std::move(ModulesIter));
8431 return new ASTIdentifierIterator(*this);
8435 namespace serialization {
8437 class ReadMethodPoolVisitor {
8440 unsigned PriorGeneration;
8441 unsigned InstanceBits = 0;
8442 unsigned FactoryBits = 0;
8443 bool InstanceHasMoreThanOneDecl = false;
8444 bool FactoryHasMoreThanOneDecl = false;
8445 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8446 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8449 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8450 unsigned PriorGeneration)
8451 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8453 bool operator()(ModuleFile &M) {
8454 if (!M.SelectorLookupTable)
8457 // If we've already searched this module file, skip it now.
8458 if (M.Generation <= PriorGeneration)
8461 ++Reader.NumMethodPoolTableLookups;
8462 ASTSelectorLookupTable *PoolTable
8463 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8464 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8465 if (Pos == PoolTable->end())
8468 ++Reader.NumMethodPoolTableHits;
8469 ++Reader.NumSelectorsRead;
8470 // FIXME: Not quite happy with the statistics here. We probably should
8471 // disable this tracking when called via LoadSelector.
8472 // Also, should entries without methods count as misses?
8473 ++Reader.NumMethodPoolEntriesRead;
8474 ASTSelectorLookupTrait::data_type Data = *Pos;
8475 if (Reader.DeserializationListener)
8476 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8478 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8479 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8480 InstanceBits = Data.InstanceBits;
8481 FactoryBits = Data.FactoryBits;
8482 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8483 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8487 /// Retrieve the instance methods found by this visitor.
8488 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8489 return InstanceMethods;
8492 /// Retrieve the instance methods found by this visitor.
8493 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8494 return FactoryMethods;
8497 unsigned getInstanceBits() const { return InstanceBits; }
8498 unsigned getFactoryBits() const { return FactoryBits; }
8500 bool instanceHasMoreThanOneDecl() const {
8501 return InstanceHasMoreThanOneDecl;
8504 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8507 } // namespace serialization
8508 } // namespace clang
8510 /// Add the given set of methods to the method list.
8511 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8512 ObjCMethodList &List) {
8513 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8514 S.addMethodToGlobalList(&List, Methods[I]);
8518 void ASTReader::ReadMethodPool(Selector Sel) {
8519 // Get the selector generation and update it to the current generation.
8520 unsigned &Generation = SelectorGeneration[Sel];
8521 unsigned PriorGeneration = Generation;
8522 Generation = getGeneration();
8523 SelectorOutOfDate[Sel] = false;
8525 // Search for methods defined with this selector.
8526 ++NumMethodPoolLookups;
8527 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8528 ModuleMgr.visit(Visitor);
8530 if (Visitor.getInstanceMethods().empty() &&
8531 Visitor.getFactoryMethods().empty())
8534 ++NumMethodPoolHits;
8539 Sema &S = *getSema();
8540 Sema::GlobalMethodPool::iterator Pos
8541 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8543 Pos->second.first.setBits(Visitor.getInstanceBits());
8544 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8545 Pos->second.second.setBits(Visitor.getFactoryBits());
8546 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8548 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8549 // when building a module we keep every method individually and may need to
8550 // update hasMoreThanOneDecl as we add the methods.
8551 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8552 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8555 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8556 if (SelectorOutOfDate[Sel])
8557 ReadMethodPool(Sel);
8560 void ASTReader::ReadKnownNamespaces(
8561 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8564 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8565 if (NamespaceDecl *Namespace
8566 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8567 Namespaces.push_back(Namespace);
8571 void ASTReader::ReadUndefinedButUsed(
8572 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8573 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8574 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8575 SourceLocation Loc =
8576 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8577 Undefined.insert(std::make_pair(D, Loc));
8581 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8582 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8584 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8585 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8586 uint64_t Count = DelayedDeleteExprs[Idx++];
8587 for (uint64_t C = 0; C < Count; ++C) {
8588 SourceLocation DeleteLoc =
8589 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8590 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8591 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8596 void ASTReader::ReadTentativeDefinitions(
8597 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8598 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8599 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8601 TentativeDefs.push_back(Var);
8603 TentativeDefinitions.clear();
8606 void ASTReader::ReadUnusedFileScopedDecls(
8607 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8608 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8610 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8614 UnusedFileScopedDecls.clear();
8617 void ASTReader::ReadDelegatingConstructors(
8618 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8619 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8620 CXXConstructorDecl *D
8621 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8625 DelegatingCtorDecls.clear();
8628 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8629 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8631 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8635 ExtVectorDecls.clear();
8638 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8639 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8640 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8642 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8643 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8647 UnusedLocalTypedefNameCandidates.clear();
8650 void ASTReader::ReadReferencedSelectors(
8651 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8652 if (ReferencedSelectorsData.empty())
8655 // If there are @selector references added them to its pool. This is for
8656 // implementation of -Wselector.
8657 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8659 while (I < DataSize) {
8660 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8661 SourceLocation SelLoc
8662 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8663 Sels.push_back(std::make_pair(Sel, SelLoc));
8665 ReferencedSelectorsData.clear();
8668 void ASTReader::ReadWeakUndeclaredIdentifiers(
8669 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8670 if (WeakUndeclaredIdentifiers.empty())
8673 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8674 IdentifierInfo *WeakId
8675 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8676 IdentifierInfo *AliasId
8677 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8679 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8680 bool Used = WeakUndeclaredIdentifiers[I++];
8681 WeakInfo WI(AliasId, Loc);
8683 WeakIDs.push_back(std::make_pair(WeakId, WI));
8685 WeakUndeclaredIdentifiers.clear();
8688 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8689 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8690 ExternalVTableUse VT;
8691 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8692 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8693 VT.DefinitionRequired = VTableUses[Idx++];
8694 VTables.push_back(VT);
8700 void ASTReader::ReadPendingInstantiations(
8701 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8702 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8703 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8705 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8707 Pending.push_back(std::make_pair(D, Loc));
8709 PendingInstantiations.clear();
8712 void ASTReader::ReadLateParsedTemplates(
8713 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8715 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8717 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8719 auto LT = llvm::make_unique<LateParsedTemplate>();
8720 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8722 ModuleFile *F = getOwningModuleFile(LT->D);
8723 assert(F && "No module");
8725 unsigned TokN = LateParsedTemplates[Idx++];
8726 LT->Toks.reserve(TokN);
8727 for (unsigned T = 0; T < TokN; ++T)
8728 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8730 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8733 LateParsedTemplates.clear();
8736 void ASTReader::LoadSelector(Selector Sel) {
8737 // It would be complicated to avoid reading the methods anyway. So don't.
8738 ReadMethodPool(Sel);
8741 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8742 assert(ID && "Non-zero identifier ID required");
8743 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8744 IdentifiersLoaded[ID - 1] = II;
8745 if (DeserializationListener)
8746 DeserializationListener->IdentifierRead(ID, II);
8749 /// Set the globally-visible declarations associated with the given
8752 /// If the AST reader is currently in a state where the given declaration IDs
8753 /// cannot safely be resolved, they are queued until it is safe to resolve
8756 /// \param II an IdentifierInfo that refers to one or more globally-visible
8759 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8760 /// visible at global scope.
8762 /// \param Decls if non-null, this vector will be populated with the set of
8763 /// deserialized declarations. These declarations will not be pushed into
8766 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8767 const SmallVectorImpl<uint32_t> &DeclIDs,
8768 SmallVectorImpl<Decl *> *Decls) {
8769 if (NumCurrentElementsDeserializing && !Decls) {
8770 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8774 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8776 // Queue this declaration so that it will be added to the
8777 // translation unit scope and identifier's declaration chain
8778 // once a Sema object is known.
8779 PreloadedDeclIDs.push_back(DeclIDs[I]);
8783 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8785 // If we're simply supposed to record the declarations, do so now.
8787 Decls->push_back(D);
8791 // Introduce this declaration into the translation-unit scope
8792 // and add it to the declaration chain for this identifier, so
8793 // that (unqualified) name lookup will find it.
8794 pushExternalDeclIntoScope(D, II);
8798 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8802 if (IdentifiersLoaded.empty()) {
8803 Error("no identifier table in AST file");
8808 if (!IdentifiersLoaded[ID]) {
8809 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8810 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8811 ModuleFile *M = I->second;
8812 unsigned Index = ID - M->BaseIdentifierID;
8813 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8815 // All of the strings in the AST file are preceded by a 16-bit length.
8816 // Extract that 16-bit length to avoid having to execute strlen().
8817 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8818 // unsigned integers. This is important to avoid integer overflow when
8819 // we cast them to 'unsigned'.
8820 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8821 unsigned StrLen = (((unsigned) StrLenPtr[0])
8822 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8823 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8824 IdentifiersLoaded[ID] = &II;
8825 markIdentifierFromAST(*this, II);
8826 if (DeserializationListener)
8827 DeserializationListener->IdentifierRead(ID + 1, &II);
8830 return IdentifiersLoaded[ID];
8833 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8834 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8837 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8838 if (LocalID < NUM_PREDEF_IDENT_IDS)
8841 if (!M.ModuleOffsetMap.empty())
8842 ReadModuleOffsetMap(M);
8844 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8845 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8846 assert(I != M.IdentifierRemap.end()
8847 && "Invalid index into identifier index remap");
8849 return LocalID + I->second;
8852 MacroInfo *ASTReader::getMacro(MacroID ID) {
8856 if (MacrosLoaded.empty()) {
8857 Error("no macro table in AST file");
8861 ID -= NUM_PREDEF_MACRO_IDS;
8862 if (!MacrosLoaded[ID]) {
8863 GlobalMacroMapType::iterator I
8864 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8865 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8866 ModuleFile *M = I->second;
8867 unsigned Index = ID - M->BaseMacroID;
8868 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8870 if (DeserializationListener)
8871 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8875 return MacrosLoaded[ID];
8878 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8879 if (LocalID < NUM_PREDEF_MACRO_IDS)
8882 if (!M.ModuleOffsetMap.empty())
8883 ReadModuleOffsetMap(M);
8885 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8886 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8887 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8889 return LocalID + I->second;
8892 serialization::SubmoduleID
8893 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8894 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8897 if (!M.ModuleOffsetMap.empty())
8898 ReadModuleOffsetMap(M);
8900 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8901 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8902 assert(I != M.SubmoduleRemap.end()
8903 && "Invalid index into submodule index remap");
8905 return LocalID + I->second;
8908 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8909 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8910 assert(GlobalID == 0 && "Unhandled global submodule ID");
8914 if (GlobalID > SubmodulesLoaded.size()) {
8915 Error("submodule ID out of range in AST file");
8919 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8922 Module *ASTReader::getModule(unsigned ID) {
8923 return getSubmodule(ID);
8926 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8927 ModuleFile *MF = getOwningModuleFile(D);
8928 return MF && MF->PCHHasObjectFile;
8931 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8933 // It's a module, look it up by submodule ID.
8934 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8935 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8937 // It's a prefix (preamble, PCH, ...). Look it up by index.
8938 unsigned IndexFromEnd = ID >> 1;
8939 assert(IndexFromEnd && "got reference to unknown module file");
8940 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8944 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8948 // For a file representing a module, use the submodule ID of the top-level
8949 // module as the file ID. For any other kind of file, the number of such
8950 // files loaded beforehand will be the same on reload.
8951 // FIXME: Is this true even if we have an explicit module file and a PCH?
8953 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8955 auto PCHModules = getModuleManager().pch_modules();
8956 auto I = llvm::find(PCHModules, F);
8957 assert(I != PCHModules.end() && "emitting reference to unknown file");
8958 return (I - PCHModules.end()) << 1;
8961 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8962 ASTReader::getSourceDescriptor(unsigned ID) {
8963 if (const Module *M = getSubmodule(ID))
8964 return ExternalASTSource::ASTSourceDescriptor(*M);
8966 // If there is only a single PCH, return it instead.
8967 // Chained PCH are not supported.
8968 const auto &PCHChain = ModuleMgr.pch_modules();
8969 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8970 ModuleFile &MF = ModuleMgr.getPrimaryModule();
8971 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8972 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8973 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8979 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8980 auto I = DefinitionSource.find(FD);
8981 if (I == DefinitionSource.end())
8982 return EK_ReplyHazy;
8983 return I->second ? EK_Never : EK_Always;
8986 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8987 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8990 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8994 if (ID > SelectorsLoaded.size()) {
8995 Error("selector ID out of range in AST file");
8999 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9000 // Load this selector from the selector table.
9001 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9002 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9003 ModuleFile &M = *I->second;
9004 ASTSelectorLookupTrait Trait(*this, M);
9005 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9006 SelectorsLoaded[ID - 1] =
9007 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9008 if (DeserializationListener)
9009 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9012 return SelectorsLoaded[ID - 1];
9015 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9016 return DecodeSelector(ID);
9019 uint32_t ASTReader::GetNumExternalSelectors() {
9020 // ID 0 (the null selector) is considered an external selector.
9021 return getTotalNumSelectors() + 1;
9024 serialization::SelectorID
9025 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9026 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9029 if (!M.ModuleOffsetMap.empty())
9030 ReadModuleOffsetMap(M);
9032 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9033 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9034 assert(I != M.SelectorRemap.end()
9035 && "Invalid index into selector index remap");
9037 return LocalID + I->second;
9041 ASTReader::ReadDeclarationName(ModuleFile &F,
9042 const RecordData &Record, unsigned &Idx) {
9043 ASTContext &Context = getContext();
9044 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
9046 case DeclarationName::Identifier:
9047 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
9049 case DeclarationName::ObjCZeroArgSelector:
9050 case DeclarationName::ObjCOneArgSelector:
9051 case DeclarationName::ObjCMultiArgSelector:
9052 return DeclarationName(ReadSelector(F, Record, Idx));
9054 case DeclarationName::CXXConstructorName:
9055 return Context.DeclarationNames.getCXXConstructorName(
9056 Context.getCanonicalType(readType(F, Record, Idx)));
9058 case DeclarationName::CXXDestructorName:
9059 return Context.DeclarationNames.getCXXDestructorName(
9060 Context.getCanonicalType(readType(F, Record, Idx)));
9062 case DeclarationName::CXXDeductionGuideName:
9063 return Context.DeclarationNames.getCXXDeductionGuideName(
9064 ReadDeclAs<TemplateDecl>(F, Record, Idx));
9066 case DeclarationName::CXXConversionFunctionName:
9067 return Context.DeclarationNames.getCXXConversionFunctionName(
9068 Context.getCanonicalType(readType(F, Record, Idx)));
9070 case DeclarationName::CXXOperatorName:
9071 return Context.DeclarationNames.getCXXOperatorName(
9072 (OverloadedOperatorKind)Record[Idx++]);
9074 case DeclarationName::CXXLiteralOperatorName:
9075 return Context.DeclarationNames.getCXXLiteralOperatorName(
9076 GetIdentifierInfo(F, Record, Idx));
9078 case DeclarationName::CXXUsingDirective:
9079 return DeclarationName::getUsingDirectiveName();
9082 llvm_unreachable("Invalid NameKind!");
9085 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
9086 DeclarationNameLoc &DNLoc,
9087 DeclarationName Name,
9088 const RecordData &Record, unsigned &Idx) {
9089 switch (Name.getNameKind()) {
9090 case DeclarationName::CXXConstructorName:
9091 case DeclarationName::CXXDestructorName:
9092 case DeclarationName::CXXConversionFunctionName:
9093 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
9096 case DeclarationName::CXXOperatorName:
9097 DNLoc.CXXOperatorName.BeginOpNameLoc
9098 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9099 DNLoc.CXXOperatorName.EndOpNameLoc
9100 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9103 case DeclarationName::CXXLiteralOperatorName:
9104 DNLoc.CXXLiteralOperatorName.OpNameLoc
9105 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9108 case DeclarationName::Identifier:
9109 case DeclarationName::ObjCZeroArgSelector:
9110 case DeclarationName::ObjCOneArgSelector:
9111 case DeclarationName::ObjCMultiArgSelector:
9112 case DeclarationName::CXXUsingDirective:
9113 case DeclarationName::CXXDeductionGuideName:
9118 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
9119 DeclarationNameInfo &NameInfo,
9120 const RecordData &Record, unsigned &Idx) {
9121 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
9122 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
9123 DeclarationNameLoc DNLoc;
9124 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
9125 NameInfo.setInfo(DNLoc);
9128 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
9129 const RecordData &Record, unsigned &Idx) {
9130 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
9131 unsigned NumTPLists = Record[Idx++];
9132 Info.NumTemplParamLists = NumTPLists;
9134 Info.TemplParamLists =
9135 new (getContext()) TemplateParameterList *[NumTPLists];
9136 for (unsigned i = 0; i != NumTPLists; ++i)
9137 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
9142 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
9144 ASTContext &Context = getContext();
9145 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
9147 case TemplateName::Template:
9148 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
9150 case TemplateName::OverloadedTemplate: {
9151 unsigned size = Record[Idx++];
9152 UnresolvedSet<8> Decls;
9154 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
9156 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
9159 case TemplateName::AssumedTemplate: {
9160 DeclarationName Name = ReadDeclarationName(F, Record, Idx);
9161 return Context.getAssumedTemplateName(Name);
9164 case TemplateName::QualifiedTemplate: {
9165 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9166 bool hasTemplKeyword = Record[Idx++];
9167 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
9168 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
9171 case TemplateName::DependentTemplate: {
9172 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9173 if (Record[Idx++]) // isIdentifier
9174 return Context.getDependentTemplateName(NNS,
9175 GetIdentifierInfo(F, Record,
9177 return Context.getDependentTemplateName(NNS,
9178 (OverloadedOperatorKind)Record[Idx++]);
9181 case TemplateName::SubstTemplateTemplateParm: {
9182 TemplateTemplateParmDecl *param
9183 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9184 if (!param) return TemplateName();
9185 TemplateName replacement = ReadTemplateName(F, Record, Idx);
9186 return Context.getSubstTemplateTemplateParm(param, replacement);
9189 case TemplateName::SubstTemplateTemplateParmPack: {
9190 TemplateTemplateParmDecl *Param
9191 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9193 return TemplateName();
9195 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
9196 if (ArgPack.getKind() != TemplateArgument::Pack)
9197 return TemplateName();
9199 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
9203 llvm_unreachable("Unhandled template name kind!");
9206 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
9207 const RecordData &Record,
9209 bool Canonicalize) {
9210 ASTContext &Context = getContext();
9212 // The caller wants a canonical template argument. Sometimes the AST only
9213 // wants template arguments in canonical form (particularly as the template
9214 // argument lists of template specializations) so ensure we preserve that
9215 // canonical form across serialization.
9216 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
9217 return Context.getCanonicalTemplateArgument(Arg);
9220 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
9222 case TemplateArgument::Null:
9223 return TemplateArgument();
9224 case TemplateArgument::Type:
9225 return TemplateArgument(readType(F, Record, Idx));
9226 case TemplateArgument::Declaration: {
9227 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
9228 return TemplateArgument(D, readType(F, Record, Idx));
9230 case TemplateArgument::NullPtr:
9231 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
9232 case TemplateArgument::Integral: {
9233 llvm::APSInt Value = ReadAPSInt(Record, Idx);
9234 QualType T = readType(F, Record, Idx);
9235 return TemplateArgument(Context, Value, T);
9237 case TemplateArgument::Template:
9238 return TemplateArgument(ReadTemplateName(F, Record, Idx));
9239 case TemplateArgument::TemplateExpansion: {
9240 TemplateName Name = ReadTemplateName(F, Record, Idx);
9241 Optional<unsigned> NumTemplateExpansions;
9242 if (unsigned NumExpansions = Record[Idx++])
9243 NumTemplateExpansions = NumExpansions - 1;
9244 return TemplateArgument(Name, NumTemplateExpansions);
9246 case TemplateArgument::Expression:
9247 return TemplateArgument(ReadExpr(F));
9248 case TemplateArgument::Pack: {
9249 unsigned NumArgs = Record[Idx++];
9250 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
9251 for (unsigned I = 0; I != NumArgs; ++I)
9252 Args[I] = ReadTemplateArgument(F, Record, Idx);
9253 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
9257 llvm_unreachable("Unhandled template argument kind!");
9260 TemplateParameterList *
9261 ASTReader::ReadTemplateParameterList(ModuleFile &F,
9262 const RecordData &Record, unsigned &Idx) {
9263 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
9264 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
9265 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
9267 unsigned NumParams = Record[Idx++];
9268 SmallVector<NamedDecl *, 16> Params;
9269 Params.reserve(NumParams);
9271 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
9274 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9275 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
9276 return TemplateParams;
9281 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
9282 ModuleFile &F, const RecordData &Record,
9283 unsigned &Idx, bool Canonicalize) {
9284 unsigned NumTemplateArgs = Record[Idx++];
9285 TemplArgs.reserve(NumTemplateArgs);
9286 while (NumTemplateArgs--)
9287 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
9290 /// Read a UnresolvedSet structure.
9291 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
9292 const RecordData &Record, unsigned &Idx) {
9293 unsigned NumDecls = Record[Idx++];
9294 Set.reserve(getContext(), NumDecls);
9295 while (NumDecls--) {
9296 DeclID ID = ReadDeclID(F, Record, Idx);
9297 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
9298 Set.addLazyDecl(getContext(), ID, AS);
9303 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
9304 const RecordData &Record, unsigned &Idx) {
9305 bool isVirtual = static_cast<bool>(Record[Idx++]);
9306 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
9307 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
9308 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
9309 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
9310 SourceRange Range = ReadSourceRange(F, Record, Idx);
9311 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
9312 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9314 Result.setInheritConstructors(inheritConstructors);
9318 CXXCtorInitializer **
9319 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
9321 ASTContext &Context = getContext();
9322 unsigned NumInitializers = Record[Idx++];
9323 assert(NumInitializers && "wrote ctor initializers but have no inits");
9324 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9325 for (unsigned i = 0; i != NumInitializers; ++i) {
9326 TypeSourceInfo *TInfo = nullptr;
9327 bool IsBaseVirtual = false;
9328 FieldDecl *Member = nullptr;
9329 IndirectFieldDecl *IndirectMember = nullptr;
9331 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
9333 case CTOR_INITIALIZER_BASE:
9334 TInfo = GetTypeSourceInfo(F, Record, Idx);
9335 IsBaseVirtual = Record[Idx++];
9338 case CTOR_INITIALIZER_DELEGATING:
9339 TInfo = GetTypeSourceInfo(F, Record, Idx);
9342 case CTOR_INITIALIZER_MEMBER:
9343 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
9346 case CTOR_INITIALIZER_INDIRECT_MEMBER:
9347 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
9351 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
9352 Expr *Init = ReadExpr(F);
9353 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
9354 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
9356 CXXCtorInitializer *BOMInit;
9357 if (Type == CTOR_INITIALIZER_BASE)
9358 BOMInit = new (Context)
9359 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9360 RParenLoc, MemberOrEllipsisLoc);
9361 else if (Type == CTOR_INITIALIZER_DELEGATING)
9362 BOMInit = new (Context)
9363 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9365 BOMInit = new (Context)
9366 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9369 BOMInit = new (Context)
9370 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9371 LParenLoc, Init, RParenLoc);
9373 if (/*IsWritten*/Record[Idx++]) {
9374 unsigned SourceOrder = Record[Idx++];
9375 BOMInit->setSourceOrder(SourceOrder);
9378 CtorInitializers[i] = BOMInit;
9381 return CtorInitializers;
9384 NestedNameSpecifier *
9385 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
9386 const RecordData &Record, unsigned &Idx) {
9387 ASTContext &Context = getContext();
9388 unsigned N = Record[Idx++];
9389 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
9390 for (unsigned I = 0; I != N; ++I) {
9391 NestedNameSpecifier::SpecifierKind Kind
9392 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9394 case NestedNameSpecifier::Identifier: {
9395 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9396 NNS = NestedNameSpecifier::Create(Context, Prev, II);
9400 case NestedNameSpecifier::Namespace: {
9401 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9402 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
9406 case NestedNameSpecifier::NamespaceAlias: {
9407 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9408 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
9412 case NestedNameSpecifier::TypeSpec:
9413 case NestedNameSpecifier::TypeSpecWithTemplate: {
9414 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9418 bool Template = Record[Idx++];
9419 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9423 case NestedNameSpecifier::Global:
9424 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9425 // No associated value, and there can't be a prefix.
9428 case NestedNameSpecifier::Super: {
9429 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9430 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9439 NestedNameSpecifierLoc
9440 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9442 ASTContext &Context = getContext();
9443 unsigned N = Record[Idx++];
9444 NestedNameSpecifierLocBuilder Builder;
9445 for (unsigned I = 0; I != N; ++I) {
9446 NestedNameSpecifier::SpecifierKind Kind
9447 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9449 case NestedNameSpecifier::Identifier: {
9450 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9451 SourceRange Range = ReadSourceRange(F, Record, Idx);
9452 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9456 case NestedNameSpecifier::Namespace: {
9457 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9458 SourceRange Range = ReadSourceRange(F, Record, Idx);
9459 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9463 case NestedNameSpecifier::NamespaceAlias: {
9464 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9465 SourceRange Range = ReadSourceRange(F, Record, Idx);
9466 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9470 case NestedNameSpecifier::TypeSpec:
9471 case NestedNameSpecifier::TypeSpecWithTemplate: {
9472 bool Template = Record[Idx++];
9473 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9475 return NestedNameSpecifierLoc();
9476 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9478 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9479 Builder.Extend(Context,
9480 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9481 T->getTypeLoc(), ColonColonLoc);
9485 case NestedNameSpecifier::Global: {
9486 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9487 Builder.MakeGlobal(Context, ColonColonLoc);
9491 case NestedNameSpecifier::Super: {
9492 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9493 SourceRange Range = ReadSourceRange(F, Record, Idx);
9494 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9500 return Builder.getWithLocInContext(Context);
9504 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9506 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9507 SourceLocation end = ReadSourceLocation(F, Record, Idx);
9508 return SourceRange(beg, end);
9511 static FixedPointSemantics
9512 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
9514 unsigned Width = Record[Idx++];
9515 unsigned Scale = Record[Idx++];
9516 uint64_t Tmp = Record[Idx++];
9517 bool IsSigned = Tmp & 0x1;
9518 bool IsSaturated = Tmp & 0x2;
9519 bool HasUnsignedPadding = Tmp & 0x4;
9520 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
9521 HasUnsignedPadding);
9524 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
9525 unsigned Kind = Record[Idx++];
9529 case APValue::Indeterminate:
9530 return APValue::IndeterminateValue();
9532 return APValue(ReadAPSInt(Record, Idx));
9533 case APValue::Float: {
9534 const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
9535 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9536 return APValue(ReadAPFloat(Record, FloatSema, Idx));
9538 case APValue::FixedPoint: {
9539 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
9540 return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
9542 case APValue::ComplexInt: {
9543 llvm::APSInt First = ReadAPSInt(Record, Idx);
9544 return APValue(std::move(First), ReadAPSInt(Record, Idx));
9546 case APValue::ComplexFloat: {
9547 const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
9548 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9549 llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
9550 const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
9551 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9552 return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
9554 case APValue::LValue:
9555 case APValue::Vector:
9556 case APValue::Array:
9557 case APValue::Struct:
9558 case APValue::Union:
9559 case APValue::MemberPointer:
9560 case APValue::AddrLabelDiff:
9561 // TODO : Handle all these APValue::ValueKind.
9564 llvm_unreachable("Invalid APValue::ValueKind");
9567 /// Read an integral value
9568 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9569 unsigned BitWidth = Record[Idx++];
9570 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9571 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9576 /// Read a signed integral value
9577 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9578 bool isUnsigned = Record[Idx++];
9579 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9582 /// Read a floating-point value
9583 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9584 const llvm::fltSemantics &Sem,
9586 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9590 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9591 unsigned Len = Record[Idx++];
9592 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9597 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9599 std::string Filename = ReadString(Record, Idx);
9600 ResolveImportedPath(F, Filename);
9604 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9605 const RecordData &Record, unsigned &Idx) {
9606 std::string Filename = ReadString(Record, Idx);
9607 if (!BaseDirectory.empty())
9608 ResolveImportedPath(Filename, BaseDirectory);
9612 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9614 unsigned Major = Record[Idx++];
9615 unsigned Minor = Record[Idx++];
9616 unsigned Subminor = Record[Idx++];
9618 return VersionTuple(Major);
9620 return VersionTuple(Major, Minor - 1);
9621 return VersionTuple(Major, Minor - 1, Subminor - 1);
9624 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9625 const RecordData &Record,
9627 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9628 return CXXTemporary::Create(getContext(), Decl);
9631 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9632 return Diag(CurrentImportLoc, DiagID);
9635 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9636 return Diags.Report(Loc, DiagID);
9639 /// Retrieve the identifier table associated with the
9641 IdentifierTable &ASTReader::getIdentifierTable() {
9642 return PP.getIdentifierTable();
9645 /// Record that the given ID maps to the given switch-case
9647 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9648 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9649 "Already have a SwitchCase with this ID");
9650 (*CurrSwitchCaseStmts)[ID] = SC;
9653 /// Retrieve the switch-case statement with the given ID.
9654 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9655 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9656 return (*CurrSwitchCaseStmts)[ID];
9659 void ASTReader::ClearSwitchCaseIDs() {
9660 CurrSwitchCaseStmts->clear();
9663 void ASTReader::ReadComments() {
9664 ASTContext &Context = getContext();
9665 std::vector<RawComment *> Comments;
9666 for (SmallVectorImpl<std::pair<BitstreamCursor,
9667 serialization::ModuleFile *>>::iterator
9668 I = CommentsCursors.begin(),
9669 E = CommentsCursors.end();
9672 BitstreamCursor &Cursor = I->first;
9673 serialization::ModuleFile &F = *I->second;
9674 SavedStreamPosition SavedPosition(Cursor);
9678 Expected<llvm::BitstreamEntry> MaybeEntry =
9679 Cursor.advanceSkippingSubblocks(
9680 BitstreamCursor::AF_DontPopBlockAtEnd);
9682 Error(MaybeEntry.takeError());
9685 llvm::BitstreamEntry Entry = MaybeEntry.get();
9687 switch (Entry.Kind) {
9688 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9689 case llvm::BitstreamEntry::Error:
9690 Error("malformed block record in AST file");
9692 case llvm::BitstreamEntry::EndBlock:
9694 case llvm::BitstreamEntry::Record:
9695 // The interesting case.
9701 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9702 if (!MaybeComment) {
9703 Error(MaybeComment.takeError());
9706 switch ((CommentRecordTypes)MaybeComment.get()) {
9707 case COMMENTS_RAW_COMMENT: {
9709 SourceRange SR = ReadSourceRange(F, Record, Idx);
9710 RawComment::CommentKind Kind =
9711 (RawComment::CommentKind) Record[Idx++];
9712 bool IsTrailingComment = Record[Idx++];
9713 bool IsAlmostTrailingComment = Record[Idx++];
9714 Comments.push_back(new (Context) RawComment(
9715 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9721 // De-serialized SourceLocations get negative FileIDs for other modules,
9722 // potentially invalidating the original order. Sort it again.
9723 llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr));
9724 Context.Comments.addDeserializedComments(Comments);
9728 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9729 bool IncludeSystem, bool Complain,
9730 llvm::function_ref<void(const serialization::InputFile &IF,
9731 bool isSystem)> Visitor) {
9732 unsigned NumUserInputs = MF.NumUserInputFiles;
9733 unsigned NumInputs = MF.InputFilesLoaded.size();
9734 assert(NumUserInputs <= NumInputs);
9735 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9736 for (unsigned I = 0; I < N; ++I) {
9737 bool IsSystem = I >= NumUserInputs;
9738 InputFile IF = getInputFile(MF, I+1, Complain);
9739 Visitor(IF, IsSystem);
9743 void ASTReader::visitTopLevelModuleMaps(
9744 serialization::ModuleFile &MF,
9745 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9746 unsigned NumInputs = MF.InputFilesLoaded.size();
9747 for (unsigned I = 0; I < NumInputs; ++I) {
9748 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9749 if (IFI.TopLevelModuleMap)
9750 // FIXME: This unnecessarily re-reads the InputFileInfo.
9751 if (auto *FE = getInputFile(MF, I + 1).getFile())
9756 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9757 // If we know the owning module, use it.
9758 if (Module *M = D->getImportedOwningModule())
9759 return M->getFullModuleName();
9761 // Otherwise, use the name of the top-level module the decl is within.
9762 if (ModuleFile *M = getOwningModuleFile(D))
9763 return M->ModuleName;
9765 // Not from a module.
9769 void ASTReader::finishPendingActions() {
9770 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9771 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9772 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9773 !PendingUpdateRecords.empty()) {
9774 // If any identifiers with corresponding top-level declarations have
9775 // been loaded, load those declarations now.
9776 using TopLevelDeclsMap =
9777 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9778 TopLevelDeclsMap TopLevelDecls;
9780 while (!PendingIdentifierInfos.empty()) {
9781 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9782 SmallVector<uint32_t, 4> DeclIDs =
9783 std::move(PendingIdentifierInfos.back().second);
9784 PendingIdentifierInfos.pop_back();
9786 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9789 // Load each function type that we deferred loading because it was a
9790 // deduced type that might refer to a local type declared within itself.
9791 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9792 auto *FD = PendingFunctionTypes[I].first;
9793 FD->setType(GetType(PendingFunctionTypes[I].second));
9795 // If we gave a function a deduced return type, remember that we need to
9796 // propagate that along the redeclaration chain.
9797 auto *DT = FD->getReturnType()->getContainedDeducedType();
9798 if (DT && DT->isDeduced())
9799 PendingDeducedTypeUpdates.insert(
9800 {FD->getCanonicalDecl(), FD->getReturnType()});
9802 PendingFunctionTypes.clear();
9804 // For each decl chain that we wanted to complete while deserializing, mark
9805 // it as "still needs to be completed".
9806 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9807 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9809 PendingIncompleteDeclChains.clear();
9811 // Load pending declaration chains.
9812 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9813 loadPendingDeclChain(PendingDeclChains[I].first,
9814 PendingDeclChains[I].second);
9815 PendingDeclChains.clear();
9817 // Make the most recent of the top-level declarations visible.
9818 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9819 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9820 IdentifierInfo *II = TLD->first;
9821 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9822 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9826 // Load any pending macro definitions.
9827 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9828 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9829 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9830 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9831 // Initialize the macro history from chained-PCHs ahead of module imports.
9832 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9834 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9835 if (!Info.M->isModule())
9836 resolvePendingMacro(II, Info);
9838 // Handle module imports.
9839 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9841 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9842 if (Info.M->isModule())
9843 resolvePendingMacro(II, Info);
9846 PendingMacroIDs.clear();
9848 // Wire up the DeclContexts for Decls that we delayed setting until
9849 // recursive loading is completed.
9850 while (!PendingDeclContextInfos.empty()) {
9851 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9852 PendingDeclContextInfos.pop_front();
9853 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9854 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9855 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9858 // Perform any pending declaration updates.
9859 while (!PendingUpdateRecords.empty()) {
9860 auto Update = PendingUpdateRecords.pop_back_val();
9861 ReadingKindTracker ReadingKind(Read_Decl, *this);
9862 loadDeclUpdateRecords(Update);
9866 // At this point, all update records for loaded decls are in place, so any
9867 // fake class definitions should have become real.
9868 assert(PendingFakeDefinitionData.empty() &&
9869 "faked up a class definition but never saw the real one");
9871 // If we deserialized any C++ or Objective-C class definitions, any
9872 // Objective-C protocol definitions, or any redeclarable templates, make sure
9873 // that all redeclarations point to the definitions. Note that this can only
9874 // happen now, after the redeclaration chains have been fully wired.
9875 for (Decl *D : PendingDefinitions) {
9876 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9877 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9878 // Make sure that the TagType points at the definition.
9879 const_cast<TagType*>(TagT)->decl = TD;
9882 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9883 for (auto *R = getMostRecentExistingDecl(RD); R;
9884 R = R->getPreviousDecl()) {
9886 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9887 "declaration thinks it's the definition but it isn't");
9888 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9895 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9896 // Make sure that the ObjCInterfaceType points at the definition.
9897 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9900 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9901 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9906 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9907 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9908 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9913 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9914 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9915 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9917 PendingDefinitions.clear();
9919 // Load the bodies of any functions or methods we've encountered. We do
9920 // this now (delayed) so that we can be sure that the declaration chains
9921 // have been fully wired up (hasBody relies on this).
9922 // FIXME: We shouldn't require complete redeclaration chains here.
9923 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9924 PBEnd = PendingBodies.end();
9925 PB != PBEnd; ++PB) {
9926 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9927 // For a function defined inline within a class template, force the
9928 // canonical definition to be the one inside the canonical definition of
9929 // the template. This ensures that we instantiate from a correct view
9932 // Sadly we can't do this more generally: we can't be sure that all
9933 // copies of an arbitrary class definition will have the same members
9934 // defined (eg, some member functions may not be instantiated, and some
9935 // special members may or may not have been implicitly defined).
9936 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9937 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9940 // FIXME: Check for =delete/=default?
9941 // FIXME: Complain about ODR violations here?
9942 const FunctionDecl *Defn = nullptr;
9943 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9944 FD->setLazyBody(PB->second);
9946 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9947 mergeDefinitionVisibility(NonConstDefn, FD);
9949 if (!FD->isLateTemplateParsed() &&
9950 !NonConstDefn->isLateTemplateParsed() &&
9951 FD->getODRHash() != NonConstDefn->getODRHash()) {
9952 if (!isa<CXXMethodDecl>(FD)) {
9953 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9954 } else if (FD->getLexicalParent()->isFileContext() &&
9955 NonConstDefn->getLexicalParent()->isFileContext()) {
9956 // Only diagnose out-of-line method definitions. If they are
9957 // in class definitions, then an error will be generated when
9958 // processing the class bodies.
9959 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9966 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9967 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9968 MD->setLazyBody(PB->second);
9970 PendingBodies.clear();
9973 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9974 getContext().deduplicateMergedDefinitonsFor(ND);
9975 PendingMergedDefinitionsToDeduplicate.clear();
9978 void ASTReader::diagnoseOdrViolations() {
9979 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9980 PendingFunctionOdrMergeFailures.empty() &&
9981 PendingEnumOdrMergeFailures.empty())
9984 // Trigger the import of the full definition of each class that had any
9985 // odr-merging problems, so we can produce better diagnostics for them.
9986 // These updates may in turn find and diagnose some ODR failures, so take
9987 // ownership of the set first.
9988 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9989 PendingOdrMergeFailures.clear();
9990 for (auto &Merge : OdrMergeFailures) {
9991 Merge.first->buildLookup();
9992 Merge.first->decls_begin();
9993 Merge.first->bases_begin();
9994 Merge.first->vbases_begin();
9995 for (auto &RecordPair : Merge.second) {
9996 auto *RD = RecordPair.first;
10003 // Trigger the import of functions.
10004 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10005 PendingFunctionOdrMergeFailures.clear();
10006 for (auto &Merge : FunctionOdrMergeFailures) {
10007 Merge.first->buildLookup();
10008 Merge.first->decls_begin();
10009 Merge.first->getBody();
10010 for (auto &FD : Merge.second) {
10017 // Trigger the import of enums.
10018 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10019 PendingEnumOdrMergeFailures.clear();
10020 for (auto &Merge : EnumOdrMergeFailures) {
10021 Merge.first->decls_begin();
10022 for (auto &Enum : Merge.second) {
10023 Enum->decls_begin();
10027 // For each declaration from a merged context, check that the canonical
10028 // definition of that context also contains a declaration of the same
10031 // Caution: this loop does things that might invalidate iterators into
10032 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10033 while (!PendingOdrMergeChecks.empty()) {
10034 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10036 // FIXME: Skip over implicit declarations for now. This matters for things
10037 // like implicitly-declared special member functions. This isn't entirely
10038 // correct; we can end up with multiple unmerged declarations of the same
10039 // implicit entity.
10040 if (D->isImplicit())
10043 DeclContext *CanonDef = D->getDeclContext();
10045 bool Found = false;
10046 const Decl *DCanon = D->getCanonicalDecl();
10048 for (auto RI : D->redecls()) {
10049 if (RI->getLexicalDeclContext() == CanonDef) {
10057 // Quick check failed, time to do the slow thing. Note, we can't just
10058 // look up the name of D in CanonDef here, because the member that is
10059 // in CanonDef might not be found by name lookup (it might have been
10060 // replaced by a more recent declaration in the lookup table), and we
10061 // can't necessarily find it in the redeclaration chain because it might
10062 // be merely mergeable, not redeclarable.
10063 llvm::SmallVector<const NamedDecl*, 4> Candidates;
10064 for (auto *CanonMember : CanonDef->decls()) {
10065 if (CanonMember->getCanonicalDecl() == DCanon) {
10066 // This can happen if the declaration is merely mergeable and not
10067 // actually redeclarable (we looked for redeclarations earlier).
10069 // FIXME: We should be able to detect this more efficiently, without
10070 // pulling in all of the members of CanonDef.
10074 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
10075 if (ND->getDeclName() == D->getDeclName())
10076 Candidates.push_back(ND);
10080 // The AST doesn't like TagDecls becoming invalid after they've been
10081 // completed. We only really need to mark FieldDecls as invalid here.
10082 if (!isa<TagDecl>(D))
10083 D->setInvalidDecl();
10085 // Ensure we don't accidentally recursively enter deserialization while
10086 // we're producing our diagnostic.
10087 Deserializing RecursionGuard(this);
10089 std::string CanonDefModule =
10090 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
10091 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
10092 << D << getOwningModuleNameForDiagnostic(D)
10093 << CanonDef << CanonDefModule.empty() << CanonDefModule;
10095 if (Candidates.empty())
10096 Diag(cast<Decl>(CanonDef)->getLocation(),
10097 diag::note_module_odr_violation_no_possible_decls) << D;
10099 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10100 Diag(Candidates[I]->getLocation(),
10101 diag::note_module_odr_violation_possible_decl)
10105 DiagnosedOdrMergeFailures.insert(CanonDef);
10109 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
10110 EnumOdrMergeFailures.empty())
10113 // Ensure we don't accidentally recursively enter deserialization while
10114 // we're producing our diagnostics.
10115 Deserializing RecursionGuard(this);
10117 // Common code for hashing helpers.
10119 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
10121 Hash.AddQualType(Ty);
10122 return Hash.CalculateHash();
10125 auto ComputeODRHash = [&Hash](const Stmt *S) {
10129 return Hash.CalculateHash();
10132 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
10135 Hash.AddSubDecl(D);
10136 return Hash.CalculateHash();
10139 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
10141 Hash.AddTemplateArgument(TA);
10142 return Hash.CalculateHash();
10145 auto ComputeTemplateParameterListODRHash =
10146 [&Hash](const TemplateParameterList *TPL) {
10149 Hash.AddTemplateParameterList(TPL);
10150 return Hash.CalculateHash();
10153 // Issue any pending ODR-failure diagnostics.
10154 for (auto &Merge : OdrMergeFailures) {
10155 // If we've already pointed out a specific problem with this class, don't
10156 // bother issuing a general "something's different" diagnostic.
10157 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10160 bool Diagnosed = false;
10161 CXXRecordDecl *FirstRecord = Merge.first;
10162 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10163 for (auto &RecordPair : Merge.second) {
10164 CXXRecordDecl *SecondRecord = RecordPair.first;
10165 // Multiple different declarations got merged together; tell the user
10166 // where they came from.
10167 if (FirstRecord == SecondRecord)
10170 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10172 auto *FirstDD = FirstRecord->DefinitionData;
10173 auto *SecondDD = RecordPair.second;
10175 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10177 // Diagnostics from DefinitionData are emitted here.
10178 if (FirstDD != SecondDD) {
10179 enum ODRDefinitionDataDifference {
10186 auto ODRDiagError = [FirstRecord, &FirstModule,
10187 this](SourceLocation Loc, SourceRange Range,
10188 ODRDefinitionDataDifference DiffType) {
10189 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10190 << FirstRecord << FirstModule.empty() << FirstModule << Range
10193 auto ODRDiagNote = [&SecondModule,
10194 this](SourceLocation Loc, SourceRange Range,
10195 ODRDefinitionDataDifference DiffType) {
10196 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10197 << SecondModule << Range << DiffType;
10200 unsigned FirstNumBases = FirstDD->NumBases;
10201 unsigned FirstNumVBases = FirstDD->NumVBases;
10202 unsigned SecondNumBases = SecondDD->NumBases;
10203 unsigned SecondNumVBases = SecondDD->NumVBases;
10205 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10206 unsigned NumBases = DD->NumBases;
10207 if (NumBases == 0) return SourceRange();
10208 auto bases = DD->bases();
10209 return SourceRange(bases[0].getBeginLoc(),
10210 bases[NumBases - 1].getEndLoc());
10213 if (FirstNumBases != SecondNumBases) {
10214 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10217 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10224 if (FirstNumVBases != SecondNumVBases) {
10225 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10228 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10230 << SecondNumVBases;
10235 auto FirstBases = FirstDD->bases();
10236 auto SecondBases = SecondDD->bases();
10238 for (i = 0; i < FirstNumBases; ++i) {
10239 auto FirstBase = FirstBases[i];
10240 auto SecondBase = SecondBases[i];
10241 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10242 ComputeQualTypeODRHash(SecondBase.getType())) {
10243 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10245 << (i + 1) << FirstBase.getType();
10246 ODRDiagNote(SecondRecord->getLocation(),
10247 SecondBase.getSourceRange(), BaseType)
10248 << (i + 1) << SecondBase.getType();
10252 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10253 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10255 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10256 ODRDiagNote(SecondRecord->getLocation(),
10257 SecondBase.getSourceRange(), BaseVirtual)
10258 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10262 if (FirstBase.getAccessSpecifierAsWritten() !=
10263 SecondBase.getAccessSpecifierAsWritten()) {
10264 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10266 << (i + 1) << FirstBase.getType()
10267 << (int)FirstBase.getAccessSpecifierAsWritten();
10268 ODRDiagNote(SecondRecord->getLocation(),
10269 SecondBase.getSourceRange(), BaseAccess)
10270 << (i + 1) << SecondBase.getType()
10271 << (int)SecondBase.getAccessSpecifierAsWritten();
10276 if (i != FirstNumBases) {
10282 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10284 const ClassTemplateDecl *FirstTemplate =
10285 FirstRecord->getDescribedClassTemplate();
10286 const ClassTemplateDecl *SecondTemplate =
10287 SecondRecord->getDescribedClassTemplate();
10289 assert(!FirstTemplate == !SecondTemplate &&
10290 "Both pointers should be null or non-null");
10292 enum ODRTemplateDifference {
10295 ParamSingleDefaultArgument,
10296 ParamDifferentDefaultArgument,
10299 if (FirstTemplate && SecondTemplate) {
10300 DeclHashes FirstTemplateHashes;
10301 DeclHashes SecondTemplateHashes;
10303 auto PopulateTemplateParameterHashs =
10304 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10305 const ClassTemplateDecl *TD) {
10306 for (auto *D : TD->getTemplateParameters()->asArray()) {
10307 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10311 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10312 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10314 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10315 "Number of template parameters should be equal.");
10317 auto FirstIt = FirstTemplateHashes.begin();
10318 auto FirstEnd = FirstTemplateHashes.end();
10319 auto SecondIt = SecondTemplateHashes.begin();
10320 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10321 if (FirstIt->second == SecondIt->second)
10324 auto ODRDiagError = [FirstRecord, &FirstModule,
10325 this](SourceLocation Loc, SourceRange Range,
10326 ODRTemplateDifference DiffType) {
10327 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10328 << FirstRecord << FirstModule.empty() << FirstModule << Range
10331 auto ODRDiagNote = [&SecondModule,
10332 this](SourceLocation Loc, SourceRange Range,
10333 ODRTemplateDifference DiffType) {
10334 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10335 << SecondModule << Range << DiffType;
10338 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10339 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10341 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10342 "Parameter Decl's should be the same kind.");
10344 DeclarationName FirstName = FirstDecl->getDeclName();
10345 DeclarationName SecondName = SecondDecl->getDeclName();
10347 if (FirstName != SecondName) {
10348 const bool FirstNameEmpty =
10349 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10350 const bool SecondNameEmpty =
10351 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10352 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10353 "Both template parameters cannot be unnamed.");
10354 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10355 FirstNameEmpty ? ParamEmptyName : ParamName)
10357 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10358 SecondNameEmpty ? ParamEmptyName : ParamName)
10363 switch (FirstDecl->getKind()) {
10365 llvm_unreachable("Invalid template parameter type.");
10366 case Decl::TemplateTypeParm: {
10367 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10368 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10369 const bool HasFirstDefaultArgument =
10370 FirstParam->hasDefaultArgument() &&
10371 !FirstParam->defaultArgumentWasInherited();
10372 const bool HasSecondDefaultArgument =
10373 SecondParam->hasDefaultArgument() &&
10374 !SecondParam->defaultArgumentWasInherited();
10376 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10377 ODRDiagError(FirstDecl->getLocation(),
10378 FirstDecl->getSourceRange(),
10379 ParamSingleDefaultArgument)
10380 << HasFirstDefaultArgument;
10381 ODRDiagNote(SecondDecl->getLocation(),
10382 SecondDecl->getSourceRange(),
10383 ParamSingleDefaultArgument)
10384 << HasSecondDefaultArgument;
10388 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10389 "Expecting default arguments.");
10391 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10392 ParamDifferentDefaultArgument);
10393 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10394 ParamDifferentDefaultArgument);
10398 case Decl::NonTypeTemplateParm: {
10399 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10400 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10401 const bool HasFirstDefaultArgument =
10402 FirstParam->hasDefaultArgument() &&
10403 !FirstParam->defaultArgumentWasInherited();
10404 const bool HasSecondDefaultArgument =
10405 SecondParam->hasDefaultArgument() &&
10406 !SecondParam->defaultArgumentWasInherited();
10408 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10409 ODRDiagError(FirstDecl->getLocation(),
10410 FirstDecl->getSourceRange(),
10411 ParamSingleDefaultArgument)
10412 << HasFirstDefaultArgument;
10413 ODRDiagNote(SecondDecl->getLocation(),
10414 SecondDecl->getSourceRange(),
10415 ParamSingleDefaultArgument)
10416 << HasSecondDefaultArgument;
10420 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10421 "Expecting default arguments.");
10423 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10424 ParamDifferentDefaultArgument);
10425 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10426 ParamDifferentDefaultArgument);
10430 case Decl::TemplateTemplateParm: {
10431 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10432 const auto *SecondParam =
10433 cast<TemplateTemplateParmDecl>(SecondDecl);
10434 const bool HasFirstDefaultArgument =
10435 FirstParam->hasDefaultArgument() &&
10436 !FirstParam->defaultArgumentWasInherited();
10437 const bool HasSecondDefaultArgument =
10438 SecondParam->hasDefaultArgument() &&
10439 !SecondParam->defaultArgumentWasInherited();
10441 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10442 ODRDiagError(FirstDecl->getLocation(),
10443 FirstDecl->getSourceRange(),
10444 ParamSingleDefaultArgument)
10445 << HasFirstDefaultArgument;
10446 ODRDiagNote(SecondDecl->getLocation(),
10447 SecondDecl->getSourceRange(),
10448 ParamSingleDefaultArgument)
10449 << HasSecondDefaultArgument;
10453 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10454 "Expecting default arguments.");
10456 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10457 ParamDifferentDefaultArgument);
10458 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10459 ParamDifferentDefaultArgument);
10468 if (FirstIt != FirstEnd) {
10474 DeclHashes FirstHashes;
10475 DeclHashes SecondHashes;
10477 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10478 DeclHashes &Hashes, CXXRecordDecl *Record) {
10479 for (auto *D : Record->decls()) {
10480 // Due to decl merging, the first CXXRecordDecl is the parent of
10481 // Decls in both records.
10482 if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10484 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10487 PopulateHashes(FirstHashes, FirstRecord);
10488 PopulateHashes(SecondHashes, SecondRecord);
10490 // Used with err_module_odr_violation_mismatch_decl and
10491 // note_module_odr_violation_mismatch_decl
10492 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10507 } FirstDiffType = Other,
10508 SecondDiffType = Other;
10510 auto DifferenceSelector = [](Decl *D) {
10511 assert(D && "valid Decl required");
10512 switch (D->getKind()) {
10515 case Decl::AccessSpec:
10516 switch (D->getAccess()) {
10518 return PublicSpecifer;
10520 return PrivateSpecifer;
10522 return ProtectedSpecifer;
10526 llvm_unreachable("Invalid access specifier");
10527 case Decl::StaticAssert:
10528 return StaticAssert;
10531 case Decl::CXXMethod:
10532 case Decl::CXXConstructor:
10533 case Decl::CXXDestructor:
10535 case Decl::TypeAlias:
10537 case Decl::Typedef:
10543 case Decl::FunctionTemplate:
10544 return FunctionTemplate;
10548 Decl *FirstDecl = nullptr;
10549 Decl *SecondDecl = nullptr;
10550 auto FirstIt = FirstHashes.begin();
10551 auto SecondIt = SecondHashes.begin();
10553 // If there is a diagnoseable difference, FirstDiffType and
10554 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10555 // filled in if not EndOfClass.
10556 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10557 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10558 FirstIt->second == SecondIt->second) {
10564 FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10565 SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10567 FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10569 SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10574 if (FirstDiffType == Other || SecondDiffType == Other) {
10575 // Reaching this point means an unexpected Decl was encountered
10576 // or no difference was detected. This causes a generic error
10577 // message to be emitted.
10578 Diag(FirstRecord->getLocation(),
10579 diag::err_module_odr_violation_different_definitions)
10580 << FirstRecord << FirstModule.empty() << FirstModule;
10583 Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10584 << FirstRecord << FirstDecl->getSourceRange();
10587 Diag(SecondRecord->getLocation(),
10588 diag::note_module_odr_violation_different_definitions)
10592 Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10593 << SecondDecl->getSourceRange();
10600 if (FirstDiffType != SecondDiffType) {
10601 SourceLocation FirstLoc;
10602 SourceRange FirstRange;
10603 if (FirstDiffType == EndOfClass) {
10604 FirstLoc = FirstRecord->getBraceRange().getEnd();
10606 FirstLoc = FirstIt->first->getLocation();
10607 FirstRange = FirstIt->first->getSourceRange();
10609 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10610 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10613 SourceLocation SecondLoc;
10614 SourceRange SecondRange;
10615 if (SecondDiffType == EndOfClass) {
10616 SecondLoc = SecondRecord->getBraceRange().getEnd();
10618 SecondLoc = SecondDecl->getLocation();
10619 SecondRange = SecondDecl->getSourceRange();
10621 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10622 << SecondModule << SecondRange << SecondDiffType;
10627 assert(FirstDiffType == SecondDiffType);
10629 // Used with err_module_odr_violation_mismatch_decl_diff and
10630 // note_module_odr_violation_mismatch_decl_diff
10631 enum ODRDeclDifference {
10632 StaticAssertCondition,
10633 StaticAssertMessage,
10634 StaticAssertOnlyMessage,
10637 FieldSingleBitField,
10638 FieldDifferentWidthBitField,
10639 FieldSingleMutable,
10640 FieldSingleInitializer,
10641 FieldDifferentInitializers,
10650 MethodNumberParameters,
10651 MethodParameterType,
10652 MethodParameterName,
10653 MethodParameterSingleDefaultArgument,
10654 MethodParameterDifferentDefaultArgument,
10655 MethodNoTemplateArguments,
10656 MethodDifferentNumberTemplateArguments,
10657 MethodDifferentTemplateArgument,
10659 MethodDifferentBody,
10664 VarSingleInitializer,
10665 VarDifferentInitializer,
10667 FriendTypeFunction,
10670 FunctionTemplateDifferentNumberParameters,
10671 FunctionTemplateParameterDifferentKind,
10672 FunctionTemplateParameterName,
10673 FunctionTemplateParameterSingleDefaultArgument,
10674 FunctionTemplateParameterDifferentDefaultArgument,
10675 FunctionTemplateParameterDifferentType,
10676 FunctionTemplatePackParameter,
10679 // These lambdas have the common portions of the ODR diagnostics. This
10680 // has the same return as Diag(), so addition parameters can be passed
10681 // in with operator<<
10682 auto ODRDiagError = [FirstRecord, &FirstModule, this](
10683 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10684 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10685 << FirstRecord << FirstModule.empty() << FirstModule << Range
10688 auto ODRDiagNote = [&SecondModule, this](
10689 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10690 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10691 << SecondModule << Range << DiffType;
10694 switch (FirstDiffType) {
10697 case PublicSpecifer:
10698 case PrivateSpecifer:
10699 case ProtectedSpecifer:
10700 llvm_unreachable("Invalid diff type");
10702 case StaticAssert: {
10703 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10704 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10706 Expr *FirstExpr = FirstSA->getAssertExpr();
10707 Expr *SecondExpr = SecondSA->getAssertExpr();
10708 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10709 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10710 if (FirstODRHash != SecondODRHash) {
10711 ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10712 StaticAssertCondition);
10713 ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10714 StaticAssertCondition);
10719 StringLiteral *FirstStr = FirstSA->getMessage();
10720 StringLiteral *SecondStr = SecondSA->getMessage();
10721 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10722 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10723 SourceLocation FirstLoc, SecondLoc;
10724 SourceRange FirstRange, SecondRange;
10726 FirstLoc = FirstStr->getBeginLoc();
10727 FirstRange = FirstStr->getSourceRange();
10729 FirstLoc = FirstSA->getBeginLoc();
10730 FirstRange = FirstSA->getSourceRange();
10733 SecondLoc = SecondStr->getBeginLoc();
10734 SecondRange = SecondStr->getSourceRange();
10736 SecondLoc = SecondSA->getBeginLoc();
10737 SecondRange = SecondSA->getSourceRange();
10739 ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10740 << (FirstStr == nullptr);
10741 ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10742 << (SecondStr == nullptr);
10747 if (FirstStr && SecondStr &&
10748 FirstStr->getString() != SecondStr->getString()) {
10749 ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10750 StaticAssertMessage);
10751 ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10752 StaticAssertMessage);
10759 FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10760 FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10761 IdentifierInfo *FirstII = FirstField->getIdentifier();
10762 IdentifierInfo *SecondII = SecondField->getIdentifier();
10763 if (FirstII->getName() != SecondII->getName()) {
10764 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10767 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10775 assert(getContext().hasSameType(FirstField->getType(),
10776 SecondField->getType()));
10778 QualType FirstType = FirstField->getType();
10779 QualType SecondType = SecondField->getType();
10780 if (ComputeQualTypeODRHash(FirstType) !=
10781 ComputeQualTypeODRHash(SecondType)) {
10782 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10784 << FirstII << FirstType;
10785 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10787 << SecondII << SecondType;
10793 const bool IsFirstBitField = FirstField->isBitField();
10794 const bool IsSecondBitField = SecondField->isBitField();
10795 if (IsFirstBitField != IsSecondBitField) {
10796 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10797 FieldSingleBitField)
10798 << FirstII << IsFirstBitField;
10799 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10800 FieldSingleBitField)
10801 << SecondII << IsSecondBitField;
10806 if (IsFirstBitField && IsSecondBitField) {
10807 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10808 FieldDifferentWidthBitField)
10809 << FirstII << FirstField->getBitWidth()->getSourceRange();
10810 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10811 FieldDifferentWidthBitField)
10812 << SecondII << SecondField->getBitWidth()->getSourceRange();
10817 const bool IsFirstMutable = FirstField->isMutable();
10818 const bool IsSecondMutable = SecondField->isMutable();
10819 if (IsFirstMutable != IsSecondMutable) {
10820 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10821 FieldSingleMutable)
10822 << FirstII << IsFirstMutable;
10823 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10824 FieldSingleMutable)
10825 << SecondII << IsSecondMutable;
10830 const Expr *FirstInitializer = FirstField->getInClassInitializer();
10831 const Expr *SecondInitializer = SecondField->getInClassInitializer();
10832 if ((!FirstInitializer && SecondInitializer) ||
10833 (FirstInitializer && !SecondInitializer)) {
10834 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10835 FieldSingleInitializer)
10836 << FirstII << (FirstInitializer != nullptr);
10837 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10838 FieldSingleInitializer)
10839 << SecondII << (SecondInitializer != nullptr);
10844 if (FirstInitializer && SecondInitializer) {
10845 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10846 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10847 if (FirstInitHash != SecondInitHash) {
10848 ODRDiagError(FirstField->getLocation(),
10849 FirstField->getSourceRange(),
10850 FieldDifferentInitializers)
10851 << FirstII << FirstInitializer->getSourceRange();
10852 ODRDiagNote(SecondField->getLocation(),
10853 SecondField->getSourceRange(),
10854 FieldDifferentInitializers)
10855 << SecondII << SecondInitializer->getSourceRange();
10870 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10871 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10872 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10875 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10876 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10877 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10878 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10879 auto FirstName = FirstMethod->getDeclName();
10880 auto SecondName = SecondMethod->getDeclName();
10881 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10882 ODRDiagError(FirstMethod->getLocation(),
10883 FirstMethod->getSourceRange(), MethodName)
10884 << FirstMethodType << FirstName;
10885 ODRDiagNote(SecondMethod->getLocation(),
10886 SecondMethod->getSourceRange(), MethodName)
10887 << SecondMethodType << SecondName;
10893 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10894 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10895 if (FirstDeleted != SecondDeleted) {
10896 ODRDiagError(FirstMethod->getLocation(),
10897 FirstMethod->getSourceRange(), MethodDeleted)
10898 << FirstMethodType << FirstName << FirstDeleted;
10900 ODRDiagNote(SecondMethod->getLocation(),
10901 SecondMethod->getSourceRange(), MethodDeleted)
10902 << SecondMethodType << SecondName << SecondDeleted;
10907 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10908 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10909 if (FirstDefaulted != SecondDefaulted) {
10910 ODRDiagError(FirstMethod->getLocation(),
10911 FirstMethod->getSourceRange(), MethodDefaulted)
10912 << FirstMethodType << FirstName << FirstDefaulted;
10914 ODRDiagNote(SecondMethod->getLocation(),
10915 SecondMethod->getSourceRange(), MethodDefaulted)
10916 << SecondMethodType << SecondName << SecondDefaulted;
10921 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10922 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10923 const bool FirstPure = FirstMethod->isPure();
10924 const bool SecondPure = SecondMethod->isPure();
10925 if ((FirstVirtual || SecondVirtual) &&
10926 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10927 ODRDiagError(FirstMethod->getLocation(),
10928 FirstMethod->getSourceRange(), MethodVirtual)
10929 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10930 ODRDiagNote(SecondMethod->getLocation(),
10931 SecondMethod->getSourceRange(), MethodVirtual)
10932 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10937 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10938 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10939 // class needs to be checked instead.
10940 const auto FirstStorage = FirstMethod->getStorageClass();
10941 const auto SecondStorage = SecondMethod->getStorageClass();
10942 const bool FirstStatic = FirstStorage == SC_Static;
10943 const bool SecondStatic = SecondStorage == SC_Static;
10944 if (FirstStatic != SecondStatic) {
10945 ODRDiagError(FirstMethod->getLocation(),
10946 FirstMethod->getSourceRange(), MethodStatic)
10947 << FirstMethodType << FirstName << FirstStatic;
10948 ODRDiagNote(SecondMethod->getLocation(),
10949 SecondMethod->getSourceRange(), MethodStatic)
10950 << SecondMethodType << SecondName << SecondStatic;
10955 const bool FirstVolatile = FirstMethod->isVolatile();
10956 const bool SecondVolatile = SecondMethod->isVolatile();
10957 if (FirstVolatile != SecondVolatile) {
10958 ODRDiagError(FirstMethod->getLocation(),
10959 FirstMethod->getSourceRange(), MethodVolatile)
10960 << FirstMethodType << FirstName << FirstVolatile;
10961 ODRDiagNote(SecondMethod->getLocation(),
10962 SecondMethod->getSourceRange(), MethodVolatile)
10963 << SecondMethodType << SecondName << SecondVolatile;
10968 const bool FirstConst = FirstMethod->isConst();
10969 const bool SecondConst = SecondMethod->isConst();
10970 if (FirstConst != SecondConst) {
10971 ODRDiagError(FirstMethod->getLocation(),
10972 FirstMethod->getSourceRange(), MethodConst)
10973 << FirstMethodType << FirstName << FirstConst;
10974 ODRDiagNote(SecondMethod->getLocation(),
10975 SecondMethod->getSourceRange(), MethodConst)
10976 << SecondMethodType << SecondName << SecondConst;
10981 const bool FirstInline = FirstMethod->isInlineSpecified();
10982 const bool SecondInline = SecondMethod->isInlineSpecified();
10983 if (FirstInline != SecondInline) {
10984 ODRDiagError(FirstMethod->getLocation(),
10985 FirstMethod->getSourceRange(), MethodInline)
10986 << FirstMethodType << FirstName << FirstInline;
10987 ODRDiagNote(SecondMethod->getLocation(),
10988 SecondMethod->getSourceRange(), MethodInline)
10989 << SecondMethodType << SecondName << SecondInline;
10994 const unsigned FirstNumParameters = FirstMethod->param_size();
10995 const unsigned SecondNumParameters = SecondMethod->param_size();
10996 if (FirstNumParameters != SecondNumParameters) {
10997 ODRDiagError(FirstMethod->getLocation(),
10998 FirstMethod->getSourceRange(), MethodNumberParameters)
10999 << FirstMethodType << FirstName << FirstNumParameters;
11000 ODRDiagNote(SecondMethod->getLocation(),
11001 SecondMethod->getSourceRange(), MethodNumberParameters)
11002 << SecondMethodType << SecondName << SecondNumParameters;
11007 // Need this status boolean to know when break out of the switch.
11008 bool ParameterMismatch = false;
11009 for (unsigned I = 0; I < FirstNumParameters; ++I) {
11010 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
11011 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
11013 QualType FirstParamType = FirstParam->getType();
11014 QualType SecondParamType = SecondParam->getType();
11015 if (FirstParamType != SecondParamType &&
11016 ComputeQualTypeODRHash(FirstParamType) !=
11017 ComputeQualTypeODRHash(SecondParamType)) {
11018 if (const DecayedType *ParamDecayedType =
11019 FirstParamType->getAs<DecayedType>()) {
11020 ODRDiagError(FirstMethod->getLocation(),
11021 FirstMethod->getSourceRange(), MethodParameterType)
11022 << FirstMethodType << FirstName << (I + 1) << FirstParamType
11023 << true << ParamDecayedType->getOriginalType();
11025 ODRDiagError(FirstMethod->getLocation(),
11026 FirstMethod->getSourceRange(), MethodParameterType)
11027 << FirstMethodType << FirstName << (I + 1) << FirstParamType
11031 if (const DecayedType *ParamDecayedType =
11032 SecondParamType->getAs<DecayedType>()) {
11033 ODRDiagNote(SecondMethod->getLocation(),
11034 SecondMethod->getSourceRange(), MethodParameterType)
11035 << SecondMethodType << SecondName << (I + 1)
11036 << SecondParamType << true
11037 << ParamDecayedType->getOriginalType();
11039 ODRDiagNote(SecondMethod->getLocation(),
11040 SecondMethod->getSourceRange(), MethodParameterType)
11041 << SecondMethodType << SecondName << (I + 1)
11042 << SecondParamType << false;
11044 ParameterMismatch = true;
11048 DeclarationName FirstParamName = FirstParam->getDeclName();
11049 DeclarationName SecondParamName = SecondParam->getDeclName();
11050 if (FirstParamName != SecondParamName) {
11051 ODRDiagError(FirstMethod->getLocation(),
11052 FirstMethod->getSourceRange(), MethodParameterName)
11053 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
11054 ODRDiagNote(SecondMethod->getLocation(),
11055 SecondMethod->getSourceRange(), MethodParameterName)
11056 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
11057 ParameterMismatch = true;
11061 const Expr *FirstInit = FirstParam->getInit();
11062 const Expr *SecondInit = SecondParam->getInit();
11063 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11064 ODRDiagError(FirstMethod->getLocation(),
11065 FirstMethod->getSourceRange(),
11066 MethodParameterSingleDefaultArgument)
11067 << FirstMethodType << FirstName << (I + 1)
11068 << (FirstInit == nullptr)
11069 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11070 ODRDiagNote(SecondMethod->getLocation(),
11071 SecondMethod->getSourceRange(),
11072 MethodParameterSingleDefaultArgument)
11073 << SecondMethodType << SecondName << (I + 1)
11074 << (SecondInit == nullptr)
11075 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11076 ParameterMismatch = true;
11080 if (FirstInit && SecondInit &&
11081 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11082 ODRDiagError(FirstMethod->getLocation(),
11083 FirstMethod->getSourceRange(),
11084 MethodParameterDifferentDefaultArgument)
11085 << FirstMethodType << FirstName << (I + 1)
11086 << FirstInit->getSourceRange();
11087 ODRDiagNote(SecondMethod->getLocation(),
11088 SecondMethod->getSourceRange(),
11089 MethodParameterDifferentDefaultArgument)
11090 << SecondMethodType << SecondName << (I + 1)
11091 << SecondInit->getSourceRange();
11092 ParameterMismatch = true;
11098 if (ParameterMismatch) {
11103 const auto *FirstTemplateArgs =
11104 FirstMethod->getTemplateSpecializationArgs();
11105 const auto *SecondTemplateArgs =
11106 SecondMethod->getTemplateSpecializationArgs();
11108 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
11109 (!FirstTemplateArgs && SecondTemplateArgs)) {
11110 ODRDiagError(FirstMethod->getLocation(),
11111 FirstMethod->getSourceRange(), MethodNoTemplateArguments)
11112 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
11113 ODRDiagNote(SecondMethod->getLocation(),
11114 SecondMethod->getSourceRange(), MethodNoTemplateArguments)
11115 << SecondMethodType << SecondName
11116 << (SecondTemplateArgs != nullptr);
11122 if (FirstTemplateArgs && SecondTemplateArgs) {
11123 // Remove pack expansions from argument list.
11124 auto ExpandTemplateArgumentList =
11125 [](const TemplateArgumentList *TAL) {
11126 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
11127 for (const TemplateArgument &TA : TAL->asArray()) {
11128 if (TA.getKind() != TemplateArgument::Pack) {
11129 ExpandedList.push_back(&TA);
11132 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
11133 ExpandedList.push_back(&PackTA);
11136 return ExpandedList;
11138 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
11139 ExpandTemplateArgumentList(FirstTemplateArgs);
11140 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
11141 ExpandTemplateArgumentList(SecondTemplateArgs);
11143 if (FirstExpandedList.size() != SecondExpandedList.size()) {
11144 ODRDiagError(FirstMethod->getLocation(),
11145 FirstMethod->getSourceRange(),
11146 MethodDifferentNumberTemplateArguments)
11147 << FirstMethodType << FirstName
11148 << (unsigned)FirstExpandedList.size();
11149 ODRDiagNote(SecondMethod->getLocation(),
11150 SecondMethod->getSourceRange(),
11151 MethodDifferentNumberTemplateArguments)
11152 << SecondMethodType << SecondName
11153 << (unsigned)SecondExpandedList.size();
11159 bool TemplateArgumentMismatch = false;
11160 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
11161 const TemplateArgument &FirstTA = *FirstExpandedList[i],
11162 &SecondTA = *SecondExpandedList[i];
11163 if (ComputeTemplateArgumentODRHash(FirstTA) ==
11164 ComputeTemplateArgumentODRHash(SecondTA)) {
11168 ODRDiagError(FirstMethod->getLocation(),
11169 FirstMethod->getSourceRange(),
11170 MethodDifferentTemplateArgument)
11171 << FirstMethodType << FirstName << FirstTA << i + 1;
11172 ODRDiagNote(SecondMethod->getLocation(),
11173 SecondMethod->getSourceRange(),
11174 MethodDifferentTemplateArgument)
11175 << SecondMethodType << SecondName << SecondTA << i + 1;
11177 TemplateArgumentMismatch = true;
11181 if (TemplateArgumentMismatch) {
11187 // Compute the hash of the method as if it has no body.
11188 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
11190 Hash.AddFunctionDecl(D, true /*SkipBody*/);
11191 return Hash.CalculateHash();
11194 // Compare the hash generated to the hash stored. A difference means
11195 // that a body was present in the original source. Due to merging,
11196 // the stardard way of detecting a body will not work.
11197 const bool HasFirstBody =
11198 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
11199 const bool HasSecondBody =
11200 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
11202 if (HasFirstBody != HasSecondBody) {
11203 ODRDiagError(FirstMethod->getLocation(),
11204 FirstMethod->getSourceRange(), MethodSingleBody)
11205 << FirstMethodType << FirstName << HasFirstBody;
11206 ODRDiagNote(SecondMethod->getLocation(),
11207 SecondMethod->getSourceRange(), MethodSingleBody)
11208 << SecondMethodType << SecondName << HasSecondBody;
11213 if (HasFirstBody && HasSecondBody) {
11214 ODRDiagError(FirstMethod->getLocation(),
11215 FirstMethod->getSourceRange(), MethodDifferentBody)
11216 << FirstMethodType << FirstName;
11217 ODRDiagNote(SecondMethod->getLocation(),
11218 SecondMethod->getSourceRange(), MethodDifferentBody)
11219 << SecondMethodType << SecondName;
11228 TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
11229 TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
11230 auto FirstName = FirstTD->getDeclName();
11231 auto SecondName = SecondTD->getDeclName();
11232 if (FirstName != SecondName) {
11233 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11235 << (FirstDiffType == TypeAlias) << FirstName;
11236 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11238 << (FirstDiffType == TypeAlias) << SecondName;
11243 QualType FirstType = FirstTD->getUnderlyingType();
11244 QualType SecondType = SecondTD->getUnderlyingType();
11245 if (ComputeQualTypeODRHash(FirstType) !=
11246 ComputeQualTypeODRHash(SecondType)) {
11247 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11249 << (FirstDiffType == TypeAlias) << FirstName << FirstType;
11250 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11252 << (FirstDiffType == TypeAlias) << SecondName << SecondType;
11259 VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
11260 VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
11261 auto FirstName = FirstVD->getDeclName();
11262 auto SecondName = SecondVD->getDeclName();
11263 if (FirstName != SecondName) {
11264 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11267 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11274 QualType FirstType = FirstVD->getType();
11275 QualType SecondType = SecondVD->getType();
11276 if (ComputeQualTypeODRHash(FirstType) !=
11277 ComputeQualTypeODRHash(SecondType)) {
11278 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11280 << FirstName << FirstType;
11281 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11283 << SecondName << SecondType;
11288 const Expr *FirstInit = FirstVD->getInit();
11289 const Expr *SecondInit = SecondVD->getInit();
11290 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11291 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11292 VarSingleInitializer)
11293 << FirstName << (FirstInit == nullptr)
11294 << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
11295 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11296 VarSingleInitializer)
11297 << SecondName << (SecondInit == nullptr)
11298 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11303 if (FirstInit && SecondInit &&
11304 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11305 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11306 VarDifferentInitializer)
11307 << FirstName << FirstInit->getSourceRange();
11308 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11309 VarDifferentInitializer)
11310 << SecondName << SecondInit->getSourceRange();
11315 const bool FirstIsConstexpr = FirstVD->isConstexpr();
11316 const bool SecondIsConstexpr = SecondVD->isConstexpr();
11317 if (FirstIsConstexpr != SecondIsConstexpr) {
11318 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11320 << FirstName << FirstIsConstexpr;
11321 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11323 << SecondName << SecondIsConstexpr;
11330 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
11331 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
11333 NamedDecl *FirstND = FirstFriend->getFriendDecl();
11334 NamedDecl *SecondND = SecondFriend->getFriendDecl();
11336 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
11337 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
11339 if (FirstND && SecondND) {
11340 ODRDiagError(FirstFriend->getFriendLoc(),
11341 FirstFriend->getSourceRange(), FriendFunction)
11343 ODRDiagNote(SecondFriend->getFriendLoc(),
11344 SecondFriend->getSourceRange(), FriendFunction)
11351 if (FirstTSI && SecondTSI) {
11352 QualType FirstFriendType = FirstTSI->getType();
11353 QualType SecondFriendType = SecondTSI->getType();
11354 assert(ComputeQualTypeODRHash(FirstFriendType) !=
11355 ComputeQualTypeODRHash(SecondFriendType));
11356 ODRDiagError(FirstFriend->getFriendLoc(),
11357 FirstFriend->getSourceRange(), FriendType)
11358 << FirstFriendType;
11359 ODRDiagNote(SecondFriend->getFriendLoc(),
11360 SecondFriend->getSourceRange(), FriendType)
11361 << SecondFriendType;
11366 ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
11367 FriendTypeFunction)
11368 << (FirstTSI == nullptr);
11369 ODRDiagNote(SecondFriend->getFriendLoc(),
11370 SecondFriend->getSourceRange(), FriendTypeFunction)
11371 << (SecondTSI == nullptr);
11376 case FunctionTemplate: {
11377 FunctionTemplateDecl *FirstTemplate =
11378 cast<FunctionTemplateDecl>(FirstDecl);
11379 FunctionTemplateDecl *SecondTemplate =
11380 cast<FunctionTemplateDecl>(SecondDecl);
11382 TemplateParameterList *FirstTPL =
11383 FirstTemplate->getTemplateParameters();
11384 TemplateParameterList *SecondTPL =
11385 SecondTemplate->getTemplateParameters();
11387 if (FirstTPL->size() != SecondTPL->size()) {
11388 ODRDiagError(FirstTemplate->getLocation(),
11389 FirstTemplate->getSourceRange(),
11390 FunctionTemplateDifferentNumberParameters)
11391 << FirstTemplate << FirstTPL->size();
11392 ODRDiagNote(SecondTemplate->getLocation(),
11393 SecondTemplate->getSourceRange(),
11394 FunctionTemplateDifferentNumberParameters)
11395 << SecondTemplate << SecondTPL->size();
11401 bool ParameterMismatch = false;
11402 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11403 NamedDecl *FirstParam = FirstTPL->getParam(i);
11404 NamedDecl *SecondParam = SecondTPL->getParam(i);
11406 if (FirstParam->getKind() != SecondParam->getKind()) {
11408 TemplateTypeParameter,
11409 NonTypeTemplateParameter,
11410 TemplateTemplateParameter,
11412 auto GetParamType = [](NamedDecl *D) {
11413 switch (D->getKind()) {
11415 llvm_unreachable("Unexpected template parameter type");
11416 case Decl::TemplateTypeParm:
11417 return TemplateTypeParameter;
11418 case Decl::NonTypeTemplateParm:
11419 return NonTypeTemplateParameter;
11420 case Decl::TemplateTemplateParm:
11421 return TemplateTemplateParameter;
11425 ODRDiagError(FirstTemplate->getLocation(),
11426 FirstTemplate->getSourceRange(),
11427 FunctionTemplateParameterDifferentKind)
11428 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11429 ODRDiagNote(SecondTemplate->getLocation(),
11430 SecondTemplate->getSourceRange(),
11431 FunctionTemplateParameterDifferentKind)
11432 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11434 ParameterMismatch = true;
11438 if (FirstParam->getName() != SecondParam->getName()) {
11439 ODRDiagError(FirstTemplate->getLocation(),
11440 FirstTemplate->getSourceRange(),
11441 FunctionTemplateParameterName)
11442 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11444 ODRDiagNote(SecondTemplate->getLocation(),
11445 SecondTemplate->getSourceRange(),
11446 FunctionTemplateParameterName)
11447 << SecondTemplate << (i + 1)
11448 << (bool)SecondParam->getIdentifier() << SecondParam;
11449 ParameterMismatch = true;
11453 if (isa<TemplateTypeParmDecl>(FirstParam) &&
11454 isa<TemplateTypeParmDecl>(SecondParam)) {
11455 TemplateTypeParmDecl *FirstTTPD =
11456 cast<TemplateTypeParmDecl>(FirstParam);
11457 TemplateTypeParmDecl *SecondTTPD =
11458 cast<TemplateTypeParmDecl>(SecondParam);
11459 bool HasFirstDefaultArgument =
11460 FirstTTPD->hasDefaultArgument() &&
11461 !FirstTTPD->defaultArgumentWasInherited();
11462 bool HasSecondDefaultArgument =
11463 SecondTTPD->hasDefaultArgument() &&
11464 !SecondTTPD->defaultArgumentWasInherited();
11465 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11466 ODRDiagError(FirstTemplate->getLocation(),
11467 FirstTemplate->getSourceRange(),
11468 FunctionTemplateParameterSingleDefaultArgument)
11469 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11470 ODRDiagNote(SecondTemplate->getLocation(),
11471 SecondTemplate->getSourceRange(),
11472 FunctionTemplateParameterSingleDefaultArgument)
11473 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11474 ParameterMismatch = true;
11478 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11479 QualType FirstType = FirstTTPD->getDefaultArgument();
11480 QualType SecondType = SecondTTPD->getDefaultArgument();
11481 if (ComputeQualTypeODRHash(FirstType) !=
11482 ComputeQualTypeODRHash(SecondType)) {
11483 ODRDiagError(FirstTemplate->getLocation(),
11484 FirstTemplate->getSourceRange(),
11485 FunctionTemplateParameterDifferentDefaultArgument)
11486 << FirstTemplate << (i + 1) << FirstType;
11487 ODRDiagNote(SecondTemplate->getLocation(),
11488 SecondTemplate->getSourceRange(),
11489 FunctionTemplateParameterDifferentDefaultArgument)
11490 << SecondTemplate << (i + 1) << SecondType;
11491 ParameterMismatch = true;
11496 if (FirstTTPD->isParameterPack() !=
11497 SecondTTPD->isParameterPack()) {
11498 ODRDiagError(FirstTemplate->getLocation(),
11499 FirstTemplate->getSourceRange(),
11500 FunctionTemplatePackParameter)
11501 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11502 ODRDiagNote(SecondTemplate->getLocation(),
11503 SecondTemplate->getSourceRange(),
11504 FunctionTemplatePackParameter)
11505 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11506 ParameterMismatch = true;
11511 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11512 isa<TemplateTemplateParmDecl>(SecondParam)) {
11513 TemplateTemplateParmDecl *FirstTTPD =
11514 cast<TemplateTemplateParmDecl>(FirstParam);
11515 TemplateTemplateParmDecl *SecondTTPD =
11516 cast<TemplateTemplateParmDecl>(SecondParam);
11518 TemplateParameterList *FirstTPL =
11519 FirstTTPD->getTemplateParameters();
11520 TemplateParameterList *SecondTPL =
11521 SecondTTPD->getTemplateParameters();
11523 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11524 ComputeTemplateParameterListODRHash(SecondTPL)) {
11525 ODRDiagError(FirstTemplate->getLocation(),
11526 FirstTemplate->getSourceRange(),
11527 FunctionTemplateParameterDifferentType)
11528 << FirstTemplate << (i + 1);
11529 ODRDiagNote(SecondTemplate->getLocation(),
11530 SecondTemplate->getSourceRange(),
11531 FunctionTemplateParameterDifferentType)
11532 << SecondTemplate << (i + 1);
11533 ParameterMismatch = true;
11537 bool HasFirstDefaultArgument =
11538 FirstTTPD->hasDefaultArgument() &&
11539 !FirstTTPD->defaultArgumentWasInherited();
11540 bool HasSecondDefaultArgument =
11541 SecondTTPD->hasDefaultArgument() &&
11542 !SecondTTPD->defaultArgumentWasInherited();
11543 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11544 ODRDiagError(FirstTemplate->getLocation(),
11545 FirstTemplate->getSourceRange(),
11546 FunctionTemplateParameterSingleDefaultArgument)
11547 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11548 ODRDiagNote(SecondTemplate->getLocation(),
11549 SecondTemplate->getSourceRange(),
11550 FunctionTemplateParameterSingleDefaultArgument)
11551 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11552 ParameterMismatch = true;
11556 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11557 TemplateArgument FirstTA =
11558 FirstTTPD->getDefaultArgument().getArgument();
11559 TemplateArgument SecondTA =
11560 SecondTTPD->getDefaultArgument().getArgument();
11561 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11562 ComputeTemplateArgumentODRHash(SecondTA)) {
11563 ODRDiagError(FirstTemplate->getLocation(),
11564 FirstTemplate->getSourceRange(),
11565 FunctionTemplateParameterDifferentDefaultArgument)
11566 << FirstTemplate << (i + 1) << FirstTA;
11567 ODRDiagNote(SecondTemplate->getLocation(),
11568 SecondTemplate->getSourceRange(),
11569 FunctionTemplateParameterDifferentDefaultArgument)
11570 << SecondTemplate << (i + 1) << SecondTA;
11571 ParameterMismatch = true;
11576 if (FirstTTPD->isParameterPack() !=
11577 SecondTTPD->isParameterPack()) {
11578 ODRDiagError(FirstTemplate->getLocation(),
11579 FirstTemplate->getSourceRange(),
11580 FunctionTemplatePackParameter)
11581 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11582 ODRDiagNote(SecondTemplate->getLocation(),
11583 SecondTemplate->getSourceRange(),
11584 FunctionTemplatePackParameter)
11585 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11586 ParameterMismatch = true;
11591 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11592 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11593 NonTypeTemplateParmDecl *FirstNTTPD =
11594 cast<NonTypeTemplateParmDecl>(FirstParam);
11595 NonTypeTemplateParmDecl *SecondNTTPD =
11596 cast<NonTypeTemplateParmDecl>(SecondParam);
11598 QualType FirstType = FirstNTTPD->getType();
11599 QualType SecondType = SecondNTTPD->getType();
11600 if (ComputeQualTypeODRHash(FirstType) !=
11601 ComputeQualTypeODRHash(SecondType)) {
11602 ODRDiagError(FirstTemplate->getLocation(),
11603 FirstTemplate->getSourceRange(),
11604 FunctionTemplateParameterDifferentType)
11605 << FirstTemplate << (i + 1);
11606 ODRDiagNote(SecondTemplate->getLocation(),
11607 SecondTemplate->getSourceRange(),
11608 FunctionTemplateParameterDifferentType)
11609 << SecondTemplate << (i + 1);
11610 ParameterMismatch = true;
11614 bool HasFirstDefaultArgument =
11615 FirstNTTPD->hasDefaultArgument() &&
11616 !FirstNTTPD->defaultArgumentWasInherited();
11617 bool HasSecondDefaultArgument =
11618 SecondNTTPD->hasDefaultArgument() &&
11619 !SecondNTTPD->defaultArgumentWasInherited();
11620 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11621 ODRDiagError(FirstTemplate->getLocation(),
11622 FirstTemplate->getSourceRange(),
11623 FunctionTemplateParameterSingleDefaultArgument)
11624 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11625 ODRDiagNote(SecondTemplate->getLocation(),
11626 SecondTemplate->getSourceRange(),
11627 FunctionTemplateParameterSingleDefaultArgument)
11628 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11629 ParameterMismatch = true;
11633 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11634 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11635 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11636 if (ComputeODRHash(FirstDefaultArgument) !=
11637 ComputeODRHash(SecondDefaultArgument)) {
11638 ODRDiagError(FirstTemplate->getLocation(),
11639 FirstTemplate->getSourceRange(),
11640 FunctionTemplateParameterDifferentDefaultArgument)
11641 << FirstTemplate << (i + 1) << FirstDefaultArgument;
11642 ODRDiagNote(SecondTemplate->getLocation(),
11643 SecondTemplate->getSourceRange(),
11644 FunctionTemplateParameterDifferentDefaultArgument)
11645 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11646 ParameterMismatch = true;
11651 if (FirstNTTPD->isParameterPack() !=
11652 SecondNTTPD->isParameterPack()) {
11653 ODRDiagError(FirstTemplate->getLocation(),
11654 FirstTemplate->getSourceRange(),
11655 FunctionTemplatePackParameter)
11656 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11657 ODRDiagNote(SecondTemplate->getLocation(),
11658 SecondTemplate->getSourceRange(),
11659 FunctionTemplatePackParameter)
11660 << SecondTemplate << (i + 1)
11661 << SecondNTTPD->isParameterPack();
11662 ParameterMismatch = true;
11668 if (ParameterMismatch) {
11680 Diag(FirstDecl->getLocation(),
11681 diag::err_module_odr_violation_mismatch_decl_unknown)
11682 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11683 << FirstDecl->getSourceRange();
11684 Diag(SecondDecl->getLocation(),
11685 diag::note_module_odr_violation_mismatch_decl_unknown)
11686 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11691 // All definitions are updates to the same declaration. This happens if a
11692 // module instantiates the declaration of a class template specialization
11693 // and two or more other modules instantiate its definition.
11695 // FIXME: Indicate which modules had instantiations of this definition.
11696 // FIXME: How can this even happen?
11697 Diag(Merge.first->getLocation(),
11698 diag::err_module_odr_violation_different_instantiations)
11703 // Issue ODR failures diagnostics for functions.
11704 for (auto &Merge : FunctionOdrMergeFailures) {
11705 enum ODRFunctionDifference {
11709 ParameterSingleDefaultArgument,
11710 ParameterDifferentDefaultArgument,
11714 FunctionDecl *FirstFunction = Merge.first;
11715 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11717 bool Diagnosed = false;
11718 for (auto &SecondFunction : Merge.second) {
11720 if (FirstFunction == SecondFunction)
11723 std::string SecondModule =
11724 getOwningModuleNameForDiagnostic(SecondFunction);
11726 auto ODRDiagError = [FirstFunction, &FirstModule,
11727 this](SourceLocation Loc, SourceRange Range,
11728 ODRFunctionDifference DiffType) {
11729 return Diag(Loc, diag::err_module_odr_violation_function)
11730 << FirstFunction << FirstModule.empty() << FirstModule << Range
11733 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11735 ODRFunctionDifference DiffType) {
11736 return Diag(Loc, diag::note_module_odr_violation_function)
11737 << SecondModule << Range << DiffType;
11740 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11741 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11742 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11743 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11744 << FirstFunction->getReturnType();
11745 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11746 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11747 << SecondFunction->getReturnType();
11752 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11753 "Merged functions with different number of parameters");
11755 auto ParamSize = FirstFunction->param_size();
11756 bool ParameterMismatch = false;
11757 for (unsigned I = 0; I < ParamSize; ++I) {
11758 auto *FirstParam = FirstFunction->getParamDecl(I);
11759 auto *SecondParam = SecondFunction->getParamDecl(I);
11761 assert(getContext().hasSameType(FirstParam->getType(),
11762 SecondParam->getType()) &&
11763 "Merged function has different parameter types.");
11765 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11766 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11768 << I + 1 << FirstParam->getDeclName();
11769 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11771 << I + 1 << SecondParam->getDeclName();
11772 ParameterMismatch = true;
11776 QualType FirstParamType = FirstParam->getType();
11777 QualType SecondParamType = SecondParam->getType();
11778 if (FirstParamType != SecondParamType &&
11779 ComputeQualTypeODRHash(FirstParamType) !=
11780 ComputeQualTypeODRHash(SecondParamType)) {
11781 if (const DecayedType *ParamDecayedType =
11782 FirstParamType->getAs<DecayedType>()) {
11783 ODRDiagError(FirstParam->getLocation(),
11784 FirstParam->getSourceRange(), ParameterType)
11785 << (I + 1) << FirstParamType << true
11786 << ParamDecayedType->getOriginalType();
11788 ODRDiagError(FirstParam->getLocation(),
11789 FirstParam->getSourceRange(), ParameterType)
11790 << (I + 1) << FirstParamType << false;
11793 if (const DecayedType *ParamDecayedType =
11794 SecondParamType->getAs<DecayedType>()) {
11795 ODRDiagNote(SecondParam->getLocation(),
11796 SecondParam->getSourceRange(), ParameterType)
11797 << (I + 1) << SecondParamType << true
11798 << ParamDecayedType->getOriginalType();
11800 ODRDiagNote(SecondParam->getLocation(),
11801 SecondParam->getSourceRange(), ParameterType)
11802 << (I + 1) << SecondParamType << false;
11804 ParameterMismatch = true;
11808 const Expr *FirstInit = FirstParam->getInit();
11809 const Expr *SecondInit = SecondParam->getInit();
11810 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11811 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11812 ParameterSingleDefaultArgument)
11813 << (I + 1) << (FirstInit == nullptr)
11814 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11815 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11816 ParameterSingleDefaultArgument)
11817 << (I + 1) << (SecondInit == nullptr)
11818 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11819 ParameterMismatch = true;
11823 if (FirstInit && SecondInit &&
11824 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11825 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11826 ParameterDifferentDefaultArgument)
11827 << (I + 1) << FirstInit->getSourceRange();
11828 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11829 ParameterDifferentDefaultArgument)
11830 << (I + 1) << SecondInit->getSourceRange();
11831 ParameterMismatch = true;
11835 assert(ComputeSubDeclODRHash(FirstParam) ==
11836 ComputeSubDeclODRHash(SecondParam) &&
11837 "Undiagnosed parameter difference.");
11840 if (ParameterMismatch) {
11845 // If no error has been generated before now, assume the problem is in
11846 // the body and generate a message.
11847 ODRDiagError(FirstFunction->getLocation(),
11848 FirstFunction->getSourceRange(), FunctionBody);
11849 ODRDiagNote(SecondFunction->getLocation(),
11850 SecondFunction->getSourceRange(), FunctionBody);
11855 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11858 // Issue ODR failures diagnostics for enums.
11859 for (auto &Merge : EnumOdrMergeFailures) {
11860 enum ODREnumDifference {
11862 EnumTagKeywordMismatch,
11863 SingleSpecifiedType,
11864 DifferentSpecifiedTypes,
11865 DifferentNumberEnumConstants,
11867 EnumConstantSingleInitilizer,
11868 EnumConstantDifferentInitilizer,
11871 // If we've already pointed out a specific problem with this enum, don't
11872 // bother issuing a general "something's different" diagnostic.
11873 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11876 EnumDecl *FirstEnum = Merge.first;
11877 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11880 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11881 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11882 DeclHashes &Hashes, EnumDecl *Enum) {
11883 for (auto *D : Enum->decls()) {
11884 // Due to decl merging, the first EnumDecl is the parent of
11885 // Decls in both records.
11886 if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11888 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11889 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11890 ComputeSubDeclODRHash(D));
11893 DeclHashes FirstHashes;
11894 PopulateHashes(FirstHashes, FirstEnum);
11895 bool Diagnosed = false;
11896 for (auto &SecondEnum : Merge.second) {
11898 if (FirstEnum == SecondEnum)
11901 std::string SecondModule =
11902 getOwningModuleNameForDiagnostic(SecondEnum);
11904 auto ODRDiagError = [FirstEnum, &FirstModule,
11905 this](SourceLocation Loc, SourceRange Range,
11906 ODREnumDifference DiffType) {
11907 return Diag(Loc, diag::err_module_odr_violation_enum)
11908 << FirstEnum << FirstModule.empty() << FirstModule << Range
11911 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11913 ODREnumDifference DiffType) {
11914 return Diag(Loc, diag::note_module_odr_violation_enum)
11915 << SecondModule << Range << DiffType;
11918 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11919 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11921 << FirstEnum->isScoped();
11922 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11924 << SecondEnum->isScoped();
11929 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11930 if (FirstEnum->isScopedUsingClassTag() !=
11931 SecondEnum->isScopedUsingClassTag()) {
11932 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11933 EnumTagKeywordMismatch)
11934 << FirstEnum->isScopedUsingClassTag();
11935 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11936 EnumTagKeywordMismatch)
11937 << SecondEnum->isScopedUsingClassTag();
11943 QualType FirstUnderlyingType =
11944 FirstEnum->getIntegerTypeSourceInfo()
11945 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11947 QualType SecondUnderlyingType =
11948 SecondEnum->getIntegerTypeSourceInfo()
11949 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11951 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11952 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11953 SingleSpecifiedType)
11954 << !FirstUnderlyingType.isNull();
11955 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11956 SingleSpecifiedType)
11957 << !SecondUnderlyingType.isNull();
11962 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11963 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11964 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11965 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11966 DifferentSpecifiedTypes)
11967 << FirstUnderlyingType;
11968 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11969 DifferentSpecifiedTypes)
11970 << SecondUnderlyingType;
11976 DeclHashes SecondHashes;
11977 PopulateHashes(SecondHashes, SecondEnum);
11979 if (FirstHashes.size() != SecondHashes.size()) {
11980 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11981 DifferentNumberEnumConstants)
11982 << (int)FirstHashes.size();
11983 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11984 DifferentNumberEnumConstants)
11985 << (int)SecondHashes.size();
11990 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11991 if (FirstHashes[I].second == SecondHashes[I].second)
11993 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11994 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11996 if (FirstEnumConstant->getDeclName() !=
11997 SecondEnumConstant->getDeclName()) {
11999 ODRDiagError(FirstEnumConstant->getLocation(),
12000 FirstEnumConstant->getSourceRange(), EnumConstantName)
12001 << I + 1 << FirstEnumConstant;
12002 ODRDiagNote(SecondEnumConstant->getLocation(),
12003 SecondEnumConstant->getSourceRange(), EnumConstantName)
12004 << I + 1 << SecondEnumConstant;
12009 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
12010 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
12011 if (!FirstInit && !SecondInit)
12014 if (!FirstInit || !SecondInit) {
12015 ODRDiagError(FirstEnumConstant->getLocation(),
12016 FirstEnumConstant->getSourceRange(),
12017 EnumConstantSingleInitilizer)
12018 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
12019 ODRDiagNote(SecondEnumConstant->getLocation(),
12020 SecondEnumConstant->getSourceRange(),
12021 EnumConstantSingleInitilizer)
12022 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
12027 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
12028 ODRDiagError(FirstEnumConstant->getLocation(),
12029 FirstEnumConstant->getSourceRange(),
12030 EnumConstantDifferentInitilizer)
12031 << I + 1 << FirstEnumConstant;
12032 ODRDiagNote(SecondEnumConstant->getLocation(),
12033 SecondEnumConstant->getSourceRange(),
12034 EnumConstantDifferentInitilizer)
12035 << I + 1 << SecondEnumConstant;
12043 assert(Diagnosed && "Unable to emit ODR diagnostic.");
12047 void ASTReader::StartedDeserializing() {
12048 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
12049 ReadTimer->startTimer();
12052 void ASTReader::FinishedDeserializing() {
12053 assert(NumCurrentElementsDeserializing &&
12054 "FinishedDeserializing not paired with StartedDeserializing");
12055 if (NumCurrentElementsDeserializing == 1) {
12056 // We decrease NumCurrentElementsDeserializing only after pending actions
12057 // are finished, to avoid recursively re-calling finishPendingActions().
12058 finishPendingActions();
12060 --NumCurrentElementsDeserializing;
12062 if (NumCurrentElementsDeserializing == 0) {
12063 // Propagate exception specification and deduced type updates along
12064 // redeclaration chains.
12066 // We do this now rather than in finishPendingActions because we want to
12067 // be able to walk the complete redeclaration chains of the updated decls.
12068 while (!PendingExceptionSpecUpdates.empty() ||
12069 !PendingDeducedTypeUpdates.empty()) {
12070 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
12071 PendingExceptionSpecUpdates.clear();
12072 for (auto Update : ESUpdates) {
12073 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12074 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
12075 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
12076 if (auto *Listener = getContext().getASTMutationListener())
12077 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
12078 for (auto *Redecl : Update.second->redecls())
12079 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
12082 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
12083 PendingDeducedTypeUpdates.clear();
12084 for (auto Update : DTUpdates) {
12085 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12086 // FIXME: If the return type is already deduced, check that it matches.
12087 getContext().adjustDeducedFunctionResultType(Update.first,
12093 ReadTimer->stopTimer();
12095 diagnoseOdrViolations();
12097 // We are not in recursive loading, so it's safe to pass the "interesting"
12098 // decls to the consumer.
12100 PassInterestingDeclsToConsumer();
12104 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
12105 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
12106 // Remove any fake results before adding any real ones.
12107 auto It = PendingFakeLookupResults.find(II);
12108 if (It != PendingFakeLookupResults.end()) {
12109 for (auto *ND : It->second)
12110 SemaObj->IdResolver.RemoveDecl(ND);
12111 // FIXME: this works around module+PCH performance issue.
12112 // Rather than erase the result from the map, which is O(n), just clear
12113 // the vector of NamedDecls.
12114 It->second.clear();
12118 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
12119 SemaObj->TUScope->AddDecl(D);
12120 } else if (SemaObj->TUScope) {
12121 // Adding the decl to IdResolver may have failed because it was already in
12122 // (even though it was not added in scope). If it is already in, make sure
12123 // it gets in the scope as well.
12124 if (std::find(SemaObj->IdResolver.begin(Name),
12125 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
12126 SemaObj->TUScope->AddDecl(D);
12130 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
12131 ASTContext *Context,
12132 const PCHContainerReader &PCHContainerRdr,
12133 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
12134 StringRef isysroot, bool DisableValidation,
12135 bool AllowASTWithCompilerErrors,
12136 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
12137 bool UseGlobalIndex,
12138 std::unique_ptr<llvm::Timer> ReadTimer)
12139 : Listener(DisableValidation
12140 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
12141 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
12142 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
12143 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
12144 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
12145 PCHContainerRdr, PP.getHeaderSearchInfo()),
12146 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
12147 DisableValidation(DisableValidation),
12148 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
12149 AllowConfigurationMismatch(AllowConfigurationMismatch),
12150 ValidateSystemInputs(ValidateSystemInputs),
12151 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
12152 SourceMgr.setExternalSLocEntrySource(this);
12154 for (const auto &Ext : Extensions) {
12155 auto BlockName = Ext->getExtensionMetadata().BlockName;
12156 auto Known = ModuleFileExtensions.find(BlockName);
12157 if (Known != ModuleFileExtensions.end()) {
12158 Diags.Report(diag::warn_duplicate_module_file_extension)
12163 ModuleFileExtensions.insert({BlockName, Ext});
12167 ASTReader::~ASTReader() {
12168 if (OwnsDeserializationListener)
12169 delete DeserializationListener;
12172 IdentifierResolver &ASTReader::getIdResolver() {
12173 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
12176 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
12177 unsigned AbbrevID) {
12180 return Cursor.readRecord(AbbrevID, Record);
12182 //===----------------------------------------------------------------------===//
12183 //// OMPClauseReader implementation
12184 ////===----------------------------------------------------------------------===//
12186 OMPClause *OMPClauseReader::readClause() {
12188 switch (Record.readInt()) {
12190 C = new (Context) OMPIfClause();
12193 C = new (Context) OMPFinalClause();
12195 case OMPC_num_threads:
12196 C = new (Context) OMPNumThreadsClause();
12199 C = new (Context) OMPSafelenClause();
12202 C = new (Context) OMPSimdlenClause();
12204 case OMPC_allocator:
12205 C = new (Context) OMPAllocatorClause();
12207 case OMPC_collapse:
12208 C = new (Context) OMPCollapseClause();
12211 C = new (Context) OMPDefaultClause();
12213 case OMPC_proc_bind:
12214 C = new (Context) OMPProcBindClause();
12216 case OMPC_schedule:
12217 C = new (Context) OMPScheduleClause();
12220 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
12223 C = new (Context) OMPNowaitClause();
12226 C = new (Context) OMPUntiedClause();
12228 case OMPC_mergeable:
12229 C = new (Context) OMPMergeableClause();
12232 C = new (Context) OMPReadClause();
12235 C = new (Context) OMPWriteClause();
12238 C = new (Context) OMPUpdateClause();
12241 C = new (Context) OMPCaptureClause();
12244 C = new (Context) OMPSeqCstClause();
12247 C = new (Context) OMPThreadsClause();
12250 C = new (Context) OMPSIMDClause();
12253 C = new (Context) OMPNogroupClause();
12255 case OMPC_unified_address:
12256 C = new (Context) OMPUnifiedAddressClause();
12258 case OMPC_unified_shared_memory:
12259 C = new (Context) OMPUnifiedSharedMemoryClause();
12261 case OMPC_reverse_offload:
12262 C = new (Context) OMPReverseOffloadClause();
12264 case OMPC_dynamic_allocators:
12265 C = new (Context) OMPDynamicAllocatorsClause();
12267 case OMPC_atomic_default_mem_order:
12268 C = new (Context) OMPAtomicDefaultMemOrderClause();
12271 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
12273 case OMPC_firstprivate:
12274 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
12276 case OMPC_lastprivate:
12277 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
12280 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
12282 case OMPC_reduction:
12283 C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
12285 case OMPC_task_reduction:
12286 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
12288 case OMPC_in_reduction:
12289 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
12292 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
12295 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
12298 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
12300 case OMPC_copyprivate:
12301 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
12304 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
12306 case OMPC_depend: {
12307 unsigned NumVars = Record.readInt();
12308 unsigned NumLoops = Record.readInt();
12309 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
12313 C = new (Context) OMPDeviceClause();
12316 OMPMappableExprListSizeTy Sizes;
12317 Sizes.NumVars = Record.readInt();
12318 Sizes.NumUniqueDeclarations = Record.readInt();
12319 Sizes.NumComponentLists = Record.readInt();
12320 Sizes.NumComponents = Record.readInt();
12321 C = OMPMapClause::CreateEmpty(Context, Sizes);
12324 case OMPC_num_teams:
12325 C = new (Context) OMPNumTeamsClause();
12327 case OMPC_thread_limit:
12328 C = new (Context) OMPThreadLimitClause();
12330 case OMPC_priority:
12331 C = new (Context) OMPPriorityClause();
12333 case OMPC_grainsize:
12334 C = new (Context) OMPGrainsizeClause();
12336 case OMPC_num_tasks:
12337 C = new (Context) OMPNumTasksClause();
12340 C = new (Context) OMPHintClause();
12342 case OMPC_dist_schedule:
12343 C = new (Context) OMPDistScheduleClause();
12345 case OMPC_defaultmap:
12346 C = new (Context) OMPDefaultmapClause();
12349 OMPMappableExprListSizeTy Sizes;
12350 Sizes.NumVars = Record.readInt();
12351 Sizes.NumUniqueDeclarations = Record.readInt();
12352 Sizes.NumComponentLists = Record.readInt();
12353 Sizes.NumComponents = Record.readInt();
12354 C = OMPToClause::CreateEmpty(Context, Sizes);
12358 OMPMappableExprListSizeTy Sizes;
12359 Sizes.NumVars = Record.readInt();
12360 Sizes.NumUniqueDeclarations = Record.readInt();
12361 Sizes.NumComponentLists = Record.readInt();
12362 Sizes.NumComponents = Record.readInt();
12363 C = OMPFromClause::CreateEmpty(Context, Sizes);
12366 case OMPC_use_device_ptr: {
12367 OMPMappableExprListSizeTy Sizes;
12368 Sizes.NumVars = Record.readInt();
12369 Sizes.NumUniqueDeclarations = Record.readInt();
12370 Sizes.NumComponentLists = Record.readInt();
12371 Sizes.NumComponents = Record.readInt();
12372 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12375 case OMPC_is_device_ptr: {
12376 OMPMappableExprListSizeTy Sizes;
12377 Sizes.NumVars = Record.readInt();
12378 Sizes.NumUniqueDeclarations = Record.readInt();
12379 Sizes.NumComponentLists = Record.readInt();
12380 Sizes.NumComponents = Record.readInt();
12381 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12384 case OMPC_allocate:
12385 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12389 C->setLocStart(Record.readSourceLocation());
12390 C->setLocEnd(Record.readSourceLocation());
12395 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12396 C->setPreInitStmt(Record.readSubStmt(),
12397 static_cast<OpenMPDirectiveKind>(Record.readInt()));
12400 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12401 VisitOMPClauseWithPreInit(C);
12402 C->setPostUpdateExpr(Record.readSubExpr());
12405 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12406 VisitOMPClauseWithPreInit(C);
12407 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12408 C->setNameModifierLoc(Record.readSourceLocation());
12409 C->setColonLoc(Record.readSourceLocation());
12410 C->setCondition(Record.readSubExpr());
12411 C->setLParenLoc(Record.readSourceLocation());
12414 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12415 C->setCondition(Record.readSubExpr());
12416 C->setLParenLoc(Record.readSourceLocation());
12419 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12420 VisitOMPClauseWithPreInit(C);
12421 C->setNumThreads(Record.readSubExpr());
12422 C->setLParenLoc(Record.readSourceLocation());
12425 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12426 C->setSafelen(Record.readSubExpr());
12427 C->setLParenLoc(Record.readSourceLocation());
12430 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12431 C->setSimdlen(Record.readSubExpr());
12432 C->setLParenLoc(Record.readSourceLocation());
12435 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12436 C->setAllocator(Record.readExpr());
12437 C->setLParenLoc(Record.readSourceLocation());
12440 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12441 C->setNumForLoops(Record.readSubExpr());
12442 C->setLParenLoc(Record.readSourceLocation());
12445 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12447 static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
12448 C->setLParenLoc(Record.readSourceLocation());
12449 C->setDefaultKindKwLoc(Record.readSourceLocation());
12452 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12453 C->setProcBindKind(
12454 static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
12455 C->setLParenLoc(Record.readSourceLocation());
12456 C->setProcBindKindKwLoc(Record.readSourceLocation());
12459 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12460 VisitOMPClauseWithPreInit(C);
12461 C->setScheduleKind(
12462 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12463 C->setFirstScheduleModifier(
12464 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12465 C->setSecondScheduleModifier(
12466 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12467 C->setChunkSize(Record.readSubExpr());
12468 C->setLParenLoc(Record.readSourceLocation());
12469 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12470 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12471 C->setScheduleKindLoc(Record.readSourceLocation());
12472 C->setCommaLoc(Record.readSourceLocation());
12475 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12476 C->setNumForLoops(Record.readSubExpr());
12477 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12478 C->setLoopNumIterations(I, Record.readSubExpr());
12479 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12480 C->setLoopCounter(I, Record.readSubExpr());
12481 C->setLParenLoc(Record.readSourceLocation());
12484 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12486 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12488 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12490 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12492 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12494 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12496 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12498 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12500 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12502 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12504 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12506 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12508 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12509 OMPUnifiedSharedMemoryClause *) {}
12511 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12514 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12517 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12518 OMPAtomicDefaultMemOrderClause *C) {
12519 C->setAtomicDefaultMemOrderKind(
12520 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12521 C->setLParenLoc(Record.readSourceLocation());
12522 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12525 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12526 C->setLParenLoc(Record.readSourceLocation());
12527 unsigned NumVars = C->varlist_size();
12528 SmallVector<Expr *, 16> Vars;
12529 Vars.reserve(NumVars);
12530 for (unsigned i = 0; i != NumVars; ++i)
12531 Vars.push_back(Record.readSubExpr());
12532 C->setVarRefs(Vars);
12534 for (unsigned i = 0; i != NumVars; ++i)
12535 Vars.push_back(Record.readSubExpr());
12536 C->setPrivateCopies(Vars);
12539 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12540 VisitOMPClauseWithPreInit(C);
12541 C->setLParenLoc(Record.readSourceLocation());
12542 unsigned NumVars = C->varlist_size();
12543 SmallVector<Expr *, 16> Vars;
12544 Vars.reserve(NumVars);
12545 for (unsigned i = 0; i != NumVars; ++i)
12546 Vars.push_back(Record.readSubExpr());
12547 C->setVarRefs(Vars);
12549 for (unsigned i = 0; i != NumVars; ++i)
12550 Vars.push_back(Record.readSubExpr());
12551 C->setPrivateCopies(Vars);
12553 for (unsigned i = 0; i != NumVars; ++i)
12554 Vars.push_back(Record.readSubExpr());
12558 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12559 VisitOMPClauseWithPostUpdate(C);
12560 C->setLParenLoc(Record.readSourceLocation());
12561 unsigned NumVars = C->varlist_size();
12562 SmallVector<Expr *, 16> Vars;
12563 Vars.reserve(NumVars);
12564 for (unsigned i = 0; i != NumVars; ++i)
12565 Vars.push_back(Record.readSubExpr());
12566 C->setVarRefs(Vars);
12568 for (unsigned i = 0; i != NumVars; ++i)
12569 Vars.push_back(Record.readSubExpr());
12570 C->setPrivateCopies(Vars);
12572 for (unsigned i = 0; i != NumVars; ++i)
12573 Vars.push_back(Record.readSubExpr());
12574 C->setSourceExprs(Vars);
12576 for (unsigned i = 0; i != NumVars; ++i)
12577 Vars.push_back(Record.readSubExpr());
12578 C->setDestinationExprs(Vars);
12580 for (unsigned i = 0; i != NumVars; ++i)
12581 Vars.push_back(Record.readSubExpr());
12582 C->setAssignmentOps(Vars);
12585 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12586 C->setLParenLoc(Record.readSourceLocation());
12587 unsigned NumVars = C->varlist_size();
12588 SmallVector<Expr *, 16> Vars;
12589 Vars.reserve(NumVars);
12590 for (unsigned i = 0; i != NumVars; ++i)
12591 Vars.push_back(Record.readSubExpr());
12592 C->setVarRefs(Vars);
12595 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12596 VisitOMPClauseWithPostUpdate(C);
12597 C->setLParenLoc(Record.readSourceLocation());
12598 C->setColonLoc(Record.readSourceLocation());
12599 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12600 DeclarationNameInfo DNI;
12601 Record.readDeclarationNameInfo(DNI);
12602 C->setQualifierLoc(NNSL);
12603 C->setNameInfo(DNI);
12605 unsigned NumVars = C->varlist_size();
12606 SmallVector<Expr *, 16> Vars;
12607 Vars.reserve(NumVars);
12608 for (unsigned i = 0; i != NumVars; ++i)
12609 Vars.push_back(Record.readSubExpr());
12610 C->setVarRefs(Vars);
12612 for (unsigned i = 0; i != NumVars; ++i)
12613 Vars.push_back(Record.readSubExpr());
12614 C->setPrivates(Vars);
12616 for (unsigned i = 0; i != NumVars; ++i)
12617 Vars.push_back(Record.readSubExpr());
12618 C->setLHSExprs(Vars);
12620 for (unsigned i = 0; i != NumVars; ++i)
12621 Vars.push_back(Record.readSubExpr());
12622 C->setRHSExprs(Vars);
12624 for (unsigned i = 0; i != NumVars; ++i)
12625 Vars.push_back(Record.readSubExpr());
12626 C->setReductionOps(Vars);
12629 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12630 VisitOMPClauseWithPostUpdate(C);
12631 C->setLParenLoc(Record.readSourceLocation());
12632 C->setColonLoc(Record.readSourceLocation());
12633 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12634 DeclarationNameInfo DNI;
12635 Record.readDeclarationNameInfo(DNI);
12636 C->setQualifierLoc(NNSL);
12637 C->setNameInfo(DNI);
12639 unsigned NumVars = C->varlist_size();
12640 SmallVector<Expr *, 16> Vars;
12641 Vars.reserve(NumVars);
12642 for (unsigned I = 0; I != NumVars; ++I)
12643 Vars.push_back(Record.readSubExpr());
12644 C->setVarRefs(Vars);
12646 for (unsigned I = 0; I != NumVars; ++I)
12647 Vars.push_back(Record.readSubExpr());
12648 C->setPrivates(Vars);
12650 for (unsigned I = 0; I != NumVars; ++I)
12651 Vars.push_back(Record.readSubExpr());
12652 C->setLHSExprs(Vars);
12654 for (unsigned I = 0; I != NumVars; ++I)
12655 Vars.push_back(Record.readSubExpr());
12656 C->setRHSExprs(Vars);
12658 for (unsigned I = 0; I != NumVars; ++I)
12659 Vars.push_back(Record.readSubExpr());
12660 C->setReductionOps(Vars);
12663 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12664 VisitOMPClauseWithPostUpdate(C);
12665 C->setLParenLoc(Record.readSourceLocation());
12666 C->setColonLoc(Record.readSourceLocation());
12667 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12668 DeclarationNameInfo DNI;
12669 Record.readDeclarationNameInfo(DNI);
12670 C->setQualifierLoc(NNSL);
12671 C->setNameInfo(DNI);
12673 unsigned NumVars = C->varlist_size();
12674 SmallVector<Expr *, 16> Vars;
12675 Vars.reserve(NumVars);
12676 for (unsigned I = 0; I != NumVars; ++I)
12677 Vars.push_back(Record.readSubExpr());
12678 C->setVarRefs(Vars);
12680 for (unsigned I = 0; I != NumVars; ++I)
12681 Vars.push_back(Record.readSubExpr());
12682 C->setPrivates(Vars);
12684 for (unsigned I = 0; I != NumVars; ++I)
12685 Vars.push_back(Record.readSubExpr());
12686 C->setLHSExprs(Vars);
12688 for (unsigned I = 0; I != NumVars; ++I)
12689 Vars.push_back(Record.readSubExpr());
12690 C->setRHSExprs(Vars);
12692 for (unsigned I = 0; I != NumVars; ++I)
12693 Vars.push_back(Record.readSubExpr());
12694 C->setReductionOps(Vars);
12696 for (unsigned I = 0; I != NumVars; ++I)
12697 Vars.push_back(Record.readSubExpr());
12698 C->setTaskgroupDescriptors(Vars);
12701 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12702 VisitOMPClauseWithPostUpdate(C);
12703 C->setLParenLoc(Record.readSourceLocation());
12704 C->setColonLoc(Record.readSourceLocation());
12705 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12706 C->setModifierLoc(Record.readSourceLocation());
12707 unsigned NumVars = C->varlist_size();
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->setPrivates(Vars);
12718 for (unsigned i = 0; i != NumVars; ++i)
12719 Vars.push_back(Record.readSubExpr());
12722 for (unsigned i = 0; i != NumVars; ++i)
12723 Vars.push_back(Record.readSubExpr());
12724 C->setUpdates(Vars);
12726 for (unsigned i = 0; i != NumVars; ++i)
12727 Vars.push_back(Record.readSubExpr());
12728 C->setFinals(Vars);
12729 C->setStep(Record.readSubExpr());
12730 C->setCalcStep(Record.readSubExpr());
12733 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12734 C->setLParenLoc(Record.readSourceLocation());
12735 C->setColonLoc(Record.readSourceLocation());
12736 unsigned NumVars = C->varlist_size();
12737 SmallVector<Expr *, 16> Vars;
12738 Vars.reserve(NumVars);
12739 for (unsigned i = 0; i != NumVars; ++i)
12740 Vars.push_back(Record.readSubExpr());
12741 C->setVarRefs(Vars);
12742 C->setAlignment(Record.readSubExpr());
12745 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12746 C->setLParenLoc(Record.readSourceLocation());
12747 unsigned NumVars = C->varlist_size();
12748 SmallVector<Expr *, 16> Exprs;
12749 Exprs.reserve(NumVars);
12750 for (unsigned i = 0; i != NumVars; ++i)
12751 Exprs.push_back(Record.readSubExpr());
12752 C->setVarRefs(Exprs);
12754 for (unsigned i = 0; i != NumVars; ++i)
12755 Exprs.push_back(Record.readSubExpr());
12756 C->setSourceExprs(Exprs);
12758 for (unsigned i = 0; i != NumVars; ++i)
12759 Exprs.push_back(Record.readSubExpr());
12760 C->setDestinationExprs(Exprs);
12762 for (unsigned i = 0; i != NumVars; ++i)
12763 Exprs.push_back(Record.readSubExpr());
12764 C->setAssignmentOps(Exprs);
12767 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12768 C->setLParenLoc(Record.readSourceLocation());
12769 unsigned NumVars = C->varlist_size();
12770 SmallVector<Expr *, 16> Exprs;
12771 Exprs.reserve(NumVars);
12772 for (unsigned i = 0; i != NumVars; ++i)
12773 Exprs.push_back(Record.readSubExpr());
12774 C->setVarRefs(Exprs);
12776 for (unsigned i = 0; i != NumVars; ++i)
12777 Exprs.push_back(Record.readSubExpr());
12778 C->setSourceExprs(Exprs);
12780 for (unsigned i = 0; i != NumVars; ++i)
12781 Exprs.push_back(Record.readSubExpr());
12782 C->setDestinationExprs(Exprs);
12784 for (unsigned i = 0; i != NumVars; ++i)
12785 Exprs.push_back(Record.readSubExpr());
12786 C->setAssignmentOps(Exprs);
12789 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12790 C->setLParenLoc(Record.readSourceLocation());
12791 unsigned NumVars = C->varlist_size();
12792 SmallVector<Expr *, 16> Vars;
12793 Vars.reserve(NumVars);
12794 for (unsigned i = 0; i != NumVars; ++i)
12795 Vars.push_back(Record.readSubExpr());
12796 C->setVarRefs(Vars);
12799 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12800 C->setLParenLoc(Record.readSourceLocation());
12801 C->setDependencyKind(
12802 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12803 C->setDependencyLoc(Record.readSourceLocation());
12804 C->setColonLoc(Record.readSourceLocation());
12805 unsigned NumVars = C->varlist_size();
12806 SmallVector<Expr *, 16> Vars;
12807 Vars.reserve(NumVars);
12808 for (unsigned I = 0; I != NumVars; ++I)
12809 Vars.push_back(Record.readSubExpr());
12810 C->setVarRefs(Vars);
12811 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12812 C->setLoopData(I, Record.readSubExpr());
12815 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12816 VisitOMPClauseWithPreInit(C);
12817 C->setDevice(Record.readSubExpr());
12818 C->setLParenLoc(Record.readSourceLocation());
12821 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12822 C->setLParenLoc(Record.readSourceLocation());
12823 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12824 C->setMapTypeModifier(
12825 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12826 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12828 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12829 DeclarationNameInfo DNI;
12830 Record.readDeclarationNameInfo(DNI);
12831 C->setMapperIdInfo(DNI);
12833 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12834 C->setMapLoc(Record.readSourceLocation());
12835 C->setColonLoc(Record.readSourceLocation());
12836 auto NumVars = C->varlist_size();
12837 auto UniqueDecls = C->getUniqueDeclarationsNum();
12838 auto TotalLists = C->getTotalComponentListNum();
12839 auto TotalComponents = C->getTotalComponentsNum();
12841 SmallVector<Expr *, 16> Vars;
12842 Vars.reserve(NumVars);
12843 for (unsigned i = 0; i != NumVars; ++i)
12844 Vars.push_back(Record.readExpr());
12845 C->setVarRefs(Vars);
12847 SmallVector<Expr *, 16> UDMappers;
12848 UDMappers.reserve(NumVars);
12849 for (unsigned I = 0; I < NumVars; ++I)
12850 UDMappers.push_back(Record.readExpr());
12851 C->setUDMapperRefs(UDMappers);
12853 SmallVector<ValueDecl *, 16> Decls;
12854 Decls.reserve(UniqueDecls);
12855 for (unsigned i = 0; i < UniqueDecls; ++i)
12856 Decls.push_back(Record.readDeclAs<ValueDecl>());
12857 C->setUniqueDecls(Decls);
12859 SmallVector<unsigned, 16> ListsPerDecl;
12860 ListsPerDecl.reserve(UniqueDecls);
12861 for (unsigned i = 0; i < UniqueDecls; ++i)
12862 ListsPerDecl.push_back(Record.readInt());
12863 C->setDeclNumLists(ListsPerDecl);
12865 SmallVector<unsigned, 32> ListSizes;
12866 ListSizes.reserve(TotalLists);
12867 for (unsigned i = 0; i < TotalLists; ++i)
12868 ListSizes.push_back(Record.readInt());
12869 C->setComponentListSizes(ListSizes);
12871 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12872 Components.reserve(TotalComponents);
12873 for (unsigned i = 0; i < TotalComponents; ++i) {
12874 Expr *AssociatedExpr = Record.readExpr();
12875 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12876 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12877 AssociatedExpr, AssociatedDecl));
12879 C->setComponents(Components, ListSizes);
12882 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12883 C->setLParenLoc(Record.readSourceLocation());
12884 C->setColonLoc(Record.readSourceLocation());
12885 C->setAllocator(Record.readSubExpr());
12886 unsigned NumVars = C->varlist_size();
12887 SmallVector<Expr *, 16> Vars;
12888 Vars.reserve(NumVars);
12889 for (unsigned i = 0; i != NumVars; ++i)
12890 Vars.push_back(Record.readSubExpr());
12891 C->setVarRefs(Vars);
12894 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12895 VisitOMPClauseWithPreInit(C);
12896 C->setNumTeams(Record.readSubExpr());
12897 C->setLParenLoc(Record.readSourceLocation());
12900 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12901 VisitOMPClauseWithPreInit(C);
12902 C->setThreadLimit(Record.readSubExpr());
12903 C->setLParenLoc(Record.readSourceLocation());
12906 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12907 C->setPriority(Record.readSubExpr());
12908 C->setLParenLoc(Record.readSourceLocation());
12911 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12912 C->setGrainsize(Record.readSubExpr());
12913 C->setLParenLoc(Record.readSourceLocation());
12916 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12917 C->setNumTasks(Record.readSubExpr());
12918 C->setLParenLoc(Record.readSourceLocation());
12921 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12922 C->setHint(Record.readSubExpr());
12923 C->setLParenLoc(Record.readSourceLocation());
12926 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12927 VisitOMPClauseWithPreInit(C);
12928 C->setDistScheduleKind(
12929 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12930 C->setChunkSize(Record.readSubExpr());
12931 C->setLParenLoc(Record.readSourceLocation());
12932 C->setDistScheduleKindLoc(Record.readSourceLocation());
12933 C->setCommaLoc(Record.readSourceLocation());
12936 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12937 C->setDefaultmapKind(
12938 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12939 C->setDefaultmapModifier(
12940 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12941 C->setLParenLoc(Record.readSourceLocation());
12942 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12943 C->setDefaultmapKindLoc(Record.readSourceLocation());
12946 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12947 C->setLParenLoc(Record.readSourceLocation());
12948 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12949 DeclarationNameInfo DNI;
12950 Record.readDeclarationNameInfo(DNI);
12951 C->setMapperIdInfo(DNI);
12952 auto NumVars = C->varlist_size();
12953 auto UniqueDecls = C->getUniqueDeclarationsNum();
12954 auto TotalLists = C->getTotalComponentListNum();
12955 auto TotalComponents = C->getTotalComponentsNum();
12957 SmallVector<Expr *, 16> Vars;
12958 Vars.reserve(NumVars);
12959 for (unsigned i = 0; i != NumVars; ++i)
12960 Vars.push_back(Record.readSubExpr());
12961 C->setVarRefs(Vars);
12963 SmallVector<Expr *, 16> UDMappers;
12964 UDMappers.reserve(NumVars);
12965 for (unsigned I = 0; I < NumVars; ++I)
12966 UDMappers.push_back(Record.readSubExpr());
12967 C->setUDMapperRefs(UDMappers);
12969 SmallVector<ValueDecl *, 16> Decls;
12970 Decls.reserve(UniqueDecls);
12971 for (unsigned i = 0; i < UniqueDecls; ++i)
12972 Decls.push_back(Record.readDeclAs<ValueDecl>());
12973 C->setUniqueDecls(Decls);
12975 SmallVector<unsigned, 16> ListsPerDecl;
12976 ListsPerDecl.reserve(UniqueDecls);
12977 for (unsigned i = 0; i < UniqueDecls; ++i)
12978 ListsPerDecl.push_back(Record.readInt());
12979 C->setDeclNumLists(ListsPerDecl);
12981 SmallVector<unsigned, 32> ListSizes;
12982 ListSizes.reserve(TotalLists);
12983 for (unsigned i = 0; i < TotalLists; ++i)
12984 ListSizes.push_back(Record.readInt());
12985 C->setComponentListSizes(ListSizes);
12987 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12988 Components.reserve(TotalComponents);
12989 for (unsigned i = 0; i < TotalComponents; ++i) {
12990 Expr *AssociatedExpr = Record.readSubExpr();
12991 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12992 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12993 AssociatedExpr, AssociatedDecl));
12995 C->setComponents(Components, ListSizes);
12998 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12999 C->setLParenLoc(Record.readSourceLocation());
13000 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13001 DeclarationNameInfo DNI;
13002 Record.readDeclarationNameInfo(DNI);
13003 C->setMapperIdInfo(DNI);
13004 auto NumVars = C->varlist_size();
13005 auto UniqueDecls = C->getUniqueDeclarationsNum();
13006 auto TotalLists = C->getTotalComponentListNum();
13007 auto TotalComponents = C->getTotalComponentsNum();
13009 SmallVector<Expr *, 16> Vars;
13010 Vars.reserve(NumVars);
13011 for (unsigned i = 0; i != NumVars; ++i)
13012 Vars.push_back(Record.readSubExpr());
13013 C->setVarRefs(Vars);
13015 SmallVector<Expr *, 16> UDMappers;
13016 UDMappers.reserve(NumVars);
13017 for (unsigned I = 0; I < NumVars; ++I)
13018 UDMappers.push_back(Record.readSubExpr());
13019 C->setUDMapperRefs(UDMappers);
13021 SmallVector<ValueDecl *, 16> Decls;
13022 Decls.reserve(UniqueDecls);
13023 for (unsigned i = 0; i < UniqueDecls; ++i)
13024 Decls.push_back(Record.readDeclAs<ValueDecl>());
13025 C->setUniqueDecls(Decls);
13027 SmallVector<unsigned, 16> ListsPerDecl;
13028 ListsPerDecl.reserve(UniqueDecls);
13029 for (unsigned i = 0; i < UniqueDecls; ++i)
13030 ListsPerDecl.push_back(Record.readInt());
13031 C->setDeclNumLists(ListsPerDecl);
13033 SmallVector<unsigned, 32> ListSizes;
13034 ListSizes.reserve(TotalLists);
13035 for (unsigned i = 0; i < TotalLists; ++i)
13036 ListSizes.push_back(Record.readInt());
13037 C->setComponentListSizes(ListSizes);
13039 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13040 Components.reserve(TotalComponents);
13041 for (unsigned i = 0; i < TotalComponents; ++i) {
13042 Expr *AssociatedExpr = Record.readSubExpr();
13043 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13044 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13045 AssociatedExpr, AssociatedDecl));
13047 C->setComponents(Components, ListSizes);
13050 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13051 C->setLParenLoc(Record.readSourceLocation());
13052 auto NumVars = C->varlist_size();
13053 auto UniqueDecls = C->getUniqueDeclarationsNum();
13054 auto TotalLists = C->getTotalComponentListNum();
13055 auto TotalComponents = C->getTotalComponentsNum();
13057 SmallVector<Expr *, 16> Vars;
13058 Vars.reserve(NumVars);
13059 for (unsigned i = 0; i != NumVars; ++i)
13060 Vars.push_back(Record.readSubExpr());
13061 C->setVarRefs(Vars);
13063 for (unsigned i = 0; i != NumVars; ++i)
13064 Vars.push_back(Record.readSubExpr());
13065 C->setPrivateCopies(Vars);
13067 for (unsigned i = 0; i != NumVars; ++i)
13068 Vars.push_back(Record.readSubExpr());
13071 SmallVector<ValueDecl *, 16> Decls;
13072 Decls.reserve(UniqueDecls);
13073 for (unsigned i = 0; i < UniqueDecls; ++i)
13074 Decls.push_back(Record.readDeclAs<ValueDecl>());
13075 C->setUniqueDecls(Decls);
13077 SmallVector<unsigned, 16> ListsPerDecl;
13078 ListsPerDecl.reserve(UniqueDecls);
13079 for (unsigned i = 0; i < UniqueDecls; ++i)
13080 ListsPerDecl.push_back(Record.readInt());
13081 C->setDeclNumLists(ListsPerDecl);
13083 SmallVector<unsigned, 32> ListSizes;
13084 ListSizes.reserve(TotalLists);
13085 for (unsigned i = 0; i < TotalLists; ++i)
13086 ListSizes.push_back(Record.readInt());
13087 C->setComponentListSizes(ListSizes);
13089 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13090 Components.reserve(TotalComponents);
13091 for (unsigned i = 0; i < TotalComponents; ++i) {
13092 Expr *AssociatedExpr = Record.readSubExpr();
13093 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13094 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13095 AssociatedExpr, AssociatedDecl));
13097 C->setComponents(Components, ListSizes);
13100 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13101 C->setLParenLoc(Record.readSourceLocation());
13102 auto NumVars = C->varlist_size();
13103 auto UniqueDecls = C->getUniqueDeclarationsNum();
13104 auto TotalLists = C->getTotalComponentListNum();
13105 auto TotalComponents = C->getTotalComponentsNum();
13107 SmallVector<Expr *, 16> Vars;
13108 Vars.reserve(NumVars);
13109 for (unsigned i = 0; i != NumVars; ++i)
13110 Vars.push_back(Record.readSubExpr());
13111 C->setVarRefs(Vars);
13114 SmallVector<ValueDecl *, 16> Decls;
13115 Decls.reserve(UniqueDecls);
13116 for (unsigned i = 0; i < UniqueDecls; ++i)
13117 Decls.push_back(Record.readDeclAs<ValueDecl>());
13118 C->setUniqueDecls(Decls);
13120 SmallVector<unsigned, 16> ListsPerDecl;
13121 ListsPerDecl.reserve(UniqueDecls);
13122 for (unsigned i = 0; i < UniqueDecls; ++i)
13123 ListsPerDecl.push_back(Record.readInt());
13124 C->setDeclNumLists(ListsPerDecl);
13126 SmallVector<unsigned, 32> ListSizes;
13127 ListSizes.reserve(TotalLists);
13128 for (unsigned i = 0; i < TotalLists; ++i)
13129 ListSizes.push_back(Record.readInt());
13130 C->setComponentListSizes(ListSizes);
13132 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13133 Components.reserve(TotalComponents);
13134 for (unsigned i = 0; i < TotalComponents; ++i) {
13135 Expr *AssociatedExpr = Record.readSubExpr();
13136 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13137 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13138 AssociatedExpr, AssociatedDecl));
13140 C->setComponents(Components, ListSizes);