[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Serialization / ASTReader.cpp
blob42b48d230af7a97cc8b8b8d353563907be3e4033
1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ASTReader class, which reads AST files.
11 //===----------------------------------------------------------------------===//
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/ASTStructuralEquivalence.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/AbstractTypeReader.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclBase.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclGroup.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/DeclarationName.h"
29 #include "clang/AST/Expr.h"
30 #include "clang/AST/ExprCXX.h"
31 #include "clang/AST/ExternalASTSource.h"
32 #include "clang/AST/NestedNameSpecifier.h"
33 #include "clang/AST/ODRDiagsEmitter.h"
34 #include "clang/AST/ODRHash.h"
35 #include "clang/AST/OpenMPClause.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticError.h"
46 #include "clang/Basic/DiagnosticOptions.h"
47 #include "clang/Basic/DiagnosticSema.h"
48 #include "clang/Basic/ExceptionSpecificationType.h"
49 #include "clang/Basic/FileManager.h"
50 #include "clang/Basic/FileSystemOptions.h"
51 #include "clang/Basic/IdentifierTable.h"
52 #include "clang/Basic/LLVM.h"
53 #include "clang/Basic/LangOptions.h"
54 #include "clang/Basic/Module.h"
55 #include "clang/Basic/ObjCRuntime.h"
56 #include "clang/Basic/OpenMPKinds.h"
57 #include "clang/Basic/OperatorKinds.h"
58 #include "clang/Basic/PragmaKinds.h"
59 #include "clang/Basic/Sanitizers.h"
60 #include "clang/Basic/SourceLocation.h"
61 #include "clang/Basic/SourceManager.h"
62 #include "clang/Basic/SourceManagerInternals.h"
63 #include "clang/Basic/Specifiers.h"
64 #include "clang/Basic/TargetInfo.h"
65 #include "clang/Basic/TargetOptions.h"
66 #include "clang/Basic/TokenKinds.h"
67 #include "clang/Basic/Version.h"
68 #include "clang/Lex/HeaderSearch.h"
69 #include "clang/Lex/HeaderSearchOptions.h"
70 #include "clang/Lex/MacroInfo.h"
71 #include "clang/Lex/ModuleMap.h"
72 #include "clang/Lex/PreprocessingRecord.h"
73 #include "clang/Lex/Preprocessor.h"
74 #include "clang/Lex/PreprocessorOptions.h"
75 #include "clang/Lex/Token.h"
76 #include "clang/Sema/ObjCMethodList.h"
77 #include "clang/Sema/Scope.h"
78 #include "clang/Sema/Sema.h"
79 #include "clang/Sema/Weak.h"
80 #include "clang/Serialization/ASTBitCodes.h"
81 #include "clang/Serialization/ASTDeserializationListener.h"
82 #include "clang/Serialization/ASTRecordReader.h"
83 #include "clang/Serialization/ContinuousRangeMap.h"
84 #include "clang/Serialization/GlobalModuleIndex.h"
85 #include "clang/Serialization/InMemoryModuleCache.h"
86 #include "clang/Serialization/ModuleFile.h"
87 #include "clang/Serialization/ModuleFileExtension.h"
88 #include "clang/Serialization/ModuleManager.h"
89 #include "clang/Serialization/PCHContainerOperations.h"
90 #include "clang/Serialization/SerializationDiagnostic.h"
91 #include "llvm/ADT/APFloat.h"
92 #include "llvm/ADT/APInt.h"
93 #include "llvm/ADT/APSInt.h"
94 #include "llvm/ADT/ArrayRef.h"
95 #include "llvm/ADT/DenseMap.h"
96 #include "llvm/ADT/FloatingPointMode.h"
97 #include "llvm/ADT/FoldingSet.h"
98 #include "llvm/ADT/Hashing.h"
99 #include "llvm/ADT/IntrusiveRefCntPtr.h"
100 #include "llvm/ADT/STLExtras.h"
101 #include "llvm/ADT/ScopeExit.h"
102 #include "llvm/ADT/SmallPtrSet.h"
103 #include "llvm/ADT/SmallString.h"
104 #include "llvm/ADT/SmallVector.h"
105 #include "llvm/ADT/StringExtras.h"
106 #include "llvm/ADT/StringMap.h"
107 #include "llvm/ADT/StringRef.h"
108 #include "llvm/ADT/iterator_range.h"
109 #include "llvm/Bitstream/BitstreamReader.h"
110 #include "llvm/Support/Casting.h"
111 #include "llvm/Support/Compiler.h"
112 #include "llvm/Support/Compression.h"
113 #include "llvm/Support/DJB.h"
114 #include "llvm/Support/Endian.h"
115 #include "llvm/Support/Error.h"
116 #include "llvm/Support/ErrorHandling.h"
117 #include "llvm/Support/FileSystem.h"
118 #include "llvm/Support/LEB128.h"
119 #include "llvm/Support/MemoryBuffer.h"
120 #include "llvm/Support/Path.h"
121 #include "llvm/Support/SaveAndRestore.h"
122 #include "llvm/Support/TimeProfiler.h"
123 #include "llvm/Support/Timer.h"
124 #include "llvm/Support/VersionTuple.h"
125 #include "llvm/Support/raw_ostream.h"
126 #include "llvm/TargetParser/Triple.h"
127 #include <algorithm>
128 #include <cassert>
129 #include <cstddef>
130 #include <cstdint>
131 #include <cstdio>
132 #include <ctime>
133 #include <iterator>
134 #include <limits>
135 #include <map>
136 #include <memory>
137 #include <optional>
138 #include <string>
139 #include <system_error>
140 #include <tuple>
141 #include <utility>
142 #include <vector>
144 using namespace clang;
145 using namespace clang::serialization;
146 using namespace clang::serialization::reader;
147 using llvm::BitstreamCursor;
149 //===----------------------------------------------------------------------===//
150 // ChainedASTReaderListener implementation
151 //===----------------------------------------------------------------------===//
153 bool
154 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
155 return First->ReadFullVersionInformation(FullVersion) ||
156 Second->ReadFullVersionInformation(FullVersion);
159 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
160 First->ReadModuleName(ModuleName);
161 Second->ReadModuleName(ModuleName);
164 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
165 First->ReadModuleMapFile(ModuleMapPath);
166 Second->ReadModuleMapFile(ModuleMapPath);
169 bool
170 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
171 bool Complain,
172 bool AllowCompatibleDifferences) {
173 return First->ReadLanguageOptions(LangOpts, Complain,
174 AllowCompatibleDifferences) ||
175 Second->ReadLanguageOptions(LangOpts, Complain,
176 AllowCompatibleDifferences);
179 bool ChainedASTReaderListener::ReadTargetOptions(
180 const TargetOptions &TargetOpts, bool Complain,
181 bool AllowCompatibleDifferences) {
182 return First->ReadTargetOptions(TargetOpts, Complain,
183 AllowCompatibleDifferences) ||
184 Second->ReadTargetOptions(TargetOpts, Complain,
185 AllowCompatibleDifferences);
188 bool ChainedASTReaderListener::ReadDiagnosticOptions(
189 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
190 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
191 Second->ReadDiagnosticOptions(DiagOpts, Complain);
194 bool
195 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
196 bool Complain) {
197 return First->ReadFileSystemOptions(FSOpts, Complain) ||
198 Second->ReadFileSystemOptions(FSOpts, Complain);
201 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
202 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
203 bool Complain) {
204 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
205 Complain) ||
206 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
207 Complain);
210 bool ChainedASTReaderListener::ReadPreprocessorOptions(
211 const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
212 std::string &SuggestedPredefines) {
213 return First->ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
214 SuggestedPredefines) ||
215 Second->ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
216 SuggestedPredefines);
219 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
220 unsigned Value) {
221 First->ReadCounter(M, Value);
222 Second->ReadCounter(M, Value);
225 bool ChainedASTReaderListener::needsInputFileVisitation() {
226 return First->needsInputFileVisitation() ||
227 Second->needsInputFileVisitation();
230 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
231 return First->needsSystemInputFileVisitation() ||
232 Second->needsSystemInputFileVisitation();
235 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
236 ModuleKind Kind) {
237 First->visitModuleFile(Filename, Kind);
238 Second->visitModuleFile(Filename, Kind);
241 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
242 bool isSystem,
243 bool isOverridden,
244 bool isExplicitModule) {
245 bool Continue = false;
246 if (First->needsInputFileVisitation() &&
247 (!isSystem || First->needsSystemInputFileVisitation()))
248 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
249 isExplicitModule);
250 if (Second->needsInputFileVisitation() &&
251 (!isSystem || Second->needsSystemInputFileVisitation()))
252 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
253 isExplicitModule);
254 return Continue;
257 void ChainedASTReaderListener::readModuleFileExtension(
258 const ModuleFileExtensionMetadata &Metadata) {
259 First->readModuleFileExtension(Metadata);
260 Second->readModuleFileExtension(Metadata);
263 //===----------------------------------------------------------------------===//
264 // PCH validator implementation
265 //===----------------------------------------------------------------------===//
267 ASTReaderListener::~ASTReaderListener() = default;
269 /// Compare the given set of language options against an existing set of
270 /// language options.
272 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
273 /// \param AllowCompatibleDifferences If true, differences between compatible
274 /// language options will be permitted.
276 /// \returns true if the languagae options mis-match, false otherwise.
277 static bool checkLanguageOptions(const LangOptions &LangOpts,
278 const LangOptions &ExistingLangOpts,
279 DiagnosticsEngine *Diags,
280 bool AllowCompatibleDifferences = true) {
281 #define LANGOPT(Name, Bits, Default, Description) \
282 if (ExistingLangOpts.Name != LangOpts.Name) { \
283 if (Diags) { \
284 if (Bits == 1) \
285 Diags->Report(diag::err_pch_langopt_mismatch) \
286 << Description << LangOpts.Name << ExistingLangOpts.Name; \
287 else \
288 Diags->Report(diag::err_pch_langopt_value_mismatch) \
289 << Description; \
291 return true; \
294 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
295 if (ExistingLangOpts.Name != LangOpts.Name) { \
296 if (Diags) \
297 Diags->Report(diag::err_pch_langopt_value_mismatch) \
298 << Description; \
299 return true; \
302 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
303 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
304 if (Diags) \
305 Diags->Report(diag::err_pch_langopt_value_mismatch) \
306 << Description; \
307 return true; \
310 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
311 if (!AllowCompatibleDifferences) \
312 LANGOPT(Name, Bits, Default, Description)
314 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
315 if (!AllowCompatibleDifferences) \
316 ENUM_LANGOPT(Name, Bits, Default, Description)
318 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
319 if (!AllowCompatibleDifferences) \
320 VALUE_LANGOPT(Name, Bits, Default, Description)
322 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
323 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
324 #define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description)
325 #include "clang/Basic/LangOptions.def"
327 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
328 if (Diags)
329 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
330 return true;
333 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
334 if (Diags)
335 Diags->Report(diag::err_pch_langopt_value_mismatch)
336 << "target Objective-C runtime";
337 return true;
340 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
341 LangOpts.CommentOpts.BlockCommandNames) {
342 if (Diags)
343 Diags->Report(diag::err_pch_langopt_value_mismatch)
344 << "block command names";
345 return true;
348 // Sanitizer feature mismatches are treated as compatible differences. If
349 // compatible differences aren't allowed, we still only want to check for
350 // mismatches of non-modular sanitizers (the only ones which can affect AST
351 // generation).
352 if (!AllowCompatibleDifferences) {
353 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
354 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
355 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
356 ExistingSanitizers.clear(ModularSanitizers);
357 ImportedSanitizers.clear(ModularSanitizers);
358 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
359 const std::string Flag = "-fsanitize=";
360 if (Diags) {
361 #define SANITIZER(NAME, ID) \
363 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
364 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
365 if (InExistingModule != InImportedModule) \
366 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
367 << InExistingModule << (Flag + NAME); \
369 #include "clang/Basic/Sanitizers.def"
371 return true;
375 return false;
378 /// Compare the given set of target options against an existing set of
379 /// target options.
381 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
383 /// \returns true if the target options mis-match, false otherwise.
384 static bool checkTargetOptions(const TargetOptions &TargetOpts,
385 const TargetOptions &ExistingTargetOpts,
386 DiagnosticsEngine *Diags,
387 bool AllowCompatibleDifferences = true) {
388 #define CHECK_TARGET_OPT(Field, Name) \
389 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
390 if (Diags) \
391 Diags->Report(diag::err_pch_targetopt_mismatch) \
392 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
393 return true; \
396 // The triple and ABI must match exactly.
397 CHECK_TARGET_OPT(Triple, "target");
398 CHECK_TARGET_OPT(ABI, "target ABI");
400 // We can tolerate different CPUs in many cases, notably when one CPU
401 // supports a strict superset of another. When allowing compatible
402 // differences skip this check.
403 if (!AllowCompatibleDifferences) {
404 CHECK_TARGET_OPT(CPU, "target CPU");
405 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
408 #undef CHECK_TARGET_OPT
410 // Compare feature sets.
411 SmallVector<StringRef, 4> ExistingFeatures(
412 ExistingTargetOpts.FeaturesAsWritten.begin(),
413 ExistingTargetOpts.FeaturesAsWritten.end());
414 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
415 TargetOpts.FeaturesAsWritten.end());
416 llvm::sort(ExistingFeatures);
417 llvm::sort(ReadFeatures);
419 // We compute the set difference in both directions explicitly so that we can
420 // diagnose the differences differently.
421 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
422 std::set_difference(
423 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
424 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
425 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
426 ExistingFeatures.begin(), ExistingFeatures.end(),
427 std::back_inserter(UnmatchedReadFeatures));
429 // If we are allowing compatible differences and the read feature set is
430 // a strict subset of the existing feature set, there is nothing to diagnose.
431 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
432 return false;
434 if (Diags) {
435 for (StringRef Feature : UnmatchedReadFeatures)
436 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
437 << /* is-existing-feature */ false << Feature;
438 for (StringRef Feature : UnmatchedExistingFeatures)
439 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
440 << /* is-existing-feature */ true << Feature;
443 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
446 bool
447 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
448 bool Complain,
449 bool AllowCompatibleDifferences) {
450 const LangOptions &ExistingLangOpts = PP.getLangOpts();
451 return checkLanguageOptions(LangOpts, ExistingLangOpts,
452 Complain ? &Reader.Diags : nullptr,
453 AllowCompatibleDifferences);
456 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
457 bool Complain,
458 bool AllowCompatibleDifferences) {
459 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
460 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
461 Complain ? &Reader.Diags : nullptr,
462 AllowCompatibleDifferences);
465 namespace {
467 using MacroDefinitionsMap =
468 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
469 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
471 } // namespace
473 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
474 DiagnosticsEngine &Diags,
475 bool Complain) {
476 using Level = DiagnosticsEngine::Level;
478 // Check current mappings for new -Werror mappings, and the stored mappings
479 // for cases that were explicitly mapped to *not* be errors that are now
480 // errors because of options like -Werror.
481 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
483 for (DiagnosticsEngine *MappingSource : MappingSources) {
484 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
485 diag::kind DiagID = DiagIDMappingPair.first;
486 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
487 if (CurLevel < DiagnosticsEngine::Error)
488 continue; // not significant
489 Level StoredLevel =
490 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
491 if (StoredLevel < DiagnosticsEngine::Error) {
492 if (Complain)
493 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
494 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
495 return true;
500 return false;
503 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
504 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
505 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
506 return true;
507 return Ext >= diag::Severity::Error;
510 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
511 DiagnosticsEngine &Diags, bool IsSystem,
512 bool SystemHeaderWarningsInModule,
513 bool Complain) {
514 // Top-level options
515 if (IsSystem) {
516 if (Diags.getSuppressSystemWarnings())
517 return false;
518 // If -Wsystem-headers was not enabled before, and it was not explicit,
519 // be conservative
520 if (StoredDiags.getSuppressSystemWarnings() &&
521 !SystemHeaderWarningsInModule) {
522 if (Complain)
523 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
524 return true;
528 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
529 if (Complain)
530 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
531 return true;
534 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
535 !StoredDiags.getEnableAllWarnings()) {
536 if (Complain)
537 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
538 return true;
541 if (isExtHandlingFromDiagsError(Diags) &&
542 !isExtHandlingFromDiagsError(StoredDiags)) {
543 if (Complain)
544 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
545 return true;
548 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
551 /// Return the top import module if it is implicit, nullptr otherwise.
552 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
553 Preprocessor &PP) {
554 // If the original import came from a file explicitly generated by the user,
555 // don't check the diagnostic mappings.
556 // FIXME: currently this is approximated by checking whether this is not a
557 // module import of an implicitly-loaded module file.
558 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
559 // the transitive closure of its imports, since unrelated modules cannot be
560 // imported until after this module finishes validation.
561 ModuleFile *TopImport = &*ModuleMgr.rbegin();
562 while (!TopImport->ImportedBy.empty())
563 TopImport = TopImport->ImportedBy[0];
564 if (TopImport->Kind != MK_ImplicitModule)
565 return nullptr;
567 StringRef ModuleName = TopImport->ModuleName;
568 assert(!ModuleName.empty() && "diagnostic options read before module name");
570 Module *M =
571 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
572 assert(M && "missing module");
573 return M;
576 bool PCHValidator::ReadDiagnosticOptions(
577 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
578 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
579 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
580 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
581 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
582 // This should never fail, because we would have processed these options
583 // before writing them to an ASTFile.
584 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
586 ModuleManager &ModuleMgr = Reader.getModuleManager();
587 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
589 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
590 if (!TopM)
591 return false;
593 Module *Importer = PP.getCurrentModule();
595 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
596 bool SystemHeaderWarningsInModule =
597 Importer && llvm::is_contained(ExistingOpts.SystemHeaderWarningsModules,
598 Importer->Name);
600 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
601 // contains the union of their flags.
602 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
603 SystemHeaderWarningsInModule, Complain);
606 /// Collect the macro definitions provided by the given preprocessor
607 /// options.
608 static void
609 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
610 MacroDefinitionsMap &Macros,
611 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
612 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
613 StringRef Macro = PPOpts.Macros[I].first;
614 bool IsUndef = PPOpts.Macros[I].second;
616 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
617 StringRef MacroName = MacroPair.first;
618 StringRef MacroBody = MacroPair.second;
620 // For an #undef'd macro, we only care about the name.
621 if (IsUndef) {
622 if (MacroNames && !Macros.count(MacroName))
623 MacroNames->push_back(MacroName);
625 Macros[MacroName] = std::make_pair("", true);
626 continue;
629 // For a #define'd macro, figure out the actual definition.
630 if (MacroName.size() == Macro.size())
631 MacroBody = "1";
632 else {
633 // Note: GCC drops anything following an end-of-line character.
634 StringRef::size_type End = MacroBody.find_first_of("\n\r");
635 MacroBody = MacroBody.substr(0, End);
638 if (MacroNames && !Macros.count(MacroName))
639 MacroNames->push_back(MacroName);
640 Macros[MacroName] = std::make_pair(MacroBody, false);
644 enum OptionValidation {
645 OptionValidateNone,
646 OptionValidateContradictions,
647 OptionValidateStrictMatches,
650 /// Check the preprocessor options deserialized from the control block
651 /// against the preprocessor options in an existing preprocessor.
653 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
654 /// \param Validation If set to OptionValidateNone, ignore differences in
655 /// preprocessor options. If set to OptionValidateContradictions,
656 /// require that options passed both in the AST file and on the command
657 /// line (-D or -U) match, but tolerate options missing in one or the
658 /// other. If set to OptionValidateContradictions, require that there
659 /// are no differences in the options between the two.
660 static bool checkPreprocessorOptions(
661 const PreprocessorOptions &PPOpts,
662 const PreprocessorOptions &ExistingPPOpts, bool ReadMacros,
663 DiagnosticsEngine *Diags, FileManager &FileMgr,
664 std::string &SuggestedPredefines, const LangOptions &LangOpts,
665 OptionValidation Validation = OptionValidateContradictions) {
666 if (ReadMacros) {
667 // Check macro definitions.
668 MacroDefinitionsMap ASTFileMacros;
669 collectMacroDefinitions(PPOpts, ASTFileMacros);
670 MacroDefinitionsMap ExistingMacros;
671 SmallVector<StringRef, 4> ExistingMacroNames;
672 collectMacroDefinitions(ExistingPPOpts, ExistingMacros,
673 &ExistingMacroNames);
675 // Use a line marker to enter the <command line> file, as the defines and
676 // undefines here will have come from the command line.
677 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
679 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
680 // Dig out the macro definition in the existing preprocessor options.
681 StringRef MacroName = ExistingMacroNames[I];
682 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
684 // Check whether we know anything about this macro name or not.
685 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
686 ASTFileMacros.find(MacroName);
687 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
688 if (Validation == OptionValidateStrictMatches) {
689 // If strict matches are requested, don't tolerate any extra defines
690 // on the command line that are missing in the AST file.
691 if (Diags) {
692 Diags->Report(diag::err_pch_macro_def_undef) << MacroName << true;
694 return true;
696 // FIXME: Check whether this identifier was referenced anywhere in the
697 // AST file. If so, we should reject the AST file. Unfortunately, this
698 // information isn't in the control block. What shall we do about it?
700 if (Existing.second) {
701 SuggestedPredefines += "#undef ";
702 SuggestedPredefines += MacroName.str();
703 SuggestedPredefines += '\n';
704 } else {
705 SuggestedPredefines += "#define ";
706 SuggestedPredefines += MacroName.str();
707 SuggestedPredefines += ' ';
708 SuggestedPredefines += Existing.first.str();
709 SuggestedPredefines += '\n';
711 continue;
714 // If the macro was defined in one but undef'd in the other, we have a
715 // conflict.
716 if (Existing.second != Known->second.second) {
717 if (Diags) {
718 Diags->Report(diag::err_pch_macro_def_undef)
719 << MacroName << Known->second.second;
721 return true;
724 // If the macro was #undef'd in both, or if the macro bodies are
725 // identical, it's fine.
726 if (Existing.second || Existing.first == Known->second.first) {
727 ASTFileMacros.erase(Known);
728 continue;
731 // The macro bodies differ; complain.
732 if (Diags) {
733 Diags->Report(diag::err_pch_macro_def_conflict)
734 << MacroName << Known->second.first << Existing.first;
736 return true;
739 // Leave the <command line> file and return to <built-in>.
740 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
742 if (Validation == OptionValidateStrictMatches) {
743 // If strict matches are requested, don't tolerate any extra defines in
744 // the AST file that are missing on the command line.
745 for (const auto &MacroName : ASTFileMacros.keys()) {
746 if (Diags) {
747 Diags->Report(diag::err_pch_macro_def_undef) << MacroName << false;
749 return true;
754 // Check whether we're using predefines.
755 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
756 Validation != OptionValidateNone) {
757 if (Diags) {
758 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
760 return true;
763 // Detailed record is important since it is used for the module cache hash.
764 if (LangOpts.Modules &&
765 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
766 Validation != OptionValidateNone) {
767 if (Diags) {
768 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
770 return true;
773 // Compute the #include and #include_macros lines we need.
774 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
775 StringRef File = ExistingPPOpts.Includes[I];
777 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
778 !ExistingPPOpts.PCHThroughHeader.empty()) {
779 // In case the through header is an include, we must add all the includes
780 // to the predefines so the start point can be determined.
781 SuggestedPredefines += "#include \"";
782 SuggestedPredefines += File;
783 SuggestedPredefines += "\"\n";
784 continue;
787 if (File == ExistingPPOpts.ImplicitPCHInclude)
788 continue;
790 if (llvm::is_contained(PPOpts.Includes, File))
791 continue;
793 SuggestedPredefines += "#include \"";
794 SuggestedPredefines += File;
795 SuggestedPredefines += "\"\n";
798 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
799 StringRef File = ExistingPPOpts.MacroIncludes[I];
800 if (llvm::is_contained(PPOpts.MacroIncludes, File))
801 continue;
803 SuggestedPredefines += "#__include_macros \"";
804 SuggestedPredefines += File;
805 SuggestedPredefines += "\"\n##\n";
808 return false;
811 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
812 bool ReadMacros, bool Complain,
813 std::string &SuggestedPredefines) {
814 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
816 return checkPreprocessorOptions(
817 PPOpts, ExistingPPOpts, ReadMacros, Complain ? &Reader.Diags : nullptr,
818 PP.getFileManager(), SuggestedPredefines, PP.getLangOpts());
821 bool SimpleASTReaderListener::ReadPreprocessorOptions(
822 const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
823 std::string &SuggestedPredefines) {
824 return checkPreprocessorOptions(PPOpts, PP.getPreprocessorOpts(), ReadMacros,
825 nullptr, PP.getFileManager(),
826 SuggestedPredefines, PP.getLangOpts(),
827 OptionValidateNone);
830 /// Check the header search options deserialized from the control block
831 /// against the header search options in an existing preprocessor.
833 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
834 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
835 StringRef SpecificModuleCachePath,
836 StringRef ExistingModuleCachePath,
837 DiagnosticsEngine *Diags,
838 const LangOptions &LangOpts,
839 const PreprocessorOptions &PPOpts) {
840 if (LangOpts.Modules) {
841 if (SpecificModuleCachePath != ExistingModuleCachePath &&
842 !PPOpts.AllowPCHWithDifferentModulesCachePath) {
843 if (Diags)
844 Diags->Report(diag::err_pch_modulecache_mismatch)
845 << SpecificModuleCachePath << ExistingModuleCachePath;
846 return true;
850 return false;
853 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
854 StringRef SpecificModuleCachePath,
855 bool Complain) {
856 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
857 PP.getHeaderSearchInfo().getModuleCachePath(),
858 Complain ? &Reader.Diags : nullptr,
859 PP.getLangOpts(), PP.getPreprocessorOpts());
862 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
863 PP.setCounterValue(Value);
866 //===----------------------------------------------------------------------===//
867 // AST reader implementation
868 //===----------------------------------------------------------------------===//
870 static uint64_t readULEB(const unsigned char *&P) {
871 unsigned Length = 0;
872 const char *Error = nullptr;
874 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
875 if (Error)
876 llvm::report_fatal_error(Error);
877 P += Length;
878 return Val;
881 /// Read ULEB-encoded key length and data length.
882 static std::pair<unsigned, unsigned>
883 readULEBKeyDataLength(const unsigned char *&P) {
884 unsigned KeyLen = readULEB(P);
885 if ((unsigned)KeyLen != KeyLen)
886 llvm::report_fatal_error("key too large");
888 unsigned DataLen = readULEB(P);
889 if ((unsigned)DataLen != DataLen)
890 llvm::report_fatal_error("data too large");
892 return std::make_pair(KeyLen, DataLen);
895 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
896 bool TakeOwnership) {
897 DeserializationListener = Listener;
898 OwnsDeserializationListener = TakeOwnership;
901 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
902 return serialization::ComputeHash(Sel);
905 std::pair<unsigned, unsigned>
906 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
907 return readULEBKeyDataLength(d);
910 ASTSelectorLookupTrait::internal_key_type
911 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
912 using namespace llvm::support;
914 SelectorTable &SelTable = Reader.getContext().Selectors;
915 unsigned N =
916 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
917 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
918 F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
919 if (N == 0)
920 return SelTable.getNullarySelector(FirstII);
921 else if (N == 1)
922 return SelTable.getUnarySelector(FirstII);
924 SmallVector<IdentifierInfo *, 16> Args;
925 Args.push_back(FirstII);
926 for (unsigned I = 1; I != N; ++I)
927 Args.push_back(Reader.getLocalIdentifier(
928 F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)));
930 return SelTable.getSelector(N, Args.data());
933 ASTSelectorLookupTrait::data_type
934 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
935 unsigned DataLen) {
936 using namespace llvm::support;
938 data_type Result;
940 Result.ID = Reader.getGlobalSelectorID(
941 F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
942 unsigned FullInstanceBits =
943 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
944 unsigned FullFactoryBits =
945 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
946 Result.InstanceBits = FullInstanceBits & 0x3;
947 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
948 Result.FactoryBits = FullFactoryBits & 0x3;
949 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
950 unsigned NumInstanceMethods = FullInstanceBits >> 3;
951 unsigned NumFactoryMethods = FullFactoryBits >> 3;
953 // Load instance methods
954 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
955 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
957 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)))
958 Result.Instance.push_back(Method);
961 // Load factory methods
962 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
963 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
965 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)))
966 Result.Factory.push_back(Method);
969 return Result;
972 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
973 return llvm::djbHash(a);
976 std::pair<unsigned, unsigned>
977 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
978 return readULEBKeyDataLength(d);
981 ASTIdentifierLookupTraitBase::internal_key_type
982 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
983 assert(n >= 2 && d[n-1] == '\0');
984 return StringRef((const char*) d, n-1);
987 /// Whether the given identifier is "interesting".
988 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
989 bool IsModule) {
990 return II.hadMacroDefinition() || II.isPoisoned() ||
991 (!IsModule && II.getObjCOrBuiltinID()) ||
992 II.hasRevertedTokenIDToIdentifier() ||
993 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
994 II.getFETokenInfo());
997 static bool readBit(unsigned &Bits) {
998 bool Value = Bits & 0x1;
999 Bits >>= 1;
1000 return Value;
1003 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1004 using namespace llvm::support;
1006 unsigned RawID =
1007 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1008 return Reader.getGlobalIdentifierID(F, RawID >> 1);
1011 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
1012 if (!II.isFromAST()) {
1013 II.setIsFromAST();
1014 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1015 if (isInterestingIdentifier(Reader, II, IsModule))
1016 II.setChangedSinceDeserialization();
1020 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1021 const unsigned char* d,
1022 unsigned DataLen) {
1023 using namespace llvm::support;
1025 unsigned RawID =
1026 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1027 bool IsInteresting = RawID & 0x01;
1029 // Wipe out the "is interesting" bit.
1030 RawID = RawID >> 1;
1032 // Build the IdentifierInfo and link the identifier ID with it.
1033 IdentifierInfo *II = KnownII;
1034 if (!II) {
1035 II = &Reader.getIdentifierTable().getOwn(k);
1036 KnownII = II;
1038 markIdentifierFromAST(Reader, *II);
1039 Reader.markIdentifierUpToDate(II);
1041 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
1042 if (!IsInteresting) {
1043 // For uninteresting identifiers, there's nothing else to do. Just notify
1044 // the reader that we've finished loading this identifier.
1045 Reader.SetIdentifierInfo(ID, II);
1046 return II;
1049 unsigned ObjCOrBuiltinID =
1050 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
1051 unsigned Bits =
1052 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
1053 bool CPlusPlusOperatorKeyword = readBit(Bits);
1054 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1055 bool Poisoned = readBit(Bits);
1056 bool ExtensionToken = readBit(Bits);
1057 bool HadMacroDefinition = readBit(Bits);
1059 assert(Bits == 0 && "Extra bits in the identifier?");
1060 DataLen -= 8;
1062 // Set or check the various bits in the IdentifierInfo structure.
1063 // Token IDs are read-only.
1064 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1065 II->revertTokenIDToIdentifier();
1066 if (!F.isModule())
1067 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1068 assert(II->isExtensionToken() == ExtensionToken &&
1069 "Incorrect extension token flag");
1070 (void)ExtensionToken;
1071 if (Poisoned)
1072 II->setIsPoisoned(true);
1073 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1074 "Incorrect C++ operator keyword flag");
1075 (void)CPlusPlusOperatorKeyword;
1077 // If this identifier is a macro, deserialize the macro
1078 // definition.
1079 if (HadMacroDefinition) {
1080 uint32_t MacroDirectivesOffset =
1081 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1082 DataLen -= 4;
1084 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1087 Reader.SetIdentifierInfo(ID, II);
1089 // Read all of the declarations visible at global scope with this
1090 // name.
1091 if (DataLen > 0) {
1092 SmallVector<uint32_t, 4> DeclIDs;
1093 for (; DataLen > 0; DataLen -= 4)
1094 DeclIDs.push_back(Reader.getGlobalDeclID(
1096 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)));
1097 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1100 return II;
1103 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1104 : Kind(Name.getNameKind()) {
1105 switch (Kind) {
1106 case DeclarationName::Identifier:
1107 Data = (uint64_t)Name.getAsIdentifierInfo();
1108 break;
1109 case DeclarationName::ObjCZeroArgSelector:
1110 case DeclarationName::ObjCOneArgSelector:
1111 case DeclarationName::ObjCMultiArgSelector:
1112 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1113 break;
1114 case DeclarationName::CXXOperatorName:
1115 Data = Name.getCXXOverloadedOperator();
1116 break;
1117 case DeclarationName::CXXLiteralOperatorName:
1118 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1119 break;
1120 case DeclarationName::CXXDeductionGuideName:
1121 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1122 ->getDeclName().getAsIdentifierInfo();
1123 break;
1124 case DeclarationName::CXXConstructorName:
1125 case DeclarationName::CXXDestructorName:
1126 case DeclarationName::CXXConversionFunctionName:
1127 case DeclarationName::CXXUsingDirective:
1128 Data = 0;
1129 break;
1133 unsigned DeclarationNameKey::getHash() const {
1134 llvm::FoldingSetNodeID ID;
1135 ID.AddInteger(Kind);
1137 switch (Kind) {
1138 case DeclarationName::Identifier:
1139 case DeclarationName::CXXLiteralOperatorName:
1140 case DeclarationName::CXXDeductionGuideName:
1141 ID.AddString(((IdentifierInfo*)Data)->getName());
1142 break;
1143 case DeclarationName::ObjCZeroArgSelector:
1144 case DeclarationName::ObjCOneArgSelector:
1145 case DeclarationName::ObjCMultiArgSelector:
1146 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1147 break;
1148 case DeclarationName::CXXOperatorName:
1149 ID.AddInteger((OverloadedOperatorKind)Data);
1150 break;
1151 case DeclarationName::CXXConstructorName:
1152 case DeclarationName::CXXDestructorName:
1153 case DeclarationName::CXXConversionFunctionName:
1154 case DeclarationName::CXXUsingDirective:
1155 break;
1158 return ID.ComputeHash();
1161 ModuleFile *
1162 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1163 using namespace llvm::support;
1165 uint32_t ModuleFileID =
1166 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1167 return Reader.getLocalModuleFile(F, ModuleFileID);
1170 std::pair<unsigned, unsigned>
1171 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1172 return readULEBKeyDataLength(d);
1175 ASTDeclContextNameLookupTrait::internal_key_type
1176 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1177 using namespace llvm::support;
1179 auto Kind = (DeclarationName::NameKind)*d++;
1180 uint64_t Data;
1181 switch (Kind) {
1182 case DeclarationName::Identifier:
1183 case DeclarationName::CXXLiteralOperatorName:
1184 case DeclarationName::CXXDeductionGuideName:
1185 Data = (uint64_t)Reader.getLocalIdentifier(
1186 F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
1187 break;
1188 case DeclarationName::ObjCZeroArgSelector:
1189 case DeclarationName::ObjCOneArgSelector:
1190 case DeclarationName::ObjCMultiArgSelector:
1191 Data =
1192 (uint64_t)Reader
1193 .getLocalSelector(
1195 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(
1197 .getAsOpaquePtr();
1198 break;
1199 case DeclarationName::CXXOperatorName:
1200 Data = *d++; // OverloadedOperatorKind
1201 break;
1202 case DeclarationName::CXXConstructorName:
1203 case DeclarationName::CXXDestructorName:
1204 case DeclarationName::CXXConversionFunctionName:
1205 case DeclarationName::CXXUsingDirective:
1206 Data = 0;
1207 break;
1210 return DeclarationNameKey(Kind, Data);
1213 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1214 const unsigned char *d,
1215 unsigned DataLen,
1216 data_type_builder &Val) {
1217 using namespace llvm::support;
1219 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1220 uint32_t LocalID =
1221 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1222 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1226 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1227 BitstreamCursor &Cursor,
1228 uint64_t Offset,
1229 DeclContext *DC) {
1230 assert(Offset != 0);
1232 SavedStreamPosition SavedPosition(Cursor);
1233 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1234 Error(std::move(Err));
1235 return true;
1238 RecordData Record;
1239 StringRef Blob;
1240 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1241 if (!MaybeCode) {
1242 Error(MaybeCode.takeError());
1243 return true;
1245 unsigned Code = MaybeCode.get();
1247 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1248 if (!MaybeRecCode) {
1249 Error(MaybeRecCode.takeError());
1250 return true;
1252 unsigned RecCode = MaybeRecCode.get();
1253 if (RecCode != DECL_CONTEXT_LEXICAL) {
1254 Error("Expected lexical block");
1255 return true;
1258 assert(!isa<TranslationUnitDecl>(DC) &&
1259 "expected a TU_UPDATE_LEXICAL record for TU");
1260 // If we are handling a C++ class template instantiation, we can see multiple
1261 // lexical updates for the same record. It's important that we select only one
1262 // of them, so that field numbering works properly. Just pick the first one we
1263 // see.
1264 auto &Lex = LexicalDecls[DC];
1265 if (!Lex.first) {
1266 Lex = std::make_pair(
1267 &M, llvm::ArrayRef(
1268 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1269 Blob.data()),
1270 Blob.size() / 4));
1272 DC->setHasExternalLexicalStorage(true);
1273 return false;
1276 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1277 BitstreamCursor &Cursor,
1278 uint64_t Offset,
1279 DeclID ID) {
1280 assert(Offset != 0);
1282 SavedStreamPosition SavedPosition(Cursor);
1283 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1284 Error(std::move(Err));
1285 return true;
1288 RecordData Record;
1289 StringRef Blob;
1290 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1291 if (!MaybeCode) {
1292 Error(MaybeCode.takeError());
1293 return true;
1295 unsigned Code = MaybeCode.get();
1297 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1298 if (!MaybeRecCode) {
1299 Error(MaybeRecCode.takeError());
1300 return true;
1302 unsigned RecCode = MaybeRecCode.get();
1303 if (RecCode != DECL_CONTEXT_VISIBLE) {
1304 Error("Expected visible lookup table block");
1305 return true;
1308 // We can't safely determine the primary context yet, so delay attaching the
1309 // lookup table until we're done with recursive deserialization.
1310 auto *Data = (const unsigned char*)Blob.data();
1311 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1312 return false;
1315 void ASTReader::Error(StringRef Msg) const {
1316 Error(diag::err_fe_pch_malformed, Msg);
1317 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1318 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1319 Diag(diag::note_module_cache_path)
1320 << PP.getHeaderSearchInfo().getModuleCachePath();
1324 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1325 StringRef Arg3) const {
1326 if (Diags.isDiagnosticInFlight())
1327 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1328 else
1329 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1332 void ASTReader::Error(llvm::Error &&Err) const {
1333 llvm::Error RemainingErr =
1334 handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1335 auto Diag = E.getDiagnostic().second;
1337 // Ideally we'd just emit it, but have to handle a possible in-flight
1338 // diagnostic. Note that the location is currently ignored as well.
1339 auto NumArgs = Diag.getStorage()->NumDiagArgs;
1340 assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1341 StringRef Arg1, Arg2, Arg3;
1342 switch (NumArgs) {
1343 case 3:
1344 Arg3 = Diag.getStringArg(2);
1345 [[fallthrough]];
1346 case 2:
1347 Arg2 = Diag.getStringArg(1);
1348 [[fallthrough]];
1349 case 1:
1350 Arg1 = Diag.getStringArg(0);
1352 Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1354 if (RemainingErr)
1355 Error(toString(std::move(RemainingErr)));
1358 //===----------------------------------------------------------------------===//
1359 // Source Manager Deserialization
1360 //===----------------------------------------------------------------------===//
1362 /// Read the line table in the source manager block.
1363 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1364 unsigned Idx = 0;
1365 LineTableInfo &LineTable = SourceMgr.getLineTable();
1367 // Parse the file names
1368 std::map<int, int> FileIDs;
1369 FileIDs[-1] = -1; // For unspecified filenames.
1370 for (unsigned I = 0; Record[Idx]; ++I) {
1371 // Extract the file name
1372 auto Filename = ReadPath(F, Record, Idx);
1373 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1375 ++Idx;
1377 // Parse the line entries
1378 std::vector<LineEntry> Entries;
1379 while (Idx < Record.size()) {
1380 FileID FID = ReadFileID(F, Record, Idx);
1382 // Extract the line entries
1383 unsigned NumEntries = Record[Idx++];
1384 assert(NumEntries && "no line entries for file ID");
1385 Entries.clear();
1386 Entries.reserve(NumEntries);
1387 for (unsigned I = 0; I != NumEntries; ++I) {
1388 unsigned FileOffset = Record[Idx++];
1389 unsigned LineNo = Record[Idx++];
1390 int FilenameID = FileIDs[Record[Idx++]];
1391 SrcMgr::CharacteristicKind FileKind
1392 = (SrcMgr::CharacteristicKind)Record[Idx++];
1393 unsigned IncludeOffset = Record[Idx++];
1394 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1395 FileKind, IncludeOffset));
1397 LineTable.AddEntry(FID, Entries);
1401 /// Read a source manager block
1402 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1403 using namespace SrcMgr;
1405 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1407 // Set the source-location entry cursor to the current position in
1408 // the stream. This cursor will be used to read the contents of the
1409 // source manager block initially, and then lazily read
1410 // source-location entries as needed.
1411 SLocEntryCursor = F.Stream;
1413 // The stream itself is going to skip over the source manager block.
1414 if (llvm::Error Err = F.Stream.SkipBlock())
1415 return Err;
1417 // Enter the source manager block.
1418 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1419 return Err;
1420 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1422 RecordData Record;
1423 while (true) {
1424 Expected<llvm::BitstreamEntry> MaybeE =
1425 SLocEntryCursor.advanceSkippingSubblocks();
1426 if (!MaybeE)
1427 return MaybeE.takeError();
1428 llvm::BitstreamEntry E = MaybeE.get();
1430 switch (E.Kind) {
1431 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1432 case llvm::BitstreamEntry::Error:
1433 return llvm::createStringError(std::errc::illegal_byte_sequence,
1434 "malformed block record in AST file");
1435 case llvm::BitstreamEntry::EndBlock:
1436 return llvm::Error::success();
1437 case llvm::BitstreamEntry::Record:
1438 // The interesting case.
1439 break;
1442 // Read a record.
1443 Record.clear();
1444 StringRef Blob;
1445 Expected<unsigned> MaybeRecord =
1446 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1447 if (!MaybeRecord)
1448 return MaybeRecord.takeError();
1449 switch (MaybeRecord.get()) {
1450 default: // Default behavior: ignore.
1451 break;
1453 case SM_SLOC_FILE_ENTRY:
1454 case SM_SLOC_BUFFER_ENTRY:
1455 case SM_SLOC_EXPANSION_ENTRY:
1456 // Once we hit one of the source location entries, we're done.
1457 return llvm::Error::success();
1462 llvm::Expected<SourceLocation::UIntTy>
1463 ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1464 BitstreamCursor &Cursor = F->SLocEntryCursor;
1465 SavedStreamPosition SavedPosition(Cursor);
1466 if (llvm::Error Err = Cursor.JumpToBit(F->SLocEntryOffsetsBase +
1467 F->SLocEntryOffsets[Index]))
1468 return std::move(Err);
1470 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1471 if (!MaybeEntry)
1472 return MaybeEntry.takeError();
1474 llvm::BitstreamEntry Entry = MaybeEntry.get();
1475 if (Entry.Kind != llvm::BitstreamEntry::Record)
1476 return llvm::createStringError(
1477 std::errc::illegal_byte_sequence,
1478 "incorrectly-formatted source location entry in AST file");
1480 RecordData Record;
1481 StringRef Blob;
1482 Expected<unsigned> MaybeSLOC = Cursor.readRecord(Entry.ID, Record, &Blob);
1483 if (!MaybeSLOC)
1484 return MaybeSLOC.takeError();
1486 switch (MaybeSLOC.get()) {
1487 default:
1488 return llvm::createStringError(
1489 std::errc::illegal_byte_sequence,
1490 "incorrectly-formatted source location entry in AST file");
1491 case SM_SLOC_FILE_ENTRY:
1492 case SM_SLOC_BUFFER_ENTRY:
1493 case SM_SLOC_EXPANSION_ENTRY:
1494 return F->SLocEntryBaseOffset + Record[0];
1498 int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1499 auto SLocMapI =
1500 GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - SLocOffset - 1);
1501 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1502 "Corrupted global sloc offset map");
1503 ModuleFile *F = SLocMapI->second;
1505 bool Invalid = false;
1507 auto It = llvm::upper_bound(
1508 llvm::index_range(0, F->LocalNumSLocEntries), SLocOffset,
1509 [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1510 int ID = F->SLocEntryBaseID + LocalIndex;
1511 std::size_t Index = -ID - 2;
1512 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1513 assert(!SourceMgr.SLocEntryLoaded[Index]);
1514 auto MaybeEntryOffset = readSLocOffset(F, LocalIndex);
1515 if (!MaybeEntryOffset) {
1516 Error(MaybeEntryOffset.takeError());
1517 Invalid = true;
1518 return true;
1520 SourceMgr.LoadedSLocEntryTable[Index] =
1521 SrcMgr::SLocEntry::getOffsetOnly(*MaybeEntryOffset);
1522 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1524 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1527 if (Invalid)
1528 return 0;
1530 // The iterator points to the first entry with start offset greater than the
1531 // offset of interest. The previous entry must contain the offset of interest.
1532 return F->SLocEntryBaseID + *std::prev(It);
1535 bool ASTReader::ReadSLocEntry(int ID) {
1536 if (ID == 0)
1537 return false;
1539 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1540 Error("source location entry ID out-of-range for AST file");
1541 return true;
1544 // Local helper to read the (possibly-compressed) buffer data following the
1545 // entry record.
1546 auto ReadBuffer = [this](
1547 BitstreamCursor &SLocEntryCursor,
1548 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1549 RecordData Record;
1550 StringRef Blob;
1551 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1552 if (!MaybeCode) {
1553 Error(MaybeCode.takeError());
1554 return nullptr;
1556 unsigned Code = MaybeCode.get();
1558 Expected<unsigned> MaybeRecCode =
1559 SLocEntryCursor.readRecord(Code, Record, &Blob);
1560 if (!MaybeRecCode) {
1561 Error(MaybeRecCode.takeError());
1562 return nullptr;
1564 unsigned RecCode = MaybeRecCode.get();
1566 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1567 // Inspect the first byte to differentiate zlib (\x78) and zstd
1568 // (little-endian 0xFD2FB528).
1569 const llvm::compression::Format F =
1570 Blob.size() > 0 && Blob.data()[0] == 0x78
1571 ? llvm::compression::Format::Zlib
1572 : llvm::compression::Format::Zstd;
1573 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1574 Error(Reason);
1575 return nullptr;
1577 SmallVector<uint8_t, 0> Decompressed;
1578 if (llvm::Error E = llvm::compression::decompress(
1579 F, llvm::arrayRefFromStringRef(Blob), Decompressed, Record[0])) {
1580 Error("could not decompress embedded file contents: " +
1581 llvm::toString(std::move(E)));
1582 return nullptr;
1584 return llvm::MemoryBuffer::getMemBufferCopy(
1585 llvm::toStringRef(Decompressed), Name);
1586 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1587 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1588 } else {
1589 Error("AST record has invalid code");
1590 return nullptr;
1594 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1595 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1596 F->SLocEntryOffsetsBase +
1597 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1598 Error(std::move(Err));
1599 return true;
1602 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1603 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1605 ++NumSLocEntriesRead;
1606 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1607 if (!MaybeEntry) {
1608 Error(MaybeEntry.takeError());
1609 return true;
1611 llvm::BitstreamEntry Entry = MaybeEntry.get();
1613 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1614 Error("incorrectly-formatted source location entry in AST file");
1615 return true;
1618 RecordData Record;
1619 StringRef Blob;
1620 Expected<unsigned> MaybeSLOC =
1621 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1622 if (!MaybeSLOC) {
1623 Error(MaybeSLOC.takeError());
1624 return true;
1626 switch (MaybeSLOC.get()) {
1627 default:
1628 Error("incorrectly-formatted source location entry in AST file");
1629 return true;
1631 case SM_SLOC_FILE_ENTRY: {
1632 // We will detect whether a file changed and return 'Failure' for it, but
1633 // we will also try to fail gracefully by setting up the SLocEntry.
1634 unsigned InputID = Record[4];
1635 InputFile IF = getInputFile(*F, InputID);
1636 OptionalFileEntryRef File = IF.getFile();
1637 bool OverriddenBuffer = IF.isOverridden();
1639 // Note that we only check if a File was returned. If it was out-of-date
1640 // we have complained but we will continue creating a FileID to recover
1641 // gracefully.
1642 if (!File)
1643 return true;
1645 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1646 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1647 // This is the module's main file.
1648 IncludeLoc = getImportLocation(F);
1650 SrcMgr::CharacteristicKind
1651 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1652 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1653 BaseOffset + Record[0]);
1654 SrcMgr::FileInfo &FileInfo =
1655 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1656 FileInfo.NumCreatedFIDs = Record[5];
1657 if (Record[3])
1658 FileInfo.setHasLineDirectives();
1660 unsigned NumFileDecls = Record[7];
1661 if (NumFileDecls && ContextObj) {
1662 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1663 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1664 FileDeclIDs[FID] =
1665 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1668 const SrcMgr::ContentCache &ContentCache =
1669 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1670 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1671 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1672 !ContentCache.getBufferIfLoaded()) {
1673 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1674 if (!Buffer)
1675 return true;
1676 SourceMgr.overrideFileContents(*File, std::move(Buffer));
1679 break;
1682 case SM_SLOC_BUFFER_ENTRY: {
1683 const char *Name = Blob.data();
1684 unsigned Offset = Record[0];
1685 SrcMgr::CharacteristicKind
1686 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1687 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1688 if (IncludeLoc.isInvalid() && F->isModule()) {
1689 IncludeLoc = getImportLocation(F);
1692 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1693 if (!Buffer)
1694 return true;
1695 FileID FID = SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1696 BaseOffset + Offset, IncludeLoc);
1697 if (Record[3]) {
1698 auto &FileInfo =
1699 const_cast<SrcMgr::FileInfo &>(SourceMgr.getSLocEntry(FID).getFile());
1700 FileInfo.setHasLineDirectives();
1702 break;
1705 case SM_SLOC_EXPANSION_ENTRY: {
1706 LocSeq::State Seq;
1707 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq);
1708 SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq);
1709 SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq);
1710 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd,
1711 Record[5], Record[4], ID,
1712 BaseOffset + Record[0]);
1713 break;
1717 return false;
1720 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1721 if (ID == 0)
1722 return std::make_pair(SourceLocation(), "");
1724 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1725 Error("source location entry ID out-of-range for AST file");
1726 return std::make_pair(SourceLocation(), "");
1729 // Find which module file this entry lands in.
1730 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1731 if (!M->isModule())
1732 return std::make_pair(SourceLocation(), "");
1734 // FIXME: Can we map this down to a particular submodule? That would be
1735 // ideal.
1736 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1739 /// Find the location where the module F is imported.
1740 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1741 if (F->ImportLoc.isValid())
1742 return F->ImportLoc;
1744 // Otherwise we have a PCH. It's considered to be "imported" at the first
1745 // location of its includer.
1746 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1747 // Main file is the importer.
1748 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1749 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1751 return F->ImportedBy[0]->FirstLoc;
1754 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1755 /// the abbreviations that are at the top of the block and then leave the cursor
1756 /// pointing into the block.
1757 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1758 unsigned BlockID,
1759 uint64_t *StartOfBlockOffset) {
1760 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1761 return Err;
1763 if (StartOfBlockOffset)
1764 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1766 while (true) {
1767 uint64_t Offset = Cursor.GetCurrentBitNo();
1768 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1769 if (!MaybeCode)
1770 return MaybeCode.takeError();
1771 unsigned Code = MaybeCode.get();
1773 // We expect all abbrevs to be at the start of the block.
1774 if (Code != llvm::bitc::DEFINE_ABBREV) {
1775 if (llvm::Error Err = Cursor.JumpToBit(Offset))
1776 return Err;
1777 return llvm::Error::success();
1779 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1780 return Err;
1784 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1785 unsigned &Idx) {
1786 Token Tok;
1787 Tok.startToken();
1788 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1789 Tok.setKind((tok::TokenKind)Record[Idx++]);
1790 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1792 if (Tok.isAnnotation()) {
1793 Tok.setAnnotationEndLoc(ReadSourceLocation(F, Record, Idx));
1794 switch (Tok.getKind()) {
1795 case tok::annot_pragma_loop_hint: {
1796 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1797 Info->PragmaName = ReadToken(F, Record, Idx);
1798 Info->Option = ReadToken(F, Record, Idx);
1799 unsigned NumTokens = Record[Idx++];
1800 SmallVector<Token, 4> Toks;
1801 Toks.reserve(NumTokens);
1802 for (unsigned I = 0; I < NumTokens; ++I)
1803 Toks.push_back(ReadToken(F, Record, Idx));
1804 Info->Toks = llvm::ArrayRef(Toks).copy(PP.getPreprocessorAllocator());
1805 Tok.setAnnotationValue(static_cast<void *>(Info));
1806 break;
1808 case tok::annot_pragma_pack: {
1809 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
1810 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
1811 auto SlotLabel = ReadString(Record, Idx);
1812 Info->SlotLabel =
1813 llvm::StringRef(SlotLabel).copy(PP.getPreprocessorAllocator());
1814 Info->Alignment = ReadToken(F, Record, Idx);
1815 Tok.setAnnotationValue(static_cast<void *>(Info));
1816 break;
1818 // Some annotation tokens do not use the PtrData field.
1819 case tok::annot_pragma_openmp:
1820 case tok::annot_pragma_openmp_end:
1821 case tok::annot_pragma_unused:
1822 break;
1823 default:
1824 llvm_unreachable("missing deserialization code for annotation token");
1826 } else {
1827 Tok.setLength(Record[Idx++]);
1828 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1829 Tok.setIdentifierInfo(II);
1831 return Tok;
1834 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1835 BitstreamCursor &Stream = F.MacroCursor;
1837 // Keep track of where we are in the stream, then jump back there
1838 // after reading this macro.
1839 SavedStreamPosition SavedPosition(Stream);
1841 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1842 // FIXME this drops errors on the floor.
1843 consumeError(std::move(Err));
1844 return nullptr;
1846 RecordData Record;
1847 SmallVector<IdentifierInfo*, 16> MacroParams;
1848 MacroInfo *Macro = nullptr;
1849 llvm::MutableArrayRef<Token> MacroTokens;
1851 while (true) {
1852 // Advance to the next record, but if we get to the end of the block, don't
1853 // pop it (removing all the abbreviations from the cursor) since we want to
1854 // be able to reseek within the block and read entries.
1855 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1856 Expected<llvm::BitstreamEntry> MaybeEntry =
1857 Stream.advanceSkippingSubblocks(Flags);
1858 if (!MaybeEntry) {
1859 Error(MaybeEntry.takeError());
1860 return Macro;
1862 llvm::BitstreamEntry Entry = MaybeEntry.get();
1864 switch (Entry.Kind) {
1865 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1866 case llvm::BitstreamEntry::Error:
1867 Error("malformed block record in AST file");
1868 return Macro;
1869 case llvm::BitstreamEntry::EndBlock:
1870 return Macro;
1871 case llvm::BitstreamEntry::Record:
1872 // The interesting case.
1873 break;
1876 // Read a record.
1877 Record.clear();
1878 PreprocessorRecordTypes RecType;
1879 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1880 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1881 else {
1882 Error(MaybeRecType.takeError());
1883 return Macro;
1885 switch (RecType) {
1886 case PP_MODULE_MACRO:
1887 case PP_MACRO_DIRECTIVE_HISTORY:
1888 return Macro;
1890 case PP_MACRO_OBJECT_LIKE:
1891 case PP_MACRO_FUNCTION_LIKE: {
1892 // If we already have a macro, that means that we've hit the end
1893 // of the definition of the macro we were looking for. We're
1894 // done.
1895 if (Macro)
1896 return Macro;
1898 unsigned NextIndex = 1; // Skip identifier ID.
1899 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1900 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1901 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1902 MI->setIsUsed(Record[NextIndex++]);
1903 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1904 MacroTokens = MI->allocateTokens(Record[NextIndex++],
1905 PP.getPreprocessorAllocator());
1906 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1907 // Decode function-like macro info.
1908 bool isC99VarArgs = Record[NextIndex++];
1909 bool isGNUVarArgs = Record[NextIndex++];
1910 bool hasCommaPasting = Record[NextIndex++];
1911 MacroParams.clear();
1912 unsigned NumArgs = Record[NextIndex++];
1913 for (unsigned i = 0; i != NumArgs; ++i)
1914 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1916 // Install function-like macro info.
1917 MI->setIsFunctionLike();
1918 if (isC99VarArgs) MI->setIsC99Varargs();
1919 if (isGNUVarArgs) MI->setIsGNUVarargs();
1920 if (hasCommaPasting) MI->setHasCommaPasting();
1921 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1924 // Remember that we saw this macro last so that we add the tokens that
1925 // form its body to it.
1926 Macro = MI;
1928 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1929 Record[NextIndex]) {
1930 // We have a macro definition. Register the association
1931 PreprocessedEntityID
1932 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1933 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1934 PreprocessingRecord::PPEntityID PPID =
1935 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1936 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1937 PPRec.getPreprocessedEntity(PPID));
1938 if (PPDef)
1939 PPRec.RegisterMacroDefinition(Macro, PPDef);
1942 ++NumMacrosRead;
1943 break;
1946 case PP_TOKEN: {
1947 // If we see a TOKEN before a PP_MACRO_*, then the file is
1948 // erroneous, just pretend we didn't see this.
1949 if (!Macro) break;
1950 if (MacroTokens.empty()) {
1951 Error("unexpected number of macro tokens for a macro in AST file");
1952 return Macro;
1955 unsigned Idx = 0;
1956 MacroTokens[0] = ReadToken(F, Record, Idx);
1957 MacroTokens = MacroTokens.drop_front();
1958 break;
1964 PreprocessedEntityID
1965 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1966 unsigned LocalID) const {
1967 if (!M.ModuleOffsetMap.empty())
1968 ReadModuleOffsetMap(M);
1970 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1971 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1972 assert(I != M.PreprocessedEntityRemap.end()
1973 && "Invalid index into preprocessed entity index remap");
1975 return LocalID + I->second;
1978 const FileEntry *HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
1979 FileManager &FileMgr = Reader.getFileManager();
1980 if (!Key.Imported) {
1981 if (auto File = FileMgr.getFile(Key.Filename))
1982 return *File;
1983 return nullptr;
1986 std::string Resolved = std::string(Key.Filename);
1987 Reader.ResolveImportedPath(M, Resolved);
1988 if (auto File = FileMgr.getFile(Resolved))
1989 return *File;
1990 return nullptr;
1993 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1994 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1997 HeaderFileInfoTrait::internal_key_type
1998 HeaderFileInfoTrait::GetInternalKey(external_key_type FE) {
1999 internal_key_type ikey = {FE.getSize(),
2000 M.HasTimestamps ? FE.getModificationTime() : 0,
2001 FE.getName(), /*Imported*/ false};
2002 return ikey;
2005 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2006 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2007 return false;
2009 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
2010 return true;
2012 // Determine whether the actual files are equivalent.
2013 const FileEntry *FEA = getFile(a);
2014 const FileEntry *FEB = getFile(b);
2015 return FEA && FEA == FEB;
2018 std::pair<unsigned, unsigned>
2019 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2020 return readULEBKeyDataLength(d);
2023 HeaderFileInfoTrait::internal_key_type
2024 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2025 using namespace llvm::support;
2027 internal_key_type ikey;
2028 ikey.Size =
2029 off_t(endian::readNext<uint64_t, llvm::endianness::little, unaligned>(d));
2030 ikey.ModTime = time_t(
2031 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(d));
2032 ikey.Filename = (const char *)d;
2033 ikey.Imported = true;
2034 return ikey;
2037 HeaderFileInfoTrait::data_type
2038 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2039 unsigned DataLen) {
2040 using namespace llvm::support;
2042 const unsigned char *End = d + DataLen;
2043 HeaderFileInfo HFI;
2044 unsigned Flags = *d++;
2046 bool Included = (Flags >> 6) & 0x01;
2047 if (Included)
2048 if (const FileEntry *FE = getFile(key))
2049 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2050 // deserialize this header file info again.
2051 Reader.getPreprocessor().getIncludedFiles().insert(FE);
2053 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2054 HFI.isImport |= (Flags >> 5) & 0x01;
2055 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2056 HFI.DirInfo = (Flags >> 1) & 0x07;
2057 HFI.IndexHeaderMapHeader = Flags & 0x01;
2058 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
2059 M, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
2060 if (unsigned FrameworkOffset =
2061 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)) {
2062 // The framework offset is 1 greater than the actual offset,
2063 // since 0 is used as an indicator for "no framework name".
2064 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
2065 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
2068 assert((End - d) % 4 == 0 &&
2069 "Wrong data length in HeaderFileInfo deserialization");
2070 while (d != End) {
2071 uint32_t LocalSMID =
2072 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
2073 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2074 LocalSMID >>= 3;
2076 // This header is part of a module. Associate it with the module to enable
2077 // implicit module import.
2078 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
2079 Module *Mod = Reader.getSubmodule(GlobalSMID);
2080 FileManager &FileMgr = Reader.getFileManager();
2081 ModuleMap &ModMap =
2082 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2084 std::string Filename = std::string(key.Filename);
2085 if (key.Imported)
2086 Reader.ResolveImportedPath(M, Filename);
2087 if (auto FE = FileMgr.getOptionalFileRef(Filename)) {
2088 // FIXME: NameAsWritten
2089 Module::Header H = {std::string(key.Filename), "", *FE};
2090 ModMap.addHeader(Mod, H, HeaderRole, /*Imported=*/true);
2092 HFI.isModuleHeader |= ModuleMap::isModular(HeaderRole);
2095 // This HeaderFileInfo was externally loaded.
2096 HFI.External = true;
2097 HFI.IsValid = true;
2098 return HFI;
2101 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2102 uint32_t MacroDirectivesOffset) {
2103 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2104 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
2107 void ASTReader::ReadDefinedMacros() {
2108 // Note that we are loading defined macros.
2109 Deserializing Macros(this);
2111 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
2112 BitstreamCursor &MacroCursor = I.MacroCursor;
2114 // If there was no preprocessor block, skip this file.
2115 if (MacroCursor.getBitcodeBytes().empty())
2116 continue;
2118 BitstreamCursor Cursor = MacroCursor;
2119 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
2120 Error(std::move(Err));
2121 return;
2124 RecordData Record;
2125 while (true) {
2126 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2127 if (!MaybeE) {
2128 Error(MaybeE.takeError());
2129 return;
2131 llvm::BitstreamEntry E = MaybeE.get();
2133 switch (E.Kind) {
2134 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2135 case llvm::BitstreamEntry::Error:
2136 Error("malformed block record in AST file");
2137 return;
2138 case llvm::BitstreamEntry::EndBlock:
2139 goto NextCursor;
2141 case llvm::BitstreamEntry::Record: {
2142 Record.clear();
2143 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
2144 if (!MaybeRecord) {
2145 Error(MaybeRecord.takeError());
2146 return;
2148 switch (MaybeRecord.get()) {
2149 default: // Default behavior: ignore.
2150 break;
2152 case PP_MACRO_OBJECT_LIKE:
2153 case PP_MACRO_FUNCTION_LIKE: {
2154 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
2155 if (II->isOutOfDate())
2156 updateOutOfDateIdentifier(*II);
2157 break;
2160 case PP_TOKEN:
2161 // Ignore tokens.
2162 break;
2164 break;
2168 NextCursor: ;
2172 namespace {
2174 /// Visitor class used to look up identifirs in an AST file.
2175 class IdentifierLookupVisitor {
2176 StringRef Name;
2177 unsigned NameHash;
2178 unsigned PriorGeneration;
2179 unsigned &NumIdentifierLookups;
2180 unsigned &NumIdentifierLookupHits;
2181 IdentifierInfo *Found = nullptr;
2183 public:
2184 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2185 unsigned &NumIdentifierLookups,
2186 unsigned &NumIdentifierLookupHits)
2187 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2188 PriorGeneration(PriorGeneration),
2189 NumIdentifierLookups(NumIdentifierLookups),
2190 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2192 bool operator()(ModuleFile &M) {
2193 // If we've already searched this module file, skip it now.
2194 if (M.Generation <= PriorGeneration)
2195 return true;
2197 ASTIdentifierLookupTable *IdTable
2198 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2199 if (!IdTable)
2200 return false;
2202 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2203 Found);
2204 ++NumIdentifierLookups;
2205 ASTIdentifierLookupTable::iterator Pos =
2206 IdTable->find_hashed(Name, NameHash, &Trait);
2207 if (Pos == IdTable->end())
2208 return false;
2210 // Dereferencing the iterator has the effect of building the
2211 // IdentifierInfo node and populating it with the various
2212 // declarations it needs.
2213 ++NumIdentifierLookupHits;
2214 Found = *Pos;
2215 return true;
2218 // Retrieve the identifier info found within the module
2219 // files.
2220 IdentifierInfo *getIdentifierInfo() const { return Found; }
2223 } // namespace
2225 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2226 // Note that we are loading an identifier.
2227 Deserializing AnIdentifier(this);
2229 unsigned PriorGeneration = 0;
2230 if (getContext().getLangOpts().Modules)
2231 PriorGeneration = IdentifierGeneration[&II];
2233 // If there is a global index, look there first to determine which modules
2234 // provably do not have any results for this identifier.
2235 GlobalModuleIndex::HitSet Hits;
2236 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2237 if (!loadGlobalIndex()) {
2238 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2239 HitsPtr = &Hits;
2243 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2244 NumIdentifierLookups,
2245 NumIdentifierLookupHits);
2246 ModuleMgr.visit(Visitor, HitsPtr);
2247 markIdentifierUpToDate(&II);
2250 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2251 if (!II)
2252 return;
2254 II->setOutOfDate(false);
2256 // Update the generation for this identifier.
2257 if (getContext().getLangOpts().Modules)
2258 IdentifierGeneration[II] = getGeneration();
2261 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2262 const PendingMacroInfo &PMInfo) {
2263 ModuleFile &M = *PMInfo.M;
2265 BitstreamCursor &Cursor = M.MacroCursor;
2266 SavedStreamPosition SavedPosition(Cursor);
2267 if (llvm::Error Err =
2268 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2269 Error(std::move(Err));
2270 return;
2273 struct ModuleMacroRecord {
2274 SubmoduleID SubModID;
2275 MacroInfo *MI;
2276 SmallVector<SubmoduleID, 8> Overrides;
2278 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2280 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2281 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2282 // macro histroy.
2283 RecordData Record;
2284 while (true) {
2285 Expected<llvm::BitstreamEntry> MaybeEntry =
2286 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2287 if (!MaybeEntry) {
2288 Error(MaybeEntry.takeError());
2289 return;
2291 llvm::BitstreamEntry Entry = MaybeEntry.get();
2293 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2294 Error("malformed block record in AST file");
2295 return;
2298 Record.clear();
2299 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2300 if (!MaybePP) {
2301 Error(MaybePP.takeError());
2302 return;
2304 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2305 case PP_MACRO_DIRECTIVE_HISTORY:
2306 break;
2308 case PP_MODULE_MACRO: {
2309 ModuleMacros.push_back(ModuleMacroRecord());
2310 auto &Info = ModuleMacros.back();
2311 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2312 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2313 for (int I = 2, N = Record.size(); I != N; ++I)
2314 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2315 continue;
2318 default:
2319 Error("malformed block record in AST file");
2320 return;
2323 // We found the macro directive history; that's the last record
2324 // for this macro.
2325 break;
2328 // Module macros are listed in reverse dependency order.
2330 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2331 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2332 for (auto &MMR : ModuleMacros) {
2333 Overrides.clear();
2334 for (unsigned ModID : MMR.Overrides) {
2335 Module *Mod = getSubmodule(ModID);
2336 auto *Macro = PP.getModuleMacro(Mod, II);
2337 assert(Macro && "missing definition for overridden macro");
2338 Overrides.push_back(Macro);
2341 bool Inserted = false;
2342 Module *Owner = getSubmodule(MMR.SubModID);
2343 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2347 // Don't read the directive history for a module; we don't have anywhere
2348 // to put it.
2349 if (M.isModule())
2350 return;
2352 // Deserialize the macro directives history in reverse source-order.
2353 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2354 unsigned Idx = 0, N = Record.size();
2355 while (Idx < N) {
2356 MacroDirective *MD = nullptr;
2357 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2358 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2359 switch (K) {
2360 case MacroDirective::MD_Define: {
2361 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2362 MD = PP.AllocateDefMacroDirective(MI, Loc);
2363 break;
2365 case MacroDirective::MD_Undefine:
2366 MD = PP.AllocateUndefMacroDirective(Loc);
2367 break;
2368 case MacroDirective::MD_Visibility:
2369 bool isPublic = Record[Idx++];
2370 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2371 break;
2374 if (!Latest)
2375 Latest = MD;
2376 if (Earliest)
2377 Earliest->setPrevious(MD);
2378 Earliest = MD;
2381 if (Latest)
2382 PP.setLoadedMacroDirective(II, Earliest, Latest);
2385 bool ASTReader::shouldDisableValidationForFile(
2386 const serialization::ModuleFile &M) const {
2387 if (DisableValidationKind == DisableValidationForModuleKind::None)
2388 return false;
2390 // If a PCH is loaded and validation is disabled for PCH then disable
2391 // validation for the PCH and the modules it loads.
2392 ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);
2394 switch (K) {
2395 case MK_MainFile:
2396 case MK_Preamble:
2397 case MK_PCH:
2398 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2399 case MK_ImplicitModule:
2400 case MK_ExplicitModule:
2401 case MK_PrebuiltModule:
2402 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2405 return false;
2408 InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2409 // If this ID is bogus, just return an empty input file.
2410 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2411 return InputFileInfo();
2413 // If we've already loaded this input file, return it.
2414 if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2415 return F.InputFileInfosLoaded[ID - 1];
2417 // Go find this input file.
2418 BitstreamCursor &Cursor = F.InputFilesCursor;
2419 SavedStreamPosition SavedPosition(Cursor);
2420 if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2421 F.InputFileOffsets[ID - 1])) {
2422 // FIXME this drops errors on the floor.
2423 consumeError(std::move(Err));
2426 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2427 if (!MaybeCode) {
2428 // FIXME this drops errors on the floor.
2429 consumeError(MaybeCode.takeError());
2431 unsigned Code = MaybeCode.get();
2432 RecordData Record;
2433 StringRef Blob;
2435 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2436 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2437 "invalid record type for input file");
2438 else {
2439 // FIXME this drops errors on the floor.
2440 consumeError(Maybe.takeError());
2443 assert(Record[0] == ID && "Bogus stored ID or offset");
2444 InputFileInfo R;
2445 R.StoredSize = static_cast<off_t>(Record[1]);
2446 R.StoredTime = static_cast<time_t>(Record[2]);
2447 R.Overridden = static_cast<bool>(Record[3]);
2448 R.Transient = static_cast<bool>(Record[4]);
2449 R.TopLevel = static_cast<bool>(Record[5]);
2450 R.ModuleMap = static_cast<bool>(Record[6]);
2451 std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2452 uint16_t AsRequestedLength = Record[7];
2454 std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
2455 std::string Name = Blob.substr(AsRequestedLength).str();
2457 ResolveImportedPath(F, NameAsRequested);
2458 ResolveImportedPath(F, Name);
2460 if (Name.empty())
2461 Name = NameAsRequested;
2463 return std::make_pair(std::move(NameAsRequested), std::move(Name));
2464 }();
2466 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2467 if (!MaybeEntry) // FIXME this drops errors on the floor.
2468 consumeError(MaybeEntry.takeError());
2469 llvm::BitstreamEntry Entry = MaybeEntry.get();
2470 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2471 "expected record type for input file hash");
2473 Record.clear();
2474 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2475 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2476 "invalid record type for input file hash");
2477 else {
2478 // FIXME this drops errors on the floor.
2479 consumeError(Maybe.takeError());
2481 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2482 static_cast<uint64_t>(Record[0]);
2484 // Note that we've loaded this input file info.
2485 F.InputFileInfosLoaded[ID - 1] = R;
2486 return R;
2489 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2490 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2491 // If this ID is bogus, just return an empty input file.
2492 if (ID == 0 || ID > F.InputFilesLoaded.size())
2493 return InputFile();
2495 // If we've already loaded this input file, return it.
2496 if (F.InputFilesLoaded[ID-1].getFile())
2497 return F.InputFilesLoaded[ID-1];
2499 if (F.InputFilesLoaded[ID-1].isNotFound())
2500 return InputFile();
2502 // Go find this input file.
2503 BitstreamCursor &Cursor = F.InputFilesCursor;
2504 SavedStreamPosition SavedPosition(Cursor);
2505 if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2506 F.InputFileOffsets[ID - 1])) {
2507 // FIXME this drops errors on the floor.
2508 consumeError(std::move(Err));
2511 InputFileInfo FI = getInputFileInfo(F, ID);
2512 off_t StoredSize = FI.StoredSize;
2513 time_t StoredTime = FI.StoredTime;
2514 bool Overridden = FI.Overridden;
2515 bool Transient = FI.Transient;
2516 StringRef Filename = FI.FilenameAsRequested;
2517 uint64_t StoredContentHash = FI.ContentHash;
2519 // For standard C++ modules, we don't need to check the inputs.
2520 bool SkipChecks = F.StandardCXXModule;
2522 const HeaderSearchOptions &HSOpts =
2523 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2525 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2526 // modules.
2527 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2528 SkipChecks = false;
2529 Overridden = false;
2532 OptionalFileEntryRefDegradesToFileEntryPtr File = OptionalFileEntryRef(
2533 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)));
2535 // For an overridden file, create a virtual file with the stored
2536 // size/timestamp.
2537 if ((Overridden || Transient || SkipChecks) && !File)
2538 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2540 if (!File) {
2541 if (Complain) {
2542 std::string ErrorStr = "could not find file '";
2543 ErrorStr += Filename;
2544 ErrorStr += "' referenced by AST file '";
2545 ErrorStr += F.FileName;
2546 ErrorStr += "'";
2547 Error(ErrorStr);
2549 // Record that we didn't find the file.
2550 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2551 return InputFile();
2554 // Check if there was a request to override the contents of the file
2555 // that was part of the precompiled header. Overriding such a file
2556 // can lead to problems when lexing using the source locations from the
2557 // PCH.
2558 SourceManager &SM = getSourceManager();
2559 // FIXME: Reject if the overrides are different.
2560 if ((!Overridden && !Transient) && !SkipChecks && SM.isFileOverridden(File)) {
2561 if (Complain)
2562 Error(diag::err_fe_pch_file_overridden, Filename);
2564 // After emitting the diagnostic, bypass the overriding file to recover
2565 // (this creates a separate FileEntry).
2566 File = SM.bypassFileContentsOverride(*File);
2567 if (!File) {
2568 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2569 return InputFile();
2573 struct Change {
2574 enum ModificationKind {
2575 Size,
2576 ModTime,
2577 Content,
2578 None,
2579 } Kind;
2580 std::optional<int64_t> Old = std::nullopt;
2581 std::optional<int64_t> New = std::nullopt;
2583 auto HasInputContentChanged = [&](Change OriginalChange) {
2584 assert(ValidateASTInputFilesContent &&
2585 "We should only check the content of the inputs with "
2586 "ValidateASTInputFilesContent enabled.");
2588 if (StoredContentHash == static_cast<uint64_t>(llvm::hash_code(-1)))
2589 return OriginalChange;
2591 auto MemBuffOrError = FileMgr.getBufferForFile(*File);
2592 if (!MemBuffOrError) {
2593 if (!Complain)
2594 return OriginalChange;
2595 std::string ErrorStr = "could not get buffer for file '";
2596 ErrorStr += File->getName();
2597 ErrorStr += "'";
2598 Error(ErrorStr);
2599 return OriginalChange;
2602 // FIXME: hash_value is not guaranteed to be stable!
2603 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2604 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2605 return Change{Change::None};
2607 return Change{Change::Content};
2609 auto HasInputFileChanged = [&]() {
2610 if (StoredSize != File->getSize())
2611 return Change{Change::Size, StoredSize, File->getSize()};
2612 if (!shouldDisableValidationForFile(F) && StoredTime &&
2613 StoredTime != File->getModificationTime()) {
2614 Change MTimeChange = {Change::ModTime, StoredTime,
2615 File->getModificationTime()};
2617 // In case the modification time changes but not the content,
2618 // accept the cached file as legit.
2619 if (ValidateASTInputFilesContent)
2620 return HasInputContentChanged(MTimeChange);
2622 return MTimeChange;
2624 return Change{Change::None};
2627 bool IsOutOfDate = false;
2628 auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged();
2629 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2630 // enabled, it is better to check the contents of the inputs. Since we can't
2631 // get correct modified time information for inputs from overriden inputs.
2632 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2633 F.StandardCXXModule && FileChange.Kind == Change::None)
2634 FileChange = HasInputContentChanged(FileChange);
2636 // For an overridden file, there is nothing to validate.
2637 if (!Overridden && FileChange.Kind != Change::None) {
2638 if (Complain && !Diags.isDiagnosticInFlight()) {
2639 // Build a list of the PCH imports that got us here (in reverse).
2640 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2641 while (!ImportStack.back()->ImportedBy.empty())
2642 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2644 // The top-level PCH is stale.
2645 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2646 Diag(diag::err_fe_ast_file_modified)
2647 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2648 << TopLevelPCHName << FileChange.Kind
2649 << (FileChange.Old && FileChange.New)
2650 << llvm::itostr(FileChange.Old.value_or(0))
2651 << llvm::itostr(FileChange.New.value_or(0));
2653 // Print the import stack.
2654 if (ImportStack.size() > 1) {
2655 Diag(diag::note_pch_required_by)
2656 << Filename << ImportStack[0]->FileName;
2657 for (unsigned I = 1; I < ImportStack.size(); ++I)
2658 Diag(diag::note_pch_required_by)
2659 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2662 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2665 IsOutOfDate = true;
2667 // FIXME: If the file is overridden and we've already opened it,
2668 // issue an error (or split it into a separate FileEntry).
2670 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2672 // Note that we've loaded this input file.
2673 F.InputFilesLoaded[ID-1] = IF;
2674 return IF;
2677 /// If we are loading a relocatable PCH or module file, and the filename
2678 /// is not an absolute path, add the system or module root to the beginning of
2679 /// the file name.
2680 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2681 // Resolve relative to the base directory, if we have one.
2682 if (!M.BaseDirectory.empty())
2683 return ResolveImportedPath(Filename, M.BaseDirectory);
2686 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2687 if (Filename.empty() || llvm::sys::path::is_absolute(Filename) ||
2688 Filename == "<built-in>" || Filename == "<command line>")
2689 return;
2691 SmallString<128> Buffer;
2692 llvm::sys::path::append(Buffer, Prefix, Filename);
2693 Filename.assign(Buffer.begin(), Buffer.end());
2696 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2697 switch (ARR) {
2698 case ASTReader::Failure: return true;
2699 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2700 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2701 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2702 case ASTReader::ConfigurationMismatch:
2703 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2704 case ASTReader::HadErrors: return true;
2705 case ASTReader::Success: return false;
2708 llvm_unreachable("unknown ASTReadResult");
2711 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2712 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2713 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2714 std::string &SuggestedPredefines) {
2715 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2716 // FIXME this drops errors on the floor.
2717 consumeError(std::move(Err));
2718 return Failure;
2721 // Read all of the records in the options block.
2722 RecordData Record;
2723 ASTReadResult Result = Success;
2724 while (true) {
2725 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2726 if (!MaybeEntry) {
2727 // FIXME this drops errors on the floor.
2728 consumeError(MaybeEntry.takeError());
2729 return Failure;
2731 llvm::BitstreamEntry Entry = MaybeEntry.get();
2733 switch (Entry.Kind) {
2734 case llvm::BitstreamEntry::Error:
2735 case llvm::BitstreamEntry::SubBlock:
2736 return Failure;
2738 case llvm::BitstreamEntry::EndBlock:
2739 return Result;
2741 case llvm::BitstreamEntry::Record:
2742 // The interesting case.
2743 break;
2746 // Read and process a record.
2747 Record.clear();
2748 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2749 if (!MaybeRecordType) {
2750 // FIXME this drops errors on the floor.
2751 consumeError(MaybeRecordType.takeError());
2752 return Failure;
2754 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2755 case LANGUAGE_OPTIONS: {
2756 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2757 if (ParseLanguageOptions(Record, Complain, Listener,
2758 AllowCompatibleConfigurationMismatch))
2759 Result = ConfigurationMismatch;
2760 break;
2763 case TARGET_OPTIONS: {
2764 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2765 if (ParseTargetOptions(Record, Complain, Listener,
2766 AllowCompatibleConfigurationMismatch))
2767 Result = ConfigurationMismatch;
2768 break;
2771 case FILE_SYSTEM_OPTIONS: {
2772 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2773 if (!AllowCompatibleConfigurationMismatch &&
2774 ParseFileSystemOptions(Record, Complain, Listener))
2775 Result = ConfigurationMismatch;
2776 break;
2779 case HEADER_SEARCH_OPTIONS: {
2780 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2781 if (!AllowCompatibleConfigurationMismatch &&
2782 ParseHeaderSearchOptions(Record, Complain, Listener))
2783 Result = ConfigurationMismatch;
2784 break;
2787 case PREPROCESSOR_OPTIONS:
2788 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2789 if (!AllowCompatibleConfigurationMismatch &&
2790 ParsePreprocessorOptions(Record, Complain, Listener,
2791 SuggestedPredefines))
2792 Result = ConfigurationMismatch;
2793 break;
2798 ASTReader::ASTReadResult
2799 ASTReader::ReadControlBlock(ModuleFile &F,
2800 SmallVectorImpl<ImportedModule> &Loaded,
2801 const ModuleFile *ImportedBy,
2802 unsigned ClientLoadCapabilities) {
2803 BitstreamCursor &Stream = F.Stream;
2805 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2806 Error(std::move(Err));
2807 return Failure;
2810 // Lambda to read the unhashed control block the first time it's called.
2812 // For PCM files, the unhashed control block cannot be read until after the
2813 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2814 // need to look ahead before reading the IMPORTS record. For consistency,
2815 // this block is always read somehow (see BitstreamEntry::EndBlock).
2816 bool HasReadUnhashedControlBlock = false;
2817 auto readUnhashedControlBlockOnce = [&]() {
2818 if (!HasReadUnhashedControlBlock) {
2819 HasReadUnhashedControlBlock = true;
2820 if (ASTReadResult Result =
2821 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2822 return Result;
2824 return Success;
2827 bool DisableValidation = shouldDisableValidationForFile(F);
2829 // Read all of the records and blocks in the control block.
2830 RecordData Record;
2831 unsigned NumInputs = 0;
2832 unsigned NumUserInputs = 0;
2833 StringRef BaseDirectoryAsWritten;
2834 while (true) {
2835 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2836 if (!MaybeEntry) {
2837 Error(MaybeEntry.takeError());
2838 return Failure;
2840 llvm::BitstreamEntry Entry = MaybeEntry.get();
2842 switch (Entry.Kind) {
2843 case llvm::BitstreamEntry::Error:
2844 Error("malformed block record in AST file");
2845 return Failure;
2846 case llvm::BitstreamEntry::EndBlock: {
2847 // Validate the module before returning. This call catches an AST with
2848 // no module name and no imports.
2849 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2850 return Result;
2852 // Validate input files.
2853 const HeaderSearchOptions &HSOpts =
2854 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2856 // All user input files reside at the index range [0, NumUserInputs), and
2857 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2858 // loaded module files, ignore missing inputs.
2859 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2860 F.Kind != MK_PrebuiltModule) {
2861 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2863 // If we are reading a module, we will create a verification timestamp,
2864 // so we verify all input files. Otherwise, verify only user input
2865 // files.
2867 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2868 if (HSOpts.ModulesValidateOncePerBuildSession &&
2869 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
2870 F.Kind == MK_ImplicitModule)
2871 N = NumUserInputs;
2873 for (unsigned I = 0; I < N; ++I) {
2874 InputFile IF = getInputFile(F, I+1, Complain);
2875 if (!IF.getFile() || IF.isOutOfDate())
2876 return OutOfDate;
2880 if (Listener)
2881 Listener->visitModuleFile(F.FileName, F.Kind);
2883 if (Listener && Listener->needsInputFileVisitation()) {
2884 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2885 : NumUserInputs;
2886 for (unsigned I = 0; I < N; ++I) {
2887 bool IsSystem = I >= NumUserInputs;
2888 InputFileInfo FI = getInputFileInfo(F, I + 1);
2889 Listener->visitInputFile(
2890 FI.FilenameAsRequested, IsSystem, FI.Overridden,
2891 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
2895 return Success;
2898 case llvm::BitstreamEntry::SubBlock:
2899 switch (Entry.ID) {
2900 case INPUT_FILES_BLOCK_ID:
2901 F.InputFilesCursor = Stream;
2902 if (llvm::Error Err = Stream.SkipBlock()) {
2903 Error(std::move(Err));
2904 return Failure;
2906 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2907 Error("malformed block record in AST file");
2908 return Failure;
2910 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
2911 continue;
2913 case OPTIONS_BLOCK_ID:
2914 // If we're reading the first module for this group, check its options
2915 // are compatible with ours. For modules it imports, no further checking
2916 // is required, because we checked them when we built it.
2917 if (Listener && !ImportedBy) {
2918 // Should we allow the configuration of the module file to differ from
2919 // the configuration of the current translation unit in a compatible
2920 // way?
2922 // FIXME: Allow this for files explicitly specified with -include-pch.
2923 bool AllowCompatibleConfigurationMismatch =
2924 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2926 ASTReadResult Result =
2927 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2928 AllowCompatibleConfigurationMismatch, *Listener,
2929 SuggestedPredefines);
2930 if (Result == Failure) {
2931 Error("malformed block record in AST file");
2932 return Result;
2935 if (DisableValidation ||
2936 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2937 Result = Success;
2939 // If we can't load the module, exit early since we likely
2940 // will rebuild the module anyway. The stream may be in the
2941 // middle of a block.
2942 if (Result != Success)
2943 return Result;
2944 } else if (llvm::Error Err = Stream.SkipBlock()) {
2945 Error(std::move(Err));
2946 return Failure;
2948 continue;
2950 default:
2951 if (llvm::Error Err = Stream.SkipBlock()) {
2952 Error(std::move(Err));
2953 return Failure;
2955 continue;
2958 case llvm::BitstreamEntry::Record:
2959 // The interesting case.
2960 break;
2963 // Read and process a record.
2964 Record.clear();
2965 StringRef Blob;
2966 Expected<unsigned> MaybeRecordType =
2967 Stream.readRecord(Entry.ID, Record, &Blob);
2968 if (!MaybeRecordType) {
2969 Error(MaybeRecordType.takeError());
2970 return Failure;
2972 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2973 case METADATA: {
2974 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2975 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2976 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2977 : diag::err_pch_version_too_new);
2978 return VersionMismatch;
2981 bool hasErrors = Record[7];
2982 if (hasErrors && !DisableValidation) {
2983 // If requested by the caller and the module hasn't already been read
2984 // or compiled, mark modules on error as out-of-date.
2985 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2986 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2987 return OutOfDate;
2989 if (!AllowASTWithCompilerErrors) {
2990 Diag(diag::err_pch_with_compiler_errors);
2991 return HadErrors;
2994 if (hasErrors) {
2995 Diags.ErrorOccurred = true;
2996 Diags.UncompilableErrorOccurred = true;
2997 Diags.UnrecoverableErrorOccurred = true;
3000 F.RelocatablePCH = Record[4];
3001 // Relative paths in a relocatable PCH are relative to our sysroot.
3002 if (F.RelocatablePCH)
3003 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3005 F.StandardCXXModule = Record[5];
3007 F.HasTimestamps = Record[6];
3009 const std::string &CurBranch = getClangFullRepositoryVersion();
3010 StringRef ASTBranch = Blob;
3011 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3012 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3013 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
3014 return VersionMismatch;
3016 break;
3019 case IMPORTS: {
3020 // Validate the AST before processing any imports (otherwise, untangling
3021 // them can be error-prone and expensive). A module will have a name and
3022 // will already have been validated, but this catches the PCH case.
3023 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3024 return Result;
3026 // Load each of the imported PCH files.
3027 unsigned Idx = 0, N = Record.size();
3028 while (Idx < N) {
3029 // Read information about the AST file.
3030 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3031 // Whether we're importing a standard c++ module.
3032 bool IsImportingStdCXXModule = Record[Idx++];
3033 // The import location will be the local one for now; we will adjust
3034 // all import locations of module imports after the global source
3035 // location info are setup, in ReadAST.
3036 SourceLocation ImportLoc =
3037 ReadUntranslatedSourceLocation(Record[Idx++]);
3038 off_t StoredSize = (off_t)Record[Idx++];
3039 time_t StoredModTime = (time_t)Record[Idx++];
3040 auto FirstSignatureByte = Record.begin() + Idx;
3041 ASTFileSignature StoredSignature = ASTFileSignature::create(
3042 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
3043 Idx += ASTFileSignature::size;
3045 std::string ImportedName = ReadString(Record, Idx);
3046 std::string ImportedFile;
3048 // For prebuilt and explicit modules first consult the file map for
3049 // an override. Note that here we don't search prebuilt module
3050 // directories if we're not importing standard c++ module, only the
3051 // explicit name to file mappings. Also, we will still verify the
3052 // size/signature making sure it is essentially the same file but
3053 // perhaps in a different location.
3054 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3055 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3056 ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3058 if (ImportedFile.empty()) {
3059 // It is deprecated for C++20 Named modules to use the implicitly
3060 // paths.
3061 if (IsImportingStdCXXModule)
3062 Diag(clang::diag::warn_reading_std_cxx_module_by_implicit_paths)
3063 << ImportedName;
3065 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3066 // ModuleCache as when writing.
3067 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
3068 } else
3069 SkipPath(Record, Idx);
3071 // If our client can't cope with us being out of date, we can't cope with
3072 // our dependency being missing.
3073 unsigned Capabilities = ClientLoadCapabilities;
3074 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3075 Capabilities &= ~ARR_Missing;
3077 // Load the AST file.
3078 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
3079 Loaded, StoredSize, StoredModTime,
3080 StoredSignature, Capabilities);
3082 // If we diagnosed a problem, produce a backtrace.
3083 bool recompilingFinalized =
3084 Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
3085 getModuleManager().getModuleCache().isPCMFinal(F.FileName);
3086 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
3087 Diag(diag::note_module_file_imported_by)
3088 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3089 if (recompilingFinalized)
3090 Diag(diag::note_module_file_conflict);
3092 switch (Result) {
3093 case Failure: return Failure;
3094 // If we have to ignore the dependency, we'll have to ignore this too.
3095 case Missing:
3096 case OutOfDate: return OutOfDate;
3097 case VersionMismatch: return VersionMismatch;
3098 case ConfigurationMismatch: return ConfigurationMismatch;
3099 case HadErrors: return HadErrors;
3100 case Success: break;
3103 break;
3106 case ORIGINAL_FILE:
3107 F.OriginalSourceFileID = FileID::get(Record[0]);
3108 F.ActualOriginalSourceFileName = std::string(Blob);
3109 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
3110 ResolveImportedPath(F, F.OriginalSourceFileName);
3111 break;
3113 case ORIGINAL_FILE_ID:
3114 F.OriginalSourceFileID = FileID::get(Record[0]);
3115 break;
3117 case MODULE_NAME:
3118 F.ModuleName = std::string(Blob);
3119 Diag(diag::remark_module_import)
3120 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3121 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3122 if (Listener)
3123 Listener->ReadModuleName(F.ModuleName);
3125 // Validate the AST as soon as we have a name so we can exit early on
3126 // failure.
3127 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3128 return Result;
3130 break;
3132 case MODULE_DIRECTORY: {
3133 // Save the BaseDirectory as written in the PCM for computing the module
3134 // filename for the ModuleCache.
3135 BaseDirectoryAsWritten = Blob;
3136 assert(!F.ModuleName.empty() &&
3137 "MODULE_DIRECTORY found before MODULE_NAME");
3138 F.BaseDirectory = std::string(Blob);
3139 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3140 break;
3141 // If we've already loaded a module map file covering this module, we may
3142 // have a better path for it (relative to the current build).
3143 Module *M = PP.getHeaderSearchInfo().lookupModule(
3144 F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
3145 /*AllowExtraModuleMapSearch*/ true);
3146 if (M && M->Directory) {
3147 // If we're implicitly loading a module, the base directory can't
3148 // change between the build and use.
3149 // Don't emit module relocation error if we have -fno-validate-pch
3150 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3151 DisableValidationForModuleKind::Module) &&
3152 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3153 auto BuildDir = PP.getFileManager().getDirectory(Blob);
3154 if (!BuildDir || *BuildDir != M->Directory) {
3155 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3156 Diag(diag::err_imported_module_relocated)
3157 << F.ModuleName << Blob << M->Directory->getName();
3158 return OutOfDate;
3161 F.BaseDirectory = std::string(M->Directory->getName());
3163 break;
3166 case MODULE_MAP_FILE:
3167 if (ASTReadResult Result =
3168 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3169 return Result;
3170 break;
3172 case INPUT_FILE_OFFSETS:
3173 NumInputs = Record[0];
3174 NumUserInputs = Record[1];
3175 F.InputFileOffsets =
3176 (const llvm::support::unaligned_uint64_t *)Blob.data();
3177 F.InputFilesLoaded.resize(NumInputs);
3178 F.InputFileInfosLoaded.resize(NumInputs);
3179 F.NumUserInputFiles = NumUserInputs;
3180 break;
3185 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3186 unsigned ClientLoadCapabilities) {
3187 BitstreamCursor &Stream = F.Stream;
3189 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
3190 return Err;
3191 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3193 // Read all of the records and blocks for the AST file.
3194 RecordData Record;
3195 while (true) {
3196 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3197 if (!MaybeEntry)
3198 return MaybeEntry.takeError();
3199 llvm::BitstreamEntry Entry = MaybeEntry.get();
3201 switch (Entry.Kind) {
3202 case llvm::BitstreamEntry::Error:
3203 return llvm::createStringError(
3204 std::errc::illegal_byte_sequence,
3205 "error at end of module block in AST file");
3206 case llvm::BitstreamEntry::EndBlock:
3207 // Outside of C++, we do not store a lookup map for the translation unit.
3208 // Instead, mark it as needing a lookup map to be built if this module
3209 // contains any declarations lexically within it (which it always does!).
3210 // This usually has no cost, since we very rarely need the lookup map for
3211 // the translation unit outside C++.
3212 if (ASTContext *Ctx = ContextObj) {
3213 DeclContext *DC = Ctx->getTranslationUnitDecl();
3214 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3215 DC->setMustBuildLookupTable();
3218 return llvm::Error::success();
3219 case llvm::BitstreamEntry::SubBlock:
3220 switch (Entry.ID) {
3221 case DECLTYPES_BLOCK_ID:
3222 // We lazily load the decls block, but we want to set up the
3223 // DeclsCursor cursor to point into it. Clone our current bitcode
3224 // cursor to it, enter the block and read the abbrevs in that block.
3225 // With the main cursor, we just skip over it.
3226 F.DeclsCursor = Stream;
3227 if (llvm::Error Err = Stream.SkipBlock())
3228 return Err;
3229 if (llvm::Error Err = ReadBlockAbbrevs(
3230 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3231 return Err;
3232 break;
3234 case PREPROCESSOR_BLOCK_ID:
3235 F.MacroCursor = Stream;
3236 if (!PP.getExternalSource())
3237 PP.setExternalSource(this);
3239 if (llvm::Error Err = Stream.SkipBlock())
3240 return Err;
3241 if (llvm::Error Err =
3242 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3243 return Err;
3244 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3245 break;
3247 case PREPROCESSOR_DETAIL_BLOCK_ID:
3248 F.PreprocessorDetailCursor = Stream;
3250 if (llvm::Error Err = Stream.SkipBlock()) {
3251 return Err;
3253 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3254 PREPROCESSOR_DETAIL_BLOCK_ID))
3255 return Err;
3256 F.PreprocessorDetailStartOffset
3257 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3259 if (!PP.getPreprocessingRecord())
3260 PP.createPreprocessingRecord();
3261 if (!PP.getPreprocessingRecord()->getExternalSource())
3262 PP.getPreprocessingRecord()->SetExternalSource(*this);
3263 break;
3265 case SOURCE_MANAGER_BLOCK_ID:
3266 if (llvm::Error Err = ReadSourceManagerBlock(F))
3267 return Err;
3268 break;
3270 case SUBMODULE_BLOCK_ID:
3271 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3272 return Err;
3273 break;
3275 case COMMENTS_BLOCK_ID: {
3276 BitstreamCursor C = Stream;
3278 if (llvm::Error Err = Stream.SkipBlock())
3279 return Err;
3280 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3281 return Err;
3282 CommentsCursors.push_back(std::make_pair(C, &F));
3283 break;
3286 default:
3287 if (llvm::Error Err = Stream.SkipBlock())
3288 return Err;
3289 break;
3291 continue;
3293 case llvm::BitstreamEntry::Record:
3294 // The interesting case.
3295 break;
3298 // Read and process a record.
3299 Record.clear();
3300 StringRef Blob;
3301 Expected<unsigned> MaybeRecordType =
3302 Stream.readRecord(Entry.ID, Record, &Blob);
3303 if (!MaybeRecordType)
3304 return MaybeRecordType.takeError();
3305 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3307 // If we're not loading an AST context, we don't care about most records.
3308 if (!ContextObj) {
3309 switch (RecordType) {
3310 case IDENTIFIER_TABLE:
3311 case IDENTIFIER_OFFSET:
3312 case INTERESTING_IDENTIFIERS:
3313 case STATISTICS:
3314 case PP_ASSUME_NONNULL_LOC:
3315 case PP_CONDITIONAL_STACK:
3316 case PP_COUNTER_VALUE:
3317 case SOURCE_LOCATION_OFFSETS:
3318 case MODULE_OFFSET_MAP:
3319 case SOURCE_MANAGER_LINE_TABLE:
3320 case PPD_ENTITIES_OFFSETS:
3321 case HEADER_SEARCH_TABLE:
3322 case IMPORTED_MODULES:
3323 case MACRO_OFFSET:
3324 break;
3325 default:
3326 continue;
3330 switch (RecordType) {
3331 default: // Default behavior: ignore.
3332 break;
3334 case TYPE_OFFSET: {
3335 if (F.LocalNumTypes != 0)
3336 return llvm::createStringError(
3337 std::errc::illegal_byte_sequence,
3338 "duplicate TYPE_OFFSET record in AST file");
3339 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3340 F.LocalNumTypes = Record[0];
3341 unsigned LocalBaseTypeIndex = Record[1];
3342 F.BaseTypeIndex = getTotalNumTypes();
3344 if (F.LocalNumTypes > 0) {
3345 // Introduce the global -> local mapping for types within this module.
3346 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3348 // Introduce the local -> global mapping for types within this module.
3349 F.TypeRemap.insertOrReplace(
3350 std::make_pair(LocalBaseTypeIndex,
3351 F.BaseTypeIndex - LocalBaseTypeIndex));
3353 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3355 break;
3358 case DECL_OFFSET: {
3359 if (F.LocalNumDecls != 0)
3360 return llvm::createStringError(
3361 std::errc::illegal_byte_sequence,
3362 "duplicate DECL_OFFSET record in AST file");
3363 F.DeclOffsets = (const DeclOffset *)Blob.data();
3364 F.LocalNumDecls = Record[0];
3365 unsigned LocalBaseDeclID = Record[1];
3366 F.BaseDeclID = getTotalNumDecls();
3368 if (F.LocalNumDecls > 0) {
3369 // Introduce the global -> local mapping for declarations within this
3370 // module.
3371 GlobalDeclMap.insert(
3372 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3374 // Introduce the local -> global mapping for declarations within this
3375 // module.
3376 F.DeclRemap.insertOrReplace(
3377 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3379 // Introduce the global -> local mapping for declarations within this
3380 // module.
3381 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3383 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3385 break;
3388 case TU_UPDATE_LEXICAL: {
3389 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3390 LexicalContents Contents(
3391 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3392 Blob.data()),
3393 static_cast<unsigned int>(Blob.size() / 4));
3394 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3395 TU->setHasExternalLexicalStorage(true);
3396 break;
3399 case UPDATE_VISIBLE: {
3400 unsigned Idx = 0;
3401 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3402 auto *Data = (const unsigned char*)Blob.data();
3403 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3404 // If we've already loaded the decl, perform the updates when we finish
3405 // loading this block.
3406 if (Decl *D = GetExistingDecl(ID))
3407 PendingUpdateRecords.push_back(
3408 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3409 break;
3412 case IDENTIFIER_TABLE:
3413 F.IdentifierTableData =
3414 reinterpret_cast<const unsigned char *>(Blob.data());
3415 if (Record[0]) {
3416 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3417 F.IdentifierTableData + Record[0],
3418 F.IdentifierTableData + sizeof(uint32_t),
3419 F.IdentifierTableData,
3420 ASTIdentifierLookupTrait(*this, F));
3422 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3424 break;
3426 case IDENTIFIER_OFFSET: {
3427 if (F.LocalNumIdentifiers != 0)
3428 return llvm::createStringError(
3429 std::errc::illegal_byte_sequence,
3430 "duplicate IDENTIFIER_OFFSET record in AST file");
3431 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3432 F.LocalNumIdentifiers = Record[0];
3433 unsigned LocalBaseIdentifierID = Record[1];
3434 F.BaseIdentifierID = getTotalNumIdentifiers();
3436 if (F.LocalNumIdentifiers > 0) {
3437 // Introduce the global -> local mapping for identifiers within this
3438 // module.
3439 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3440 &F));
3442 // Introduce the local -> global mapping for identifiers within this
3443 // module.
3444 F.IdentifierRemap.insertOrReplace(
3445 std::make_pair(LocalBaseIdentifierID,
3446 F.BaseIdentifierID - LocalBaseIdentifierID));
3448 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3449 + F.LocalNumIdentifiers);
3451 break;
3454 case INTERESTING_IDENTIFIERS:
3455 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3456 break;
3458 case EAGERLY_DESERIALIZED_DECLS:
3459 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3460 // about "interesting" decls (for instance, if we're building a module).
3461 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3462 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3463 break;
3465 case MODULAR_CODEGEN_DECLS:
3466 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3467 // them (ie: if we're not codegenerating this module).
3468 if (F.Kind == MK_MainFile ||
3469 getContext().getLangOpts().BuildingPCHWithObjectFile)
3470 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3471 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3472 break;
3474 case SPECIAL_TYPES:
3475 if (SpecialTypes.empty()) {
3476 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3477 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3478 break;
3481 if (SpecialTypes.size() != Record.size())
3482 return llvm::createStringError(std::errc::illegal_byte_sequence,
3483 "invalid special-types record");
3485 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3486 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3487 if (!SpecialTypes[I])
3488 SpecialTypes[I] = ID;
3489 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3490 // merge step?
3492 break;
3494 case STATISTICS:
3495 TotalNumStatements += Record[0];
3496 TotalNumMacros += Record[1];
3497 TotalLexicalDeclContexts += Record[2];
3498 TotalVisibleDeclContexts += Record[3];
3499 break;
3501 case UNUSED_FILESCOPED_DECLS:
3502 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3503 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3504 break;
3506 case DELEGATING_CTORS:
3507 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3508 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3509 break;
3511 case WEAK_UNDECLARED_IDENTIFIERS:
3512 if (Record.size() % 3 != 0)
3513 return llvm::createStringError(std::errc::illegal_byte_sequence,
3514 "invalid weak identifiers record");
3516 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3517 // files. This isn't the way to do it :)
3518 WeakUndeclaredIdentifiers.clear();
3520 // Translate the weak, undeclared identifiers into global IDs.
3521 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3522 WeakUndeclaredIdentifiers.push_back(
3523 getGlobalIdentifierID(F, Record[I++]));
3524 WeakUndeclaredIdentifiers.push_back(
3525 getGlobalIdentifierID(F, Record[I++]));
3526 WeakUndeclaredIdentifiers.push_back(
3527 ReadSourceLocation(F, Record, I).getRawEncoding());
3529 break;
3531 case SELECTOR_OFFSETS: {
3532 F.SelectorOffsets = (const uint32_t *)Blob.data();
3533 F.LocalNumSelectors = Record[0];
3534 unsigned LocalBaseSelectorID = Record[1];
3535 F.BaseSelectorID = getTotalNumSelectors();
3537 if (F.LocalNumSelectors > 0) {
3538 // Introduce the global -> local mapping for selectors within this
3539 // module.
3540 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3542 // Introduce the local -> global mapping for selectors within this
3543 // module.
3544 F.SelectorRemap.insertOrReplace(
3545 std::make_pair(LocalBaseSelectorID,
3546 F.BaseSelectorID - LocalBaseSelectorID));
3548 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3550 break;
3553 case METHOD_POOL:
3554 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3555 if (Record[0])
3556 F.SelectorLookupTable
3557 = ASTSelectorLookupTable::Create(
3558 F.SelectorLookupTableData + Record[0],
3559 F.SelectorLookupTableData,
3560 ASTSelectorLookupTrait(*this, F));
3561 TotalNumMethodPoolEntries += Record[1];
3562 break;
3564 case REFERENCED_SELECTOR_POOL:
3565 if (!Record.empty()) {
3566 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3567 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3568 Record[Idx++]));
3569 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3570 getRawEncoding());
3573 break;
3575 case PP_ASSUME_NONNULL_LOC: {
3576 unsigned Idx = 0;
3577 if (!Record.empty())
3578 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3579 ReadSourceLocation(F, Record, Idx));
3580 break;
3583 case PP_CONDITIONAL_STACK:
3584 if (!Record.empty()) {
3585 unsigned Idx = 0, End = Record.size() - 1;
3586 bool ReachedEOFWhileSkipping = Record[Idx++];
3587 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3588 if (ReachedEOFWhileSkipping) {
3589 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3590 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3591 bool FoundNonSkipPortion = Record[Idx++];
3592 bool FoundElse = Record[Idx++];
3593 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3594 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3595 FoundElse, ElseLoc);
3597 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3598 while (Idx < End) {
3599 auto Loc = ReadSourceLocation(F, Record, Idx);
3600 bool WasSkipping = Record[Idx++];
3601 bool FoundNonSkip = Record[Idx++];
3602 bool FoundElse = Record[Idx++];
3603 ConditionalStack.push_back(
3604 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3606 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3608 break;
3610 case PP_COUNTER_VALUE:
3611 if (!Record.empty() && Listener)
3612 Listener->ReadCounter(F, Record[0]);
3613 break;
3615 case FILE_SORTED_DECLS:
3616 F.FileSortedDecls = (const DeclID *)Blob.data();
3617 F.NumFileSortedDecls = Record[0];
3618 break;
3620 case SOURCE_LOCATION_OFFSETS: {
3621 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3622 F.LocalNumSLocEntries = Record[0];
3623 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3624 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3625 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3626 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3627 SLocSpaceSize);
3628 if (!F.SLocEntryBaseID) {
3629 if (!Diags.isDiagnosticInFlight()) {
3630 Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3631 SourceMgr.noteSLocAddressSpaceUsage(Diags);
3633 return llvm::createStringError(std::errc::invalid_argument,
3634 "ran out of source locations");
3636 // Make our entry in the range map. BaseID is negative and growing, so
3637 // we invert it. Because we invert it, though, we need the other end of
3638 // the range.
3639 unsigned RangeStart =
3640 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3641 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3642 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3644 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3645 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3646 GlobalSLocOffsetMap.insert(
3647 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3648 - SLocSpaceSize,&F));
3650 // Initialize the remapping table.
3651 // Invalid stays invalid.
3652 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3653 // This module. Base was 2 when being compiled.
3654 F.SLocRemap.insertOrReplace(std::make_pair(
3655 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3657 TotalNumSLocEntries += F.LocalNumSLocEntries;
3658 break;
3661 case MODULE_OFFSET_MAP:
3662 F.ModuleOffsetMap = Blob;
3663 break;
3665 case SOURCE_MANAGER_LINE_TABLE:
3666 ParseLineTable(F, Record);
3667 break;
3669 case EXT_VECTOR_DECLS:
3670 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3671 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3672 break;
3674 case VTABLE_USES:
3675 if (Record.size() % 3 != 0)
3676 return llvm::createStringError(std::errc::illegal_byte_sequence,
3677 "Invalid VTABLE_USES record");
3679 // Later tables overwrite earlier ones.
3680 // FIXME: Modules will have some trouble with this. This is clearly not
3681 // the right way to do this.
3682 VTableUses.clear();
3684 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3685 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3686 VTableUses.push_back(
3687 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3688 VTableUses.push_back(Record[Idx++]);
3690 break;
3692 case PENDING_IMPLICIT_INSTANTIATIONS:
3693 if (PendingInstantiations.size() % 2 != 0)
3694 return llvm::createStringError(
3695 std::errc::illegal_byte_sequence,
3696 "Invalid existing PendingInstantiations");
3698 if (Record.size() % 2 != 0)
3699 return llvm::createStringError(
3700 std::errc::illegal_byte_sequence,
3701 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3703 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3704 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3705 PendingInstantiations.push_back(
3706 ReadSourceLocation(F, Record, I).getRawEncoding());
3708 break;
3710 case SEMA_DECL_REFS:
3711 if (Record.size() != 3)
3712 return llvm::createStringError(std::errc::illegal_byte_sequence,
3713 "Invalid SEMA_DECL_REFS block");
3714 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3715 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3716 break;
3718 case PPD_ENTITIES_OFFSETS: {
3719 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3720 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3721 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3723 unsigned LocalBasePreprocessedEntityID = Record[0];
3725 unsigned StartingID;
3726 if (!PP.getPreprocessingRecord())
3727 PP.createPreprocessingRecord();
3728 if (!PP.getPreprocessingRecord()->getExternalSource())
3729 PP.getPreprocessingRecord()->SetExternalSource(*this);
3730 StartingID
3731 = PP.getPreprocessingRecord()
3732 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3733 F.BasePreprocessedEntityID = StartingID;
3735 if (F.NumPreprocessedEntities > 0) {
3736 // Introduce the global -> local mapping for preprocessed entities in
3737 // this module.
3738 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3740 // Introduce the local -> global mapping for preprocessed entities in
3741 // this module.
3742 F.PreprocessedEntityRemap.insertOrReplace(
3743 std::make_pair(LocalBasePreprocessedEntityID,
3744 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3747 break;
3750 case PPD_SKIPPED_RANGES: {
3751 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3752 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3753 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3755 if (!PP.getPreprocessingRecord())
3756 PP.createPreprocessingRecord();
3757 if (!PP.getPreprocessingRecord()->getExternalSource())
3758 PP.getPreprocessingRecord()->SetExternalSource(*this);
3759 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3760 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3762 if (F.NumPreprocessedSkippedRanges > 0)
3763 GlobalSkippedRangeMap.insert(
3764 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3765 break;
3768 case DECL_UPDATE_OFFSETS:
3769 if (Record.size() % 2 != 0)
3770 return llvm::createStringError(
3771 std::errc::illegal_byte_sequence,
3772 "invalid DECL_UPDATE_OFFSETS block in AST file");
3773 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3774 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3775 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3777 // If we've already loaded the decl, perform the updates when we finish
3778 // loading this block.
3779 if (Decl *D = GetExistingDecl(ID))
3780 PendingUpdateRecords.push_back(
3781 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3783 break;
3785 case OBJC_CATEGORIES_MAP:
3786 if (F.LocalNumObjCCategoriesInMap != 0)
3787 return llvm::createStringError(
3788 std::errc::illegal_byte_sequence,
3789 "duplicate OBJC_CATEGORIES_MAP record in AST file");
3791 F.LocalNumObjCCategoriesInMap = Record[0];
3792 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3793 break;
3795 case OBJC_CATEGORIES:
3796 F.ObjCCategories.swap(Record);
3797 break;
3799 case CUDA_SPECIAL_DECL_REFS:
3800 // Later tables overwrite earlier ones.
3801 // FIXME: Modules will have trouble with this.
3802 CUDASpecialDeclRefs.clear();
3803 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3804 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3805 break;
3807 case HEADER_SEARCH_TABLE:
3808 F.HeaderFileInfoTableData = Blob.data();
3809 F.LocalNumHeaderFileInfos = Record[1];
3810 if (Record[0]) {
3811 F.HeaderFileInfoTable
3812 = HeaderFileInfoLookupTable::Create(
3813 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3814 (const unsigned char *)F.HeaderFileInfoTableData,
3815 HeaderFileInfoTrait(*this, F,
3816 &PP.getHeaderSearchInfo(),
3817 Blob.data() + Record[2]));
3819 PP.getHeaderSearchInfo().SetExternalSource(this);
3820 if (!PP.getHeaderSearchInfo().getExternalLookup())
3821 PP.getHeaderSearchInfo().SetExternalLookup(this);
3823 break;
3825 case FP_PRAGMA_OPTIONS:
3826 // Later tables overwrite earlier ones.
3827 FPPragmaOptions.swap(Record);
3828 break;
3830 case OPENCL_EXTENSIONS:
3831 for (unsigned I = 0, E = Record.size(); I != E; ) {
3832 auto Name = ReadString(Record, I);
3833 auto &OptInfo = OpenCLExtensions.OptMap[Name];
3834 OptInfo.Supported = Record[I++] != 0;
3835 OptInfo.Enabled = Record[I++] != 0;
3836 OptInfo.WithPragma = Record[I++] != 0;
3837 OptInfo.Avail = Record[I++];
3838 OptInfo.Core = Record[I++];
3839 OptInfo.Opt = Record[I++];
3841 break;
3843 case TENTATIVE_DEFINITIONS:
3844 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3845 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3846 break;
3848 case KNOWN_NAMESPACES:
3849 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3850 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3851 break;
3853 case UNDEFINED_BUT_USED:
3854 if (UndefinedButUsed.size() % 2 != 0)
3855 return llvm::createStringError(std::errc::illegal_byte_sequence,
3856 "Invalid existing UndefinedButUsed");
3858 if (Record.size() % 2 != 0)
3859 return llvm::createStringError(std::errc::illegal_byte_sequence,
3860 "invalid undefined-but-used record");
3861 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3862 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3863 UndefinedButUsed.push_back(
3864 ReadSourceLocation(F, Record, I).getRawEncoding());
3866 break;
3868 case DELETE_EXPRS_TO_ANALYZE:
3869 for (unsigned I = 0, N = Record.size(); I != N;) {
3870 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3871 const uint64_t Count = Record[I++];
3872 DelayedDeleteExprs.push_back(Count);
3873 for (uint64_t C = 0; C < Count; ++C) {
3874 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3875 bool IsArrayForm = Record[I++] == 1;
3876 DelayedDeleteExprs.push_back(IsArrayForm);
3879 break;
3881 case IMPORTED_MODULES:
3882 if (!F.isModule()) {
3883 // If we aren't loading a module (which has its own exports), make
3884 // all of the imported modules visible.
3885 // FIXME: Deal with macros-only imports.
3886 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3887 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3888 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3889 if (GlobalID) {
3890 PendingImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3891 if (DeserializationListener)
3892 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3896 break;
3898 case MACRO_OFFSET: {
3899 if (F.LocalNumMacros != 0)
3900 return llvm::createStringError(
3901 std::errc::illegal_byte_sequence,
3902 "duplicate MACRO_OFFSET record in AST file");
3903 F.MacroOffsets = (const uint32_t *)Blob.data();
3904 F.LocalNumMacros = Record[0];
3905 unsigned LocalBaseMacroID = Record[1];
3906 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3907 F.BaseMacroID = getTotalNumMacros();
3909 if (F.LocalNumMacros > 0) {
3910 // Introduce the global -> local mapping for macros within this module.
3911 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3913 // Introduce the local -> global mapping for macros within this module.
3914 F.MacroRemap.insertOrReplace(
3915 std::make_pair(LocalBaseMacroID,
3916 F.BaseMacroID - LocalBaseMacroID));
3918 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3920 break;
3923 case LATE_PARSED_TEMPLATE:
3924 LateParsedTemplates.emplace_back(
3925 std::piecewise_construct, std::forward_as_tuple(&F),
3926 std::forward_as_tuple(Record.begin(), Record.end()));
3927 break;
3929 case OPTIMIZE_PRAGMA_OPTIONS:
3930 if (Record.size() != 1)
3931 return llvm::createStringError(std::errc::illegal_byte_sequence,
3932 "invalid pragma optimize record");
3933 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3934 break;
3936 case MSSTRUCT_PRAGMA_OPTIONS:
3937 if (Record.size() != 1)
3938 return llvm::createStringError(std::errc::illegal_byte_sequence,
3939 "invalid pragma ms_struct record");
3940 PragmaMSStructState = Record[0];
3941 break;
3943 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3944 if (Record.size() != 2)
3945 return llvm::createStringError(
3946 std::errc::illegal_byte_sequence,
3947 "invalid pragma pointers to members record");
3948 PragmaMSPointersToMembersState = Record[0];
3949 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3950 break;
3952 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3953 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3954 UnusedLocalTypedefNameCandidates.push_back(
3955 getGlobalDeclID(F, Record[I]));
3956 break;
3958 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3959 if (Record.size() != 1)
3960 return llvm::createStringError(std::errc::illegal_byte_sequence,
3961 "invalid cuda pragma options record");
3962 ForceCUDAHostDeviceDepth = Record[0];
3963 break;
3965 case ALIGN_PACK_PRAGMA_OPTIONS: {
3966 if (Record.size() < 3)
3967 return llvm::createStringError(std::errc::illegal_byte_sequence,
3968 "invalid pragma pack record");
3969 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3970 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3971 unsigned NumStackEntries = Record[2];
3972 unsigned Idx = 3;
3973 // Reset the stack when importing a new module.
3974 PragmaAlignPackStack.clear();
3975 for (unsigned I = 0; I < NumStackEntries; ++I) {
3976 PragmaAlignPackStackEntry Entry;
3977 Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3978 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3979 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3980 PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3981 Entry.SlotLabel = PragmaAlignPackStrings.back();
3982 PragmaAlignPackStack.push_back(Entry);
3984 break;
3987 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3988 if (Record.size() < 3)
3989 return llvm::createStringError(std::errc::illegal_byte_sequence,
3990 "invalid pragma float control record");
3991 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3992 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3993 unsigned NumStackEntries = Record[2];
3994 unsigned Idx = 3;
3995 // Reset the stack when importing a new module.
3996 FpPragmaStack.clear();
3997 for (unsigned I = 0; I < NumStackEntries; ++I) {
3998 FpPragmaStackEntry Entry;
3999 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
4000 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4001 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4002 FpPragmaStrings.push_back(ReadString(Record, Idx));
4003 Entry.SlotLabel = FpPragmaStrings.back();
4004 FpPragmaStack.push_back(Entry);
4006 break;
4009 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4010 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4011 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
4012 break;
4017 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4018 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4020 // Additional remapping information.
4021 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4022 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4023 F.ModuleOffsetMap = StringRef();
4025 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
4026 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
4027 F.SLocRemap.insert(std::make_pair(0U, 0));
4028 F.SLocRemap.insert(std::make_pair(2U, 1));
4031 // Continuous range maps we may be updating in our module.
4032 using SLocRemapBuilder =
4033 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
4034 2>::Builder;
4035 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4036 SLocRemapBuilder SLocRemap(F.SLocRemap);
4037 RemapBuilder IdentifierRemap(F.IdentifierRemap);
4038 RemapBuilder MacroRemap(F.MacroRemap);
4039 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4040 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4041 RemapBuilder SelectorRemap(F.SelectorRemap);
4042 RemapBuilder DeclRemap(F.DeclRemap);
4043 RemapBuilder TypeRemap(F.TypeRemap);
4045 while (Data < DataEnd) {
4046 // FIXME: Looking up dependency modules by filename is horrible. Let's
4047 // start fixing this with prebuilt, explicit and implicit modules and see
4048 // how it goes...
4049 using namespace llvm::support;
4050 ModuleKind Kind = static_cast<ModuleKind>(
4051 endian::readNext<uint8_t, llvm::endianness::little, unaligned>(Data));
4052 uint16_t Len =
4053 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(Data);
4054 StringRef Name = StringRef((const char*)Data, Len);
4055 Data += Len;
4056 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4057 Kind == MK_ImplicitModule
4058 ? ModuleMgr.lookupByModuleName(Name)
4059 : ModuleMgr.lookupByFileName(Name));
4060 if (!OM) {
4061 std::string Msg =
4062 "SourceLocation remap refers to unknown module, cannot find ";
4063 Msg.append(std::string(Name));
4064 Error(Msg);
4065 return;
4068 SourceLocation::UIntTy SLocOffset =
4069 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4070 uint32_t IdentifierIDOffset =
4071 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4072 uint32_t MacroIDOffset =
4073 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4074 uint32_t PreprocessedEntityIDOffset =
4075 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4076 uint32_t SubmoduleIDOffset =
4077 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4078 uint32_t SelectorIDOffset =
4079 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4080 uint32_t DeclIDOffset =
4081 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4082 uint32_t TypeIndexOffset =
4083 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4085 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4086 RemapBuilder &Remap) {
4087 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4088 if (Offset != None)
4089 Remap.insert(std::make_pair(Offset,
4090 static_cast<int>(BaseOffset - Offset)));
4093 constexpr SourceLocation::UIntTy SLocNone =
4094 std::numeric_limits<SourceLocation::UIntTy>::max();
4095 if (SLocOffset != SLocNone)
4096 SLocRemap.insert(std::make_pair(
4097 SLocOffset, static_cast<SourceLocation::IntTy>(
4098 OM->SLocEntryBaseOffset - SLocOffset)));
4100 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
4101 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4102 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4103 PreprocessedEntityRemap);
4104 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4105 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4106 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
4107 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
4109 // Global -> local mappings.
4110 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
4114 ASTReader::ASTReadResult
4115 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4116 const ModuleFile *ImportedBy,
4117 unsigned ClientLoadCapabilities) {
4118 unsigned Idx = 0;
4119 F.ModuleMapPath = ReadPath(F, Record, Idx);
4121 // Try to resolve ModuleName in the current header search context and
4122 // verify that it is found in the same module map file as we saved. If the
4123 // top-level AST file is a main file, skip this check because there is no
4124 // usable header search context.
4125 assert(!F.ModuleName.empty() &&
4126 "MODULE_NAME should come before MODULE_MAP_FILE");
4127 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4128 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4129 // An implicitly-loaded module file should have its module listed in some
4130 // module map file that we've already loaded.
4131 Module *M =
4132 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
4133 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4134 OptionalFileEntryRef ModMap =
4135 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4136 // Don't emit module relocation error if we have -fno-validate-pch
4137 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4138 DisableValidationForModuleKind::Module) &&
4139 !ModMap) {
4140 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
4141 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4142 // This module was defined by an imported (explicit) module.
4143 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
4144 << ASTFE->getName();
4145 } else {
4146 // This module was built with a different module map.
4147 Diag(diag::err_imported_module_not_found)
4148 << F.ModuleName << F.FileName
4149 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4150 << !ImportedBy;
4151 // In case it was imported by a PCH, there's a chance the user is
4152 // just missing to include the search path to the directory containing
4153 // the modulemap.
4154 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4155 Diag(diag::note_imported_by_pch_module_not_found)
4156 << llvm::sys::path::parent_path(F.ModuleMapPath);
4159 return OutOfDate;
4162 assert(M && M->Name == F.ModuleName && "found module with different name");
4164 // Check the primary module map file.
4165 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
4166 if (!StoredModMap || *StoredModMap != ModMap) {
4167 assert(ModMap && "found module is missing module map file");
4168 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4169 "top-level import should be verified");
4170 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4171 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4172 Diag(diag::err_imported_module_modmap_changed)
4173 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4174 << ModMap->getName() << F.ModuleMapPath << NotImported;
4175 return OutOfDate;
4178 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4179 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4180 // FIXME: we should use input files rather than storing names.
4181 std::string Filename = ReadPath(F, Record, Idx);
4182 auto SF = FileMgr.getOptionalFileRef(Filename, false, false);
4183 if (!SF) {
4184 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4185 Error("could not find file '" + Filename +"' referenced by AST file");
4186 return OutOfDate;
4188 AdditionalStoredMaps.insert(*SF);
4191 // Check any additional module map files (e.g. module.private.modulemap)
4192 // that are not in the pcm.
4193 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4194 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4195 // Remove files that match
4196 // Note: SmallPtrSet::erase is really remove
4197 if (!AdditionalStoredMaps.erase(ModMap)) {
4198 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4199 Diag(diag::err_module_different_modmap)
4200 << F.ModuleName << /*new*/0 << ModMap.getName();
4201 return OutOfDate;
4206 // Check any additional module map files that are in the pcm, but not
4207 // found in header search. Cases that match are already removed.
4208 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4209 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4210 Diag(diag::err_module_different_modmap)
4211 << F.ModuleName << /*not new*/1 << ModMap.getName();
4212 return OutOfDate;
4216 if (Listener)
4217 Listener->ReadModuleMapFile(F.ModuleMapPath);
4218 return Success;
4221 /// Move the given method to the back of the global list of methods.
4222 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4223 // Find the entry for this selector in the method pool.
4224 Sema::GlobalMethodPool::iterator Known
4225 = S.MethodPool.find(Method->getSelector());
4226 if (Known == S.MethodPool.end())
4227 return;
4229 // Retrieve the appropriate method list.
4230 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4231 : Known->second.second;
4232 bool Found = false;
4233 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4234 if (!Found) {
4235 if (List->getMethod() == Method) {
4236 Found = true;
4237 } else {
4238 // Keep searching.
4239 continue;
4243 if (List->getNext())
4244 List->setMethod(List->getNext()->getMethod());
4245 else
4246 List->setMethod(Method);
4250 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4251 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4252 for (Decl *D : Names) {
4253 bool wasHidden = !D->isUnconditionallyVisible();
4254 D->setVisibleDespiteOwningModule();
4256 if (wasHidden && SemaObj) {
4257 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4258 moveMethodToBackOfGlobalList(*SemaObj, Method);
4264 void ASTReader::makeModuleVisible(Module *Mod,
4265 Module::NameVisibilityKind NameVisibility,
4266 SourceLocation ImportLoc) {
4267 llvm::SmallPtrSet<Module *, 4> Visited;
4268 SmallVector<Module *, 4> Stack;
4269 Stack.push_back(Mod);
4270 while (!Stack.empty()) {
4271 Mod = Stack.pop_back_val();
4273 if (NameVisibility <= Mod->NameVisibility) {
4274 // This module already has this level of visibility (or greater), so
4275 // there is nothing more to do.
4276 continue;
4279 if (Mod->isUnimportable()) {
4280 // Modules that aren't importable cannot be made visible.
4281 continue;
4284 // Update the module's name visibility.
4285 Mod->NameVisibility = NameVisibility;
4287 // If we've already deserialized any names from this module,
4288 // mark them as visible.
4289 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4290 if (Hidden != HiddenNamesMap.end()) {
4291 auto HiddenNames = std::move(*Hidden);
4292 HiddenNamesMap.erase(Hidden);
4293 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4294 assert(!HiddenNamesMap.contains(Mod) &&
4295 "making names visible added hidden names");
4298 // Push any exported modules onto the stack to be marked as visible.
4299 SmallVector<Module *, 16> Exports;
4300 Mod->getExportedModules(Exports);
4301 for (SmallVectorImpl<Module *>::iterator
4302 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4303 Module *Exported = *I;
4304 if (Visited.insert(Exported).second)
4305 Stack.push_back(Exported);
4310 /// We've merged the definition \p MergedDef into the existing definition
4311 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4312 /// visible.
4313 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4314 NamedDecl *MergedDef) {
4315 if (!Def->isUnconditionallyVisible()) {
4316 // If MergedDef is visible or becomes visible, make the definition visible.
4317 if (MergedDef->isUnconditionallyVisible())
4318 Def->setVisibleDespiteOwningModule();
4319 else {
4320 getContext().mergeDefinitionIntoModule(
4321 Def, MergedDef->getImportedOwningModule(),
4322 /*NotifyListeners*/ false);
4323 PendingMergedDefinitionsToDeduplicate.insert(Def);
4328 bool ASTReader::loadGlobalIndex() {
4329 if (GlobalIndex)
4330 return false;
4332 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4333 !PP.getLangOpts().Modules)
4334 return true;
4336 // Try to load the global index.
4337 TriedLoadingGlobalIndex = true;
4338 StringRef ModuleCachePath
4339 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4340 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4341 GlobalModuleIndex::readIndex(ModuleCachePath);
4342 if (llvm::Error Err = std::move(Result.second)) {
4343 assert(!Result.first);
4344 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4345 return true;
4348 GlobalIndex.reset(Result.first);
4349 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4350 return false;
4353 bool ASTReader::isGlobalIndexUnavailable() const {
4354 return PP.getLangOpts().Modules && UseGlobalIndex &&
4355 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4358 static void updateModuleTimestamp(ModuleFile &MF) {
4359 // Overwrite the timestamp file contents so that file's mtime changes.
4360 std::string TimestampFilename = MF.getTimestampFilename();
4361 std::error_code EC;
4362 llvm::raw_fd_ostream OS(TimestampFilename, EC,
4363 llvm::sys::fs::OF_TextWithCRLF);
4364 if (EC)
4365 return;
4366 OS << "Timestamp file\n";
4367 OS.close();
4368 OS.clear_error(); // Avoid triggering a fatal error.
4371 /// Given a cursor at the start of an AST file, scan ahead and drop the
4372 /// cursor into the start of the given block ID, returning false on success and
4373 /// true on failure.
4374 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4375 while (true) {
4376 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4377 if (!MaybeEntry) {
4378 // FIXME this drops errors on the floor.
4379 consumeError(MaybeEntry.takeError());
4380 return true;
4382 llvm::BitstreamEntry Entry = MaybeEntry.get();
4384 switch (Entry.Kind) {
4385 case llvm::BitstreamEntry::Error:
4386 case llvm::BitstreamEntry::EndBlock:
4387 return true;
4389 case llvm::BitstreamEntry::Record:
4390 // Ignore top-level records.
4391 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4392 break;
4393 else {
4394 // FIXME this drops errors on the floor.
4395 consumeError(Skipped.takeError());
4396 return true;
4399 case llvm::BitstreamEntry::SubBlock:
4400 if (Entry.ID == BlockID) {
4401 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4402 // FIXME this drops the error on the floor.
4403 consumeError(std::move(Err));
4404 return true;
4406 // Found it!
4407 return false;
4410 if (llvm::Error Err = Cursor.SkipBlock()) {
4411 // FIXME this drops the error on the floor.
4412 consumeError(std::move(Err));
4413 return true;
4419 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4420 SourceLocation ImportLoc,
4421 unsigned ClientLoadCapabilities,
4422 ModuleFile **NewLoadedModuleFile) {
4423 llvm::TimeTraceScope scope("ReadAST", FileName);
4425 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4426 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4427 CurrentDeserializingModuleKind, Type);
4429 // Defer any pending actions until we get to the end of reading the AST file.
4430 Deserializing AnASTFile(this);
4432 // Bump the generation number.
4433 unsigned PreviousGeneration = 0;
4434 if (ContextObj)
4435 PreviousGeneration = incrementGeneration(*ContextObj);
4437 unsigned NumModules = ModuleMgr.size();
4438 SmallVector<ImportedModule, 4> Loaded;
4439 if (ASTReadResult ReadResult =
4440 ReadASTCore(FileName, Type, ImportLoc,
4441 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4442 ClientLoadCapabilities)) {
4443 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
4445 // If we find that any modules are unusable, the global index is going
4446 // to be out-of-date. Just remove it.
4447 GlobalIndex.reset();
4448 ModuleMgr.setGlobalIndex(nullptr);
4449 return ReadResult;
4452 if (NewLoadedModuleFile && !Loaded.empty())
4453 *NewLoadedModuleFile = Loaded.back().Mod;
4455 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4456 // remove modules from this point. Various fields are updated during reading
4457 // the AST block and removing the modules would result in dangling pointers.
4458 // They are generally only incidentally dereferenced, ie. a binary search
4459 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4460 // be dereferenced but it wouldn't actually be used.
4462 // Load the AST blocks of all of the modules that we loaded. We can still
4463 // hit errors parsing the ASTs at this point.
4464 for (ImportedModule &M : Loaded) {
4465 ModuleFile &F = *M.Mod;
4466 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4468 // Read the AST block.
4469 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4470 Error(std::move(Err));
4471 return Failure;
4474 // The AST block should always have a definition for the main module.
4475 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4476 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4477 return Failure;
4480 // Read the extension blocks.
4481 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4482 if (llvm::Error Err = ReadExtensionBlock(F)) {
4483 Error(std::move(Err));
4484 return Failure;
4488 // Once read, set the ModuleFile bit base offset and update the size in
4489 // bits of all files we've seen.
4490 F.GlobalBitOffset = TotalModulesSizeInBits;
4491 TotalModulesSizeInBits += F.SizeInBits;
4492 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4495 // Preload source locations and interesting indentifiers.
4496 for (ImportedModule &M : Loaded) {
4497 ModuleFile &F = *M.Mod;
4499 // Map the original source file ID into the ID space of the current
4500 // compilation.
4501 if (F.OriginalSourceFileID.isValid())
4502 F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
4504 for (auto Offset : F.PreloadIdentifierOffsets) {
4505 const unsigned char *Data = F.IdentifierTableData + Offset;
4507 ASTIdentifierLookupTrait Trait(*this, F);
4508 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4509 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4511 IdentifierInfo *II;
4512 if (!PP.getLangOpts().CPlusPlus) {
4513 // Identifiers present in both the module file and the importing
4514 // instance are marked out-of-date so that they can be deserialized
4515 // on next use via ASTReader::updateOutOfDateIdentifier().
4516 // Identifiers present in the module file but not in the importing
4517 // instance are ignored for now, preventing growth of the identifier
4518 // table. They will be deserialized on first use via ASTReader::get().
4519 auto It = PP.getIdentifierTable().find(Key);
4520 if (It == PP.getIdentifierTable().end())
4521 continue;
4522 II = It->second;
4523 } else {
4524 // With C++ modules, not many identifiers are considered interesting.
4525 // All identifiers in the module file can be placed into the identifier
4526 // table of the importing instance and marked as out-of-date. This makes
4527 // ASTReader::get() a no-op, and deserialization will take place on
4528 // first/next use via ASTReader::updateOutOfDateIdentifier().
4529 II = &PP.getIdentifierTable().getOwn(Key);
4532 II->setOutOfDate(true);
4534 // Mark this identifier as being from an AST file so that we can track
4535 // whether we need to serialize it.
4536 markIdentifierFromAST(*this, *II);
4538 // Associate the ID with the identifier so that the writer can reuse it.
4539 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4540 SetIdentifierInfo(ID, II);
4544 // Builtins and library builtins have already been initialized. Mark all
4545 // identifiers as out-of-date, so that they are deserialized on first use.
4546 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4547 for (auto &Id : PP.getIdentifierTable())
4548 Id.second->setOutOfDate(true);
4550 // Mark selectors as out of date.
4551 for (const auto &Sel : SelectorGeneration)
4552 SelectorOutOfDate[Sel.first] = true;
4554 // Setup the import locations and notify the module manager that we've
4555 // committed to these module files.
4556 for (ImportedModule &M : Loaded) {
4557 ModuleFile &F = *M.Mod;
4559 ModuleMgr.moduleFileAccepted(&F);
4561 // Set the import location.
4562 F.DirectImportLoc = ImportLoc;
4563 // FIXME: We assume that locations from PCH / preamble do not need
4564 // any translation.
4565 if (!M.ImportedBy)
4566 F.ImportLoc = M.ImportLoc;
4567 else
4568 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4571 // Resolve any unresolved module exports.
4572 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4573 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4574 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4575 Module *ResolvedMod = getSubmodule(GlobalID);
4577 switch (Unresolved.Kind) {
4578 case UnresolvedModuleRef::Conflict:
4579 if (ResolvedMod) {
4580 Module::Conflict Conflict;
4581 Conflict.Other = ResolvedMod;
4582 Conflict.Message = Unresolved.String.str();
4583 Unresolved.Mod->Conflicts.push_back(Conflict);
4585 continue;
4587 case UnresolvedModuleRef::Import:
4588 if (ResolvedMod)
4589 Unresolved.Mod->Imports.insert(ResolvedMod);
4590 continue;
4592 case UnresolvedModuleRef::Affecting:
4593 if (ResolvedMod)
4594 Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);
4595 continue;
4597 case UnresolvedModuleRef::Export:
4598 if (ResolvedMod || Unresolved.IsWildcard)
4599 Unresolved.Mod->Exports.push_back(
4600 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4601 continue;
4604 UnresolvedModuleRefs.clear();
4606 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4607 // Might be unnecessary as use declarations are only used to build the
4608 // module itself.
4610 if (ContextObj)
4611 InitializeContext();
4613 if (SemaObj)
4614 UpdateSema();
4616 if (DeserializationListener)
4617 DeserializationListener->ReaderInitialized(this);
4619 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4620 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4621 // If this AST file is a precompiled preamble, then set the
4622 // preamble file ID of the source manager to the file source file
4623 // from which the preamble was built.
4624 if (Type == MK_Preamble) {
4625 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4626 } else if (Type == MK_MainFile) {
4627 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4631 // For any Objective-C class definitions we have already loaded, make sure
4632 // that we load any additional categories.
4633 if (ContextObj) {
4634 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4635 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4636 ObjCClassesLoaded[I],
4637 PreviousGeneration);
4641 HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
4642 if (HSOpts.ModulesValidateOncePerBuildSession) {
4643 // Now we are certain that the module and all modules it depends on are
4644 // up-to-date. For implicitly-built module files, ensure the corresponding
4645 // timestamp files are up-to-date in this build session.
4646 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4647 ImportedModule &M = Loaded[I];
4648 if (M.Mod->Kind == MK_ImplicitModule &&
4649 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4650 updateModuleTimestamp(*M.Mod);
4654 return Success;
4657 static ASTFileSignature readASTFileSignature(StringRef PCH);
4659 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4660 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4661 // FIXME checking magic headers is done in other places such as
4662 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4663 // always done the same. Unify it all with a helper.
4664 if (!Stream.canSkipToPos(4))
4665 return llvm::createStringError(std::errc::illegal_byte_sequence,
4666 "file too small to contain AST file magic");
4667 for (unsigned C : {'C', 'P', 'C', 'H'})
4668 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4669 if (Res.get() != C)
4670 return llvm::createStringError(
4671 std::errc::illegal_byte_sequence,
4672 "file doesn't start with AST file magic");
4673 } else
4674 return Res.takeError();
4675 return llvm::Error::success();
4678 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4679 switch (Kind) {
4680 case MK_PCH:
4681 return 0; // PCH
4682 case MK_ImplicitModule:
4683 case MK_ExplicitModule:
4684 case MK_PrebuiltModule:
4685 return 1; // module
4686 case MK_MainFile:
4687 case MK_Preamble:
4688 return 2; // main source file
4690 llvm_unreachable("unknown module kind");
4693 ASTReader::ASTReadResult
4694 ASTReader::ReadASTCore(StringRef FileName,
4695 ModuleKind Type,
4696 SourceLocation ImportLoc,
4697 ModuleFile *ImportedBy,
4698 SmallVectorImpl<ImportedModule> &Loaded,
4699 off_t ExpectedSize, time_t ExpectedModTime,
4700 ASTFileSignature ExpectedSignature,
4701 unsigned ClientLoadCapabilities) {
4702 ModuleFile *M;
4703 std::string ErrorStr;
4704 ModuleManager::AddModuleResult AddResult
4705 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4706 getGeneration(), ExpectedSize, ExpectedModTime,
4707 ExpectedSignature, readASTFileSignature,
4708 M, ErrorStr);
4710 switch (AddResult) {
4711 case ModuleManager::AlreadyLoaded:
4712 Diag(diag::remark_module_import)
4713 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4714 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4715 return Success;
4717 case ModuleManager::NewlyLoaded:
4718 // Load module file below.
4719 break;
4721 case ModuleManager::Missing:
4722 // The module file was missing; if the client can handle that, return
4723 // it.
4724 if (ClientLoadCapabilities & ARR_Missing)
4725 return Missing;
4727 // Otherwise, return an error.
4728 Diag(diag::err_ast_file_not_found)
4729 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4730 << ErrorStr;
4731 return Failure;
4733 case ModuleManager::OutOfDate:
4734 // We couldn't load the module file because it is out-of-date. If the
4735 // client can handle out-of-date, return it.
4736 if (ClientLoadCapabilities & ARR_OutOfDate)
4737 return OutOfDate;
4739 // Otherwise, return an error.
4740 Diag(diag::err_ast_file_out_of_date)
4741 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4742 << ErrorStr;
4743 return Failure;
4746 assert(M && "Missing module file");
4748 bool ShouldFinalizePCM = false;
4749 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4750 auto &MC = getModuleManager().getModuleCache();
4751 if (ShouldFinalizePCM)
4752 MC.finalizePCM(FileName);
4753 else
4754 MC.tryToDropPCM(FileName);
4756 ModuleFile &F = *M;
4757 BitstreamCursor &Stream = F.Stream;
4758 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4759 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4761 // Sniff for the signature.
4762 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4763 Diag(diag::err_ast_file_invalid)
4764 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4765 return Failure;
4768 // This is used for compatibility with older PCH formats.
4769 bool HaveReadControlBlock = false;
4770 while (true) {
4771 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4772 if (!MaybeEntry) {
4773 Error(MaybeEntry.takeError());
4774 return Failure;
4776 llvm::BitstreamEntry Entry = MaybeEntry.get();
4778 switch (Entry.Kind) {
4779 case llvm::BitstreamEntry::Error:
4780 case llvm::BitstreamEntry::Record:
4781 case llvm::BitstreamEntry::EndBlock:
4782 Error("invalid record at top-level of AST file");
4783 return Failure;
4785 case llvm::BitstreamEntry::SubBlock:
4786 break;
4789 switch (Entry.ID) {
4790 case CONTROL_BLOCK_ID:
4791 HaveReadControlBlock = true;
4792 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4793 case Success:
4794 // Check that we didn't try to load a non-module AST file as a module.
4796 // FIXME: Should we also perform the converse check? Loading a module as
4797 // a PCH file sort of works, but it's a bit wonky.
4798 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4799 Type == MK_PrebuiltModule) &&
4800 F.ModuleName.empty()) {
4801 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4802 if (Result != OutOfDate ||
4803 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4804 Diag(diag::err_module_file_not_module) << FileName;
4805 return Result;
4807 break;
4809 case Failure: return Failure;
4810 case Missing: return Missing;
4811 case OutOfDate: return OutOfDate;
4812 case VersionMismatch: return VersionMismatch;
4813 case ConfigurationMismatch: return ConfigurationMismatch;
4814 case HadErrors: return HadErrors;
4816 break;
4818 case AST_BLOCK_ID:
4819 if (!HaveReadControlBlock) {
4820 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4821 Diag(diag::err_pch_version_too_old);
4822 return VersionMismatch;
4825 // Record that we've loaded this module.
4826 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4827 ShouldFinalizePCM = true;
4828 return Success;
4830 default:
4831 if (llvm::Error Err = Stream.SkipBlock()) {
4832 Error(std::move(Err));
4833 return Failure;
4835 break;
4839 llvm_unreachable("unexpected break; expected return");
4842 ASTReader::ASTReadResult
4843 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4844 unsigned ClientLoadCapabilities) {
4845 const HeaderSearchOptions &HSOpts =
4846 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4847 bool AllowCompatibleConfigurationMismatch =
4848 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4849 bool DisableValidation = shouldDisableValidationForFile(F);
4851 ASTReadResult Result = readUnhashedControlBlockImpl(
4852 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4853 Listener.get(),
4854 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4856 // If F was directly imported by another module, it's implicitly validated by
4857 // the importing module.
4858 if (DisableValidation || WasImportedBy ||
4859 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4860 return Success;
4862 if (Result == Failure) {
4863 Error("malformed block record in AST file");
4864 return Failure;
4867 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4868 // If this module has already been finalized in the ModuleCache, we're stuck
4869 // with it; we can only load a single version of each module.
4871 // This can happen when a module is imported in two contexts: in one, as a
4872 // user module; in another, as a system module (due to an import from
4873 // another module marked with the [system] flag). It usually indicates a
4874 // bug in the module map: this module should also be marked with [system].
4876 // If -Wno-system-headers (the default), and the first import is as a
4877 // system module, then validation will fail during the as-user import,
4878 // since -Werror flags won't have been validated. However, it's reasonable
4879 // to treat this consistently as a system module.
4881 // If -Wsystem-headers, the PCM on disk was built with
4882 // -Wno-system-headers, and the first import is as a user module, then
4883 // validation will fail during the as-system import since the PCM on disk
4884 // doesn't guarantee that -Werror was respected. However, the -Werror
4885 // flags were checked during the initial as-user import.
4886 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4887 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4888 return Success;
4892 return Result;
4895 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4896 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4897 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4898 bool ValidateDiagnosticOptions) {
4899 // Initialize a stream.
4900 BitstreamCursor Stream(StreamData);
4902 // Sniff for the signature.
4903 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4904 // FIXME this drops the error on the floor.
4905 consumeError(std::move(Err));
4906 return Failure;
4909 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4910 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4911 return Failure;
4913 // Read all of the records in the options block.
4914 RecordData Record;
4915 ASTReadResult Result = Success;
4916 while (true) {
4917 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4918 if (!MaybeEntry) {
4919 // FIXME this drops the error on the floor.
4920 consumeError(MaybeEntry.takeError());
4921 return Failure;
4923 llvm::BitstreamEntry Entry = MaybeEntry.get();
4925 switch (Entry.Kind) {
4926 case llvm::BitstreamEntry::Error:
4927 case llvm::BitstreamEntry::SubBlock:
4928 return Failure;
4930 case llvm::BitstreamEntry::EndBlock:
4931 return Result;
4933 case llvm::BitstreamEntry::Record:
4934 // The interesting case.
4935 break;
4938 // Read and process a record.
4939 Record.clear();
4940 StringRef Blob;
4941 Expected<unsigned> MaybeRecordType =
4942 Stream.readRecord(Entry.ID, Record, &Blob);
4943 if (!MaybeRecordType) {
4944 // FIXME this drops the error.
4945 return Failure;
4947 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4948 case SIGNATURE:
4949 if (F) {
4950 F->Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
4951 assert(F->Signature != ASTFileSignature::createDummy() &&
4952 "Dummy AST file signature not backpatched in ASTWriter.");
4954 break;
4955 case AST_BLOCK_HASH:
4956 if (F) {
4957 F->ASTBlockHash = ASTFileSignature::create(Blob.begin(), Blob.end());
4958 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
4959 "Dummy AST block hash not backpatched in ASTWriter.");
4961 break;
4962 case DIAGNOSTIC_OPTIONS: {
4963 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4964 if (Listener && ValidateDiagnosticOptions &&
4965 !AllowCompatibleConfigurationMismatch &&
4966 ParseDiagnosticOptions(Record, Complain, *Listener))
4967 Result = OutOfDate; // Don't return early. Read the signature.
4968 break;
4970 case HEADER_SEARCH_PATHS: {
4971 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
4972 if (!AllowCompatibleConfigurationMismatch &&
4973 ParseHeaderSearchPaths(Record, Complain, *Listener))
4974 Result = ConfigurationMismatch;
4975 break;
4977 case DIAG_PRAGMA_MAPPINGS:
4978 if (!F)
4979 break;
4980 if (F->PragmaDiagMappings.empty())
4981 F->PragmaDiagMappings.swap(Record);
4982 else
4983 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4984 Record.begin(), Record.end());
4985 break;
4986 case HEADER_SEARCH_ENTRY_USAGE:
4987 if (!F)
4988 break;
4989 unsigned Count = Record[0];
4990 const char *Byte = Blob.data();
4991 F->SearchPathUsage = llvm::BitVector(Count, false);
4992 for (unsigned I = 0; I < Count; ++Byte)
4993 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4994 if (*Byte & (1 << Bit))
4995 F->SearchPathUsage[I] = true;
4996 break;
5001 /// Parse a record and blob containing module file extension metadata.
5002 static bool parseModuleFileExtensionMetadata(
5003 const SmallVectorImpl<uint64_t> &Record,
5004 StringRef Blob,
5005 ModuleFileExtensionMetadata &Metadata) {
5006 if (Record.size() < 4) return true;
5008 Metadata.MajorVersion = Record[0];
5009 Metadata.MinorVersion = Record[1];
5011 unsigned BlockNameLen = Record[2];
5012 unsigned UserInfoLen = Record[3];
5014 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5016 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5017 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5018 Blob.data() + BlockNameLen + UserInfoLen);
5019 return false;
5022 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5023 BitstreamCursor &Stream = F.Stream;
5025 RecordData Record;
5026 while (true) {
5027 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5028 if (!MaybeEntry)
5029 return MaybeEntry.takeError();
5030 llvm::BitstreamEntry Entry = MaybeEntry.get();
5032 switch (Entry.Kind) {
5033 case llvm::BitstreamEntry::SubBlock:
5034 if (llvm::Error Err = Stream.SkipBlock())
5035 return Err;
5036 continue;
5037 case llvm::BitstreamEntry::EndBlock:
5038 return llvm::Error::success();
5039 case llvm::BitstreamEntry::Error:
5040 return llvm::createStringError(std::errc::illegal_byte_sequence,
5041 "malformed block record in AST file");
5042 case llvm::BitstreamEntry::Record:
5043 break;
5046 Record.clear();
5047 StringRef Blob;
5048 Expected<unsigned> MaybeRecCode =
5049 Stream.readRecord(Entry.ID, Record, &Blob);
5050 if (!MaybeRecCode)
5051 return MaybeRecCode.takeError();
5052 switch (MaybeRecCode.get()) {
5053 case EXTENSION_METADATA: {
5054 ModuleFileExtensionMetadata Metadata;
5055 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5056 return llvm::createStringError(
5057 std::errc::illegal_byte_sequence,
5058 "malformed EXTENSION_METADATA in AST file");
5060 // Find a module file extension with this block name.
5061 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
5062 if (Known == ModuleFileExtensions.end()) break;
5064 // Form a reader.
5065 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
5066 F, Stream)) {
5067 F.ExtensionReaders.push_back(std::move(Reader));
5070 break;
5075 return llvm::Error::success();
5078 void ASTReader::InitializeContext() {
5079 assert(ContextObj && "no context to initialize");
5080 ASTContext &Context = *ContextObj;
5082 // If there's a listener, notify them that we "read" the translation unit.
5083 if (DeserializationListener)
5084 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
5085 Context.getTranslationUnitDecl());
5087 // FIXME: Find a better way to deal with collisions between these
5088 // built-in types. Right now, we just ignore the problem.
5090 // Load the special types.
5091 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5092 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5093 if (!Context.CFConstantStringTypeDecl)
5094 Context.setCFConstantStringType(GetType(String));
5097 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5098 QualType FileType = GetType(File);
5099 if (FileType.isNull()) {
5100 Error("FILE type is NULL");
5101 return;
5104 if (!Context.FILEDecl) {
5105 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5106 Context.setFILEDecl(Typedef->getDecl());
5107 else {
5108 const TagType *Tag = FileType->getAs<TagType>();
5109 if (!Tag) {
5110 Error("Invalid FILE type in AST file");
5111 return;
5113 Context.setFILEDecl(Tag->getDecl());
5118 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5119 QualType Jmp_bufType = GetType(Jmp_buf);
5120 if (Jmp_bufType.isNull()) {
5121 Error("jmp_buf type is NULL");
5122 return;
5125 if (!Context.jmp_bufDecl) {
5126 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5127 Context.setjmp_bufDecl(Typedef->getDecl());
5128 else {
5129 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5130 if (!Tag) {
5131 Error("Invalid jmp_buf type in AST file");
5132 return;
5134 Context.setjmp_bufDecl(Tag->getDecl());
5139 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5140 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
5141 if (Sigjmp_bufType.isNull()) {
5142 Error("sigjmp_buf type is NULL");
5143 return;
5146 if (!Context.sigjmp_bufDecl) {
5147 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5148 Context.setsigjmp_bufDecl(Typedef->getDecl());
5149 else {
5150 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5151 assert(Tag && "Invalid sigjmp_buf type in AST file");
5152 Context.setsigjmp_bufDecl(Tag->getDecl());
5157 if (unsigned ObjCIdRedef
5158 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5159 if (Context.ObjCIdRedefinitionType.isNull())
5160 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
5163 if (unsigned ObjCClassRedef
5164 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5165 if (Context.ObjCClassRedefinitionType.isNull())
5166 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
5169 if (unsigned ObjCSelRedef
5170 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5171 if (Context.ObjCSelRedefinitionType.isNull())
5172 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
5175 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5176 QualType Ucontext_tType = GetType(Ucontext_t);
5177 if (Ucontext_tType.isNull()) {
5178 Error("ucontext_t type is NULL");
5179 return;
5182 if (!Context.ucontext_tDecl) {
5183 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5184 Context.setucontext_tDecl(Typedef->getDecl());
5185 else {
5186 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5187 assert(Tag && "Invalid ucontext_t type in AST file");
5188 Context.setucontext_tDecl(Tag->getDecl());
5194 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5196 // If there were any CUDA special declarations, deserialize them.
5197 if (!CUDASpecialDeclRefs.empty()) {
5198 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5199 Context.setcudaConfigureCallDecl(
5200 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5203 // Re-export any modules that were imported by a non-module AST file.
5204 // FIXME: This does not make macro-only imports visible again.
5205 for (auto &Import : PendingImportedModules) {
5206 if (Module *Imported = getSubmodule(Import.ID)) {
5207 makeModuleVisible(Imported, Module::AllVisible,
5208 /*ImportLoc=*/Import.ImportLoc);
5209 if (Import.ImportLoc.isValid())
5210 PP.makeModuleVisible(Imported, Import.ImportLoc);
5211 // This updates visibility for Preprocessor only. For Sema, which can be
5212 // nullptr here, we do the same later, in UpdateSema().
5216 // Hand off these modules to Sema.
5217 PendingImportedModulesSema.append(PendingImportedModules);
5218 PendingImportedModules.clear();
5221 void ASTReader::finalizeForWriting() {
5222 // Nothing to do for now.
5225 /// Reads and return the signature record from \p PCH's control block, or
5226 /// else returns 0.
5227 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5228 BitstreamCursor Stream(PCH);
5229 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5230 // FIXME this drops the error on the floor.
5231 consumeError(std::move(Err));
5232 return ASTFileSignature();
5235 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5236 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5237 return ASTFileSignature();
5239 // Scan for SIGNATURE inside the diagnostic options block.
5240 ASTReader::RecordData Record;
5241 while (true) {
5242 Expected<llvm::BitstreamEntry> MaybeEntry =
5243 Stream.advanceSkippingSubblocks();
5244 if (!MaybeEntry) {
5245 // FIXME this drops the error on the floor.
5246 consumeError(MaybeEntry.takeError());
5247 return ASTFileSignature();
5249 llvm::BitstreamEntry Entry = MaybeEntry.get();
5251 if (Entry.Kind != llvm::BitstreamEntry::Record)
5252 return ASTFileSignature();
5254 Record.clear();
5255 StringRef Blob;
5256 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5257 if (!MaybeRecord) {
5258 // FIXME this drops the error on the floor.
5259 consumeError(MaybeRecord.takeError());
5260 return ASTFileSignature();
5262 if (SIGNATURE == MaybeRecord.get()) {
5263 auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5264 assert(Signature != ASTFileSignature::createDummy() &&
5265 "Dummy AST file signature not backpatched in ASTWriter.");
5266 return Signature;
5271 /// Retrieve the name of the original source file name
5272 /// directly from the AST file, without actually loading the AST
5273 /// file.
5274 std::string ASTReader::getOriginalSourceFile(
5275 const std::string &ASTFileName, FileManager &FileMgr,
5276 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5277 // Open the AST file.
5278 auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5279 /*RequiresNullTerminator=*/false);
5280 if (!Buffer) {
5281 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5282 << ASTFileName << Buffer.getError().message();
5283 return std::string();
5286 // Initialize the stream
5287 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5289 // Sniff for the signature.
5290 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5291 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5292 return std::string();
5295 // Scan for the CONTROL_BLOCK_ID block.
5296 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5297 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5298 return std::string();
5301 // Scan for ORIGINAL_FILE inside the control block.
5302 RecordData Record;
5303 while (true) {
5304 Expected<llvm::BitstreamEntry> MaybeEntry =
5305 Stream.advanceSkippingSubblocks();
5306 if (!MaybeEntry) {
5307 // FIXME this drops errors on the floor.
5308 consumeError(MaybeEntry.takeError());
5309 return std::string();
5311 llvm::BitstreamEntry Entry = MaybeEntry.get();
5313 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5314 return std::string();
5316 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5317 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5318 return std::string();
5321 Record.clear();
5322 StringRef Blob;
5323 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5324 if (!MaybeRecord) {
5325 // FIXME this drops the errors on the floor.
5326 consumeError(MaybeRecord.takeError());
5327 return std::string();
5329 if (ORIGINAL_FILE == MaybeRecord.get())
5330 return Blob.str();
5334 namespace {
5336 class SimplePCHValidator : public ASTReaderListener {
5337 const LangOptions &ExistingLangOpts;
5338 const TargetOptions &ExistingTargetOpts;
5339 const PreprocessorOptions &ExistingPPOpts;
5340 std::string ExistingModuleCachePath;
5341 FileManager &FileMgr;
5342 bool StrictOptionMatches;
5344 public:
5345 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5346 const TargetOptions &ExistingTargetOpts,
5347 const PreprocessorOptions &ExistingPPOpts,
5348 StringRef ExistingModuleCachePath, FileManager &FileMgr,
5349 bool StrictOptionMatches)
5350 : ExistingLangOpts(ExistingLangOpts),
5351 ExistingTargetOpts(ExistingTargetOpts),
5352 ExistingPPOpts(ExistingPPOpts),
5353 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5354 StrictOptionMatches(StrictOptionMatches) {}
5356 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5357 bool AllowCompatibleDifferences) override {
5358 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5359 AllowCompatibleDifferences);
5362 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5363 bool AllowCompatibleDifferences) override {
5364 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5365 AllowCompatibleDifferences);
5368 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5369 StringRef SpecificModuleCachePath,
5370 bool Complain) override {
5371 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5372 ExistingModuleCachePath, nullptr,
5373 ExistingLangOpts, ExistingPPOpts);
5376 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5377 bool ReadMacros, bool Complain,
5378 std::string &SuggestedPredefines) override {
5379 return checkPreprocessorOptions(
5380 PPOpts, ExistingPPOpts, ReadMacros, /*Diags=*/nullptr, FileMgr,
5381 SuggestedPredefines, ExistingLangOpts,
5382 StrictOptionMatches ? OptionValidateStrictMatches
5383 : OptionValidateContradictions);
5387 } // namespace
5389 bool ASTReader::readASTFileControlBlock(
5390 StringRef Filename, FileManager &FileMgr,
5391 const InMemoryModuleCache &ModuleCache,
5392 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5393 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5394 // Open the AST file.
5395 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5396 llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
5397 if (!Buffer) {
5398 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5399 // read again later, but we do not have the context here to determine if it
5400 // is safe to change the result of InMemoryModuleCache::getPCMState().
5402 // FIXME: This allows use of the VFS; we do not allow use of the
5403 // VFS when actually loading a module.
5404 auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5405 if (!BufferOrErr)
5406 return true;
5407 OwnedBuffer = std::move(*BufferOrErr);
5408 Buffer = OwnedBuffer.get();
5411 // Initialize the stream
5412 StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);
5413 BitstreamCursor Stream(Bytes);
5415 // Sniff for the signature.
5416 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5417 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5418 return true;
5421 // Scan for the CONTROL_BLOCK_ID block.
5422 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5423 return true;
5425 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5426 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5427 bool NeedsImports = Listener.needsImportVisitation();
5428 BitstreamCursor InputFilesCursor;
5429 uint64_t InputFilesOffsetBase = 0;
5431 RecordData Record;
5432 std::string ModuleDir;
5433 bool DoneWithControlBlock = false;
5434 while (!DoneWithControlBlock) {
5435 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5436 if (!MaybeEntry) {
5437 // FIXME this drops the error on the floor.
5438 consumeError(MaybeEntry.takeError());
5439 return true;
5441 llvm::BitstreamEntry Entry = MaybeEntry.get();
5443 switch (Entry.Kind) {
5444 case llvm::BitstreamEntry::SubBlock: {
5445 switch (Entry.ID) {
5446 case OPTIONS_BLOCK_ID: {
5447 std::string IgnoredSuggestedPredefines;
5448 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5449 /*AllowCompatibleConfigurationMismatch*/ false,
5450 Listener, IgnoredSuggestedPredefines) != Success)
5451 return true;
5452 break;
5455 case INPUT_FILES_BLOCK_ID:
5456 InputFilesCursor = Stream;
5457 if (llvm::Error Err = Stream.SkipBlock()) {
5458 // FIXME this drops the error on the floor.
5459 consumeError(std::move(Err));
5460 return true;
5462 if (NeedsInputFiles &&
5463 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5464 return true;
5465 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5466 break;
5468 default:
5469 if (llvm::Error Err = Stream.SkipBlock()) {
5470 // FIXME this drops the error on the floor.
5471 consumeError(std::move(Err));
5472 return true;
5474 break;
5477 continue;
5480 case llvm::BitstreamEntry::EndBlock:
5481 DoneWithControlBlock = true;
5482 break;
5484 case llvm::BitstreamEntry::Error:
5485 return true;
5487 case llvm::BitstreamEntry::Record:
5488 break;
5491 if (DoneWithControlBlock) break;
5493 Record.clear();
5494 StringRef Blob;
5495 Expected<unsigned> MaybeRecCode =
5496 Stream.readRecord(Entry.ID, Record, &Blob);
5497 if (!MaybeRecCode) {
5498 // FIXME this drops the error.
5499 return Failure;
5501 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5502 case METADATA:
5503 if (Record[0] != VERSION_MAJOR)
5504 return true;
5505 if (Listener.ReadFullVersionInformation(Blob))
5506 return true;
5507 break;
5508 case MODULE_NAME:
5509 Listener.ReadModuleName(Blob);
5510 break;
5511 case MODULE_DIRECTORY:
5512 ModuleDir = std::string(Blob);
5513 break;
5514 case MODULE_MAP_FILE: {
5515 unsigned Idx = 0;
5516 auto Path = ReadString(Record, Idx);
5517 ResolveImportedPath(Path, ModuleDir);
5518 Listener.ReadModuleMapFile(Path);
5519 break;
5521 case INPUT_FILE_OFFSETS: {
5522 if (!NeedsInputFiles)
5523 break;
5525 unsigned NumInputFiles = Record[0];
5526 unsigned NumUserFiles = Record[1];
5527 const llvm::support::unaligned_uint64_t *InputFileOffs =
5528 (const llvm::support::unaligned_uint64_t *)Blob.data();
5529 for (unsigned I = 0; I != NumInputFiles; ++I) {
5530 // Go find this input file.
5531 bool isSystemFile = I >= NumUserFiles;
5533 if (isSystemFile && !NeedsSystemInputFiles)
5534 break; // the rest are system input files
5536 BitstreamCursor &Cursor = InputFilesCursor;
5537 SavedStreamPosition SavedPosition(Cursor);
5538 if (llvm::Error Err =
5539 Cursor.JumpToBit(InputFilesOffsetBase + InputFileOffs[I])) {
5540 // FIXME this drops errors on the floor.
5541 consumeError(std::move(Err));
5544 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5545 if (!MaybeCode) {
5546 // FIXME this drops errors on the floor.
5547 consumeError(MaybeCode.takeError());
5549 unsigned Code = MaybeCode.get();
5551 RecordData Record;
5552 StringRef Blob;
5553 bool shouldContinue = false;
5554 Expected<unsigned> MaybeRecordType =
5555 Cursor.readRecord(Code, Record, &Blob);
5556 if (!MaybeRecordType) {
5557 // FIXME this drops errors on the floor.
5558 consumeError(MaybeRecordType.takeError());
5560 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5561 case INPUT_FILE_HASH:
5562 break;
5563 case INPUT_FILE:
5564 bool Overridden = static_cast<bool>(Record[3]);
5565 std::string Filename = std::string(Blob);
5566 ResolveImportedPath(Filename, ModuleDir);
5567 shouldContinue = Listener.visitInputFile(
5568 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5569 break;
5571 if (!shouldContinue)
5572 break;
5574 break;
5577 case IMPORTS: {
5578 if (!NeedsImports)
5579 break;
5581 unsigned Idx = 0, N = Record.size();
5582 while (Idx < N) {
5583 // Read information about the AST file.
5585 // Kind, StandardCXXModule, ImportLoc, Size, ModTime, Signature
5586 Idx += 1 + 1 + 1 + 1 + 1 + ASTFileSignature::size;
5587 std::string ModuleName = ReadString(Record, Idx);
5588 std::string Filename = ReadString(Record, Idx);
5589 ResolveImportedPath(Filename, ModuleDir);
5590 Listener.visitImport(ModuleName, Filename);
5592 break;
5595 default:
5596 // No other validation to perform.
5597 break;
5601 // Look for module file extension blocks, if requested.
5602 if (FindModuleFileExtensions) {
5603 BitstreamCursor SavedStream = Stream;
5604 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5605 bool DoneWithExtensionBlock = false;
5606 while (!DoneWithExtensionBlock) {
5607 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5608 if (!MaybeEntry) {
5609 // FIXME this drops the error.
5610 return true;
5612 llvm::BitstreamEntry Entry = MaybeEntry.get();
5614 switch (Entry.Kind) {
5615 case llvm::BitstreamEntry::SubBlock:
5616 if (llvm::Error Err = Stream.SkipBlock()) {
5617 // FIXME this drops the error on the floor.
5618 consumeError(std::move(Err));
5619 return true;
5621 continue;
5623 case llvm::BitstreamEntry::EndBlock:
5624 DoneWithExtensionBlock = true;
5625 continue;
5627 case llvm::BitstreamEntry::Error:
5628 return true;
5630 case llvm::BitstreamEntry::Record:
5631 break;
5634 Record.clear();
5635 StringRef Blob;
5636 Expected<unsigned> MaybeRecCode =
5637 Stream.readRecord(Entry.ID, Record, &Blob);
5638 if (!MaybeRecCode) {
5639 // FIXME this drops the error.
5640 return true;
5642 switch (MaybeRecCode.get()) {
5643 case EXTENSION_METADATA: {
5644 ModuleFileExtensionMetadata Metadata;
5645 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5646 return true;
5648 Listener.readModuleFileExtension(Metadata);
5649 break;
5654 Stream = SavedStream;
5657 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5658 if (readUnhashedControlBlockImpl(
5659 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5660 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5661 ValidateDiagnosticOptions) != Success)
5662 return true;
5664 return false;
5667 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5668 const InMemoryModuleCache &ModuleCache,
5669 const PCHContainerReader &PCHContainerRdr,
5670 const LangOptions &LangOpts,
5671 const TargetOptions &TargetOpts,
5672 const PreprocessorOptions &PPOpts,
5673 StringRef ExistingModuleCachePath,
5674 bool RequireStrictOptionMatches) {
5675 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5676 ExistingModuleCachePath, FileMgr,
5677 RequireStrictOptionMatches);
5678 return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
5679 PCHContainerRdr,
5680 /*FindModuleFileExtensions=*/false, validator,
5681 /*ValidateDiagnosticOptions=*/true);
5684 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5685 unsigned ClientLoadCapabilities) {
5686 // Enter the submodule block.
5687 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5688 return Err;
5690 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5691 bool First = true;
5692 Module *CurrentModule = nullptr;
5693 RecordData Record;
5694 while (true) {
5695 Expected<llvm::BitstreamEntry> MaybeEntry =
5696 F.Stream.advanceSkippingSubblocks();
5697 if (!MaybeEntry)
5698 return MaybeEntry.takeError();
5699 llvm::BitstreamEntry Entry = MaybeEntry.get();
5701 switch (Entry.Kind) {
5702 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5703 case llvm::BitstreamEntry::Error:
5704 return llvm::createStringError(std::errc::illegal_byte_sequence,
5705 "malformed block record in AST file");
5706 case llvm::BitstreamEntry::EndBlock:
5707 return llvm::Error::success();
5708 case llvm::BitstreamEntry::Record:
5709 // The interesting case.
5710 break;
5713 // Read a record.
5714 StringRef Blob;
5715 Record.clear();
5716 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5717 if (!MaybeKind)
5718 return MaybeKind.takeError();
5719 unsigned Kind = MaybeKind.get();
5721 if ((Kind == SUBMODULE_METADATA) != First)
5722 return llvm::createStringError(
5723 std::errc::illegal_byte_sequence,
5724 "submodule metadata record should be at beginning of block");
5725 First = false;
5727 // Submodule information is only valid if we have a current module.
5728 // FIXME: Should we error on these cases?
5729 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5730 Kind != SUBMODULE_DEFINITION)
5731 continue;
5733 switch (Kind) {
5734 default: // Default behavior: ignore.
5735 break;
5737 case SUBMODULE_DEFINITION: {
5738 if (Record.size() < 13)
5739 return llvm::createStringError(std::errc::illegal_byte_sequence,
5740 "malformed module definition");
5742 StringRef Name = Blob;
5743 unsigned Idx = 0;
5744 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5745 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5746 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5747 SourceLocation DefinitionLoc = ReadSourceLocation(F, Record[Idx++]);
5748 bool IsFramework = Record[Idx++];
5749 bool IsExplicit = Record[Idx++];
5750 bool IsSystem = Record[Idx++];
5751 bool IsExternC = Record[Idx++];
5752 bool InferSubmodules = Record[Idx++];
5753 bool InferExplicitSubmodules = Record[Idx++];
5754 bool InferExportWildcard = Record[Idx++];
5755 bool ConfigMacrosExhaustive = Record[Idx++];
5756 bool ModuleMapIsPrivate = Record[Idx++];
5757 bool NamedModuleHasInit = Record[Idx++];
5759 Module *ParentModule = nullptr;
5760 if (Parent)
5761 ParentModule = getSubmodule(Parent);
5763 // Retrieve this (sub)module from the module map, creating it if
5764 // necessary.
5765 CurrentModule =
5766 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5767 .first;
5769 // FIXME: Call ModMap.setInferredModuleAllowedBy()
5771 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5772 if (GlobalIndex >= SubmodulesLoaded.size() ||
5773 SubmodulesLoaded[GlobalIndex])
5774 return llvm::createStringError(std::errc::invalid_argument,
5775 "too many submodules");
5777 if (!ParentModule) {
5778 if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
5779 // Don't emit module relocation error if we have -fno-validate-pch
5780 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5781 DisableValidationForModuleKind::Module) &&
5782 CurFile != F.File) {
5783 auto ConflictError =
5784 PartialDiagnostic(diag::err_module_file_conflict,
5785 ContextObj->DiagAllocator)
5786 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5787 << F.File->getName();
5788 return DiagnosticError::create(CurrentImportLoc, ConflictError);
5792 F.DidReadTopLevelSubmodule = true;
5793 CurrentModule->setASTFile(F.File);
5794 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5797 CurrentModule->Kind = Kind;
5798 CurrentModule->DefinitionLoc = DefinitionLoc;
5799 CurrentModule->Signature = F.Signature;
5800 CurrentModule->IsFromModuleFile = true;
5801 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5802 CurrentModule->IsExternC = IsExternC;
5803 CurrentModule->InferSubmodules = InferSubmodules;
5804 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5805 CurrentModule->InferExportWildcard = InferExportWildcard;
5806 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5807 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5808 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
5809 if (DeserializationListener)
5810 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5812 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5814 // Clear out data that will be replaced by what is in the module file.
5815 CurrentModule->LinkLibraries.clear();
5816 CurrentModule->ConfigMacros.clear();
5817 CurrentModule->UnresolvedConflicts.clear();
5818 CurrentModule->Conflicts.clear();
5820 // The module is available unless it's missing a requirement; relevant
5821 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5822 // Missing headers that were present when the module was built do not
5823 // make it unavailable -- if we got this far, this must be an explicitly
5824 // imported module file.
5825 CurrentModule->Requirements.clear();
5826 CurrentModule->MissingHeaders.clear();
5827 CurrentModule->IsUnimportable =
5828 ParentModule && ParentModule->IsUnimportable;
5829 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5830 break;
5833 case SUBMODULE_UMBRELLA_HEADER: {
5834 // FIXME: This doesn't work for framework modules as `Filename` is the
5835 // name as written in the module file and does not include
5836 // `Headers/`, so this path will never exist.
5837 std::string Filename = std::string(Blob);
5838 ResolveImportedPath(F, Filename);
5839 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) {
5840 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
5841 // FIXME: NameAsWritten
5842 ModMap.setUmbrellaHeaderAsWritten(CurrentModule, *Umbrella, Blob, "");
5844 // Note that it's too late at this point to return out of date if the
5845 // name from the PCM doesn't match up with the one in the module map,
5846 // but also quite unlikely since we will have already checked the
5847 // modification time and size of the module map file itself.
5849 break;
5852 case SUBMODULE_HEADER:
5853 case SUBMODULE_EXCLUDED_HEADER:
5854 case SUBMODULE_PRIVATE_HEADER:
5855 // We lazily associate headers with their modules via the HeaderInfo table.
5856 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5857 // of complete filenames or remove it entirely.
5858 break;
5860 case SUBMODULE_TEXTUAL_HEADER:
5861 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5862 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5863 // them here.
5864 break;
5866 case SUBMODULE_TOPHEADER: {
5867 std::string HeaderName(Blob);
5868 ResolveImportedPath(F, HeaderName);
5869 CurrentModule->addTopHeaderFilename(HeaderName);
5870 break;
5873 case SUBMODULE_UMBRELLA_DIR: {
5874 // See comments in SUBMODULE_UMBRELLA_HEADER
5875 std::string Dirname = std::string(Blob);
5876 ResolveImportedPath(F, Dirname);
5877 if (auto Umbrella =
5878 PP.getFileManager().getOptionalDirectoryRef(Dirname)) {
5879 if (!CurrentModule->getUmbrellaDirAsWritten()) {
5880 // FIXME: NameAsWritten
5881 ModMap.setUmbrellaDirAsWritten(CurrentModule, *Umbrella, Blob, "");
5884 break;
5887 case SUBMODULE_METADATA: {
5888 F.BaseSubmoduleID = getTotalNumSubmodules();
5889 F.LocalNumSubmodules = Record[0];
5890 unsigned LocalBaseSubmoduleID = Record[1];
5891 if (F.LocalNumSubmodules > 0) {
5892 // Introduce the global -> local mapping for submodules within this
5893 // module.
5894 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5896 // Introduce the local -> global mapping for submodules within this
5897 // module.
5898 F.SubmoduleRemap.insertOrReplace(
5899 std::make_pair(LocalBaseSubmoduleID,
5900 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5902 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5904 break;
5907 case SUBMODULE_IMPORTS:
5908 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5909 UnresolvedModuleRef Unresolved;
5910 Unresolved.File = &F;
5911 Unresolved.Mod = CurrentModule;
5912 Unresolved.ID = Record[Idx];
5913 Unresolved.Kind = UnresolvedModuleRef::Import;
5914 Unresolved.IsWildcard = false;
5915 UnresolvedModuleRefs.push_back(Unresolved);
5917 break;
5919 case SUBMODULE_AFFECTING_MODULES:
5920 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5921 UnresolvedModuleRef Unresolved;
5922 Unresolved.File = &F;
5923 Unresolved.Mod = CurrentModule;
5924 Unresolved.ID = Record[Idx];
5925 Unresolved.Kind = UnresolvedModuleRef::Affecting;
5926 Unresolved.IsWildcard = false;
5927 UnresolvedModuleRefs.push_back(Unresolved);
5929 break;
5931 case SUBMODULE_EXPORTS:
5932 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5933 UnresolvedModuleRef Unresolved;
5934 Unresolved.File = &F;
5935 Unresolved.Mod = CurrentModule;
5936 Unresolved.ID = Record[Idx];
5937 Unresolved.Kind = UnresolvedModuleRef::Export;
5938 Unresolved.IsWildcard = Record[Idx + 1];
5939 UnresolvedModuleRefs.push_back(Unresolved);
5942 // Once we've loaded the set of exports, there's no reason to keep
5943 // the parsed, unresolved exports around.
5944 CurrentModule->UnresolvedExports.clear();
5945 break;
5947 case SUBMODULE_REQUIRES:
5948 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5949 PP.getTargetInfo());
5950 break;
5952 case SUBMODULE_LINK_LIBRARY:
5953 ModMap.resolveLinkAsDependencies(CurrentModule);
5954 CurrentModule->LinkLibraries.push_back(
5955 Module::LinkLibrary(std::string(Blob), Record[0]));
5956 break;
5958 case SUBMODULE_CONFIG_MACRO:
5959 CurrentModule->ConfigMacros.push_back(Blob.str());
5960 break;
5962 case SUBMODULE_CONFLICT: {
5963 UnresolvedModuleRef Unresolved;
5964 Unresolved.File = &F;
5965 Unresolved.Mod = CurrentModule;
5966 Unresolved.ID = Record[0];
5967 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5968 Unresolved.IsWildcard = false;
5969 Unresolved.String = Blob;
5970 UnresolvedModuleRefs.push_back(Unresolved);
5971 break;
5974 case SUBMODULE_INITIALIZERS: {
5975 if (!ContextObj)
5976 break;
5977 SmallVector<uint32_t, 16> Inits;
5978 for (auto &ID : Record)
5979 Inits.push_back(getGlobalDeclID(F, ID));
5980 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5981 break;
5984 case SUBMODULE_EXPORT_AS:
5985 CurrentModule->ExportAsModule = Blob.str();
5986 ModMap.addLinkAsDependency(CurrentModule);
5987 break;
5992 /// Parse the record that corresponds to a LangOptions data
5993 /// structure.
5995 /// This routine parses the language options from the AST file and then gives
5996 /// them to the AST listener if one is set.
5998 /// \returns true if the listener deems the file unacceptable, false otherwise.
5999 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6000 bool Complain,
6001 ASTReaderListener &Listener,
6002 bool AllowCompatibleDifferences) {
6003 LangOptions LangOpts;
6004 unsigned Idx = 0;
6005 #define LANGOPT(Name, Bits, Default, Description) \
6006 LangOpts.Name = Record[Idx++];
6007 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
6008 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6009 #include "clang/Basic/LangOptions.def"
6010 #define SANITIZER(NAME, ID) \
6011 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6012 #include "clang/Basic/Sanitizers.def"
6014 for (unsigned N = Record[Idx++]; N; --N)
6015 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
6017 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6018 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6019 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6021 LangOpts.CurrentModule = ReadString(Record, Idx);
6023 // Comment options.
6024 for (unsigned N = Record[Idx++]; N; --N) {
6025 LangOpts.CommentOpts.BlockCommandNames.push_back(
6026 ReadString(Record, Idx));
6028 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6030 // OpenMP offloading options.
6031 for (unsigned N = Record[Idx++]; N; --N) {
6032 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
6035 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6037 return Listener.ReadLanguageOptions(LangOpts, Complain,
6038 AllowCompatibleDifferences);
6041 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
6042 ASTReaderListener &Listener,
6043 bool AllowCompatibleDifferences) {
6044 unsigned Idx = 0;
6045 TargetOptions TargetOpts;
6046 TargetOpts.Triple = ReadString(Record, Idx);
6047 TargetOpts.CPU = ReadString(Record, Idx);
6048 TargetOpts.TuneCPU = ReadString(Record, Idx);
6049 TargetOpts.ABI = ReadString(Record, Idx);
6050 for (unsigned N = Record[Idx++]; N; --N) {
6051 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
6053 for (unsigned N = Record[Idx++]; N; --N) {
6054 TargetOpts.Features.push_back(ReadString(Record, Idx));
6057 return Listener.ReadTargetOptions(TargetOpts, Complain,
6058 AllowCompatibleDifferences);
6061 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
6062 ASTReaderListener &Listener) {
6063 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
6064 unsigned Idx = 0;
6065 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
6066 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6067 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
6068 #include "clang/Basic/DiagnosticOptions.def"
6070 for (unsigned N = Record[Idx++]; N; --N)
6071 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
6072 for (unsigned N = Record[Idx++]; N; --N)
6073 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
6075 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
6078 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6079 ASTReaderListener &Listener) {
6080 FileSystemOptions FSOpts;
6081 unsigned Idx = 0;
6082 FSOpts.WorkingDir = ReadString(Record, Idx);
6083 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6086 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6087 bool Complain,
6088 ASTReaderListener &Listener) {
6089 HeaderSearchOptions HSOpts;
6090 unsigned Idx = 0;
6091 HSOpts.Sysroot = ReadString(Record, Idx);
6093 HSOpts.ResourceDir = ReadString(Record, Idx);
6094 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6095 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6096 HSOpts.DisableModuleHash = Record[Idx++];
6097 HSOpts.ImplicitModuleMaps = Record[Idx++];
6098 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6099 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6100 HSOpts.UseBuiltinIncludes = Record[Idx++];
6101 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6102 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6103 HSOpts.UseLibcxx = Record[Idx++];
6104 std::string SpecificModuleCachePath = ReadString(Record, Idx);
6106 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
6107 Complain);
6110 bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6111 ASTReaderListener &Listener) {
6112 HeaderSearchOptions HSOpts;
6113 unsigned Idx = 0;
6115 // Include entries.
6116 for (unsigned N = Record[Idx++]; N; --N) {
6117 std::string Path = ReadString(Record, Idx);
6118 frontend::IncludeDirGroup Group
6119 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6120 bool IsFramework = Record[Idx++];
6121 bool IgnoreSysRoot = Record[Idx++];
6122 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
6123 IgnoreSysRoot);
6126 // System header prefixes.
6127 for (unsigned N = Record[Idx++]; N; --N) {
6128 std::string Prefix = ReadString(Record, Idx);
6129 bool IsSystemHeader = Record[Idx++];
6130 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
6133 // VFS overlay files.
6134 for (unsigned N = Record[Idx++]; N; --N) {
6135 std::string VFSOverlayFile = ReadString(Record, Idx);
6136 HSOpts.VFSOverlayFiles.emplace_back(std::move(VFSOverlayFile));
6139 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6142 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6143 bool Complain,
6144 ASTReaderListener &Listener,
6145 std::string &SuggestedPredefines) {
6146 PreprocessorOptions PPOpts;
6147 unsigned Idx = 0;
6149 // Macro definitions/undefs
6150 bool ReadMacros = Record[Idx++];
6151 if (ReadMacros) {
6152 for (unsigned N = Record[Idx++]; N; --N) {
6153 std::string Macro = ReadString(Record, Idx);
6154 bool IsUndef = Record[Idx++];
6155 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
6159 // Includes
6160 for (unsigned N = Record[Idx++]; N; --N) {
6161 PPOpts.Includes.push_back(ReadString(Record, Idx));
6164 // Macro Includes
6165 for (unsigned N = Record[Idx++]; N; --N) {
6166 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
6169 PPOpts.UsePredefines = Record[Idx++];
6170 PPOpts.DetailedRecord = Record[Idx++];
6171 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6172 PPOpts.ObjCXXARCStandardLibrary =
6173 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6174 SuggestedPredefines.clear();
6175 return Listener.ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
6176 SuggestedPredefines);
6179 std::pair<ModuleFile *, unsigned>
6180 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6181 GlobalPreprocessedEntityMapType::iterator
6182 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
6183 assert(I != GlobalPreprocessedEntityMap.end() &&
6184 "Corrupted global preprocessed entity map");
6185 ModuleFile *M = I->second;
6186 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6187 return std::make_pair(M, LocalIndex);
6190 llvm::iterator_range<PreprocessingRecord::iterator>
6191 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6192 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6193 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
6194 Mod.NumPreprocessedEntities);
6196 return llvm::make_range(PreprocessingRecord::iterator(),
6197 PreprocessingRecord::iterator());
6200 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6201 unsigned int ClientLoadCapabilities) {
6202 return ClientLoadCapabilities & ARR_OutOfDate &&
6203 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
6206 llvm::iterator_range<ASTReader::ModuleDeclIterator>
6207 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6208 return llvm::make_range(
6209 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6210 ModuleDeclIterator(this, &Mod,
6211 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6214 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6215 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
6216 assert(I != GlobalSkippedRangeMap.end() &&
6217 "Corrupted global skipped range map");
6218 ModuleFile *M = I->second;
6219 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6220 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6221 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6222 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
6223 TranslateSourceLocation(*M, RawRange.getEnd()));
6224 assert(Range.isValid());
6225 return Range;
6228 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6229 PreprocessedEntityID PPID = Index+1;
6230 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6231 ModuleFile &M = *PPInfo.first;
6232 unsigned LocalIndex = PPInfo.second;
6233 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6235 if (!PP.getPreprocessingRecord()) {
6236 Error("no preprocessing record");
6237 return nullptr;
6240 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6241 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6242 M.MacroOffsetsBase + PPOffs.BitOffset)) {
6243 Error(std::move(Err));
6244 return nullptr;
6247 Expected<llvm::BitstreamEntry> MaybeEntry =
6248 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
6249 if (!MaybeEntry) {
6250 Error(MaybeEntry.takeError());
6251 return nullptr;
6253 llvm::BitstreamEntry Entry = MaybeEntry.get();
6255 if (Entry.Kind != llvm::BitstreamEntry::Record)
6256 return nullptr;
6258 // Read the record.
6259 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
6260 TranslateSourceLocation(M, PPOffs.getEnd()));
6261 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6262 StringRef Blob;
6263 RecordData Record;
6264 Expected<unsigned> MaybeRecType =
6265 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
6266 if (!MaybeRecType) {
6267 Error(MaybeRecType.takeError());
6268 return nullptr;
6270 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6271 case PPD_MACRO_EXPANSION: {
6272 bool isBuiltin = Record[0];
6273 IdentifierInfo *Name = nullptr;
6274 MacroDefinitionRecord *Def = nullptr;
6275 if (isBuiltin)
6276 Name = getLocalIdentifier(M, Record[1]);
6277 else {
6278 PreprocessedEntityID GlobalID =
6279 getGlobalPreprocessedEntityID(M, Record[1]);
6280 Def = cast<MacroDefinitionRecord>(
6281 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6284 MacroExpansion *ME;
6285 if (isBuiltin)
6286 ME = new (PPRec) MacroExpansion(Name, Range);
6287 else
6288 ME = new (PPRec) MacroExpansion(Def, Range);
6290 return ME;
6293 case PPD_MACRO_DEFINITION: {
6294 // Decode the identifier info and then check again; if the macro is
6295 // still defined and associated with the identifier,
6296 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6297 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6299 if (DeserializationListener)
6300 DeserializationListener->MacroDefinitionRead(PPID, MD);
6302 return MD;
6305 case PPD_INCLUSION_DIRECTIVE: {
6306 const char *FullFileNameStart = Blob.data() + Record[0];
6307 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6308 OptionalFileEntryRef File;
6309 if (!FullFileName.empty())
6310 File = PP.getFileManager().getOptionalFileRef(FullFileName);
6312 // FIXME: Stable encoding
6313 InclusionDirective::InclusionKind Kind
6314 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6315 InclusionDirective *ID
6316 = new (PPRec) InclusionDirective(PPRec, Kind,
6317 StringRef(Blob.data(), Record[0]),
6318 Record[1], Record[3],
6319 File,
6320 Range);
6321 return ID;
6325 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6328 /// Find the next module that contains entities and return the ID
6329 /// of the first entry.
6331 /// \param SLocMapI points at a chunk of a module that contains no
6332 /// preprocessed entities or the entities it contains are not the ones we are
6333 /// looking for.
6334 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6335 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6336 ++SLocMapI;
6337 for (GlobalSLocOffsetMapType::const_iterator
6338 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6339 ModuleFile &M = *SLocMapI->second;
6340 if (M.NumPreprocessedEntities)
6341 return M.BasePreprocessedEntityID;
6344 return getTotalNumPreprocessedEntities();
6347 namespace {
6349 struct PPEntityComp {
6350 const ASTReader &Reader;
6351 ModuleFile &M;
6353 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6355 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6356 SourceLocation LHS = getLoc(L);
6357 SourceLocation RHS = getLoc(R);
6358 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6361 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6362 SourceLocation LHS = getLoc(L);
6363 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6366 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6367 SourceLocation RHS = getLoc(R);
6368 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6371 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6372 return Reader.TranslateSourceLocation(M, PPE.getBegin());
6376 } // namespace
6378 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6379 bool EndsAfter) const {
6380 if (SourceMgr.isLocalSourceLocation(Loc))
6381 return getTotalNumPreprocessedEntities();
6383 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6384 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6385 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6386 "Corrupted global sloc offset map");
6388 if (SLocMapI->second->NumPreprocessedEntities == 0)
6389 return findNextPreprocessedEntity(SLocMapI);
6391 ModuleFile &M = *SLocMapI->second;
6393 using pp_iterator = const PPEntityOffset *;
6395 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6396 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6398 size_t Count = M.NumPreprocessedEntities;
6399 size_t Half;
6400 pp_iterator First = pp_begin;
6401 pp_iterator PPI;
6403 if (EndsAfter) {
6404 PPI = std::upper_bound(pp_begin, pp_end, Loc,
6405 PPEntityComp(*this, M));
6406 } else {
6407 // Do a binary search manually instead of using std::lower_bound because
6408 // The end locations of entities may be unordered (when a macro expansion
6409 // is inside another macro argument), but for this case it is not important
6410 // whether we get the first macro expansion or its containing macro.
6411 while (Count > 0) {
6412 Half = Count / 2;
6413 PPI = First;
6414 std::advance(PPI, Half);
6415 if (SourceMgr.isBeforeInTranslationUnit(
6416 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6417 First = PPI;
6418 ++First;
6419 Count = Count - Half - 1;
6420 } else
6421 Count = Half;
6425 if (PPI == pp_end)
6426 return findNextPreprocessedEntity(SLocMapI);
6428 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6431 /// Returns a pair of [Begin, End) indices of preallocated
6432 /// preprocessed entities that \arg Range encompasses.
6433 std::pair<unsigned, unsigned>
6434 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6435 if (Range.isInvalid())
6436 return std::make_pair(0,0);
6437 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6439 PreprocessedEntityID BeginID =
6440 findPreprocessedEntity(Range.getBegin(), false);
6441 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6442 return std::make_pair(BeginID, EndID);
6445 /// Optionally returns true or false if the preallocated preprocessed
6446 /// entity with index \arg Index came from file \arg FID.
6447 std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6448 FileID FID) {
6449 if (FID.isInvalid())
6450 return false;
6452 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6453 ModuleFile &M = *PPInfo.first;
6454 unsigned LocalIndex = PPInfo.second;
6455 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6457 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6458 if (Loc.isInvalid())
6459 return false;
6461 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6462 return true;
6463 else
6464 return false;
6467 namespace {
6469 /// Visitor used to search for information about a header file.
6470 class HeaderFileInfoVisitor {
6471 FileEntryRef FE;
6472 std::optional<HeaderFileInfo> HFI;
6474 public:
6475 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
6477 bool operator()(ModuleFile &M) {
6478 HeaderFileInfoLookupTable *Table
6479 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6480 if (!Table)
6481 return false;
6483 // Look in the on-disk hash table for an entry for this file name.
6484 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6485 if (Pos == Table->end())
6486 return false;
6488 HFI = *Pos;
6489 return true;
6492 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6495 } // namespace
6497 HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
6498 HeaderFileInfoVisitor Visitor(FE);
6499 ModuleMgr.visit(Visitor);
6500 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6501 return *HFI;
6503 return HeaderFileInfo();
6506 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6507 using DiagState = DiagnosticsEngine::DiagState;
6508 SmallVector<DiagState *, 32> DiagStates;
6510 for (ModuleFile &F : ModuleMgr) {
6511 unsigned Idx = 0;
6512 auto &Record = F.PragmaDiagMappings;
6513 if (Record.empty())
6514 continue;
6516 DiagStates.clear();
6518 auto ReadDiagState = [&](const DiagState &BasedOn,
6519 bool IncludeNonPragmaStates) {
6520 unsigned BackrefID = Record[Idx++];
6521 if (BackrefID != 0)
6522 return DiagStates[BackrefID - 1];
6524 // A new DiagState was created here.
6525 Diag.DiagStates.push_back(BasedOn);
6526 DiagState *NewState = &Diag.DiagStates.back();
6527 DiagStates.push_back(NewState);
6528 unsigned Size = Record[Idx++];
6529 assert(Idx + Size * 2 <= Record.size() &&
6530 "Invalid data, not enough diag/map pairs");
6531 while (Size--) {
6532 unsigned DiagID = Record[Idx++];
6533 DiagnosticMapping NewMapping =
6534 DiagnosticMapping::deserialize(Record[Idx++]);
6535 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6536 continue;
6538 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6540 // If this mapping was specified as a warning but the severity was
6541 // upgraded due to diagnostic settings, simulate the current diagnostic
6542 // settings (and use a warning).
6543 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6544 NewMapping.setSeverity(diag::Severity::Warning);
6545 NewMapping.setUpgradedFromWarning(false);
6548 Mapping = NewMapping;
6550 return NewState;
6553 // Read the first state.
6554 DiagState *FirstState;
6555 if (F.Kind == MK_ImplicitModule) {
6556 // Implicitly-built modules are reused with different diagnostic
6557 // settings. Use the initial diagnostic state from Diag to simulate this
6558 // compilation's diagnostic settings.
6559 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6560 DiagStates.push_back(FirstState);
6562 // Skip the initial diagnostic state from the serialized module.
6563 assert(Record[1] == 0 &&
6564 "Invalid data, unexpected backref in initial state");
6565 Idx = 3 + Record[2] * 2;
6566 assert(Idx < Record.size() &&
6567 "Invalid data, not enough state change pairs in initial state");
6568 } else if (F.isModule()) {
6569 // For an explicit module, preserve the flags from the module build
6570 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6571 // -Wblah flags.
6572 unsigned Flags = Record[Idx++];
6573 DiagState Initial;
6574 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6575 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6576 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6577 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6578 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6579 Initial.ExtBehavior = (diag::Severity)Flags;
6580 FirstState = ReadDiagState(Initial, true);
6582 assert(F.OriginalSourceFileID.isValid());
6584 // Set up the root buffer of the module to start with the initial
6585 // diagnostic state of the module itself, to cover files that contain no
6586 // explicit transitions (for which we did not serialize anything).
6587 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6588 .StateTransitions.push_back({FirstState, 0});
6589 } else {
6590 // For prefix ASTs, start with whatever the user configured on the
6591 // command line.
6592 Idx++; // Skip flags.
6593 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
6596 // Read the state transitions.
6597 unsigned NumLocations = Record[Idx++];
6598 while (NumLocations--) {
6599 assert(Idx < Record.size() &&
6600 "Invalid data, missing pragma diagnostic states");
6601 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6602 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6603 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6604 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6605 unsigned Transitions = Record[Idx++];
6607 // Note that we don't need to set up Parent/ParentOffset here, because
6608 // we won't be changing the diagnostic state within imported FileIDs
6609 // (other than perhaps appending to the main source file, which has no
6610 // parent).
6611 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6612 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6613 for (unsigned I = 0; I != Transitions; ++I) {
6614 unsigned Offset = Record[Idx++];
6615 auto *State = ReadDiagState(*FirstState, false);
6616 F.StateTransitions.push_back({State, Offset});
6620 // Read the final state.
6621 assert(Idx < Record.size() &&
6622 "Invalid data, missing final pragma diagnostic state");
6623 SourceLocation CurStateLoc = ReadSourceLocation(F, Record[Idx++]);
6624 auto *CurState = ReadDiagState(*FirstState, false);
6626 if (!F.isModule()) {
6627 Diag.DiagStatesByLoc.CurDiagState = CurState;
6628 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6630 // Preserve the property that the imaginary root file describes the
6631 // current state.
6632 FileID NullFile;
6633 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6634 if (T.empty())
6635 T.push_back({CurState, 0});
6636 else
6637 T[0].State = CurState;
6640 // Don't try to read these mappings again.
6641 Record.clear();
6645 /// Get the correct cursor and offset for loading a type.
6646 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6647 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6648 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6649 ModuleFile *M = I->second;
6650 return RecordLocation(
6651 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6652 M->DeclsBlockStartOffset);
6655 static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6656 switch (code) {
6657 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6658 case TYPE_##CODE_ID: return Type::CLASS_ID;
6659 #include "clang/Serialization/TypeBitCodes.def"
6660 default:
6661 return std::nullopt;
6665 /// Read and return the type with the given index..
6667 /// The index is the type ID, shifted and minus the number of predefs. This
6668 /// routine actually reads the record corresponding to the type at the given
6669 /// location. It is a helper routine for GetType, which deals with reading type
6670 /// IDs.
6671 QualType ASTReader::readTypeRecord(unsigned Index) {
6672 assert(ContextObj && "reading type with no AST context");
6673 ASTContext &Context = *ContextObj;
6674 RecordLocation Loc = TypeCursorForIndex(Index);
6675 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6677 // Keep track of where we are in the stream, then jump back there
6678 // after reading this type.
6679 SavedStreamPosition SavedPosition(DeclsCursor);
6681 ReadingKindTracker ReadingKind(Read_Type, *this);
6683 // Note that we are loading a type record.
6684 Deserializing AType(this);
6686 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6687 Error(std::move(Err));
6688 return QualType();
6690 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6691 if (!RawCode) {
6692 Error(RawCode.takeError());
6693 return QualType();
6696 ASTRecordReader Record(*this, *Loc.F);
6697 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6698 if (!Code) {
6699 Error(Code.takeError());
6700 return QualType();
6702 if (Code.get() == TYPE_EXT_QUAL) {
6703 QualType baseType = Record.readQualType();
6704 Qualifiers quals = Record.readQualifiers();
6705 return Context.getQualifiedType(baseType, quals);
6708 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6709 if (!maybeClass) {
6710 Error("Unexpected code for type");
6711 return QualType();
6714 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6715 return TypeReader.read(*maybeClass);
6718 namespace clang {
6720 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6721 using LocSeq = SourceLocationSequence;
6723 ASTRecordReader &Reader;
6724 LocSeq *Seq;
6726 SourceLocation readSourceLocation() { return Reader.readSourceLocation(Seq); }
6727 SourceRange readSourceRange() { return Reader.readSourceRange(Seq); }
6729 TypeSourceInfo *GetTypeSourceInfo() {
6730 return Reader.readTypeSourceInfo();
6733 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6734 return Reader.readNestedNameSpecifierLoc();
6737 Attr *ReadAttr() {
6738 return Reader.readAttr();
6741 public:
6742 TypeLocReader(ASTRecordReader &Reader, LocSeq *Seq)
6743 : Reader(Reader), Seq(Seq) {}
6745 // We want compile-time assurance that we've enumerated all of
6746 // these, so unfortunately we have to declare them first, then
6747 // define them out-of-line.
6748 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6749 #define TYPELOC(CLASS, PARENT) \
6750 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6751 #include "clang/AST/TypeLocNodes.def"
6753 void VisitFunctionTypeLoc(FunctionTypeLoc);
6754 void VisitArrayTypeLoc(ArrayTypeLoc);
6757 } // namespace clang
6759 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6760 // nothing to do
6763 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6764 TL.setBuiltinLoc(readSourceLocation());
6765 if (TL.needsExtraLocalData()) {
6766 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6767 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6768 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6769 TL.setModeAttr(Reader.readInt());
6773 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6774 TL.setNameLoc(readSourceLocation());
6777 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6778 TL.setStarLoc(readSourceLocation());
6781 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6782 // nothing to do
6785 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6786 // nothing to do
6789 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6790 TL.setExpansionLoc(readSourceLocation());
6793 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6794 TL.setCaretLoc(readSourceLocation());
6797 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6798 TL.setAmpLoc(readSourceLocation());
6801 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6802 TL.setAmpAmpLoc(readSourceLocation());
6805 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6806 TL.setStarLoc(readSourceLocation());
6807 TL.setClassTInfo(GetTypeSourceInfo());
6810 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6811 TL.setLBracketLoc(readSourceLocation());
6812 TL.setRBracketLoc(readSourceLocation());
6813 if (Reader.readBool())
6814 TL.setSizeExpr(Reader.readExpr());
6815 else
6816 TL.setSizeExpr(nullptr);
6819 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6820 VisitArrayTypeLoc(TL);
6823 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6824 VisitArrayTypeLoc(TL);
6827 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6828 VisitArrayTypeLoc(TL);
6831 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6832 DependentSizedArrayTypeLoc TL) {
6833 VisitArrayTypeLoc(TL);
6836 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6837 DependentAddressSpaceTypeLoc TL) {
6839 TL.setAttrNameLoc(readSourceLocation());
6840 TL.setAttrOperandParensRange(readSourceRange());
6841 TL.setAttrExprOperand(Reader.readExpr());
6844 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6845 DependentSizedExtVectorTypeLoc TL) {
6846 TL.setNameLoc(readSourceLocation());
6849 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6850 TL.setNameLoc(readSourceLocation());
6853 void TypeLocReader::VisitDependentVectorTypeLoc(
6854 DependentVectorTypeLoc TL) {
6855 TL.setNameLoc(readSourceLocation());
6858 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6859 TL.setNameLoc(readSourceLocation());
6862 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6863 TL.setAttrNameLoc(readSourceLocation());
6864 TL.setAttrOperandParensRange(readSourceRange());
6865 TL.setAttrRowOperand(Reader.readExpr());
6866 TL.setAttrColumnOperand(Reader.readExpr());
6869 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6870 DependentSizedMatrixTypeLoc TL) {
6871 TL.setAttrNameLoc(readSourceLocation());
6872 TL.setAttrOperandParensRange(readSourceRange());
6873 TL.setAttrRowOperand(Reader.readExpr());
6874 TL.setAttrColumnOperand(Reader.readExpr());
6877 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6878 TL.setLocalRangeBegin(readSourceLocation());
6879 TL.setLParenLoc(readSourceLocation());
6880 TL.setRParenLoc(readSourceLocation());
6881 TL.setExceptionSpecRange(readSourceRange());
6882 TL.setLocalRangeEnd(readSourceLocation());
6883 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6884 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6888 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6889 VisitFunctionTypeLoc(TL);
6892 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6893 VisitFunctionTypeLoc(TL);
6896 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6897 TL.setNameLoc(readSourceLocation());
6900 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6901 TL.setNameLoc(readSourceLocation());
6904 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6905 TL.setNameLoc(readSourceLocation());
6908 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6909 TL.setTypeofLoc(readSourceLocation());
6910 TL.setLParenLoc(readSourceLocation());
6911 TL.setRParenLoc(readSourceLocation());
6914 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6915 TL.setTypeofLoc(readSourceLocation());
6916 TL.setLParenLoc(readSourceLocation());
6917 TL.setRParenLoc(readSourceLocation());
6918 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
6921 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6922 TL.setDecltypeLoc(readSourceLocation());
6923 TL.setRParenLoc(readSourceLocation());
6926 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6927 TL.setKWLoc(readSourceLocation());
6928 TL.setLParenLoc(readSourceLocation());
6929 TL.setRParenLoc(readSourceLocation());
6930 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6933 ConceptReference *ASTRecordReader::readConceptReference() {
6934 auto NNS = readNestedNameSpecifierLoc();
6935 auto TemplateKWLoc = readSourceLocation();
6936 auto ConceptNameLoc = readDeclarationNameInfo();
6937 auto FoundDecl = readDeclAs<NamedDecl>();
6938 auto NamedConcept = readDeclAs<ConceptDecl>();
6939 auto *CR = ConceptReference::Create(
6940 getContext(), NNS, TemplateKWLoc, ConceptNameLoc, FoundDecl, NamedConcept,
6941 (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
6942 return CR;
6945 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6946 TL.setNameLoc(readSourceLocation());
6947 if (Reader.readBool())
6948 TL.setConceptReference(Reader.readConceptReference());
6949 if (Reader.readBool())
6950 TL.setRParenLoc(readSourceLocation());
6953 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6954 DeducedTemplateSpecializationTypeLoc TL) {
6955 TL.setTemplateNameLoc(readSourceLocation());
6958 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6959 TL.setNameLoc(readSourceLocation());
6962 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6963 TL.setNameLoc(readSourceLocation());
6966 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6967 TL.setAttr(ReadAttr());
6970 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
6971 // Nothing to do.
6974 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6975 TL.setNameLoc(readSourceLocation());
6978 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6979 SubstTemplateTypeParmTypeLoc TL) {
6980 TL.setNameLoc(readSourceLocation());
6983 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6984 SubstTemplateTypeParmPackTypeLoc TL) {
6985 TL.setNameLoc(readSourceLocation());
6988 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6989 TemplateSpecializationTypeLoc TL) {
6990 TL.setTemplateKeywordLoc(readSourceLocation());
6991 TL.setTemplateNameLoc(readSourceLocation());
6992 TL.setLAngleLoc(readSourceLocation());
6993 TL.setRAngleLoc(readSourceLocation());
6994 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6995 TL.setArgLocInfo(i,
6996 Reader.readTemplateArgumentLocInfo(
6997 TL.getTypePtr()->template_arguments()[i].getKind()));
7000 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7001 TL.setLParenLoc(readSourceLocation());
7002 TL.setRParenLoc(readSourceLocation());
7005 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7006 TL.setElaboratedKeywordLoc(readSourceLocation());
7007 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7010 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7011 TL.setNameLoc(readSourceLocation());
7014 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7015 TL.setElaboratedKeywordLoc(readSourceLocation());
7016 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7017 TL.setNameLoc(readSourceLocation());
7020 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7021 DependentTemplateSpecializationTypeLoc TL) {
7022 TL.setElaboratedKeywordLoc(readSourceLocation());
7023 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7024 TL.setTemplateKeywordLoc(readSourceLocation());
7025 TL.setTemplateNameLoc(readSourceLocation());
7026 TL.setLAngleLoc(readSourceLocation());
7027 TL.setRAngleLoc(readSourceLocation());
7028 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7029 TL.setArgLocInfo(I,
7030 Reader.readTemplateArgumentLocInfo(
7031 TL.getTypePtr()->template_arguments()[I].getKind()));
7034 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7035 TL.setEllipsisLoc(readSourceLocation());
7038 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7039 TL.setNameLoc(readSourceLocation());
7040 TL.setNameEndLoc(readSourceLocation());
7043 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7044 if (TL.getNumProtocols()) {
7045 TL.setProtocolLAngleLoc(readSourceLocation());
7046 TL.setProtocolRAngleLoc(readSourceLocation());
7048 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7049 TL.setProtocolLoc(i, readSourceLocation());
7052 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7053 TL.setHasBaseTypeAsWritten(Reader.readBool());
7054 TL.setTypeArgsLAngleLoc(readSourceLocation());
7055 TL.setTypeArgsRAngleLoc(readSourceLocation());
7056 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7057 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7058 TL.setProtocolLAngleLoc(readSourceLocation());
7059 TL.setProtocolRAngleLoc(readSourceLocation());
7060 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7061 TL.setProtocolLoc(i, readSourceLocation());
7064 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7065 TL.setStarLoc(readSourceLocation());
7068 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7069 TL.setKWLoc(readSourceLocation());
7070 TL.setLParenLoc(readSourceLocation());
7071 TL.setRParenLoc(readSourceLocation());
7074 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7075 TL.setKWLoc(readSourceLocation());
7078 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7079 TL.setNameLoc(readSourceLocation());
7081 void TypeLocReader::VisitDependentBitIntTypeLoc(
7082 clang::DependentBitIntTypeLoc TL) {
7083 TL.setNameLoc(readSourceLocation());
7086 void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) {
7087 LocSeq::State Seq(ParentSeq);
7088 TypeLocReader TLR(*this, Seq);
7089 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7090 TLR.Visit(TL);
7093 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7094 QualType InfoTy = readType();
7095 if (InfoTy.isNull())
7096 return nullptr;
7098 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7099 readTypeLoc(TInfo->getTypeLoc());
7100 return TInfo;
7103 QualType ASTReader::GetType(TypeID ID) {
7104 assert(ContextObj && "reading type with no AST context");
7105 ASTContext &Context = *ContextObj;
7107 unsigned FastQuals = ID & Qualifiers::FastMask;
7108 unsigned Index = ID >> Qualifiers::FastWidth;
7110 if (Index < NUM_PREDEF_TYPE_IDS) {
7111 QualType T;
7112 switch ((PredefinedTypeIDs)Index) {
7113 case PREDEF_TYPE_LAST_ID:
7114 // We should never use this one.
7115 llvm_unreachable("Invalid predefined type");
7116 break;
7117 case PREDEF_TYPE_NULL_ID:
7118 return QualType();
7119 case PREDEF_TYPE_VOID_ID:
7120 T = Context.VoidTy;
7121 break;
7122 case PREDEF_TYPE_BOOL_ID:
7123 T = Context.BoolTy;
7124 break;
7125 case PREDEF_TYPE_CHAR_U_ID:
7126 case PREDEF_TYPE_CHAR_S_ID:
7127 // FIXME: Check that the signedness of CharTy is correct!
7128 T = Context.CharTy;
7129 break;
7130 case PREDEF_TYPE_UCHAR_ID:
7131 T = Context.UnsignedCharTy;
7132 break;
7133 case PREDEF_TYPE_USHORT_ID:
7134 T = Context.UnsignedShortTy;
7135 break;
7136 case PREDEF_TYPE_UINT_ID:
7137 T = Context.UnsignedIntTy;
7138 break;
7139 case PREDEF_TYPE_ULONG_ID:
7140 T = Context.UnsignedLongTy;
7141 break;
7142 case PREDEF_TYPE_ULONGLONG_ID:
7143 T = Context.UnsignedLongLongTy;
7144 break;
7145 case PREDEF_TYPE_UINT128_ID:
7146 T = Context.UnsignedInt128Ty;
7147 break;
7148 case PREDEF_TYPE_SCHAR_ID:
7149 T = Context.SignedCharTy;
7150 break;
7151 case PREDEF_TYPE_WCHAR_ID:
7152 T = Context.WCharTy;
7153 break;
7154 case PREDEF_TYPE_SHORT_ID:
7155 T = Context.ShortTy;
7156 break;
7157 case PREDEF_TYPE_INT_ID:
7158 T = Context.IntTy;
7159 break;
7160 case PREDEF_TYPE_LONG_ID:
7161 T = Context.LongTy;
7162 break;
7163 case PREDEF_TYPE_LONGLONG_ID:
7164 T = Context.LongLongTy;
7165 break;
7166 case PREDEF_TYPE_INT128_ID:
7167 T = Context.Int128Ty;
7168 break;
7169 case PREDEF_TYPE_BFLOAT16_ID:
7170 T = Context.BFloat16Ty;
7171 break;
7172 case PREDEF_TYPE_HALF_ID:
7173 T = Context.HalfTy;
7174 break;
7175 case PREDEF_TYPE_FLOAT_ID:
7176 T = Context.FloatTy;
7177 break;
7178 case PREDEF_TYPE_DOUBLE_ID:
7179 T = Context.DoubleTy;
7180 break;
7181 case PREDEF_TYPE_LONGDOUBLE_ID:
7182 T = Context.LongDoubleTy;
7183 break;
7184 case PREDEF_TYPE_SHORT_ACCUM_ID:
7185 T = Context.ShortAccumTy;
7186 break;
7187 case PREDEF_TYPE_ACCUM_ID:
7188 T = Context.AccumTy;
7189 break;
7190 case PREDEF_TYPE_LONG_ACCUM_ID:
7191 T = Context.LongAccumTy;
7192 break;
7193 case PREDEF_TYPE_USHORT_ACCUM_ID:
7194 T = Context.UnsignedShortAccumTy;
7195 break;
7196 case PREDEF_TYPE_UACCUM_ID:
7197 T = Context.UnsignedAccumTy;
7198 break;
7199 case PREDEF_TYPE_ULONG_ACCUM_ID:
7200 T = Context.UnsignedLongAccumTy;
7201 break;
7202 case PREDEF_TYPE_SHORT_FRACT_ID:
7203 T = Context.ShortFractTy;
7204 break;
7205 case PREDEF_TYPE_FRACT_ID:
7206 T = Context.FractTy;
7207 break;
7208 case PREDEF_TYPE_LONG_FRACT_ID:
7209 T = Context.LongFractTy;
7210 break;
7211 case PREDEF_TYPE_USHORT_FRACT_ID:
7212 T = Context.UnsignedShortFractTy;
7213 break;
7214 case PREDEF_TYPE_UFRACT_ID:
7215 T = Context.UnsignedFractTy;
7216 break;
7217 case PREDEF_TYPE_ULONG_FRACT_ID:
7218 T = Context.UnsignedLongFractTy;
7219 break;
7220 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7221 T = Context.SatShortAccumTy;
7222 break;
7223 case PREDEF_TYPE_SAT_ACCUM_ID:
7224 T = Context.SatAccumTy;
7225 break;
7226 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7227 T = Context.SatLongAccumTy;
7228 break;
7229 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7230 T = Context.SatUnsignedShortAccumTy;
7231 break;
7232 case PREDEF_TYPE_SAT_UACCUM_ID:
7233 T = Context.SatUnsignedAccumTy;
7234 break;
7235 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7236 T = Context.SatUnsignedLongAccumTy;
7237 break;
7238 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7239 T = Context.SatShortFractTy;
7240 break;
7241 case PREDEF_TYPE_SAT_FRACT_ID:
7242 T = Context.SatFractTy;
7243 break;
7244 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7245 T = Context.SatLongFractTy;
7246 break;
7247 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7248 T = Context.SatUnsignedShortFractTy;
7249 break;
7250 case PREDEF_TYPE_SAT_UFRACT_ID:
7251 T = Context.SatUnsignedFractTy;
7252 break;
7253 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7254 T = Context.SatUnsignedLongFractTy;
7255 break;
7256 case PREDEF_TYPE_FLOAT16_ID:
7257 T = Context.Float16Ty;
7258 break;
7259 case PREDEF_TYPE_FLOAT128_ID:
7260 T = Context.Float128Ty;
7261 break;
7262 case PREDEF_TYPE_IBM128_ID:
7263 T = Context.Ibm128Ty;
7264 break;
7265 case PREDEF_TYPE_OVERLOAD_ID:
7266 T = Context.OverloadTy;
7267 break;
7268 case PREDEF_TYPE_BOUND_MEMBER:
7269 T = Context.BoundMemberTy;
7270 break;
7271 case PREDEF_TYPE_PSEUDO_OBJECT:
7272 T = Context.PseudoObjectTy;
7273 break;
7274 case PREDEF_TYPE_DEPENDENT_ID:
7275 T = Context.DependentTy;
7276 break;
7277 case PREDEF_TYPE_UNKNOWN_ANY:
7278 T = Context.UnknownAnyTy;
7279 break;
7280 case PREDEF_TYPE_NULLPTR_ID:
7281 T = Context.NullPtrTy;
7282 break;
7283 case PREDEF_TYPE_CHAR8_ID:
7284 T = Context.Char8Ty;
7285 break;
7286 case PREDEF_TYPE_CHAR16_ID:
7287 T = Context.Char16Ty;
7288 break;
7289 case PREDEF_TYPE_CHAR32_ID:
7290 T = Context.Char32Ty;
7291 break;
7292 case PREDEF_TYPE_OBJC_ID:
7293 T = Context.ObjCBuiltinIdTy;
7294 break;
7295 case PREDEF_TYPE_OBJC_CLASS:
7296 T = Context.ObjCBuiltinClassTy;
7297 break;
7298 case PREDEF_TYPE_OBJC_SEL:
7299 T = Context.ObjCBuiltinSelTy;
7300 break;
7301 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7302 case PREDEF_TYPE_##Id##_ID: \
7303 T = Context.SingletonId; \
7304 break;
7305 #include "clang/Basic/OpenCLImageTypes.def"
7306 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7307 case PREDEF_TYPE_##Id##_ID: \
7308 T = Context.Id##Ty; \
7309 break;
7310 #include "clang/Basic/OpenCLExtensionTypes.def"
7311 case PREDEF_TYPE_SAMPLER_ID:
7312 T = Context.OCLSamplerTy;
7313 break;
7314 case PREDEF_TYPE_EVENT_ID:
7315 T = Context.OCLEventTy;
7316 break;
7317 case PREDEF_TYPE_CLK_EVENT_ID:
7318 T = Context.OCLClkEventTy;
7319 break;
7320 case PREDEF_TYPE_QUEUE_ID:
7321 T = Context.OCLQueueTy;
7322 break;
7323 case PREDEF_TYPE_RESERVE_ID_ID:
7324 T = Context.OCLReserveIDTy;
7325 break;
7326 case PREDEF_TYPE_AUTO_DEDUCT:
7327 T = Context.getAutoDeductType();
7328 break;
7329 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7330 T = Context.getAutoRRefDeductType();
7331 break;
7332 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7333 T = Context.ARCUnbridgedCastTy;
7334 break;
7335 case PREDEF_TYPE_BUILTIN_FN:
7336 T = Context.BuiltinFnTy;
7337 break;
7338 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7339 T = Context.IncompleteMatrixIdxTy;
7340 break;
7341 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7342 T = Context.OMPArraySectionTy;
7343 break;
7344 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7345 T = Context.OMPArraySectionTy;
7346 break;
7347 case PREDEF_TYPE_OMP_ITERATOR:
7348 T = Context.OMPIteratorTy;
7349 break;
7350 #define SVE_TYPE(Name, Id, SingletonId) \
7351 case PREDEF_TYPE_##Id##_ID: \
7352 T = Context.SingletonId; \
7353 break;
7354 #include "clang/Basic/AArch64SVEACLETypes.def"
7355 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7356 case PREDEF_TYPE_##Id##_ID: \
7357 T = Context.Id##Ty; \
7358 break;
7359 #include "clang/Basic/PPCTypes.def"
7360 #define RVV_TYPE(Name, Id, SingletonId) \
7361 case PREDEF_TYPE_##Id##_ID: \
7362 T = Context.SingletonId; \
7363 break;
7364 #include "clang/Basic/RISCVVTypes.def"
7365 #define WASM_TYPE(Name, Id, SingletonId) \
7366 case PREDEF_TYPE_##Id##_ID: \
7367 T = Context.SingletonId; \
7368 break;
7369 #include "clang/Basic/WebAssemblyReferenceTypes.def"
7372 assert(!T.isNull() && "Unknown predefined type");
7373 return T.withFastQualifiers(FastQuals);
7376 Index -= NUM_PREDEF_TYPE_IDS;
7377 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7378 if (TypesLoaded[Index].isNull()) {
7379 TypesLoaded[Index] = readTypeRecord(Index);
7380 if (TypesLoaded[Index].isNull())
7381 return QualType();
7383 TypesLoaded[Index]->setFromAST();
7384 if (DeserializationListener)
7385 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7386 TypesLoaded[Index]);
7389 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7392 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7393 return GetType(getGlobalTypeID(F, LocalID));
7396 serialization::TypeID
7397 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7398 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7399 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7401 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7402 return LocalID;
7404 if (!F.ModuleOffsetMap.empty())
7405 ReadModuleOffsetMap(F);
7407 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7408 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7409 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7411 unsigned GlobalIndex = LocalIndex + I->second;
7412 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7415 TemplateArgumentLocInfo
7416 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7417 switch (Kind) {
7418 case TemplateArgument::Expression:
7419 return readExpr();
7420 case TemplateArgument::Type:
7421 return readTypeSourceInfo();
7422 case TemplateArgument::Template: {
7423 NestedNameSpecifierLoc QualifierLoc =
7424 readNestedNameSpecifierLoc();
7425 SourceLocation TemplateNameLoc = readSourceLocation();
7426 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7427 TemplateNameLoc, SourceLocation());
7429 case TemplateArgument::TemplateExpansion: {
7430 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7431 SourceLocation TemplateNameLoc = readSourceLocation();
7432 SourceLocation EllipsisLoc = readSourceLocation();
7433 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7434 TemplateNameLoc, EllipsisLoc);
7436 case TemplateArgument::Null:
7437 case TemplateArgument::Integral:
7438 case TemplateArgument::Declaration:
7439 case TemplateArgument::NullPtr:
7440 case TemplateArgument::Pack:
7441 // FIXME: Is this right?
7442 return TemplateArgumentLocInfo();
7444 llvm_unreachable("unexpected template argument loc");
7447 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7448 TemplateArgument Arg = readTemplateArgument();
7450 if (Arg.getKind() == TemplateArgument::Expression) {
7451 if (readBool()) // bool InfoHasSameExpr.
7452 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7454 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7457 void ASTRecordReader::readTemplateArgumentListInfo(
7458 TemplateArgumentListInfo &Result) {
7459 Result.setLAngleLoc(readSourceLocation());
7460 Result.setRAngleLoc(readSourceLocation());
7461 unsigned NumArgsAsWritten = readInt();
7462 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7463 Result.addArgument(readTemplateArgumentLoc());
7466 const ASTTemplateArgumentListInfo *
7467 ASTRecordReader::readASTTemplateArgumentListInfo() {
7468 TemplateArgumentListInfo Result;
7469 readTemplateArgumentListInfo(Result);
7470 return ASTTemplateArgumentListInfo::Create(getContext(), Result);
7473 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7474 return GetDecl(ID);
7477 void ASTReader::CompleteRedeclChain(const Decl *D) {
7478 if (NumCurrentElementsDeserializing) {
7479 // We arrange to not care about the complete redeclaration chain while we're
7480 // deserializing. Just remember that the AST has marked this one as complete
7481 // but that it's not actually complete yet, so we know we still need to
7482 // complete it later.
7483 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7484 return;
7487 if (!D->getDeclContext()) {
7488 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7489 return;
7492 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7494 // If this is a named declaration, complete it by looking it up
7495 // within its context.
7497 // FIXME: Merging a function definition should merge
7498 // all mergeable entities within it.
7499 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(DC)) {
7500 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7501 if (!getContext().getLangOpts().CPlusPlus &&
7502 isa<TranslationUnitDecl>(DC)) {
7503 // Outside of C++, we don't have a lookup table for the TU, so update
7504 // the identifier instead. (For C++ modules, we don't store decls
7505 // in the serialized identifier table, so we do the lookup in the TU.)
7506 auto *II = Name.getAsIdentifierInfo();
7507 assert(II && "non-identifier name in C?");
7508 if (II->isOutOfDate())
7509 updateOutOfDateIdentifier(*II);
7510 } else
7511 DC->lookup(Name);
7512 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7513 // Find all declarations of this kind from the relevant context.
7514 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7515 auto *DC = cast<DeclContext>(DCDecl);
7516 SmallVector<Decl*, 8> Decls;
7517 FindExternalLexicalDecls(
7518 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7523 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7524 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7525 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7526 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7527 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7528 if (auto *Template = FD->getPrimaryTemplate())
7529 Template->LoadLazySpecializations();
7533 CXXCtorInitializer **
7534 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7535 RecordLocation Loc = getLocalBitOffset(Offset);
7536 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7537 SavedStreamPosition SavedPosition(Cursor);
7538 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7539 Error(std::move(Err));
7540 return nullptr;
7542 ReadingKindTracker ReadingKind(Read_Decl, *this);
7543 Deserializing D(this);
7545 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7546 if (!MaybeCode) {
7547 Error(MaybeCode.takeError());
7548 return nullptr;
7550 unsigned Code = MaybeCode.get();
7552 ASTRecordReader Record(*this, *Loc.F);
7553 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7554 if (!MaybeRecCode) {
7555 Error(MaybeRecCode.takeError());
7556 return nullptr;
7558 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7559 Error("malformed AST file: missing C++ ctor initializers");
7560 return nullptr;
7563 return Record.readCXXCtorInitializers();
7566 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7567 assert(ContextObj && "reading base specifiers with no AST context");
7568 ASTContext &Context = *ContextObj;
7570 RecordLocation Loc = getLocalBitOffset(Offset);
7571 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7572 SavedStreamPosition SavedPosition(Cursor);
7573 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7574 Error(std::move(Err));
7575 return nullptr;
7577 ReadingKindTracker ReadingKind(Read_Decl, *this);
7578 Deserializing D(this);
7580 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7581 if (!MaybeCode) {
7582 Error(MaybeCode.takeError());
7583 return nullptr;
7585 unsigned Code = MaybeCode.get();
7587 ASTRecordReader Record(*this, *Loc.F);
7588 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7589 if (!MaybeRecCode) {
7590 Error(MaybeCode.takeError());
7591 return nullptr;
7593 unsigned RecCode = MaybeRecCode.get();
7595 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7596 Error("malformed AST file: missing C++ base specifiers");
7597 return nullptr;
7600 unsigned NumBases = Record.readInt();
7601 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7602 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7603 for (unsigned I = 0; I != NumBases; ++I)
7604 Bases[I] = Record.readCXXBaseSpecifier();
7605 return Bases;
7608 serialization::DeclID
7609 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7610 if (LocalID < NUM_PREDEF_DECL_IDS)
7611 return LocalID;
7613 if (!F.ModuleOffsetMap.empty())
7614 ReadModuleOffsetMap(F);
7616 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7617 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7618 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7620 return LocalID + I->second;
7623 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7624 ModuleFile &M) const {
7625 // Predefined decls aren't from any module.
7626 if (ID < NUM_PREDEF_DECL_IDS)
7627 return false;
7629 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7630 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7633 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7634 if (!D->isFromASTFile())
7635 return nullptr;
7636 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7637 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7638 return I->second;
7641 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7642 if (ID < NUM_PREDEF_DECL_IDS)
7643 return SourceLocation();
7645 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7647 if (Index > DeclsLoaded.size()) {
7648 Error("declaration ID out-of-range for AST file");
7649 return SourceLocation();
7652 if (Decl *D = DeclsLoaded[Index])
7653 return D->getLocation();
7655 SourceLocation Loc;
7656 DeclCursorForID(ID, Loc);
7657 return Loc;
7660 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7661 switch (ID) {
7662 case PREDEF_DECL_NULL_ID:
7663 return nullptr;
7665 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7666 return Context.getTranslationUnitDecl();
7668 case PREDEF_DECL_OBJC_ID_ID:
7669 return Context.getObjCIdDecl();
7671 case PREDEF_DECL_OBJC_SEL_ID:
7672 return Context.getObjCSelDecl();
7674 case PREDEF_DECL_OBJC_CLASS_ID:
7675 return Context.getObjCClassDecl();
7677 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7678 return Context.getObjCProtocolDecl();
7680 case PREDEF_DECL_INT_128_ID:
7681 return Context.getInt128Decl();
7683 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7684 return Context.getUInt128Decl();
7686 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7687 return Context.getObjCInstanceTypeDecl();
7689 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7690 return Context.getBuiltinVaListDecl();
7692 case PREDEF_DECL_VA_LIST_TAG:
7693 return Context.getVaListTagDecl();
7695 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7696 return Context.getBuiltinMSVaListDecl();
7698 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7699 return Context.getMSGuidTagDecl();
7701 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7702 return Context.getExternCContextDecl();
7704 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7705 return Context.getMakeIntegerSeqDecl();
7707 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7708 return Context.getCFConstantStringDecl();
7710 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7711 return Context.getCFConstantStringTagDecl();
7713 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7714 return Context.getTypePackElementDecl();
7716 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7719 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7720 assert(ContextObj && "reading decl with no AST context");
7721 if (ID < NUM_PREDEF_DECL_IDS) {
7722 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7723 if (D) {
7724 // Track that we have merged the declaration with ID \p ID into the
7725 // pre-existing predefined declaration \p D.
7726 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7727 if (Merged.empty())
7728 Merged.push_back(ID);
7730 return D;
7733 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7735 if (Index >= DeclsLoaded.size()) {
7736 assert(0 && "declaration ID out-of-range for AST file");
7737 Error("declaration ID out-of-range for AST file");
7738 return nullptr;
7741 return DeclsLoaded[Index];
7744 Decl *ASTReader::GetDecl(DeclID ID) {
7745 if (ID < NUM_PREDEF_DECL_IDS)
7746 return GetExistingDecl(ID);
7748 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7750 if (Index >= DeclsLoaded.size()) {
7751 assert(0 && "declaration ID out-of-range for AST file");
7752 Error("declaration ID out-of-range for AST file");
7753 return nullptr;
7756 if (!DeclsLoaded[Index]) {
7757 ReadDeclRecord(ID);
7758 if (DeserializationListener)
7759 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7762 return DeclsLoaded[Index];
7765 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7766 DeclID GlobalID) {
7767 if (GlobalID < NUM_PREDEF_DECL_IDS)
7768 return GlobalID;
7770 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7771 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7772 ModuleFile *Owner = I->second;
7774 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7775 = M.GlobalToLocalDeclIDs.find(Owner);
7776 if (Pos == M.GlobalToLocalDeclIDs.end())
7777 return 0;
7779 return GlobalID - Owner->BaseDeclID + Pos->second;
7782 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7783 const RecordData &Record,
7784 unsigned &Idx) {
7785 if (Idx >= Record.size()) {
7786 Error("Corrupted AST file");
7787 return 0;
7790 return getGlobalDeclID(F, Record[Idx++]);
7793 /// Resolve the offset of a statement into a statement.
7795 /// This operation will read a new statement from the external
7796 /// source each time it is called, and is meant to be used via a
7797 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7798 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7799 // Switch case IDs are per Decl.
7800 ClearSwitchCaseIDs();
7802 // Offset here is a global offset across the entire chain.
7803 RecordLocation Loc = getLocalBitOffset(Offset);
7804 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7805 Error(std::move(Err));
7806 return nullptr;
7808 assert(NumCurrentElementsDeserializing == 0 &&
7809 "should not be called while already deserializing");
7810 Deserializing D(this);
7811 return ReadStmtFromStream(*Loc.F);
7814 void ASTReader::FindExternalLexicalDecls(
7815 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7816 SmallVectorImpl<Decl *> &Decls) {
7817 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7819 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7820 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7821 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7822 auto K = (Decl::Kind)+LexicalDecls[I];
7823 if (!IsKindWeWant(K))
7824 continue;
7826 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7828 // Don't add predefined declarations to the lexical context more
7829 // than once.
7830 if (ID < NUM_PREDEF_DECL_IDS) {
7831 if (PredefsVisited[ID])
7832 continue;
7834 PredefsVisited[ID] = true;
7837 if (Decl *D = GetLocalDecl(*M, ID)) {
7838 assert(D->getKind() == K && "wrong kind for lexical decl");
7839 if (!DC->isDeclInLexicalTraversal(D))
7840 Decls.push_back(D);
7845 if (isa<TranslationUnitDecl>(DC)) {
7846 for (const auto &Lexical : TULexicalDecls)
7847 Visit(Lexical.first, Lexical.second);
7848 } else {
7849 auto I = LexicalDecls.find(DC);
7850 if (I != LexicalDecls.end())
7851 Visit(I->second.first, I->second.second);
7854 ++NumLexicalDeclContextsRead;
7857 namespace {
7859 class DeclIDComp {
7860 ASTReader &Reader;
7861 ModuleFile &Mod;
7863 public:
7864 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7866 bool operator()(LocalDeclID L, LocalDeclID R) const {
7867 SourceLocation LHS = getLocation(L);
7868 SourceLocation RHS = getLocation(R);
7869 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7872 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7873 SourceLocation RHS = getLocation(R);
7874 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7877 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7878 SourceLocation LHS = getLocation(L);
7879 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7882 SourceLocation getLocation(LocalDeclID ID) const {
7883 return Reader.getSourceManager().getFileLoc(
7884 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7888 } // namespace
7890 void ASTReader::FindFileRegionDecls(FileID File,
7891 unsigned Offset, unsigned Length,
7892 SmallVectorImpl<Decl *> &Decls) {
7893 SourceManager &SM = getSourceManager();
7895 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7896 if (I == FileDeclIDs.end())
7897 return;
7899 FileDeclsInfo &DInfo = I->second;
7900 if (DInfo.Decls.empty())
7901 return;
7903 SourceLocation
7904 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7905 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7907 DeclIDComp DIDComp(*this, *DInfo.Mod);
7908 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7909 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7910 if (BeginIt != DInfo.Decls.begin())
7911 --BeginIt;
7913 // If we are pointing at a top-level decl inside an objc container, we need
7914 // to backtrack until we find it otherwise we will fail to report that the
7915 // region overlaps with an objc container.
7916 while (BeginIt != DInfo.Decls.begin() &&
7917 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7918 ->isTopLevelDeclInObjCContainer())
7919 --BeginIt;
7921 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7922 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7923 if (EndIt != DInfo.Decls.end())
7924 ++EndIt;
7926 for (ArrayRef<serialization::LocalDeclID>::iterator
7927 DIt = BeginIt; DIt != EndIt; ++DIt)
7928 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7931 bool
7932 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7933 DeclarationName Name) {
7934 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7935 "DeclContext has no visible decls in storage");
7936 if (!Name)
7937 return false;
7939 auto It = Lookups.find(DC);
7940 if (It == Lookups.end())
7941 return false;
7943 Deserializing LookupResults(this);
7945 // Load the list of declarations.
7946 SmallVector<NamedDecl *, 64> Decls;
7947 llvm::SmallPtrSet<NamedDecl *, 8> Found;
7948 for (DeclID ID : It->second.Table.find(Name)) {
7949 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7950 if (ND->getDeclName() == Name && Found.insert(ND).second)
7951 Decls.push_back(ND);
7954 ++NumVisibleDeclContextsRead;
7955 SetExternalVisibleDeclsForName(DC, Name, Decls);
7956 return !Decls.empty();
7959 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7960 if (!DC->hasExternalVisibleStorage())
7961 return;
7963 auto It = Lookups.find(DC);
7964 assert(It != Lookups.end() &&
7965 "have external visible storage but no lookup tables");
7967 DeclsMap Decls;
7969 for (DeclID ID : It->second.Table.findAll()) {
7970 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7971 Decls[ND->getDeclName()].push_back(ND);
7974 ++NumVisibleDeclContextsRead;
7976 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7977 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7979 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7982 const serialization::reader::DeclContextLookupTable *
7983 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7984 auto I = Lookups.find(Primary);
7985 return I == Lookups.end() ? nullptr : &I->second;
7988 /// Under non-PCH compilation the consumer receives the objc methods
7989 /// before receiving the implementation, and codegen depends on this.
7990 /// We simulate this by deserializing and passing to consumer the methods of the
7991 /// implementation before passing the deserialized implementation decl.
7992 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7993 ASTConsumer *Consumer) {
7994 assert(ImplD && Consumer);
7996 for (auto *I : ImplD->methods())
7997 Consumer->HandleInterestingDecl(DeclGroupRef(I));
7999 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8002 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8003 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8004 PassObjCImplDeclToConsumer(ImplD, Consumer);
8005 else
8006 Consumer->HandleInterestingDecl(DeclGroupRef(D));
8009 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8010 this->Consumer = Consumer;
8012 if (Consumer)
8013 PassInterestingDeclsToConsumer();
8015 if (DeserializationListener)
8016 DeserializationListener->ReaderInitialized(this);
8019 void ASTReader::PrintStats() {
8020 std::fprintf(stderr, "*** AST File Statistics:\n");
8022 unsigned NumTypesLoaded =
8023 TypesLoaded.size() - llvm::count(TypesLoaded.materialized(), QualType());
8024 unsigned NumDeclsLoaded =
8025 DeclsLoaded.size() -
8026 llvm::count(DeclsLoaded.materialized(), (Decl *)nullptr);
8027 unsigned NumIdentifiersLoaded =
8028 IdentifiersLoaded.size() -
8029 llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
8030 unsigned NumMacrosLoaded =
8031 MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
8032 unsigned NumSelectorsLoaded =
8033 SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
8035 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8036 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
8037 NumSLocEntriesRead, TotalNumSLocEntries,
8038 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8039 if (!TypesLoaded.empty())
8040 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
8041 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8042 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8043 if (!DeclsLoaded.empty())
8044 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
8045 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8046 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8047 if (!IdentifiersLoaded.empty())
8048 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
8049 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8050 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8051 if (!MacrosLoaded.empty())
8052 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
8053 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8054 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8055 if (!SelectorsLoaded.empty())
8056 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
8057 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8058 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8059 if (TotalNumStatements)
8060 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
8061 NumStatementsRead, TotalNumStatements,
8062 ((float)NumStatementsRead/TotalNumStatements * 100));
8063 if (TotalNumMacros)
8064 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
8065 NumMacrosRead, TotalNumMacros,
8066 ((float)NumMacrosRead/TotalNumMacros * 100));
8067 if (TotalLexicalDeclContexts)
8068 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
8069 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8070 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8071 * 100));
8072 if (TotalVisibleDeclContexts)
8073 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
8074 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8075 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8076 * 100));
8077 if (TotalNumMethodPoolEntries)
8078 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
8079 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8080 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8081 * 100));
8082 if (NumMethodPoolLookups)
8083 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
8084 NumMethodPoolHits, NumMethodPoolLookups,
8085 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8086 if (NumMethodPoolTableLookups)
8087 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
8088 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8089 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8090 * 100.0));
8091 if (NumIdentifierLookupHits)
8092 std::fprintf(stderr,
8093 " %u / %u identifier table lookups succeeded (%f%%)\n",
8094 NumIdentifierLookupHits, NumIdentifierLookups,
8095 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8097 if (GlobalIndex) {
8098 std::fprintf(stderr, "\n");
8099 GlobalIndex->printStats();
8102 std::fprintf(stderr, "\n");
8103 dump();
8104 std::fprintf(stderr, "\n");
8107 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8108 LLVM_DUMP_METHOD static void
8109 dumpModuleIDMap(StringRef Name,
8110 const ContinuousRangeMap<Key, ModuleFile *,
8111 InitialCapacity> &Map) {
8112 if (Map.begin() == Map.end())
8113 return;
8115 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8117 llvm::errs() << Name << ":\n";
8118 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8119 I != IEnd; ++I) {
8120 llvm::errs() << " " << I->first << " -> " << I->second->FileName
8121 << "\n";
8125 LLVM_DUMP_METHOD void ASTReader::dump() {
8126 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8127 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8128 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8129 dumpModuleIDMap("Global type map", GlobalTypeMap);
8130 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8131 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8132 dumpModuleIDMap("Global macro map", GlobalMacroMap);
8133 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8134 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8135 dumpModuleIDMap("Global preprocessed entity map",
8136 GlobalPreprocessedEntityMap);
8138 llvm::errs() << "\n*** PCH/Modules Loaded:";
8139 for (ModuleFile &M : ModuleMgr)
8140 M.dump();
8143 /// Return the amount of memory used by memory buffers, breaking down
8144 /// by heap-backed versus mmap'ed memory.
8145 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8146 for (ModuleFile &I : ModuleMgr) {
8147 if (llvm::MemoryBuffer *buf = I.Buffer) {
8148 size_t bytes = buf->getBufferSize();
8149 switch (buf->getBufferKind()) {
8150 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8151 sizes.malloc_bytes += bytes;
8152 break;
8153 case llvm::MemoryBuffer::MemoryBuffer_MMap:
8154 sizes.mmap_bytes += bytes;
8155 break;
8161 void ASTReader::InitializeSema(Sema &S) {
8162 SemaObj = &S;
8163 S.addExternalSource(this);
8165 // Makes sure any declarations that were deserialized "too early"
8166 // still get added to the identifier's declaration chains.
8167 for (uint64_t ID : PreloadedDeclIDs) {
8168 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8169 pushExternalDeclIntoScope(D, D->getDeclName());
8171 PreloadedDeclIDs.clear();
8173 // FIXME: What happens if these are changed by a module import?
8174 if (!FPPragmaOptions.empty()) {
8175 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8176 FPOptionsOverride NewOverrides =
8177 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
8178 SemaObj->CurFPFeatures =
8179 NewOverrides.applyOverrides(SemaObj->getLangOpts());
8182 SemaObj->OpenCLFeatures = OpenCLExtensions;
8184 UpdateSema();
8187 void ASTReader::UpdateSema() {
8188 assert(SemaObj && "no Sema to update");
8190 // Load the offsets of the declarations that Sema references.
8191 // They will be lazily deserialized when needed.
8192 if (!SemaDeclRefs.empty()) {
8193 assert(SemaDeclRefs.size() % 3 == 0);
8194 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8195 if (!SemaObj->StdNamespace)
8196 SemaObj->StdNamespace = SemaDeclRefs[I];
8197 if (!SemaObj->StdBadAlloc)
8198 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8199 if (!SemaObj->StdAlignValT)
8200 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8202 SemaDeclRefs.clear();
8205 // Update the state of pragmas. Use the same API as if we had encountered the
8206 // pragma in the source.
8207 if(OptimizeOffPragmaLocation.isValid())
8208 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8209 if (PragmaMSStructState != -1)
8210 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8211 if (PointersToMembersPragmaLocation.isValid()) {
8212 SemaObj->ActOnPragmaMSPointersToMembers(
8213 (LangOptions::PragmaMSPointersToMembersKind)
8214 PragmaMSPointersToMembersState,
8215 PointersToMembersPragmaLocation);
8217 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8219 if (PragmaAlignPackCurrentValue) {
8220 // The bottom of the stack might have a default value. It must be adjusted
8221 // to the current value to ensure that the packing state is preserved after
8222 // popping entries that were included/imported from a PCH/module.
8223 bool DropFirst = false;
8224 if (!PragmaAlignPackStack.empty() &&
8225 PragmaAlignPackStack.front().Location.isInvalid()) {
8226 assert(PragmaAlignPackStack.front().Value ==
8227 SemaObj->AlignPackStack.DefaultValue &&
8228 "Expected a default alignment value");
8229 SemaObj->AlignPackStack.Stack.emplace_back(
8230 PragmaAlignPackStack.front().SlotLabel,
8231 SemaObj->AlignPackStack.CurrentValue,
8232 SemaObj->AlignPackStack.CurrentPragmaLocation,
8233 PragmaAlignPackStack.front().PushLocation);
8234 DropFirst = true;
8236 for (const auto &Entry :
8237 llvm::ArrayRef(PragmaAlignPackStack).drop_front(DropFirst ? 1 : 0)) {
8238 SemaObj->AlignPackStack.Stack.emplace_back(
8239 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8241 if (PragmaAlignPackCurrentLocation.isInvalid()) {
8242 assert(*PragmaAlignPackCurrentValue ==
8243 SemaObj->AlignPackStack.DefaultValue &&
8244 "Expected a default align and pack value");
8245 // Keep the current values.
8246 } else {
8247 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
8248 SemaObj->AlignPackStack.CurrentPragmaLocation =
8249 PragmaAlignPackCurrentLocation;
8252 if (FpPragmaCurrentValue) {
8253 // The bottom of the stack might have a default value. It must be adjusted
8254 // to the current value to ensure that fp-pragma state is preserved after
8255 // popping entries that were included/imported from a PCH/module.
8256 bool DropFirst = false;
8257 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
8258 assert(FpPragmaStack.front().Value ==
8259 SemaObj->FpPragmaStack.DefaultValue &&
8260 "Expected a default pragma float_control value");
8261 SemaObj->FpPragmaStack.Stack.emplace_back(
8262 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
8263 SemaObj->FpPragmaStack.CurrentPragmaLocation,
8264 FpPragmaStack.front().PushLocation);
8265 DropFirst = true;
8267 for (const auto &Entry :
8268 llvm::ArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
8269 SemaObj->FpPragmaStack.Stack.emplace_back(
8270 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8271 if (FpPragmaCurrentLocation.isInvalid()) {
8272 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8273 "Expected a default pragma float_control value");
8274 // Keep the current values.
8275 } else {
8276 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8277 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8281 // For non-modular AST files, restore visiblity of modules.
8282 for (auto &Import : PendingImportedModulesSema) {
8283 if (Import.ImportLoc.isInvalid())
8284 continue;
8285 if (Module *Imported = getSubmodule(Import.ID)) {
8286 SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
8289 PendingImportedModulesSema.clear();
8292 IdentifierInfo *ASTReader::get(StringRef Name) {
8293 // Note that we are loading an identifier.
8294 Deserializing AnIdentifier(this);
8296 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8297 NumIdentifierLookups,
8298 NumIdentifierLookupHits);
8300 // We don't need to do identifier table lookups in C++ modules (we preload
8301 // all interesting declarations, and don't need to use the scope for name
8302 // lookups). Perform the lookup in PCH files, though, since we don't build
8303 // a complete initial identifier table if we're carrying on from a PCH.
8304 if (PP.getLangOpts().CPlusPlus) {
8305 for (auto *F : ModuleMgr.pch_modules())
8306 if (Visitor(*F))
8307 break;
8308 } else {
8309 // If there is a global index, look there first to determine which modules
8310 // provably do not have any results for this identifier.
8311 GlobalModuleIndex::HitSet Hits;
8312 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8313 if (!loadGlobalIndex()) {
8314 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8315 HitsPtr = &Hits;
8319 ModuleMgr.visit(Visitor, HitsPtr);
8322 IdentifierInfo *II = Visitor.getIdentifierInfo();
8323 markIdentifierUpToDate(II);
8324 return II;
8327 namespace clang {
8329 /// An identifier-lookup iterator that enumerates all of the
8330 /// identifiers stored within a set of AST files.
8331 class ASTIdentifierIterator : public IdentifierIterator {
8332 /// The AST reader whose identifiers are being enumerated.
8333 const ASTReader &Reader;
8335 /// The current index into the chain of AST files stored in
8336 /// the AST reader.
8337 unsigned Index;
8339 /// The current position within the identifier lookup table
8340 /// of the current AST file.
8341 ASTIdentifierLookupTable::key_iterator Current;
8343 /// The end position within the identifier lookup table of
8344 /// the current AST file.
8345 ASTIdentifierLookupTable::key_iterator End;
8347 /// Whether to skip any modules in the ASTReader.
8348 bool SkipModules;
8350 public:
8351 explicit ASTIdentifierIterator(const ASTReader &Reader,
8352 bool SkipModules = false);
8354 StringRef Next() override;
8357 } // namespace clang
8359 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8360 bool SkipModules)
8361 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8364 StringRef ASTIdentifierIterator::Next() {
8365 while (Current == End) {
8366 // If we have exhausted all of our AST files, we're done.
8367 if (Index == 0)
8368 return StringRef();
8370 --Index;
8371 ModuleFile &F = Reader.ModuleMgr[Index];
8372 if (SkipModules && F.isModule())
8373 continue;
8375 ASTIdentifierLookupTable *IdTable =
8376 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8377 Current = IdTable->key_begin();
8378 End = IdTable->key_end();
8381 // We have any identifiers remaining in the current AST file; return
8382 // the next one.
8383 StringRef Result = *Current;
8384 ++Current;
8385 return Result;
8388 namespace {
8390 /// A utility for appending two IdentifierIterators.
8391 class ChainedIdentifierIterator : public IdentifierIterator {
8392 std::unique_ptr<IdentifierIterator> Current;
8393 std::unique_ptr<IdentifierIterator> Queued;
8395 public:
8396 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8397 std::unique_ptr<IdentifierIterator> Second)
8398 : Current(std::move(First)), Queued(std::move(Second)) {}
8400 StringRef Next() override {
8401 if (!Current)
8402 return StringRef();
8404 StringRef result = Current->Next();
8405 if (!result.empty())
8406 return result;
8408 // Try the queued iterator, which may itself be empty.
8409 Current.reset();
8410 std::swap(Current, Queued);
8411 return Next();
8415 } // namespace
8417 IdentifierIterator *ASTReader::getIdentifiers() {
8418 if (!loadGlobalIndex()) {
8419 std::unique_ptr<IdentifierIterator> ReaderIter(
8420 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8421 std::unique_ptr<IdentifierIterator> ModulesIter(
8422 GlobalIndex->createIdentifierIterator());
8423 return new ChainedIdentifierIterator(std::move(ReaderIter),
8424 std::move(ModulesIter));
8427 return new ASTIdentifierIterator(*this);
8430 namespace clang {
8431 namespace serialization {
8433 class ReadMethodPoolVisitor {
8434 ASTReader &Reader;
8435 Selector Sel;
8436 unsigned PriorGeneration;
8437 unsigned InstanceBits = 0;
8438 unsigned FactoryBits = 0;
8439 bool InstanceHasMoreThanOneDecl = false;
8440 bool FactoryHasMoreThanOneDecl = false;
8441 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8442 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8444 public:
8445 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8446 unsigned PriorGeneration)
8447 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8449 bool operator()(ModuleFile &M) {
8450 if (!M.SelectorLookupTable)
8451 return false;
8453 // If we've already searched this module file, skip it now.
8454 if (M.Generation <= PriorGeneration)
8455 return true;
8457 ++Reader.NumMethodPoolTableLookups;
8458 ASTSelectorLookupTable *PoolTable
8459 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8460 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8461 if (Pos == PoolTable->end())
8462 return false;
8464 ++Reader.NumMethodPoolTableHits;
8465 ++Reader.NumSelectorsRead;
8466 // FIXME: Not quite happy with the statistics here. We probably should
8467 // disable this tracking when called via LoadSelector.
8468 // Also, should entries without methods count as misses?
8469 ++Reader.NumMethodPoolEntriesRead;
8470 ASTSelectorLookupTrait::data_type Data = *Pos;
8471 if (Reader.DeserializationListener)
8472 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8474 // Append methods in the reverse order, so that later we can process them
8475 // in the order they appear in the source code by iterating through
8476 // the vector in the reverse order.
8477 InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8478 FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8479 InstanceBits = Data.InstanceBits;
8480 FactoryBits = Data.FactoryBits;
8481 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8482 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8483 return false;
8486 /// Retrieve the instance methods found by this visitor.
8487 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8488 return InstanceMethods;
8491 /// Retrieve the instance methods found by this visitor.
8492 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8493 return FactoryMethods;
8496 unsigned getInstanceBits() const { return InstanceBits; }
8497 unsigned getFactoryBits() const { return FactoryBits; }
8499 bool instanceHasMoreThanOneDecl() const {
8500 return InstanceHasMoreThanOneDecl;
8503 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8506 } // namespace serialization
8507 } // namespace clang
8509 /// Add the given set of methods to the method list.
8510 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8511 ObjCMethodList &List) {
8512 for (ObjCMethodDecl *M : llvm::reverse(Methods))
8513 S.addMethodToGlobalList(&List, M);
8516 void ASTReader::ReadMethodPool(Selector Sel) {
8517 // Get the selector generation and update it to the current generation.
8518 unsigned &Generation = SelectorGeneration[Sel];
8519 unsigned PriorGeneration = Generation;
8520 Generation = getGeneration();
8521 SelectorOutOfDate[Sel] = false;
8523 // Search for methods defined with this selector.
8524 ++NumMethodPoolLookups;
8525 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8526 ModuleMgr.visit(Visitor);
8528 if (Visitor.getInstanceMethods().empty() &&
8529 Visitor.getFactoryMethods().empty())
8530 return;
8532 ++NumMethodPoolHits;
8534 if (!getSema())
8535 return;
8537 Sema &S = *getSema();
8538 Sema::GlobalMethodPool::iterator Pos =
8539 S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8540 .first;
8542 Pos->second.first.setBits(Visitor.getInstanceBits());
8543 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8544 Pos->second.second.setBits(Visitor.getFactoryBits());
8545 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8547 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8548 // when building a module we keep every method individually and may need to
8549 // update hasMoreThanOneDecl as we add the methods.
8550 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8551 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8554 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8555 if (SelectorOutOfDate[Sel])
8556 ReadMethodPool(Sel);
8559 void ASTReader::ReadKnownNamespaces(
8560 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8561 Namespaces.clear();
8563 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8564 if (NamespaceDecl *Namespace
8565 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8566 Namespaces.push_back(Namespace);
8570 void ASTReader::ReadUndefinedButUsed(
8571 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8572 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8573 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8574 SourceLocation Loc =
8575 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8576 Undefined.insert(std::make_pair(D, Loc));
8580 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8581 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8582 Exprs) {
8583 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8584 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8585 uint64_t Count = DelayedDeleteExprs[Idx++];
8586 for (uint64_t C = 0; C < Count; ++C) {
8587 SourceLocation DeleteLoc =
8588 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8589 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8590 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8595 void ASTReader::ReadTentativeDefinitions(
8596 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8597 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8598 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8599 if (Var)
8600 TentativeDefs.push_back(Var);
8602 TentativeDefinitions.clear();
8605 void ASTReader::ReadUnusedFileScopedDecls(
8606 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8607 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8608 DeclaratorDecl *D
8609 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8610 if (D)
8611 Decls.push_back(D);
8613 UnusedFileScopedDecls.clear();
8616 void ASTReader::ReadDelegatingConstructors(
8617 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8618 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8619 CXXConstructorDecl *D
8620 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8621 if (D)
8622 Decls.push_back(D);
8624 DelegatingCtorDecls.clear();
8627 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8628 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8629 TypedefNameDecl *D
8630 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8631 if (D)
8632 Decls.push_back(D);
8634 ExtVectorDecls.clear();
8637 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8638 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8639 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8640 ++I) {
8641 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8642 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8643 if (D)
8644 Decls.insert(D);
8646 UnusedLocalTypedefNameCandidates.clear();
8649 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8650 llvm::SmallSetVector<Decl *, 4> &Decls) {
8651 for (auto I : DeclsToCheckForDeferredDiags) {
8652 auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8653 if (D)
8654 Decls.insert(D);
8656 DeclsToCheckForDeferredDiags.clear();
8659 void ASTReader::ReadReferencedSelectors(
8660 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8661 if (ReferencedSelectorsData.empty())
8662 return;
8664 // If there are @selector references added them to its pool. This is for
8665 // implementation of -Wselector.
8666 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8667 unsigned I = 0;
8668 while (I < DataSize) {
8669 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8670 SourceLocation SelLoc
8671 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8672 Sels.push_back(std::make_pair(Sel, SelLoc));
8674 ReferencedSelectorsData.clear();
8677 void ASTReader::ReadWeakUndeclaredIdentifiers(
8678 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8679 if (WeakUndeclaredIdentifiers.empty())
8680 return;
8682 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8683 IdentifierInfo *WeakId
8684 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8685 IdentifierInfo *AliasId
8686 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8687 SourceLocation Loc =
8688 SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8689 WeakInfo WI(AliasId, Loc);
8690 WeakIDs.push_back(std::make_pair(WeakId, WI));
8692 WeakUndeclaredIdentifiers.clear();
8695 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8696 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8697 ExternalVTableUse VT;
8698 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8699 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8700 VT.DefinitionRequired = VTableUses[Idx++];
8701 VTables.push_back(VT);
8704 VTableUses.clear();
8707 void ASTReader::ReadPendingInstantiations(
8708 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8709 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8710 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8711 SourceLocation Loc
8712 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8714 Pending.push_back(std::make_pair(D, Loc));
8716 PendingInstantiations.clear();
8719 void ASTReader::ReadLateParsedTemplates(
8720 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8721 &LPTMap) {
8722 for (auto &LPT : LateParsedTemplates) {
8723 ModuleFile *FMod = LPT.first;
8724 RecordDataImpl &LateParsed = LPT.second;
8725 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8726 /* In loop */) {
8727 FunctionDecl *FD =
8728 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8730 auto LT = std::make_unique<LateParsedTemplate>();
8731 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8732 LT->FPO = FPOptions::getFromOpaqueInt(LateParsed[Idx++]);
8734 ModuleFile *F = getOwningModuleFile(LT->D);
8735 assert(F && "No module");
8737 unsigned TokN = LateParsed[Idx++];
8738 LT->Toks.reserve(TokN);
8739 for (unsigned T = 0; T < TokN; ++T)
8740 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8742 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8746 LateParsedTemplates.clear();
8749 void ASTReader::AssignedLambdaNumbering(const CXXRecordDecl *Lambda) {
8750 if (Lambda->getLambdaContextDecl()) {
8751 // Keep track of this lambda so it can be merged with another lambda that
8752 // is loaded later.
8753 LambdaDeclarationsForMerging.insert(
8754 {{Lambda->getLambdaContextDecl()->getCanonicalDecl(),
8755 Lambda->getLambdaIndexInContext()},
8756 const_cast<CXXRecordDecl *>(Lambda)});
8760 void ASTReader::LoadSelector(Selector Sel) {
8761 // It would be complicated to avoid reading the methods anyway. So don't.
8762 ReadMethodPool(Sel);
8765 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8766 assert(ID && "Non-zero identifier ID required");
8767 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8768 IdentifiersLoaded[ID - 1] = II;
8769 if (DeserializationListener)
8770 DeserializationListener->IdentifierRead(ID, II);
8773 /// Set the globally-visible declarations associated with the given
8774 /// identifier.
8776 /// If the AST reader is currently in a state where the given declaration IDs
8777 /// cannot safely be resolved, they are queued until it is safe to resolve
8778 /// them.
8780 /// \param II an IdentifierInfo that refers to one or more globally-visible
8781 /// declarations.
8783 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8784 /// visible at global scope.
8786 /// \param Decls if non-null, this vector will be populated with the set of
8787 /// deserialized declarations. These declarations will not be pushed into
8788 /// scope.
8789 void
8790 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8791 const SmallVectorImpl<uint32_t> &DeclIDs,
8792 SmallVectorImpl<Decl *> *Decls) {
8793 if (NumCurrentElementsDeserializing && !Decls) {
8794 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8795 return;
8798 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8799 if (!SemaObj) {
8800 // Queue this declaration so that it will be added to the
8801 // translation unit scope and identifier's declaration chain
8802 // once a Sema object is known.
8803 PreloadedDeclIDs.push_back(DeclIDs[I]);
8804 continue;
8807 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8809 // If we're simply supposed to record the declarations, do so now.
8810 if (Decls) {
8811 Decls->push_back(D);
8812 continue;
8815 // Introduce this declaration into the translation-unit scope
8816 // and add it to the declaration chain for this identifier, so
8817 // that (unqualified) name lookup will find it.
8818 pushExternalDeclIntoScope(D, II);
8822 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8823 if (ID == 0)
8824 return nullptr;
8826 if (IdentifiersLoaded.empty()) {
8827 Error("no identifier table in AST file");
8828 return nullptr;
8831 ID -= 1;
8832 if (!IdentifiersLoaded[ID]) {
8833 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8834 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8835 ModuleFile *M = I->second;
8836 unsigned Index = ID - M->BaseIdentifierID;
8837 const unsigned char *Data =
8838 M->IdentifierTableData + M->IdentifierOffsets[Index];
8840 ASTIdentifierLookupTrait Trait(*this, *M);
8841 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8842 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8843 auto &II = PP.getIdentifierTable().get(Key);
8844 IdentifiersLoaded[ID] = &II;
8845 markIdentifierFromAST(*this, II);
8846 if (DeserializationListener)
8847 DeserializationListener->IdentifierRead(ID + 1, &II);
8850 return IdentifiersLoaded[ID];
8853 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8854 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8857 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8858 if (LocalID < NUM_PREDEF_IDENT_IDS)
8859 return LocalID;
8861 if (!M.ModuleOffsetMap.empty())
8862 ReadModuleOffsetMap(M);
8864 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8865 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8866 assert(I != M.IdentifierRemap.end()
8867 && "Invalid index into identifier index remap");
8869 return LocalID + I->second;
8872 MacroInfo *ASTReader::getMacro(MacroID ID) {
8873 if (ID == 0)
8874 return nullptr;
8876 if (MacrosLoaded.empty()) {
8877 Error("no macro table in AST file");
8878 return nullptr;
8881 ID -= NUM_PREDEF_MACRO_IDS;
8882 if (!MacrosLoaded[ID]) {
8883 GlobalMacroMapType::iterator I
8884 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8885 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8886 ModuleFile *M = I->second;
8887 unsigned Index = ID - M->BaseMacroID;
8888 MacrosLoaded[ID] =
8889 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8891 if (DeserializationListener)
8892 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8893 MacrosLoaded[ID]);
8896 return MacrosLoaded[ID];
8899 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8900 if (LocalID < NUM_PREDEF_MACRO_IDS)
8901 return LocalID;
8903 if (!M.ModuleOffsetMap.empty())
8904 ReadModuleOffsetMap(M);
8906 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8907 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8908 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8910 return LocalID + I->second;
8913 serialization::SubmoduleID
8914 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8915 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8916 return LocalID;
8918 if (!M.ModuleOffsetMap.empty())
8919 ReadModuleOffsetMap(M);
8921 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8922 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8923 assert(I != M.SubmoduleRemap.end()
8924 && "Invalid index into submodule index remap");
8926 return LocalID + I->second;
8929 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8930 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8931 assert(GlobalID == 0 && "Unhandled global submodule ID");
8932 return nullptr;
8935 if (GlobalID > SubmodulesLoaded.size()) {
8936 Error("submodule ID out of range in AST file");
8937 return nullptr;
8940 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8943 Module *ASTReader::getModule(unsigned ID) {
8944 return getSubmodule(ID);
8947 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8948 if (ID & 1) {
8949 // It's a module, look it up by submodule ID.
8950 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8951 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8952 } else {
8953 // It's a prefix (preamble, PCH, ...). Look it up by index.
8954 unsigned IndexFromEnd = ID >> 1;
8955 assert(IndexFromEnd && "got reference to unknown module file");
8956 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8960 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8961 if (!F)
8962 return 1;
8964 // For a file representing a module, use the submodule ID of the top-level
8965 // module as the file ID. For any other kind of file, the number of such
8966 // files loaded beforehand will be the same on reload.
8967 // FIXME: Is this true even if we have an explicit module file and a PCH?
8968 if (F->isModule())
8969 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8971 auto PCHModules = getModuleManager().pch_modules();
8972 auto I = llvm::find(PCHModules, F);
8973 assert(I != PCHModules.end() && "emitting reference to unknown file");
8974 return (I - PCHModules.end()) << 1;
8977 std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
8978 if (Module *M = getSubmodule(ID))
8979 return ASTSourceDescriptor(*M);
8981 // If there is only a single PCH, return it instead.
8982 // Chained PCH are not supported.
8983 const auto &PCHChain = ModuleMgr.pch_modules();
8984 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8985 ModuleFile &MF = ModuleMgr.getPrimaryModule();
8986 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8987 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8988 return ASTSourceDescriptor(ModuleName,
8989 llvm::sys::path::parent_path(MF.FileName),
8990 FileName, MF.Signature);
8992 return std::nullopt;
8995 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8996 auto I = DefinitionSource.find(FD);
8997 if (I == DefinitionSource.end())
8998 return EK_ReplyHazy;
8999 return I->second ? EK_Never : EK_Always;
9002 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9003 return DecodeSelector(getGlobalSelectorID(M, LocalID));
9006 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9007 if (ID == 0)
9008 return Selector();
9010 if (ID > SelectorsLoaded.size()) {
9011 Error("selector ID out of range in AST file");
9012 return Selector();
9015 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9016 // Load this selector from the selector table.
9017 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9018 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9019 ModuleFile &M = *I->second;
9020 ASTSelectorLookupTrait Trait(*this, M);
9021 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9022 SelectorsLoaded[ID - 1] =
9023 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9024 if (DeserializationListener)
9025 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9028 return SelectorsLoaded[ID - 1];
9031 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9032 return DecodeSelector(ID);
9035 uint32_t ASTReader::GetNumExternalSelectors() {
9036 // ID 0 (the null selector) is considered an external selector.
9037 return getTotalNumSelectors() + 1;
9040 serialization::SelectorID
9041 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9042 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9043 return LocalID;
9045 if (!M.ModuleOffsetMap.empty())
9046 ReadModuleOffsetMap(M);
9048 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9049 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9050 assert(I != M.SelectorRemap.end()
9051 && "Invalid index into selector index remap");
9053 return LocalID + I->second;
9056 DeclarationNameLoc
9057 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
9058 switch (Name.getNameKind()) {
9059 case DeclarationName::CXXConstructorName:
9060 case DeclarationName::CXXDestructorName:
9061 case DeclarationName::CXXConversionFunctionName:
9062 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
9064 case DeclarationName::CXXOperatorName:
9065 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
9067 case DeclarationName::CXXLiteralOperatorName:
9068 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
9069 readSourceLocation());
9071 case DeclarationName::Identifier:
9072 case DeclarationName::ObjCZeroArgSelector:
9073 case DeclarationName::ObjCOneArgSelector:
9074 case DeclarationName::ObjCMultiArgSelector:
9075 case DeclarationName::CXXUsingDirective:
9076 case DeclarationName::CXXDeductionGuideName:
9077 break;
9079 return DeclarationNameLoc();
9082 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
9083 DeclarationNameInfo NameInfo;
9084 NameInfo.setName(readDeclarationName());
9085 NameInfo.setLoc(readSourceLocation());
9086 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
9087 return NameInfo;
9090 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
9091 Info.QualifierLoc = readNestedNameSpecifierLoc();
9092 unsigned NumTPLists = readInt();
9093 Info.NumTemplParamLists = NumTPLists;
9094 if (NumTPLists) {
9095 Info.TemplParamLists =
9096 new (getContext()) TemplateParameterList *[NumTPLists];
9097 for (unsigned i = 0; i != NumTPLists; ++i)
9098 Info.TemplParamLists[i] = readTemplateParameterList();
9102 TemplateParameterList *
9103 ASTRecordReader::readTemplateParameterList() {
9104 SourceLocation TemplateLoc = readSourceLocation();
9105 SourceLocation LAngleLoc = readSourceLocation();
9106 SourceLocation RAngleLoc = readSourceLocation();
9108 unsigned NumParams = readInt();
9109 SmallVector<NamedDecl *, 16> Params;
9110 Params.reserve(NumParams);
9111 while (NumParams--)
9112 Params.push_back(readDeclAs<NamedDecl>());
9114 bool HasRequiresClause = readBool();
9115 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
9117 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9118 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9119 return TemplateParams;
9122 void ASTRecordReader::readTemplateArgumentList(
9123 SmallVectorImpl<TemplateArgument> &TemplArgs,
9124 bool Canonicalize) {
9125 unsigned NumTemplateArgs = readInt();
9126 TemplArgs.reserve(NumTemplateArgs);
9127 while (NumTemplateArgs--)
9128 TemplArgs.push_back(readTemplateArgument(Canonicalize));
9131 /// Read a UnresolvedSet structure.
9132 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
9133 unsigned NumDecls = readInt();
9134 Set.reserve(getContext(), NumDecls);
9135 while (NumDecls--) {
9136 DeclID ID = readDeclID();
9137 AccessSpecifier AS = (AccessSpecifier) readInt();
9138 Set.addLazyDecl(getContext(), ID, AS);
9142 CXXBaseSpecifier
9143 ASTRecordReader::readCXXBaseSpecifier() {
9144 bool isVirtual = readBool();
9145 bool isBaseOfClass = readBool();
9146 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
9147 bool inheritConstructors = readBool();
9148 TypeSourceInfo *TInfo = readTypeSourceInfo();
9149 SourceRange Range = readSourceRange();
9150 SourceLocation EllipsisLoc = readSourceLocation();
9151 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9152 EllipsisLoc);
9153 Result.setInheritConstructors(inheritConstructors);
9154 return Result;
9157 CXXCtorInitializer **
9158 ASTRecordReader::readCXXCtorInitializers() {
9159 ASTContext &Context = getContext();
9160 unsigned NumInitializers = readInt();
9161 assert(NumInitializers && "wrote ctor initializers but have no inits");
9162 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9163 for (unsigned i = 0; i != NumInitializers; ++i) {
9164 TypeSourceInfo *TInfo = nullptr;
9165 bool IsBaseVirtual = false;
9166 FieldDecl *Member = nullptr;
9167 IndirectFieldDecl *IndirectMember = nullptr;
9169 CtorInitializerType Type = (CtorInitializerType) readInt();
9170 switch (Type) {
9171 case CTOR_INITIALIZER_BASE:
9172 TInfo = readTypeSourceInfo();
9173 IsBaseVirtual = readBool();
9174 break;
9176 case CTOR_INITIALIZER_DELEGATING:
9177 TInfo = readTypeSourceInfo();
9178 break;
9180 case CTOR_INITIALIZER_MEMBER:
9181 Member = readDeclAs<FieldDecl>();
9182 break;
9184 case CTOR_INITIALIZER_INDIRECT_MEMBER:
9185 IndirectMember = readDeclAs<IndirectFieldDecl>();
9186 break;
9189 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
9190 Expr *Init = readExpr();
9191 SourceLocation LParenLoc = readSourceLocation();
9192 SourceLocation RParenLoc = readSourceLocation();
9194 CXXCtorInitializer *BOMInit;
9195 if (Type == CTOR_INITIALIZER_BASE)
9196 BOMInit = new (Context)
9197 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9198 RParenLoc, MemberOrEllipsisLoc);
9199 else if (Type == CTOR_INITIALIZER_DELEGATING)
9200 BOMInit = new (Context)
9201 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9202 else if (Member)
9203 BOMInit = new (Context)
9204 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9205 Init, RParenLoc);
9206 else
9207 BOMInit = new (Context)
9208 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9209 LParenLoc, Init, RParenLoc);
9211 if (/*IsWritten*/readBool()) {
9212 unsigned SourceOrder = readInt();
9213 BOMInit->setSourceOrder(SourceOrder);
9216 CtorInitializers[i] = BOMInit;
9219 return CtorInitializers;
9222 NestedNameSpecifierLoc
9223 ASTRecordReader::readNestedNameSpecifierLoc() {
9224 ASTContext &Context = getContext();
9225 unsigned N = readInt();
9226 NestedNameSpecifierLocBuilder Builder;
9227 for (unsigned I = 0; I != N; ++I) {
9228 auto Kind = readNestedNameSpecifierKind();
9229 switch (Kind) {
9230 case NestedNameSpecifier::Identifier: {
9231 IdentifierInfo *II = readIdentifier();
9232 SourceRange Range = readSourceRange();
9233 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9234 break;
9237 case NestedNameSpecifier::Namespace: {
9238 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
9239 SourceRange Range = readSourceRange();
9240 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9241 break;
9244 case NestedNameSpecifier::NamespaceAlias: {
9245 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
9246 SourceRange Range = readSourceRange();
9247 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9248 break;
9251 case NestedNameSpecifier::TypeSpec:
9252 case NestedNameSpecifier::TypeSpecWithTemplate: {
9253 bool Template = readBool();
9254 TypeSourceInfo *T = readTypeSourceInfo();
9255 if (!T)
9256 return NestedNameSpecifierLoc();
9257 SourceLocation ColonColonLoc = readSourceLocation();
9259 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9260 Builder.Extend(Context,
9261 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9262 T->getTypeLoc(), ColonColonLoc);
9263 break;
9266 case NestedNameSpecifier::Global: {
9267 SourceLocation ColonColonLoc = readSourceLocation();
9268 Builder.MakeGlobal(Context, ColonColonLoc);
9269 break;
9272 case NestedNameSpecifier::Super: {
9273 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
9274 SourceRange Range = readSourceRange();
9275 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9276 break;
9281 return Builder.getWithLocInContext(Context);
9284 SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9285 unsigned &Idx, LocSeq *Seq) {
9286 SourceLocation beg = ReadSourceLocation(F, Record, Idx, Seq);
9287 SourceLocation end = ReadSourceLocation(F, Record, Idx, Seq);
9288 return SourceRange(beg, end);
9291 /// Read a floating-point value
9292 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9293 return llvm::APFloat(Sem, readAPInt());
9296 // Read a string
9297 std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9298 unsigned Len = Record[Idx++];
9299 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9300 Idx += Len;
9301 return Result;
9304 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9305 unsigned &Idx) {
9306 std::string Filename = ReadString(Record, Idx);
9307 ResolveImportedPath(F, Filename);
9308 return Filename;
9311 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9312 const RecordData &Record, unsigned &Idx) {
9313 std::string Filename = ReadString(Record, Idx);
9314 if (!BaseDirectory.empty())
9315 ResolveImportedPath(Filename, BaseDirectory);
9316 return Filename;
9319 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9320 unsigned &Idx) {
9321 unsigned Major = Record[Idx++];
9322 unsigned Minor = Record[Idx++];
9323 unsigned Subminor = Record[Idx++];
9324 if (Minor == 0)
9325 return VersionTuple(Major);
9326 if (Subminor == 0)
9327 return VersionTuple(Major, Minor - 1);
9328 return VersionTuple(Major, Minor - 1, Subminor - 1);
9331 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9332 const RecordData &Record,
9333 unsigned &Idx) {
9334 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9335 return CXXTemporary::Create(getContext(), Decl);
9338 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9339 return Diag(CurrentImportLoc, DiagID);
9342 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9343 return Diags.Report(Loc, DiagID);
9346 /// Retrieve the identifier table associated with the
9347 /// preprocessor.
9348 IdentifierTable &ASTReader::getIdentifierTable() {
9349 return PP.getIdentifierTable();
9352 /// Record that the given ID maps to the given switch-case
9353 /// statement.
9354 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9355 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9356 "Already have a SwitchCase with this ID");
9357 (*CurrSwitchCaseStmts)[ID] = SC;
9360 /// Retrieve the switch-case statement with the given ID.
9361 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9362 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9363 return (*CurrSwitchCaseStmts)[ID];
9366 void ASTReader::ClearSwitchCaseIDs() {
9367 CurrSwitchCaseStmts->clear();
9370 void ASTReader::ReadComments() {
9371 ASTContext &Context = getContext();
9372 std::vector<RawComment *> Comments;
9373 for (SmallVectorImpl<std::pair<BitstreamCursor,
9374 serialization::ModuleFile *>>::iterator
9375 I = CommentsCursors.begin(),
9376 E = CommentsCursors.end();
9377 I != E; ++I) {
9378 Comments.clear();
9379 BitstreamCursor &Cursor = I->first;
9380 serialization::ModuleFile &F = *I->second;
9381 SavedStreamPosition SavedPosition(Cursor);
9383 RecordData Record;
9384 while (true) {
9385 Expected<llvm::BitstreamEntry> MaybeEntry =
9386 Cursor.advanceSkippingSubblocks(
9387 BitstreamCursor::AF_DontPopBlockAtEnd);
9388 if (!MaybeEntry) {
9389 Error(MaybeEntry.takeError());
9390 return;
9392 llvm::BitstreamEntry Entry = MaybeEntry.get();
9394 switch (Entry.Kind) {
9395 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9396 case llvm::BitstreamEntry::Error:
9397 Error("malformed block record in AST file");
9398 return;
9399 case llvm::BitstreamEntry::EndBlock:
9400 goto NextCursor;
9401 case llvm::BitstreamEntry::Record:
9402 // The interesting case.
9403 break;
9406 // Read a record.
9407 Record.clear();
9408 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9409 if (!MaybeComment) {
9410 Error(MaybeComment.takeError());
9411 return;
9413 switch ((CommentRecordTypes)MaybeComment.get()) {
9414 case COMMENTS_RAW_COMMENT: {
9415 unsigned Idx = 0;
9416 SourceRange SR = ReadSourceRange(F, Record, Idx);
9417 RawComment::CommentKind Kind =
9418 (RawComment::CommentKind) Record[Idx++];
9419 bool IsTrailingComment = Record[Idx++];
9420 bool IsAlmostTrailingComment = Record[Idx++];
9421 Comments.push_back(new (Context) RawComment(
9422 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9423 break;
9427 NextCursor:
9428 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9429 FileToOffsetToComment;
9430 for (RawComment *C : Comments) {
9431 SourceLocation CommentLoc = C->getBeginLoc();
9432 if (CommentLoc.isValid()) {
9433 std::pair<FileID, unsigned> Loc =
9434 SourceMgr.getDecomposedLoc(CommentLoc);
9435 if (Loc.first.isValid())
9436 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9442 void ASTReader::visitInputFileInfos(
9443 serialization::ModuleFile &MF, bool IncludeSystem,
9444 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
9445 bool IsSystem)>
9446 Visitor) {
9447 unsigned NumUserInputs = MF.NumUserInputFiles;
9448 unsigned NumInputs = MF.InputFilesLoaded.size();
9449 assert(NumUserInputs <= NumInputs);
9450 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9451 for (unsigned I = 0; I < N; ++I) {
9452 bool IsSystem = I >= NumUserInputs;
9453 InputFileInfo IFI = getInputFileInfo(MF, I+1);
9454 Visitor(IFI, IsSystem);
9458 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9459 bool IncludeSystem, bool Complain,
9460 llvm::function_ref<void(const serialization::InputFile &IF,
9461 bool isSystem)> Visitor) {
9462 unsigned NumUserInputs = MF.NumUserInputFiles;
9463 unsigned NumInputs = MF.InputFilesLoaded.size();
9464 assert(NumUserInputs <= NumInputs);
9465 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9466 for (unsigned I = 0; I < N; ++I) {
9467 bool IsSystem = I >= NumUserInputs;
9468 InputFile IF = getInputFile(MF, I+1, Complain);
9469 Visitor(IF, IsSystem);
9473 void ASTReader::visitTopLevelModuleMaps(
9474 serialization::ModuleFile &MF,
9475 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
9476 unsigned NumInputs = MF.InputFilesLoaded.size();
9477 for (unsigned I = 0; I < NumInputs; ++I) {
9478 InputFileInfo IFI = getInputFileInfo(MF, I + 1);
9479 if (IFI.TopLevel && IFI.ModuleMap)
9480 if (auto FE = getInputFile(MF, I + 1).getFile())
9481 Visitor(*FE);
9485 void ASTReader::finishPendingActions() {
9486 while (
9487 !PendingIdentifierInfos.empty() || !PendingDeducedFunctionTypes.empty() ||
9488 !PendingDeducedVarTypes.empty() || !PendingIncompleteDeclChains.empty() ||
9489 !PendingDeclChains.empty() || !PendingMacroIDs.empty() ||
9490 !PendingDeclContextInfos.empty() || !PendingUpdateRecords.empty() ||
9491 !PendingObjCExtensionIvarRedeclarations.empty()) {
9492 // If any identifiers with corresponding top-level declarations have
9493 // been loaded, load those declarations now.
9494 using TopLevelDeclsMap =
9495 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9496 TopLevelDeclsMap TopLevelDecls;
9498 while (!PendingIdentifierInfos.empty()) {
9499 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9500 SmallVector<uint32_t, 4> DeclIDs =
9501 std::move(PendingIdentifierInfos.back().second);
9502 PendingIdentifierInfos.pop_back();
9504 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9507 // Load each function type that we deferred loading because it was a
9508 // deduced type that might refer to a local type declared within itself.
9509 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
9510 auto *FD = PendingDeducedFunctionTypes[I].first;
9511 FD->setType(GetType(PendingDeducedFunctionTypes[I].second));
9513 // If we gave a function a deduced return type, remember that we need to
9514 // propagate that along the redeclaration chain.
9515 auto *DT = FD->getReturnType()->getContainedDeducedType();
9516 if (DT && DT->isDeduced())
9517 PendingDeducedTypeUpdates.insert(
9518 {FD->getCanonicalDecl(), FD->getReturnType()});
9520 PendingDeducedFunctionTypes.clear();
9522 // Load each variable type that we deferred loading because it was a
9523 // deduced type that might refer to a local type declared within itself.
9524 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
9525 auto *VD = PendingDeducedVarTypes[I].first;
9526 VD->setType(GetType(PendingDeducedVarTypes[I].second));
9528 PendingDeducedVarTypes.clear();
9530 // For each decl chain that we wanted to complete while deserializing, mark
9531 // it as "still needs to be completed".
9532 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9533 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9535 PendingIncompleteDeclChains.clear();
9537 // Load pending declaration chains.
9538 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9539 loadPendingDeclChain(PendingDeclChains[I].first,
9540 PendingDeclChains[I].second);
9541 PendingDeclChains.clear();
9543 // Make the most recent of the top-level declarations visible.
9544 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9545 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9546 IdentifierInfo *II = TLD->first;
9547 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9548 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9552 // Load any pending macro definitions.
9553 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9554 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9555 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9556 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9557 // Initialize the macro history from chained-PCHs ahead of module imports.
9558 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9559 ++IDIdx) {
9560 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9561 if (!Info.M->isModule())
9562 resolvePendingMacro(II, Info);
9564 // Handle module imports.
9565 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9566 ++IDIdx) {
9567 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9568 if (Info.M->isModule())
9569 resolvePendingMacro(II, Info);
9572 PendingMacroIDs.clear();
9574 // Wire up the DeclContexts for Decls that we delayed setting until
9575 // recursive loading is completed.
9576 while (!PendingDeclContextInfos.empty()) {
9577 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9578 PendingDeclContextInfos.pop_front();
9579 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9580 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9581 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9584 // Perform any pending declaration updates.
9585 while (!PendingUpdateRecords.empty()) {
9586 auto Update = PendingUpdateRecords.pop_back_val();
9587 ReadingKindTracker ReadingKind(Read_Decl, *this);
9588 loadDeclUpdateRecords(Update);
9591 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
9592 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
9593 auto DuplicateIvars =
9594 PendingObjCExtensionIvarRedeclarations.back().second;
9595 llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
9596 StructuralEquivalenceContext Ctx(
9597 ExtensionsPair.first->getASTContext(),
9598 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
9599 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
9600 /*Complain =*/false,
9601 /*ErrorOnTagTypeMismatch =*/true);
9602 if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) {
9603 // Merge redeclared ivars with their predecessors.
9604 for (auto IvarPair : DuplicateIvars) {
9605 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
9606 // Change semantic DeclContext but keep the lexical one.
9607 Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(),
9608 Ivar->getLexicalDeclContext(),
9609 getContext());
9610 getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl());
9612 // Invalidate duplicate extension and the cached ivar list.
9613 ExtensionsPair.first->setInvalidDecl();
9614 ExtensionsPair.second->getClassInterface()
9615 ->getDefinition()
9616 ->setIvarList(nullptr);
9617 } else {
9618 for (auto IvarPair : DuplicateIvars) {
9619 Diag(IvarPair.first->getLocation(),
9620 diag::err_duplicate_ivar_declaration)
9621 << IvarPair.first->getIdentifier();
9622 Diag(IvarPair.second->getLocation(), diag::note_previous_definition);
9625 PendingObjCExtensionIvarRedeclarations.pop_back();
9629 // At this point, all update records for loaded decls are in place, so any
9630 // fake class definitions should have become real.
9631 assert(PendingFakeDefinitionData.empty() &&
9632 "faked up a class definition but never saw the real one");
9634 // If we deserialized any C++ or Objective-C class definitions, any
9635 // Objective-C protocol definitions, or any redeclarable templates, make sure
9636 // that all redeclarations point to the definitions. Note that this can only
9637 // happen now, after the redeclaration chains have been fully wired.
9638 for (Decl *D : PendingDefinitions) {
9639 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9640 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9641 // Make sure that the TagType points at the definition.
9642 const_cast<TagType*>(TagT)->decl = TD;
9645 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9646 for (auto *R = getMostRecentExistingDecl(RD); R;
9647 R = R->getPreviousDecl()) {
9648 assert((R == D) ==
9649 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9650 "declaration thinks it's the definition but it isn't");
9651 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9655 continue;
9658 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9659 // Make sure that the ObjCInterfaceType points at the definition.
9660 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9661 ->Decl = ID;
9663 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9664 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9666 continue;
9669 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9670 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9671 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9673 continue;
9676 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9677 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9678 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9680 PendingDefinitions.clear();
9682 // Load the bodies of any functions or methods we've encountered. We do
9683 // this now (delayed) so that we can be sure that the declaration chains
9684 // have been fully wired up (hasBody relies on this).
9685 // FIXME: We shouldn't require complete redeclaration chains here.
9686 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9687 PBEnd = PendingBodies.end();
9688 PB != PBEnd; ++PB) {
9689 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9690 // For a function defined inline within a class template, force the
9691 // canonical definition to be the one inside the canonical definition of
9692 // the template. This ensures that we instantiate from a correct view
9693 // of the template.
9695 // Sadly we can't do this more generally: we can't be sure that all
9696 // copies of an arbitrary class definition will have the same members
9697 // defined (eg, some member functions may not be instantiated, and some
9698 // special members may or may not have been implicitly defined).
9699 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9700 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9701 continue;
9703 // FIXME: Check for =delete/=default?
9704 const FunctionDecl *Defn = nullptr;
9705 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9706 FD->setLazyBody(PB->second);
9707 } else {
9708 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9709 mergeDefinitionVisibility(NonConstDefn, FD);
9711 if (!FD->isLateTemplateParsed() &&
9712 !NonConstDefn->isLateTemplateParsed() &&
9713 FD->getODRHash() != NonConstDefn->getODRHash()) {
9714 if (!isa<CXXMethodDecl>(FD)) {
9715 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9716 } else if (FD->getLexicalParent()->isFileContext() &&
9717 NonConstDefn->getLexicalParent()->isFileContext()) {
9718 // Only diagnose out-of-line method definitions. If they are
9719 // in class definitions, then an error will be generated when
9720 // processing the class bodies.
9721 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9725 continue;
9728 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9729 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9730 MD->setLazyBody(PB->second);
9732 PendingBodies.clear();
9734 // Inform any classes that had members added that they now have more members.
9735 for (auto [RD, MD] : PendingAddedClassMembers) {
9736 RD->addedMember(MD);
9738 PendingAddedClassMembers.clear();
9740 // Do some cleanup.
9741 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9742 getContext().deduplicateMergedDefinitonsFor(ND);
9743 PendingMergedDefinitionsToDeduplicate.clear();
9746 void ASTReader::diagnoseOdrViolations() {
9747 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9748 PendingRecordOdrMergeFailures.empty() &&
9749 PendingFunctionOdrMergeFailures.empty() &&
9750 PendingEnumOdrMergeFailures.empty() &&
9751 PendingObjCInterfaceOdrMergeFailures.empty() &&
9752 PendingObjCProtocolOdrMergeFailures.empty())
9753 return;
9755 // Trigger the import of the full definition of each class that had any
9756 // odr-merging problems, so we can produce better diagnostics for them.
9757 // These updates may in turn find and diagnose some ODR failures, so take
9758 // ownership of the set first.
9759 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9760 PendingOdrMergeFailures.clear();
9761 for (auto &Merge : OdrMergeFailures) {
9762 Merge.first->buildLookup();
9763 Merge.first->decls_begin();
9764 Merge.first->bases_begin();
9765 Merge.first->vbases_begin();
9766 for (auto &RecordPair : Merge.second) {
9767 auto *RD = RecordPair.first;
9768 RD->decls_begin();
9769 RD->bases_begin();
9770 RD->vbases_begin();
9774 // Trigger the import of the full definition of each record in C/ObjC.
9775 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
9776 PendingRecordOdrMergeFailures.clear();
9777 for (auto &Merge : RecordOdrMergeFailures) {
9778 Merge.first->decls_begin();
9779 for (auto &D : Merge.second)
9780 D->decls_begin();
9783 // Trigger the import of the full interface definition.
9784 auto ObjCInterfaceOdrMergeFailures =
9785 std::move(PendingObjCInterfaceOdrMergeFailures);
9786 PendingObjCInterfaceOdrMergeFailures.clear();
9787 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
9788 Merge.first->decls_begin();
9789 for (auto &InterfacePair : Merge.second)
9790 InterfacePair.first->decls_begin();
9793 // Trigger the import of functions.
9794 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9795 PendingFunctionOdrMergeFailures.clear();
9796 for (auto &Merge : FunctionOdrMergeFailures) {
9797 Merge.first->buildLookup();
9798 Merge.first->decls_begin();
9799 Merge.first->getBody();
9800 for (auto &FD : Merge.second) {
9801 FD->buildLookup();
9802 FD->decls_begin();
9803 FD->getBody();
9807 // Trigger the import of enums.
9808 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9809 PendingEnumOdrMergeFailures.clear();
9810 for (auto &Merge : EnumOdrMergeFailures) {
9811 Merge.first->decls_begin();
9812 for (auto &Enum : Merge.second) {
9813 Enum->decls_begin();
9817 // Trigger the import of the full protocol definition.
9818 auto ObjCProtocolOdrMergeFailures =
9819 std::move(PendingObjCProtocolOdrMergeFailures);
9820 PendingObjCProtocolOdrMergeFailures.clear();
9821 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
9822 Merge.first->decls_begin();
9823 for (auto &ProtocolPair : Merge.second)
9824 ProtocolPair.first->decls_begin();
9827 // For each declaration from a merged context, check that the canonical
9828 // definition of that context also contains a declaration of the same
9829 // entity.
9831 // Caution: this loop does things that might invalidate iterators into
9832 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9833 while (!PendingOdrMergeChecks.empty()) {
9834 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9836 // FIXME: Skip over implicit declarations for now. This matters for things
9837 // like implicitly-declared special member functions. This isn't entirely
9838 // correct; we can end up with multiple unmerged declarations of the same
9839 // implicit entity.
9840 if (D->isImplicit())
9841 continue;
9843 DeclContext *CanonDef = D->getDeclContext();
9845 bool Found = false;
9846 const Decl *DCanon = D->getCanonicalDecl();
9848 for (auto *RI : D->redecls()) {
9849 if (RI->getLexicalDeclContext() == CanonDef) {
9850 Found = true;
9851 break;
9854 if (Found)
9855 continue;
9857 // Quick check failed, time to do the slow thing. Note, we can't just
9858 // look up the name of D in CanonDef here, because the member that is
9859 // in CanonDef might not be found by name lookup (it might have been
9860 // replaced by a more recent declaration in the lookup table), and we
9861 // can't necessarily find it in the redeclaration chain because it might
9862 // be merely mergeable, not redeclarable.
9863 llvm::SmallVector<const NamedDecl*, 4> Candidates;
9864 for (auto *CanonMember : CanonDef->decls()) {
9865 if (CanonMember->getCanonicalDecl() == DCanon) {
9866 // This can happen if the declaration is merely mergeable and not
9867 // actually redeclarable (we looked for redeclarations earlier).
9869 // FIXME: We should be able to detect this more efficiently, without
9870 // pulling in all of the members of CanonDef.
9871 Found = true;
9872 break;
9874 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9875 if (ND->getDeclName() == D->getDeclName())
9876 Candidates.push_back(ND);
9879 if (!Found) {
9880 // The AST doesn't like TagDecls becoming invalid after they've been
9881 // completed. We only really need to mark FieldDecls as invalid here.
9882 if (!isa<TagDecl>(D))
9883 D->setInvalidDecl();
9885 // Ensure we don't accidentally recursively enter deserialization while
9886 // we're producing our diagnostic.
9887 Deserializing RecursionGuard(this);
9889 std::string CanonDefModule =
9890 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
9891 cast<Decl>(CanonDef));
9892 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9893 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
9894 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9896 if (Candidates.empty())
9897 Diag(cast<Decl>(CanonDef)->getLocation(),
9898 diag::note_module_odr_violation_no_possible_decls) << D;
9899 else {
9900 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9901 Diag(Candidates[I]->getLocation(),
9902 diag::note_module_odr_violation_possible_decl)
9903 << Candidates[I];
9906 DiagnosedOdrMergeFailures.insert(CanonDef);
9910 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
9911 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
9912 ObjCInterfaceOdrMergeFailures.empty() &&
9913 ObjCProtocolOdrMergeFailures.empty())
9914 return;
9916 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
9917 getPreprocessor().getLangOpts());
9919 // Issue any pending ODR-failure diagnostics.
9920 for (auto &Merge : OdrMergeFailures) {
9921 // If we've already pointed out a specific problem with this class, don't
9922 // bother issuing a general "something's different" diagnostic.
9923 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9924 continue;
9926 bool Diagnosed = false;
9927 CXXRecordDecl *FirstRecord = Merge.first;
9928 for (auto &RecordPair : Merge.second) {
9929 if (DiagsEmitter.diagnoseMismatch(FirstRecord, RecordPair.first,
9930 RecordPair.second)) {
9931 Diagnosed = true;
9932 break;
9936 if (!Diagnosed) {
9937 // All definitions are updates to the same declaration. This happens if a
9938 // module instantiates the declaration of a class template specialization
9939 // and two or more other modules instantiate its definition.
9941 // FIXME: Indicate which modules had instantiations of this definition.
9942 // FIXME: How can this even happen?
9943 Diag(Merge.first->getLocation(),
9944 diag::err_module_odr_violation_different_instantiations)
9945 << Merge.first;
9949 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
9950 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
9951 for (auto &Merge : RecordOdrMergeFailures) {
9952 // If we've already pointed out a specific problem with this class, don't
9953 // bother issuing a general "something's different" diagnostic.
9954 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9955 continue;
9957 RecordDecl *FirstRecord = Merge.first;
9958 bool Diagnosed = false;
9959 for (auto *SecondRecord : Merge.second) {
9960 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
9961 Diagnosed = true;
9962 break;
9965 (void)Diagnosed;
9966 assert(Diagnosed && "Unable to emit ODR diagnostic.");
9969 // Issue ODR failures diagnostics for functions.
9970 for (auto &Merge : FunctionOdrMergeFailures) {
9971 FunctionDecl *FirstFunction = Merge.first;
9972 bool Diagnosed = false;
9973 for (auto &SecondFunction : Merge.second) {
9974 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
9975 Diagnosed = true;
9976 break;
9979 (void)Diagnosed;
9980 assert(Diagnosed && "Unable to emit ODR diagnostic.");
9983 // Issue ODR failures diagnostics for enums.
9984 for (auto &Merge : EnumOdrMergeFailures) {
9985 // If we've already pointed out a specific problem with this enum, don't
9986 // bother issuing a general "something's different" diagnostic.
9987 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9988 continue;
9990 EnumDecl *FirstEnum = Merge.first;
9991 bool Diagnosed = false;
9992 for (auto &SecondEnum : Merge.second) {
9993 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
9994 Diagnosed = true;
9995 break;
9998 (void)Diagnosed;
9999 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10002 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10003 // If we've already pointed out a specific problem with this interface,
10004 // don't bother issuing a general "something's different" diagnostic.
10005 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10006 continue;
10008 bool Diagnosed = false;
10009 ObjCInterfaceDecl *FirstID = Merge.first;
10010 for (auto &InterfacePair : Merge.second) {
10011 if (DiagsEmitter.diagnoseMismatch(FirstID, InterfacePair.first,
10012 InterfacePair.second)) {
10013 Diagnosed = true;
10014 break;
10017 (void)Diagnosed;
10018 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10021 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10022 // If we've already pointed out a specific problem with this protocol,
10023 // don't bother issuing a general "something's different" diagnostic.
10024 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10025 continue;
10027 ObjCProtocolDecl *FirstProtocol = Merge.first;
10028 bool Diagnosed = false;
10029 for (auto &ProtocolPair : Merge.second) {
10030 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, ProtocolPair.first,
10031 ProtocolPair.second)) {
10032 Diagnosed = true;
10033 break;
10036 (void)Diagnosed;
10037 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10041 void ASTReader::StartedDeserializing() {
10042 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
10043 ReadTimer->startTimer();
10046 void ASTReader::FinishedDeserializing() {
10047 assert(NumCurrentElementsDeserializing &&
10048 "FinishedDeserializing not paired with StartedDeserializing");
10049 if (NumCurrentElementsDeserializing == 1) {
10050 // We decrease NumCurrentElementsDeserializing only after pending actions
10051 // are finished, to avoid recursively re-calling finishPendingActions().
10052 finishPendingActions();
10054 --NumCurrentElementsDeserializing;
10056 if (NumCurrentElementsDeserializing == 0) {
10057 // Propagate exception specification and deduced type updates along
10058 // redeclaration chains.
10060 // We do this now rather than in finishPendingActions because we want to
10061 // be able to walk the complete redeclaration chains of the updated decls.
10062 while (!PendingExceptionSpecUpdates.empty() ||
10063 !PendingDeducedTypeUpdates.empty()) {
10064 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10065 PendingExceptionSpecUpdates.clear();
10066 for (auto Update : ESUpdates) {
10067 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10068 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10069 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10070 if (auto *Listener = getContext().getASTMutationListener())
10071 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
10072 for (auto *Redecl : Update.second->redecls())
10073 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
10076 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10077 PendingDeducedTypeUpdates.clear();
10078 for (auto Update : DTUpdates) {
10079 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10080 // FIXME: If the return type is already deduced, check that it matches.
10081 getContext().adjustDeducedFunctionResultType(Update.first,
10082 Update.second);
10086 if (ReadTimer)
10087 ReadTimer->stopTimer();
10089 diagnoseOdrViolations();
10091 // We are not in recursive loading, so it's safe to pass the "interesting"
10092 // decls to the consumer.
10093 if (Consumer)
10094 PassInterestingDeclsToConsumer();
10098 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
10099 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
10100 // Remove any fake results before adding any real ones.
10101 auto It = PendingFakeLookupResults.find(II);
10102 if (It != PendingFakeLookupResults.end()) {
10103 for (auto *ND : It->second)
10104 SemaObj->IdResolver.RemoveDecl(ND);
10105 // FIXME: this works around module+PCH performance issue.
10106 // Rather than erase the result from the map, which is O(n), just clear
10107 // the vector of NamedDecls.
10108 It->second.clear();
10112 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
10113 SemaObj->TUScope->AddDecl(D);
10114 } else if (SemaObj->TUScope) {
10115 // Adding the decl to IdResolver may have failed because it was already in
10116 // (even though it was not added in scope). If it is already in, make sure
10117 // it gets in the scope as well.
10118 if (llvm::is_contained(SemaObj->IdResolver.decls(Name), D))
10119 SemaObj->TUScope->AddDecl(D);
10123 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10124 ASTContext *Context,
10125 const PCHContainerReader &PCHContainerRdr,
10126 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
10127 StringRef isysroot,
10128 DisableValidationForModuleKind DisableValidationKind,
10129 bool AllowASTWithCompilerErrors,
10130 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
10131 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
10132 std::unique_ptr<llvm::Timer> ReadTimer)
10133 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
10134 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
10135 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
10136 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
10137 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
10138 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
10139 PCHContainerRdr, PP.getHeaderSearchInfo()),
10140 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
10141 DisableValidationKind(DisableValidationKind),
10142 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
10143 AllowConfigurationMismatch(AllowConfigurationMismatch),
10144 ValidateSystemInputs(ValidateSystemInputs),
10145 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
10146 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10147 SourceMgr.setExternalSLocEntrySource(this);
10149 for (const auto &Ext : Extensions) {
10150 auto BlockName = Ext->getExtensionMetadata().BlockName;
10151 auto Known = ModuleFileExtensions.find(BlockName);
10152 if (Known != ModuleFileExtensions.end()) {
10153 Diags.Report(diag::warn_duplicate_module_file_extension)
10154 << BlockName;
10155 continue;
10158 ModuleFileExtensions.insert({BlockName, Ext});
10162 ASTReader::~ASTReader() {
10163 if (OwnsDeserializationListener)
10164 delete DeserializationListener;
10167 IdentifierResolver &ASTReader::getIdResolver() {
10168 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
10171 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
10172 unsigned AbbrevID) {
10173 Idx = 0;
10174 Record.clear();
10175 return Cursor.readRecord(AbbrevID, Record);
10177 //===----------------------------------------------------------------------===//
10178 //// OMPClauseReader implementation
10179 ////===----------------------------------------------------------------------===//
10181 // This has to be in namespace clang because it's friended by all
10182 // of the OMP clauses.
10183 namespace clang {
10185 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
10186 ASTRecordReader &Record;
10187 ASTContext &Context;
10189 public:
10190 OMPClauseReader(ASTRecordReader &Record)
10191 : Record(Record), Context(Record.getContext()) {}
10192 #define GEN_CLANG_CLAUSE_CLASS
10193 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
10194 #include "llvm/Frontend/OpenMP/OMP.inc"
10195 OMPClause *readClause();
10196 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
10197 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
10200 } // end namespace clang
10202 OMPClause *ASTRecordReader::readOMPClause() {
10203 return OMPClauseReader(*this).readClause();
10206 OMPClause *OMPClauseReader::readClause() {
10207 OMPClause *C = nullptr;
10208 switch (llvm::omp::Clause(Record.readInt())) {
10209 case llvm::omp::OMPC_if:
10210 C = new (Context) OMPIfClause();
10211 break;
10212 case llvm::omp::OMPC_final:
10213 C = new (Context) OMPFinalClause();
10214 break;
10215 case llvm::omp::OMPC_num_threads:
10216 C = new (Context) OMPNumThreadsClause();
10217 break;
10218 case llvm::omp::OMPC_safelen:
10219 C = new (Context) OMPSafelenClause();
10220 break;
10221 case llvm::omp::OMPC_simdlen:
10222 C = new (Context) OMPSimdlenClause();
10223 break;
10224 case llvm::omp::OMPC_sizes: {
10225 unsigned NumSizes = Record.readInt();
10226 C = OMPSizesClause::CreateEmpty(Context, NumSizes);
10227 break;
10229 case llvm::omp::OMPC_full:
10230 C = OMPFullClause::CreateEmpty(Context);
10231 break;
10232 case llvm::omp::OMPC_partial:
10233 C = OMPPartialClause::CreateEmpty(Context);
10234 break;
10235 case llvm::omp::OMPC_allocator:
10236 C = new (Context) OMPAllocatorClause();
10237 break;
10238 case llvm::omp::OMPC_collapse:
10239 C = new (Context) OMPCollapseClause();
10240 break;
10241 case llvm::omp::OMPC_default:
10242 C = new (Context) OMPDefaultClause();
10243 break;
10244 case llvm::omp::OMPC_proc_bind:
10245 C = new (Context) OMPProcBindClause();
10246 break;
10247 case llvm::omp::OMPC_schedule:
10248 C = new (Context) OMPScheduleClause();
10249 break;
10250 case llvm::omp::OMPC_ordered:
10251 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
10252 break;
10253 case llvm::omp::OMPC_nowait:
10254 C = new (Context) OMPNowaitClause();
10255 break;
10256 case llvm::omp::OMPC_untied:
10257 C = new (Context) OMPUntiedClause();
10258 break;
10259 case llvm::omp::OMPC_mergeable:
10260 C = new (Context) OMPMergeableClause();
10261 break;
10262 case llvm::omp::OMPC_read:
10263 C = new (Context) OMPReadClause();
10264 break;
10265 case llvm::omp::OMPC_write:
10266 C = new (Context) OMPWriteClause();
10267 break;
10268 case llvm::omp::OMPC_update:
10269 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
10270 break;
10271 case llvm::omp::OMPC_capture:
10272 C = new (Context) OMPCaptureClause();
10273 break;
10274 case llvm::omp::OMPC_compare:
10275 C = new (Context) OMPCompareClause();
10276 break;
10277 case llvm::omp::OMPC_seq_cst:
10278 C = new (Context) OMPSeqCstClause();
10279 break;
10280 case llvm::omp::OMPC_acq_rel:
10281 C = new (Context) OMPAcqRelClause();
10282 break;
10283 case llvm::omp::OMPC_acquire:
10284 C = new (Context) OMPAcquireClause();
10285 break;
10286 case llvm::omp::OMPC_release:
10287 C = new (Context) OMPReleaseClause();
10288 break;
10289 case llvm::omp::OMPC_relaxed:
10290 C = new (Context) OMPRelaxedClause();
10291 break;
10292 case llvm::omp::OMPC_threads:
10293 C = new (Context) OMPThreadsClause();
10294 break;
10295 case llvm::omp::OMPC_simd:
10296 C = new (Context) OMPSIMDClause();
10297 break;
10298 case llvm::omp::OMPC_nogroup:
10299 C = new (Context) OMPNogroupClause();
10300 break;
10301 case llvm::omp::OMPC_unified_address:
10302 C = new (Context) OMPUnifiedAddressClause();
10303 break;
10304 case llvm::omp::OMPC_unified_shared_memory:
10305 C = new (Context) OMPUnifiedSharedMemoryClause();
10306 break;
10307 case llvm::omp::OMPC_reverse_offload:
10308 C = new (Context) OMPReverseOffloadClause();
10309 break;
10310 case llvm::omp::OMPC_dynamic_allocators:
10311 C = new (Context) OMPDynamicAllocatorsClause();
10312 break;
10313 case llvm::omp::OMPC_atomic_default_mem_order:
10314 C = new (Context) OMPAtomicDefaultMemOrderClause();
10315 break;
10316 case llvm::omp::OMPC_at:
10317 C = new (Context) OMPAtClause();
10318 break;
10319 case llvm::omp::OMPC_severity:
10320 C = new (Context) OMPSeverityClause();
10321 break;
10322 case llvm::omp::OMPC_message:
10323 C = new (Context) OMPMessageClause();
10324 break;
10325 case llvm::omp::OMPC_private:
10326 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
10327 break;
10328 case llvm::omp::OMPC_firstprivate:
10329 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
10330 break;
10331 case llvm::omp::OMPC_lastprivate:
10332 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
10333 break;
10334 case llvm::omp::OMPC_shared:
10335 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
10336 break;
10337 case llvm::omp::OMPC_reduction: {
10338 unsigned N = Record.readInt();
10339 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
10340 C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
10341 break;
10343 case llvm::omp::OMPC_task_reduction:
10344 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
10345 break;
10346 case llvm::omp::OMPC_in_reduction:
10347 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
10348 break;
10349 case llvm::omp::OMPC_linear:
10350 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
10351 break;
10352 case llvm::omp::OMPC_aligned:
10353 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
10354 break;
10355 case llvm::omp::OMPC_copyin:
10356 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
10357 break;
10358 case llvm::omp::OMPC_copyprivate:
10359 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
10360 break;
10361 case llvm::omp::OMPC_flush:
10362 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
10363 break;
10364 case llvm::omp::OMPC_depobj:
10365 C = OMPDepobjClause::CreateEmpty(Context);
10366 break;
10367 case llvm::omp::OMPC_depend: {
10368 unsigned NumVars = Record.readInt();
10369 unsigned NumLoops = Record.readInt();
10370 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
10371 break;
10373 case llvm::omp::OMPC_device:
10374 C = new (Context) OMPDeviceClause();
10375 break;
10376 case llvm::omp::OMPC_map: {
10377 OMPMappableExprListSizeTy Sizes;
10378 Sizes.NumVars = Record.readInt();
10379 Sizes.NumUniqueDeclarations = Record.readInt();
10380 Sizes.NumComponentLists = Record.readInt();
10381 Sizes.NumComponents = Record.readInt();
10382 C = OMPMapClause::CreateEmpty(Context, Sizes);
10383 break;
10385 case llvm::omp::OMPC_num_teams:
10386 C = new (Context) OMPNumTeamsClause();
10387 break;
10388 case llvm::omp::OMPC_thread_limit:
10389 C = new (Context) OMPThreadLimitClause();
10390 break;
10391 case llvm::omp::OMPC_priority:
10392 C = new (Context) OMPPriorityClause();
10393 break;
10394 case llvm::omp::OMPC_grainsize:
10395 C = new (Context) OMPGrainsizeClause();
10396 break;
10397 case llvm::omp::OMPC_num_tasks:
10398 C = new (Context) OMPNumTasksClause();
10399 break;
10400 case llvm::omp::OMPC_hint:
10401 C = new (Context) OMPHintClause();
10402 break;
10403 case llvm::omp::OMPC_dist_schedule:
10404 C = new (Context) OMPDistScheduleClause();
10405 break;
10406 case llvm::omp::OMPC_defaultmap:
10407 C = new (Context) OMPDefaultmapClause();
10408 break;
10409 case llvm::omp::OMPC_to: {
10410 OMPMappableExprListSizeTy Sizes;
10411 Sizes.NumVars = Record.readInt();
10412 Sizes.NumUniqueDeclarations = Record.readInt();
10413 Sizes.NumComponentLists = Record.readInt();
10414 Sizes.NumComponents = Record.readInt();
10415 C = OMPToClause::CreateEmpty(Context, Sizes);
10416 break;
10418 case llvm::omp::OMPC_from: {
10419 OMPMappableExprListSizeTy Sizes;
10420 Sizes.NumVars = Record.readInt();
10421 Sizes.NumUniqueDeclarations = Record.readInt();
10422 Sizes.NumComponentLists = Record.readInt();
10423 Sizes.NumComponents = Record.readInt();
10424 C = OMPFromClause::CreateEmpty(Context, Sizes);
10425 break;
10427 case llvm::omp::OMPC_use_device_ptr: {
10428 OMPMappableExprListSizeTy Sizes;
10429 Sizes.NumVars = Record.readInt();
10430 Sizes.NumUniqueDeclarations = Record.readInt();
10431 Sizes.NumComponentLists = Record.readInt();
10432 Sizes.NumComponents = Record.readInt();
10433 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
10434 break;
10436 case llvm::omp::OMPC_use_device_addr: {
10437 OMPMappableExprListSizeTy Sizes;
10438 Sizes.NumVars = Record.readInt();
10439 Sizes.NumUniqueDeclarations = Record.readInt();
10440 Sizes.NumComponentLists = Record.readInt();
10441 Sizes.NumComponents = Record.readInt();
10442 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
10443 break;
10445 case llvm::omp::OMPC_is_device_ptr: {
10446 OMPMappableExprListSizeTy Sizes;
10447 Sizes.NumVars = Record.readInt();
10448 Sizes.NumUniqueDeclarations = Record.readInt();
10449 Sizes.NumComponentLists = Record.readInt();
10450 Sizes.NumComponents = Record.readInt();
10451 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
10452 break;
10454 case llvm::omp::OMPC_has_device_addr: {
10455 OMPMappableExprListSizeTy Sizes;
10456 Sizes.NumVars = Record.readInt();
10457 Sizes.NumUniqueDeclarations = Record.readInt();
10458 Sizes.NumComponentLists = Record.readInt();
10459 Sizes.NumComponents = Record.readInt();
10460 C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes);
10461 break;
10463 case llvm::omp::OMPC_allocate:
10464 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
10465 break;
10466 case llvm::omp::OMPC_nontemporal:
10467 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
10468 break;
10469 case llvm::omp::OMPC_inclusive:
10470 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
10471 break;
10472 case llvm::omp::OMPC_exclusive:
10473 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
10474 break;
10475 case llvm::omp::OMPC_order:
10476 C = new (Context) OMPOrderClause();
10477 break;
10478 case llvm::omp::OMPC_init:
10479 C = OMPInitClause::CreateEmpty(Context, Record.readInt());
10480 break;
10481 case llvm::omp::OMPC_use:
10482 C = new (Context) OMPUseClause();
10483 break;
10484 case llvm::omp::OMPC_destroy:
10485 C = new (Context) OMPDestroyClause();
10486 break;
10487 case llvm::omp::OMPC_novariants:
10488 C = new (Context) OMPNovariantsClause();
10489 break;
10490 case llvm::omp::OMPC_nocontext:
10491 C = new (Context) OMPNocontextClause();
10492 break;
10493 case llvm::omp::OMPC_detach:
10494 C = new (Context) OMPDetachClause();
10495 break;
10496 case llvm::omp::OMPC_uses_allocators:
10497 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
10498 break;
10499 case llvm::omp::OMPC_affinity:
10500 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
10501 break;
10502 case llvm::omp::OMPC_filter:
10503 C = new (Context) OMPFilterClause();
10504 break;
10505 case llvm::omp::OMPC_bind:
10506 C = OMPBindClause::CreateEmpty(Context);
10507 break;
10508 case llvm::omp::OMPC_align:
10509 C = new (Context) OMPAlignClause();
10510 break;
10511 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
10512 C = new (Context) OMPXDynCGroupMemClause();
10513 break;
10514 case llvm::omp::OMPC_doacross: {
10515 unsigned NumVars = Record.readInt();
10516 unsigned NumLoops = Record.readInt();
10517 C = OMPDoacrossClause::CreateEmpty(Context, NumVars, NumLoops);
10518 break;
10520 case llvm::omp::OMPC_ompx_attribute:
10521 C = new (Context) OMPXAttributeClause();
10522 break;
10523 case llvm::omp::OMPC_ompx_bare:
10524 C = new (Context) OMPXBareClause();
10525 break;
10526 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \
10527 case llvm::omp::Enum: \
10528 break;
10529 #include "llvm/Frontend/OpenMP/OMPKinds.def"
10530 default:
10531 break;
10533 assert(C && "Unknown OMPClause type");
10535 Visit(C);
10536 C->setLocStart(Record.readSourceLocation());
10537 C->setLocEnd(Record.readSourceLocation());
10539 return C;
10542 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
10543 C->setPreInitStmt(Record.readSubStmt(),
10544 static_cast<OpenMPDirectiveKind>(Record.readInt()));
10547 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
10548 VisitOMPClauseWithPreInit(C);
10549 C->setPostUpdateExpr(Record.readSubExpr());
10552 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
10553 VisitOMPClauseWithPreInit(C);
10554 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
10555 C->setNameModifierLoc(Record.readSourceLocation());
10556 C->setColonLoc(Record.readSourceLocation());
10557 C->setCondition(Record.readSubExpr());
10558 C->setLParenLoc(Record.readSourceLocation());
10561 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
10562 VisitOMPClauseWithPreInit(C);
10563 C->setCondition(Record.readSubExpr());
10564 C->setLParenLoc(Record.readSourceLocation());
10567 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
10568 VisitOMPClauseWithPreInit(C);
10569 C->setNumThreads(Record.readSubExpr());
10570 C->setLParenLoc(Record.readSourceLocation());
10573 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
10574 C->setSafelen(Record.readSubExpr());
10575 C->setLParenLoc(Record.readSourceLocation());
10578 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
10579 C->setSimdlen(Record.readSubExpr());
10580 C->setLParenLoc(Record.readSourceLocation());
10583 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
10584 for (Expr *&E : C->getSizesRefs())
10585 E = Record.readSubExpr();
10586 C->setLParenLoc(Record.readSourceLocation());
10589 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
10591 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
10592 C->setFactor(Record.readSubExpr());
10593 C->setLParenLoc(Record.readSourceLocation());
10596 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
10597 C->setAllocator(Record.readExpr());
10598 C->setLParenLoc(Record.readSourceLocation());
10601 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
10602 C->setNumForLoops(Record.readSubExpr());
10603 C->setLParenLoc(Record.readSourceLocation());
10606 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
10607 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
10608 C->setLParenLoc(Record.readSourceLocation());
10609 C->setDefaultKindKwLoc(Record.readSourceLocation());
10612 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
10613 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
10614 C->setLParenLoc(Record.readSourceLocation());
10615 C->setProcBindKindKwLoc(Record.readSourceLocation());
10618 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
10619 VisitOMPClauseWithPreInit(C);
10620 C->setScheduleKind(
10621 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
10622 C->setFirstScheduleModifier(
10623 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10624 C->setSecondScheduleModifier(
10625 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10626 C->setChunkSize(Record.readSubExpr());
10627 C->setLParenLoc(Record.readSourceLocation());
10628 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
10629 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
10630 C->setScheduleKindLoc(Record.readSourceLocation());
10631 C->setCommaLoc(Record.readSourceLocation());
10634 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
10635 C->setNumForLoops(Record.readSubExpr());
10636 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10637 C->setLoopNumIterations(I, Record.readSubExpr());
10638 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10639 C->setLoopCounter(I, Record.readSubExpr());
10640 C->setLParenLoc(Record.readSourceLocation());
10643 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
10644 C->setEventHandler(Record.readSubExpr());
10645 C->setLParenLoc(Record.readSourceLocation());
10648 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
10650 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
10652 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
10654 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
10656 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
10658 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
10659 if (C->isExtended()) {
10660 C->setLParenLoc(Record.readSourceLocation());
10661 C->setArgumentLoc(Record.readSourceLocation());
10662 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
10666 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
10668 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
10670 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
10672 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
10674 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
10676 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
10678 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
10680 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
10682 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
10684 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
10686 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
10687 unsigned NumVars = C->varlist_size();
10688 SmallVector<Expr *, 16> Vars;
10689 Vars.reserve(NumVars);
10690 for (unsigned I = 0; I != NumVars; ++I)
10691 Vars.push_back(Record.readSubExpr());
10692 C->setVarRefs(Vars);
10693 C->setIsTarget(Record.readBool());
10694 C->setIsTargetSync(Record.readBool());
10695 C->setLParenLoc(Record.readSourceLocation());
10696 C->setVarLoc(Record.readSourceLocation());
10699 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
10700 C->setInteropVar(Record.readSubExpr());
10701 C->setLParenLoc(Record.readSourceLocation());
10702 C->setVarLoc(Record.readSourceLocation());
10705 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
10706 C->setInteropVar(Record.readSubExpr());
10707 C->setLParenLoc(Record.readSourceLocation());
10708 C->setVarLoc(Record.readSourceLocation());
10711 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
10712 VisitOMPClauseWithPreInit(C);
10713 C->setCondition(Record.readSubExpr());
10714 C->setLParenLoc(Record.readSourceLocation());
10717 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
10718 VisitOMPClauseWithPreInit(C);
10719 C->setCondition(Record.readSubExpr());
10720 C->setLParenLoc(Record.readSourceLocation());
10723 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
10725 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
10726 OMPUnifiedSharedMemoryClause *) {}
10728 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
10730 void
10731 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
10734 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
10735 OMPAtomicDefaultMemOrderClause *C) {
10736 C->setAtomicDefaultMemOrderKind(
10737 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
10738 C->setLParenLoc(Record.readSourceLocation());
10739 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
10742 void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
10743 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
10744 C->setLParenLoc(Record.readSourceLocation());
10745 C->setAtKindKwLoc(Record.readSourceLocation());
10748 void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
10749 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
10750 C->setLParenLoc(Record.readSourceLocation());
10751 C->setSeverityKindKwLoc(Record.readSourceLocation());
10754 void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
10755 C->setMessageString(Record.readSubExpr());
10756 C->setLParenLoc(Record.readSourceLocation());
10759 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
10760 C->setLParenLoc(Record.readSourceLocation());
10761 unsigned NumVars = C->varlist_size();
10762 SmallVector<Expr *, 16> Vars;
10763 Vars.reserve(NumVars);
10764 for (unsigned i = 0; i != NumVars; ++i)
10765 Vars.push_back(Record.readSubExpr());
10766 C->setVarRefs(Vars);
10767 Vars.clear();
10768 for (unsigned i = 0; i != NumVars; ++i)
10769 Vars.push_back(Record.readSubExpr());
10770 C->setPrivateCopies(Vars);
10773 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
10774 VisitOMPClauseWithPreInit(C);
10775 C->setLParenLoc(Record.readSourceLocation());
10776 unsigned NumVars = C->varlist_size();
10777 SmallVector<Expr *, 16> Vars;
10778 Vars.reserve(NumVars);
10779 for (unsigned i = 0; i != NumVars; ++i)
10780 Vars.push_back(Record.readSubExpr());
10781 C->setVarRefs(Vars);
10782 Vars.clear();
10783 for (unsigned i = 0; i != NumVars; ++i)
10784 Vars.push_back(Record.readSubExpr());
10785 C->setPrivateCopies(Vars);
10786 Vars.clear();
10787 for (unsigned i = 0; i != NumVars; ++i)
10788 Vars.push_back(Record.readSubExpr());
10789 C->setInits(Vars);
10792 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
10793 VisitOMPClauseWithPostUpdate(C);
10794 C->setLParenLoc(Record.readSourceLocation());
10795 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
10796 C->setKindLoc(Record.readSourceLocation());
10797 C->setColonLoc(Record.readSourceLocation());
10798 unsigned NumVars = C->varlist_size();
10799 SmallVector<Expr *, 16> Vars;
10800 Vars.reserve(NumVars);
10801 for (unsigned i = 0; i != NumVars; ++i)
10802 Vars.push_back(Record.readSubExpr());
10803 C->setVarRefs(Vars);
10804 Vars.clear();
10805 for (unsigned i = 0; i != NumVars; ++i)
10806 Vars.push_back(Record.readSubExpr());
10807 C->setPrivateCopies(Vars);
10808 Vars.clear();
10809 for (unsigned i = 0; i != NumVars; ++i)
10810 Vars.push_back(Record.readSubExpr());
10811 C->setSourceExprs(Vars);
10812 Vars.clear();
10813 for (unsigned i = 0; i != NumVars; ++i)
10814 Vars.push_back(Record.readSubExpr());
10815 C->setDestinationExprs(Vars);
10816 Vars.clear();
10817 for (unsigned i = 0; i != NumVars; ++i)
10818 Vars.push_back(Record.readSubExpr());
10819 C->setAssignmentOps(Vars);
10822 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
10823 C->setLParenLoc(Record.readSourceLocation());
10824 unsigned NumVars = C->varlist_size();
10825 SmallVector<Expr *, 16> Vars;
10826 Vars.reserve(NumVars);
10827 for (unsigned i = 0; i != NumVars; ++i)
10828 Vars.push_back(Record.readSubExpr());
10829 C->setVarRefs(Vars);
10832 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
10833 VisitOMPClauseWithPostUpdate(C);
10834 C->setLParenLoc(Record.readSourceLocation());
10835 C->setModifierLoc(Record.readSourceLocation());
10836 C->setColonLoc(Record.readSourceLocation());
10837 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10838 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10839 C->setQualifierLoc(NNSL);
10840 C->setNameInfo(DNI);
10842 unsigned NumVars = C->varlist_size();
10843 SmallVector<Expr *, 16> Vars;
10844 Vars.reserve(NumVars);
10845 for (unsigned i = 0; i != NumVars; ++i)
10846 Vars.push_back(Record.readSubExpr());
10847 C->setVarRefs(Vars);
10848 Vars.clear();
10849 for (unsigned i = 0; i != NumVars; ++i)
10850 Vars.push_back(Record.readSubExpr());
10851 C->setPrivates(Vars);
10852 Vars.clear();
10853 for (unsigned i = 0; i != NumVars; ++i)
10854 Vars.push_back(Record.readSubExpr());
10855 C->setLHSExprs(Vars);
10856 Vars.clear();
10857 for (unsigned i = 0; i != NumVars; ++i)
10858 Vars.push_back(Record.readSubExpr());
10859 C->setRHSExprs(Vars);
10860 Vars.clear();
10861 for (unsigned i = 0; i != NumVars; ++i)
10862 Vars.push_back(Record.readSubExpr());
10863 C->setReductionOps(Vars);
10864 if (C->getModifier() == OMPC_REDUCTION_inscan) {
10865 Vars.clear();
10866 for (unsigned i = 0; i != NumVars; ++i)
10867 Vars.push_back(Record.readSubExpr());
10868 C->setInscanCopyOps(Vars);
10869 Vars.clear();
10870 for (unsigned i = 0; i != NumVars; ++i)
10871 Vars.push_back(Record.readSubExpr());
10872 C->setInscanCopyArrayTemps(Vars);
10873 Vars.clear();
10874 for (unsigned i = 0; i != NumVars; ++i)
10875 Vars.push_back(Record.readSubExpr());
10876 C->setInscanCopyArrayElems(Vars);
10880 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
10881 VisitOMPClauseWithPostUpdate(C);
10882 C->setLParenLoc(Record.readSourceLocation());
10883 C->setColonLoc(Record.readSourceLocation());
10884 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10885 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10886 C->setQualifierLoc(NNSL);
10887 C->setNameInfo(DNI);
10889 unsigned NumVars = C->varlist_size();
10890 SmallVector<Expr *, 16> Vars;
10891 Vars.reserve(NumVars);
10892 for (unsigned I = 0; I != NumVars; ++I)
10893 Vars.push_back(Record.readSubExpr());
10894 C->setVarRefs(Vars);
10895 Vars.clear();
10896 for (unsigned I = 0; I != NumVars; ++I)
10897 Vars.push_back(Record.readSubExpr());
10898 C->setPrivates(Vars);
10899 Vars.clear();
10900 for (unsigned I = 0; I != NumVars; ++I)
10901 Vars.push_back(Record.readSubExpr());
10902 C->setLHSExprs(Vars);
10903 Vars.clear();
10904 for (unsigned I = 0; I != NumVars; ++I)
10905 Vars.push_back(Record.readSubExpr());
10906 C->setRHSExprs(Vars);
10907 Vars.clear();
10908 for (unsigned I = 0; I != NumVars; ++I)
10909 Vars.push_back(Record.readSubExpr());
10910 C->setReductionOps(Vars);
10913 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
10914 VisitOMPClauseWithPostUpdate(C);
10915 C->setLParenLoc(Record.readSourceLocation());
10916 C->setColonLoc(Record.readSourceLocation());
10917 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10918 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10919 C->setQualifierLoc(NNSL);
10920 C->setNameInfo(DNI);
10922 unsigned NumVars = C->varlist_size();
10923 SmallVector<Expr *, 16> Vars;
10924 Vars.reserve(NumVars);
10925 for (unsigned I = 0; I != NumVars; ++I)
10926 Vars.push_back(Record.readSubExpr());
10927 C->setVarRefs(Vars);
10928 Vars.clear();
10929 for (unsigned I = 0; I != NumVars; ++I)
10930 Vars.push_back(Record.readSubExpr());
10931 C->setPrivates(Vars);
10932 Vars.clear();
10933 for (unsigned I = 0; I != NumVars; ++I)
10934 Vars.push_back(Record.readSubExpr());
10935 C->setLHSExprs(Vars);
10936 Vars.clear();
10937 for (unsigned I = 0; I != NumVars; ++I)
10938 Vars.push_back(Record.readSubExpr());
10939 C->setRHSExprs(Vars);
10940 Vars.clear();
10941 for (unsigned I = 0; I != NumVars; ++I)
10942 Vars.push_back(Record.readSubExpr());
10943 C->setReductionOps(Vars);
10944 Vars.clear();
10945 for (unsigned I = 0; I != NumVars; ++I)
10946 Vars.push_back(Record.readSubExpr());
10947 C->setTaskgroupDescriptors(Vars);
10950 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
10951 VisitOMPClauseWithPostUpdate(C);
10952 C->setLParenLoc(Record.readSourceLocation());
10953 C->setColonLoc(Record.readSourceLocation());
10954 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
10955 C->setModifierLoc(Record.readSourceLocation());
10956 unsigned NumVars = C->varlist_size();
10957 SmallVector<Expr *, 16> Vars;
10958 Vars.reserve(NumVars);
10959 for (unsigned i = 0; i != NumVars; ++i)
10960 Vars.push_back(Record.readSubExpr());
10961 C->setVarRefs(Vars);
10962 Vars.clear();
10963 for (unsigned i = 0; i != NumVars; ++i)
10964 Vars.push_back(Record.readSubExpr());
10965 C->setPrivates(Vars);
10966 Vars.clear();
10967 for (unsigned i = 0; i != NumVars; ++i)
10968 Vars.push_back(Record.readSubExpr());
10969 C->setInits(Vars);
10970 Vars.clear();
10971 for (unsigned i = 0; i != NumVars; ++i)
10972 Vars.push_back(Record.readSubExpr());
10973 C->setUpdates(Vars);
10974 Vars.clear();
10975 for (unsigned i = 0; i != NumVars; ++i)
10976 Vars.push_back(Record.readSubExpr());
10977 C->setFinals(Vars);
10978 C->setStep(Record.readSubExpr());
10979 C->setCalcStep(Record.readSubExpr());
10980 Vars.clear();
10981 for (unsigned I = 0; I != NumVars + 1; ++I)
10982 Vars.push_back(Record.readSubExpr());
10983 C->setUsedExprs(Vars);
10986 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
10987 C->setLParenLoc(Record.readSourceLocation());
10988 C->setColonLoc(Record.readSourceLocation());
10989 unsigned NumVars = C->varlist_size();
10990 SmallVector<Expr *, 16> Vars;
10991 Vars.reserve(NumVars);
10992 for (unsigned i = 0; i != NumVars; ++i)
10993 Vars.push_back(Record.readSubExpr());
10994 C->setVarRefs(Vars);
10995 C->setAlignment(Record.readSubExpr());
10998 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
10999 C->setLParenLoc(Record.readSourceLocation());
11000 unsigned NumVars = C->varlist_size();
11001 SmallVector<Expr *, 16> Exprs;
11002 Exprs.reserve(NumVars);
11003 for (unsigned i = 0; i != NumVars; ++i)
11004 Exprs.push_back(Record.readSubExpr());
11005 C->setVarRefs(Exprs);
11006 Exprs.clear();
11007 for (unsigned i = 0; i != NumVars; ++i)
11008 Exprs.push_back(Record.readSubExpr());
11009 C->setSourceExprs(Exprs);
11010 Exprs.clear();
11011 for (unsigned i = 0; i != NumVars; ++i)
11012 Exprs.push_back(Record.readSubExpr());
11013 C->setDestinationExprs(Exprs);
11014 Exprs.clear();
11015 for (unsigned i = 0; i != NumVars; ++i)
11016 Exprs.push_back(Record.readSubExpr());
11017 C->setAssignmentOps(Exprs);
11020 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
11021 C->setLParenLoc(Record.readSourceLocation());
11022 unsigned NumVars = C->varlist_size();
11023 SmallVector<Expr *, 16> Exprs;
11024 Exprs.reserve(NumVars);
11025 for (unsigned i = 0; i != NumVars; ++i)
11026 Exprs.push_back(Record.readSubExpr());
11027 C->setVarRefs(Exprs);
11028 Exprs.clear();
11029 for (unsigned i = 0; i != NumVars; ++i)
11030 Exprs.push_back(Record.readSubExpr());
11031 C->setSourceExprs(Exprs);
11032 Exprs.clear();
11033 for (unsigned i = 0; i != NumVars; ++i)
11034 Exprs.push_back(Record.readSubExpr());
11035 C->setDestinationExprs(Exprs);
11036 Exprs.clear();
11037 for (unsigned i = 0; i != NumVars; ++i)
11038 Exprs.push_back(Record.readSubExpr());
11039 C->setAssignmentOps(Exprs);
11042 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
11043 C->setLParenLoc(Record.readSourceLocation());
11044 unsigned NumVars = C->varlist_size();
11045 SmallVector<Expr *, 16> Vars;
11046 Vars.reserve(NumVars);
11047 for (unsigned i = 0; i != NumVars; ++i)
11048 Vars.push_back(Record.readSubExpr());
11049 C->setVarRefs(Vars);
11052 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
11053 C->setDepobj(Record.readSubExpr());
11054 C->setLParenLoc(Record.readSourceLocation());
11057 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
11058 C->setLParenLoc(Record.readSourceLocation());
11059 C->setModifier(Record.readSubExpr());
11060 C->setDependencyKind(
11061 static_cast<OpenMPDependClauseKind>(Record.readInt()));
11062 C->setDependencyLoc(Record.readSourceLocation());
11063 C->setColonLoc(Record.readSourceLocation());
11064 C->setOmpAllMemoryLoc(Record.readSourceLocation());
11065 unsigned NumVars = C->varlist_size();
11066 SmallVector<Expr *, 16> Vars;
11067 Vars.reserve(NumVars);
11068 for (unsigned I = 0; I != NumVars; ++I)
11069 Vars.push_back(Record.readSubExpr());
11070 C->setVarRefs(Vars);
11071 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11072 C->setLoopData(I, Record.readSubExpr());
11075 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
11076 VisitOMPClauseWithPreInit(C);
11077 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
11078 C->setDevice(Record.readSubExpr());
11079 C->setModifierLoc(Record.readSourceLocation());
11080 C->setLParenLoc(Record.readSourceLocation());
11083 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
11084 C->setLParenLoc(Record.readSourceLocation());
11085 bool HasIteratorModifier = false;
11086 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
11087 C->setMapTypeModifier(
11088 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
11089 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
11090 if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
11091 HasIteratorModifier = true;
11093 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11094 C->setMapperIdInfo(Record.readDeclarationNameInfo());
11095 C->setMapType(
11096 static_cast<OpenMPMapClauseKind>(Record.readInt()));
11097 C->setMapLoc(Record.readSourceLocation());
11098 C->setColonLoc(Record.readSourceLocation());
11099 auto NumVars = C->varlist_size();
11100 auto UniqueDecls = C->getUniqueDeclarationsNum();
11101 auto TotalLists = C->getTotalComponentListNum();
11102 auto TotalComponents = C->getTotalComponentsNum();
11104 SmallVector<Expr *, 16> Vars;
11105 Vars.reserve(NumVars);
11106 for (unsigned i = 0; i != NumVars; ++i)
11107 Vars.push_back(Record.readExpr());
11108 C->setVarRefs(Vars);
11110 SmallVector<Expr *, 16> UDMappers;
11111 UDMappers.reserve(NumVars);
11112 for (unsigned I = 0; I < NumVars; ++I)
11113 UDMappers.push_back(Record.readExpr());
11114 C->setUDMapperRefs(UDMappers);
11116 if (HasIteratorModifier)
11117 C->setIteratorModifier(Record.readExpr());
11119 SmallVector<ValueDecl *, 16> Decls;
11120 Decls.reserve(UniqueDecls);
11121 for (unsigned i = 0; i < UniqueDecls; ++i)
11122 Decls.push_back(Record.readDeclAs<ValueDecl>());
11123 C->setUniqueDecls(Decls);
11125 SmallVector<unsigned, 16> ListsPerDecl;
11126 ListsPerDecl.reserve(UniqueDecls);
11127 for (unsigned i = 0; i < UniqueDecls; ++i)
11128 ListsPerDecl.push_back(Record.readInt());
11129 C->setDeclNumLists(ListsPerDecl);
11131 SmallVector<unsigned, 32> ListSizes;
11132 ListSizes.reserve(TotalLists);
11133 for (unsigned i = 0; i < TotalLists; ++i)
11134 ListSizes.push_back(Record.readInt());
11135 C->setComponentListSizes(ListSizes);
11137 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11138 Components.reserve(TotalComponents);
11139 for (unsigned i = 0; i < TotalComponents; ++i) {
11140 Expr *AssociatedExprPr = Record.readExpr();
11141 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11142 Components.emplace_back(AssociatedExprPr, AssociatedDecl,
11143 /*IsNonContiguous=*/false);
11145 C->setComponents(Components, ListSizes);
11148 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
11149 C->setLParenLoc(Record.readSourceLocation());
11150 C->setColonLoc(Record.readSourceLocation());
11151 C->setAllocator(Record.readSubExpr());
11152 unsigned NumVars = C->varlist_size();
11153 SmallVector<Expr *, 16> Vars;
11154 Vars.reserve(NumVars);
11155 for (unsigned i = 0; i != NumVars; ++i)
11156 Vars.push_back(Record.readSubExpr());
11157 C->setVarRefs(Vars);
11160 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
11161 VisitOMPClauseWithPreInit(C);
11162 C->setNumTeams(Record.readSubExpr());
11163 C->setLParenLoc(Record.readSourceLocation());
11166 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
11167 VisitOMPClauseWithPreInit(C);
11168 C->setThreadLimit(Record.readSubExpr());
11169 C->setLParenLoc(Record.readSourceLocation());
11172 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
11173 VisitOMPClauseWithPreInit(C);
11174 C->setPriority(Record.readSubExpr());
11175 C->setLParenLoc(Record.readSourceLocation());
11178 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
11179 VisitOMPClauseWithPreInit(C);
11180 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
11181 C->setGrainsize(Record.readSubExpr());
11182 C->setModifierLoc(Record.readSourceLocation());
11183 C->setLParenLoc(Record.readSourceLocation());
11186 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
11187 VisitOMPClauseWithPreInit(C);
11188 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
11189 C->setNumTasks(Record.readSubExpr());
11190 C->setModifierLoc(Record.readSourceLocation());
11191 C->setLParenLoc(Record.readSourceLocation());
11194 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
11195 C->setHint(Record.readSubExpr());
11196 C->setLParenLoc(Record.readSourceLocation());
11199 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
11200 VisitOMPClauseWithPreInit(C);
11201 C->setDistScheduleKind(
11202 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
11203 C->setChunkSize(Record.readSubExpr());
11204 C->setLParenLoc(Record.readSourceLocation());
11205 C->setDistScheduleKindLoc(Record.readSourceLocation());
11206 C->setCommaLoc(Record.readSourceLocation());
11209 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
11210 C->setDefaultmapKind(
11211 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
11212 C->setDefaultmapModifier(
11213 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
11214 C->setLParenLoc(Record.readSourceLocation());
11215 C->setDefaultmapModifierLoc(Record.readSourceLocation());
11216 C->setDefaultmapKindLoc(Record.readSourceLocation());
11219 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
11220 C->setLParenLoc(Record.readSourceLocation());
11221 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11222 C->setMotionModifier(
11223 I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11224 C->setMotionModifierLoc(I, Record.readSourceLocation());
11226 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11227 C->setMapperIdInfo(Record.readDeclarationNameInfo());
11228 C->setColonLoc(Record.readSourceLocation());
11229 auto NumVars = C->varlist_size();
11230 auto UniqueDecls = C->getUniqueDeclarationsNum();
11231 auto TotalLists = C->getTotalComponentListNum();
11232 auto TotalComponents = C->getTotalComponentsNum();
11234 SmallVector<Expr *, 16> Vars;
11235 Vars.reserve(NumVars);
11236 for (unsigned i = 0; i != NumVars; ++i)
11237 Vars.push_back(Record.readSubExpr());
11238 C->setVarRefs(Vars);
11240 SmallVector<Expr *, 16> UDMappers;
11241 UDMappers.reserve(NumVars);
11242 for (unsigned I = 0; I < NumVars; ++I)
11243 UDMappers.push_back(Record.readSubExpr());
11244 C->setUDMapperRefs(UDMappers);
11246 SmallVector<ValueDecl *, 16> Decls;
11247 Decls.reserve(UniqueDecls);
11248 for (unsigned i = 0; i < UniqueDecls; ++i)
11249 Decls.push_back(Record.readDeclAs<ValueDecl>());
11250 C->setUniqueDecls(Decls);
11252 SmallVector<unsigned, 16> ListsPerDecl;
11253 ListsPerDecl.reserve(UniqueDecls);
11254 for (unsigned i = 0; i < UniqueDecls; ++i)
11255 ListsPerDecl.push_back(Record.readInt());
11256 C->setDeclNumLists(ListsPerDecl);
11258 SmallVector<unsigned, 32> ListSizes;
11259 ListSizes.reserve(TotalLists);
11260 for (unsigned i = 0; i < TotalLists; ++i)
11261 ListSizes.push_back(Record.readInt());
11262 C->setComponentListSizes(ListSizes);
11264 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11265 Components.reserve(TotalComponents);
11266 for (unsigned i = 0; i < TotalComponents; ++i) {
11267 Expr *AssociatedExprPr = Record.readSubExpr();
11268 bool IsNonContiguous = Record.readBool();
11269 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11270 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
11272 C->setComponents(Components, ListSizes);
11275 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
11276 C->setLParenLoc(Record.readSourceLocation());
11277 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11278 C->setMotionModifier(
11279 I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11280 C->setMotionModifierLoc(I, Record.readSourceLocation());
11282 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11283 C->setMapperIdInfo(Record.readDeclarationNameInfo());
11284 C->setColonLoc(Record.readSourceLocation());
11285 auto NumVars = C->varlist_size();
11286 auto UniqueDecls = C->getUniqueDeclarationsNum();
11287 auto TotalLists = C->getTotalComponentListNum();
11288 auto TotalComponents = C->getTotalComponentsNum();
11290 SmallVector<Expr *, 16> Vars;
11291 Vars.reserve(NumVars);
11292 for (unsigned i = 0; i != NumVars; ++i)
11293 Vars.push_back(Record.readSubExpr());
11294 C->setVarRefs(Vars);
11296 SmallVector<Expr *, 16> UDMappers;
11297 UDMappers.reserve(NumVars);
11298 for (unsigned I = 0; I < NumVars; ++I)
11299 UDMappers.push_back(Record.readSubExpr());
11300 C->setUDMapperRefs(UDMappers);
11302 SmallVector<ValueDecl *, 16> Decls;
11303 Decls.reserve(UniqueDecls);
11304 for (unsigned i = 0; i < UniqueDecls; ++i)
11305 Decls.push_back(Record.readDeclAs<ValueDecl>());
11306 C->setUniqueDecls(Decls);
11308 SmallVector<unsigned, 16> ListsPerDecl;
11309 ListsPerDecl.reserve(UniqueDecls);
11310 for (unsigned i = 0; i < UniqueDecls; ++i)
11311 ListsPerDecl.push_back(Record.readInt());
11312 C->setDeclNumLists(ListsPerDecl);
11314 SmallVector<unsigned, 32> ListSizes;
11315 ListSizes.reserve(TotalLists);
11316 for (unsigned i = 0; i < TotalLists; ++i)
11317 ListSizes.push_back(Record.readInt());
11318 C->setComponentListSizes(ListSizes);
11320 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11321 Components.reserve(TotalComponents);
11322 for (unsigned i = 0; i < TotalComponents; ++i) {
11323 Expr *AssociatedExprPr = Record.readSubExpr();
11324 bool IsNonContiguous = Record.readBool();
11325 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11326 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
11328 C->setComponents(Components, ListSizes);
11331 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
11332 C->setLParenLoc(Record.readSourceLocation());
11333 auto NumVars = C->varlist_size();
11334 auto UniqueDecls = C->getUniqueDeclarationsNum();
11335 auto TotalLists = C->getTotalComponentListNum();
11336 auto TotalComponents = C->getTotalComponentsNum();
11338 SmallVector<Expr *, 16> Vars;
11339 Vars.reserve(NumVars);
11340 for (unsigned i = 0; i != NumVars; ++i)
11341 Vars.push_back(Record.readSubExpr());
11342 C->setVarRefs(Vars);
11343 Vars.clear();
11344 for (unsigned i = 0; i != NumVars; ++i)
11345 Vars.push_back(Record.readSubExpr());
11346 C->setPrivateCopies(Vars);
11347 Vars.clear();
11348 for (unsigned i = 0; i != NumVars; ++i)
11349 Vars.push_back(Record.readSubExpr());
11350 C->setInits(Vars);
11352 SmallVector<ValueDecl *, 16> Decls;
11353 Decls.reserve(UniqueDecls);
11354 for (unsigned i = 0; i < UniqueDecls; ++i)
11355 Decls.push_back(Record.readDeclAs<ValueDecl>());
11356 C->setUniqueDecls(Decls);
11358 SmallVector<unsigned, 16> ListsPerDecl;
11359 ListsPerDecl.reserve(UniqueDecls);
11360 for (unsigned i = 0; i < UniqueDecls; ++i)
11361 ListsPerDecl.push_back(Record.readInt());
11362 C->setDeclNumLists(ListsPerDecl);
11364 SmallVector<unsigned, 32> ListSizes;
11365 ListSizes.reserve(TotalLists);
11366 for (unsigned i = 0; i < TotalLists; ++i)
11367 ListSizes.push_back(Record.readInt());
11368 C->setComponentListSizes(ListSizes);
11370 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11371 Components.reserve(TotalComponents);
11372 for (unsigned i = 0; i < TotalComponents; ++i) {
11373 auto *AssociatedExprPr = Record.readSubExpr();
11374 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11375 Components.emplace_back(AssociatedExprPr, AssociatedDecl,
11376 /*IsNonContiguous=*/false);
11378 C->setComponents(Components, ListSizes);
11381 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
11382 C->setLParenLoc(Record.readSourceLocation());
11383 auto NumVars = C->varlist_size();
11384 auto UniqueDecls = C->getUniqueDeclarationsNum();
11385 auto TotalLists = C->getTotalComponentListNum();
11386 auto TotalComponents = C->getTotalComponentsNum();
11388 SmallVector<Expr *, 16> Vars;
11389 Vars.reserve(NumVars);
11390 for (unsigned i = 0; i != NumVars; ++i)
11391 Vars.push_back(Record.readSubExpr());
11392 C->setVarRefs(Vars);
11394 SmallVector<ValueDecl *, 16> Decls;
11395 Decls.reserve(UniqueDecls);
11396 for (unsigned i = 0; i < UniqueDecls; ++i)
11397 Decls.push_back(Record.readDeclAs<ValueDecl>());
11398 C->setUniqueDecls(Decls);
11400 SmallVector<unsigned, 16> ListsPerDecl;
11401 ListsPerDecl.reserve(UniqueDecls);
11402 for (unsigned i = 0; i < UniqueDecls; ++i)
11403 ListsPerDecl.push_back(Record.readInt());
11404 C->setDeclNumLists(ListsPerDecl);
11406 SmallVector<unsigned, 32> ListSizes;
11407 ListSizes.reserve(TotalLists);
11408 for (unsigned i = 0; i < TotalLists; ++i)
11409 ListSizes.push_back(Record.readInt());
11410 C->setComponentListSizes(ListSizes);
11412 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11413 Components.reserve(TotalComponents);
11414 for (unsigned i = 0; i < TotalComponents; ++i) {
11415 Expr *AssociatedExpr = Record.readSubExpr();
11416 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11417 Components.emplace_back(AssociatedExpr, AssociatedDecl,
11418 /*IsNonContiguous*/ false);
11420 C->setComponents(Components, ListSizes);
11423 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
11424 C->setLParenLoc(Record.readSourceLocation());
11425 auto NumVars = C->varlist_size();
11426 auto UniqueDecls = C->getUniqueDeclarationsNum();
11427 auto TotalLists = C->getTotalComponentListNum();
11428 auto TotalComponents = C->getTotalComponentsNum();
11430 SmallVector<Expr *, 16> Vars;
11431 Vars.reserve(NumVars);
11432 for (unsigned i = 0; i != NumVars; ++i)
11433 Vars.push_back(Record.readSubExpr());
11434 C->setVarRefs(Vars);
11435 Vars.clear();
11437 SmallVector<ValueDecl *, 16> Decls;
11438 Decls.reserve(UniqueDecls);
11439 for (unsigned i = 0; i < UniqueDecls; ++i)
11440 Decls.push_back(Record.readDeclAs<ValueDecl>());
11441 C->setUniqueDecls(Decls);
11443 SmallVector<unsigned, 16> ListsPerDecl;
11444 ListsPerDecl.reserve(UniqueDecls);
11445 for (unsigned i = 0; i < UniqueDecls; ++i)
11446 ListsPerDecl.push_back(Record.readInt());
11447 C->setDeclNumLists(ListsPerDecl);
11449 SmallVector<unsigned, 32> ListSizes;
11450 ListSizes.reserve(TotalLists);
11451 for (unsigned i = 0; i < TotalLists; ++i)
11452 ListSizes.push_back(Record.readInt());
11453 C->setComponentListSizes(ListSizes);
11455 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11456 Components.reserve(TotalComponents);
11457 for (unsigned i = 0; i < TotalComponents; ++i) {
11458 Expr *AssociatedExpr = Record.readSubExpr();
11459 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11460 Components.emplace_back(AssociatedExpr, AssociatedDecl,
11461 /*IsNonContiguous=*/false);
11463 C->setComponents(Components, ListSizes);
11466 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
11467 C->setLParenLoc(Record.readSourceLocation());
11468 auto NumVars = C->varlist_size();
11469 auto UniqueDecls = C->getUniqueDeclarationsNum();
11470 auto TotalLists = C->getTotalComponentListNum();
11471 auto TotalComponents = C->getTotalComponentsNum();
11473 SmallVector<Expr *, 16> Vars;
11474 Vars.reserve(NumVars);
11475 for (unsigned I = 0; I != NumVars; ++I)
11476 Vars.push_back(Record.readSubExpr());
11477 C->setVarRefs(Vars);
11478 Vars.clear();
11480 SmallVector<ValueDecl *, 16> Decls;
11481 Decls.reserve(UniqueDecls);
11482 for (unsigned I = 0; I < UniqueDecls; ++I)
11483 Decls.push_back(Record.readDeclAs<ValueDecl>());
11484 C->setUniqueDecls(Decls);
11486 SmallVector<unsigned, 16> ListsPerDecl;
11487 ListsPerDecl.reserve(UniqueDecls);
11488 for (unsigned I = 0; I < UniqueDecls; ++I)
11489 ListsPerDecl.push_back(Record.readInt());
11490 C->setDeclNumLists(ListsPerDecl);
11492 SmallVector<unsigned, 32> ListSizes;
11493 ListSizes.reserve(TotalLists);
11494 for (unsigned i = 0; i < TotalLists; ++i)
11495 ListSizes.push_back(Record.readInt());
11496 C->setComponentListSizes(ListSizes);
11498 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11499 Components.reserve(TotalComponents);
11500 for (unsigned I = 0; I < TotalComponents; ++I) {
11501 Expr *AssociatedExpr = Record.readSubExpr();
11502 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11503 Components.emplace_back(AssociatedExpr, AssociatedDecl,
11504 /*IsNonContiguous=*/false);
11506 C->setComponents(Components, ListSizes);
11509 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
11510 C->setLParenLoc(Record.readSourceLocation());
11511 unsigned NumVars = C->varlist_size();
11512 SmallVector<Expr *, 16> Vars;
11513 Vars.reserve(NumVars);
11514 for (unsigned i = 0; i != NumVars; ++i)
11515 Vars.push_back(Record.readSubExpr());
11516 C->setVarRefs(Vars);
11517 Vars.clear();
11518 Vars.reserve(NumVars);
11519 for (unsigned i = 0; i != NumVars; ++i)
11520 Vars.push_back(Record.readSubExpr());
11521 C->setPrivateRefs(Vars);
11524 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
11525 C->setLParenLoc(Record.readSourceLocation());
11526 unsigned NumVars = C->varlist_size();
11527 SmallVector<Expr *, 16> Vars;
11528 Vars.reserve(NumVars);
11529 for (unsigned i = 0; i != NumVars; ++i)
11530 Vars.push_back(Record.readSubExpr());
11531 C->setVarRefs(Vars);
11534 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
11535 C->setLParenLoc(Record.readSourceLocation());
11536 unsigned NumVars = C->varlist_size();
11537 SmallVector<Expr *, 16> Vars;
11538 Vars.reserve(NumVars);
11539 for (unsigned i = 0; i != NumVars; ++i)
11540 Vars.push_back(Record.readSubExpr());
11541 C->setVarRefs(Vars);
11544 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
11545 C->setLParenLoc(Record.readSourceLocation());
11546 unsigned NumOfAllocators = C->getNumberOfAllocators();
11547 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
11548 Data.reserve(NumOfAllocators);
11549 for (unsigned I = 0; I != NumOfAllocators; ++I) {
11550 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
11551 D.Allocator = Record.readSubExpr();
11552 D.AllocatorTraits = Record.readSubExpr();
11553 D.LParenLoc = Record.readSourceLocation();
11554 D.RParenLoc = Record.readSourceLocation();
11556 C->setAllocatorsData(Data);
11559 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
11560 C->setLParenLoc(Record.readSourceLocation());
11561 C->setModifier(Record.readSubExpr());
11562 C->setColonLoc(Record.readSourceLocation());
11563 unsigned NumOfLocators = C->varlist_size();
11564 SmallVector<Expr *, 4> Locators;
11565 Locators.reserve(NumOfLocators);
11566 for (unsigned I = 0; I != NumOfLocators; ++I)
11567 Locators.push_back(Record.readSubExpr());
11568 C->setVarRefs(Locators);
11571 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
11572 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
11573 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
11574 C->setLParenLoc(Record.readSourceLocation());
11575 C->setKindKwLoc(Record.readSourceLocation());
11576 C->setModifierKwLoc(Record.readSourceLocation());
11579 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
11580 VisitOMPClauseWithPreInit(C);
11581 C->setThreadID(Record.readSubExpr());
11582 C->setLParenLoc(Record.readSourceLocation());
11585 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
11586 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
11587 C->setLParenLoc(Record.readSourceLocation());
11588 C->setBindKindLoc(Record.readSourceLocation());
11591 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
11592 C->setAlignment(Record.readExpr());
11593 C->setLParenLoc(Record.readSourceLocation());
11596 void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
11597 VisitOMPClauseWithPreInit(C);
11598 C->setSize(Record.readSubExpr());
11599 C->setLParenLoc(Record.readSourceLocation());
11602 void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
11603 C->setLParenLoc(Record.readSourceLocation());
11604 C->setDependenceType(
11605 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
11606 C->setDependenceLoc(Record.readSourceLocation());
11607 C->setColonLoc(Record.readSourceLocation());
11608 unsigned NumVars = C->varlist_size();
11609 SmallVector<Expr *, 16> Vars;
11610 Vars.reserve(NumVars);
11611 for (unsigned I = 0; I != NumVars; ++I)
11612 Vars.push_back(Record.readSubExpr());
11613 C->setVarRefs(Vars);
11614 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11615 C->setLoopData(I, Record.readSubExpr());
11618 void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
11619 AttrVec Attrs;
11620 Record.readAttributes(Attrs);
11621 C->setAttrs(Attrs);
11622 C->setLocStart(Record.readSourceLocation());
11623 C->setLParenLoc(Record.readSourceLocation());
11624 C->setLocEnd(Record.readSourceLocation());
11627 void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
11629 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
11630 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
11631 TI.Sets.resize(readUInt32());
11632 for (auto &Set : TI.Sets) {
11633 Set.Kind = readEnum<llvm::omp::TraitSet>();
11634 Set.Selectors.resize(readUInt32());
11635 for (auto &Selector : Set.Selectors) {
11636 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
11637 Selector.ScoreOrCondition = nullptr;
11638 if (readBool())
11639 Selector.ScoreOrCondition = readExprRef();
11640 Selector.Properties.resize(readUInt32());
11641 for (auto &Property : Selector.Properties)
11642 Property.Kind = readEnum<llvm::omp::TraitProperty>();
11645 return &TI;
11648 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
11649 if (!Data)
11650 return;
11651 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
11652 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
11653 skipInts(3);
11655 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
11656 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
11657 Clauses[I] = readOMPClause();
11658 Data->setClauses(Clauses);
11659 if (Data->hasAssociatedStmt())
11660 Data->setAssociatedStmt(readStmt());
11661 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
11662 Data->getChildren()[I] = readStmt();