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.
10//
11//===----------------------------------------------------------------------===//
12
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>
143
144using namespace clang;
145using namespace clang::serialization;
146using namespace clang::serialization::reader;
147using llvm::BitstreamCursor;
148
149//===----------------------------------------------------------------------===//
150// ChainedASTReaderListener implementation
151//===----------------------------------------------------------------------===//
152
153bool
154ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
155 return First->ReadFullVersionInformation(FullVersion) ||
156 Second->ReadFullVersionInformation(FullVersion);
157}
158
159void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
160 First->ReadModuleName(ModuleName);
161 Second->ReadModuleName(ModuleName);
162}
163
164void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
165 First->ReadModuleMapFile(ModuleMapPath);
166 Second->ReadModuleMapFile(ModuleMapPath);
167}
168
169bool
170ChainedASTReaderListener::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);
177}
178
179bool 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);
186}
187
188bool ChainedASTReaderListener::ReadDiagnosticOptions(
189 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
190 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
191 Second->ReadDiagnosticOptions(DiagOpts, Complain);
192}
193
194bool
195ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
196 bool Complain) {
197 return First->ReadFileSystemOptions(FSOpts, Complain) ||
198 Second->ReadFileSystemOptions(FSOpts, Complain);
199}
200
201bool 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);
208}
209
210bool 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);
217}
218
219void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
220 unsigned Value) {
221 First->ReadCounter(M, Value);
222 Second->ReadCounter(M, Value);
223}
224
225bool ChainedASTReaderListener::needsInputFileVisitation() {
226 return First->needsInputFileVisitation() ||
227 Second->needsInputFileVisitation();
228}
229
230bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
231 return First->needsSystemInputFileVisitation() ||
232 Second->needsSystemInputFileVisitation();
233}
234
235void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
236 ModuleKind Kind) {
237 First->visitModuleFile(Filename, Kind);
238 Second->visitModuleFile(Filename, Kind);
239}
240
241bool 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;
255}
256
257void ChainedASTReaderListener::readModuleFileExtension(
258 const ModuleFileExtensionMetadata &Metadata) {
259 First->readModuleFileExtension(Metadata);
260 Second->readModuleFileExtension(Metadata);
261}
262
263//===----------------------------------------------------------------------===//
264// PCH validator implementation
265//===----------------------------------------------------------------------===//
266
267ASTReaderListener::~ASTReaderListener() = default;
268
269/// Compare the given set of language options against an existing set of
270/// language options.
271///
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.
275///
276/// \returns true if the languagae options mis-match, false otherwise.
277static 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; \
290 } \
291 return true; \
292 }
293
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; \
300 }
301
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; \
308 }
309
310#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
311 if (!AllowCompatibleDifferences) \
312 LANGOPT(Name, Bits, Default, Description)
313
314#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
315 if (!AllowCompatibleDifferences) \
316 ENUM_LANGOPT(Name, Bits, Default, Description)
317
318#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
319 if (!AllowCompatibleDifferences) \
320 VALUE_LANGOPT(Name, Bits, Default, Description)
321
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"
326
327 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
328 if (Diags)
329 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
330 return true;
331 }
332
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;
338 }
339
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;
346 }
347
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(K: ModularSanitizers);
357 ImportedSanitizers.clear(K: ModularSanitizers);
358 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
359 const std::string Flag = "-fsanitize=";
360 if (Diags) {
361#define SANITIZER(NAME, ID) \
362 { \
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); \
368 }
369#include "clang/Basic/Sanitizers.def"
370 }
371 return true;
372 }
373 }
374
375 return false;
376}
377
378/// Compare the given set of target options against an existing set of
379/// target options.
380///
381/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
382///
383/// \returns true if the target options mis-match, false otherwise.
384static 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; \
394 }
395
396 // The triple and ABI must match exactly.
397 CHECK_TARGET_OPT(Triple, "target");
398 CHECK_TARGET_OPT(ABI, "target ABI");
399
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");
406 }
407
408#undef CHECK_TARGET_OPT
409
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(C&: ExistingFeatures);
417 llvm::sort(C&: ReadFeatures);
418
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 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
424 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
425 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
426 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
427 result: std::back_inserter(x&: UnmatchedReadFeatures));
428
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;
433
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;
441 }
442
443 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
444}
445
446bool
447PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
448 bool Complain,
449 bool AllowCompatibleDifferences) {
450 const LangOptions &ExistingLangOpts = PP.getLangOpts();
451 return checkLanguageOptions(LangOpts, ExistingLangOpts,
452 Diags: Complain ? &Reader.Diags : nullptr,
453 AllowCompatibleDifferences);
454}
455
456bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
457 bool Complain,
458 bool AllowCompatibleDifferences) {
459 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
460 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
461 Diags: Complain ? &Reader.Diags : nullptr,
462 AllowCompatibleDifferences);
463}
464
465namespace {
466
467using MacroDefinitionsMap =
468 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
469using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
470
471} // namespace
472
473static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
474 DiagnosticsEngine &Diags,
475 bool Complain) {
476 using Level = DiagnosticsEngine::Level;
477
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 };
482
483 for (DiagnosticsEngine *MappingSource : MappingSources) {
484 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
485 diag::kind DiagID = DiagIDMappingPair.first;
486 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
487 if (CurLevel < DiagnosticsEngine::Error)
488 continue; // not significant
489 Level StoredLevel =
490 StoredDiags.getDiagnosticLevel(DiagID, Loc: 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;
496 }
497 }
498 }
499
500 return false;
501}
502
503static 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;
508}
509
510static 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;
525 }
526 }
527
528 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
529 if (Complain)
530 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
531 return true;
532 }
533
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;
539 }
540
541 if (isExtHandlingFromDiagsError(Diags) &&
542 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
543 if (Complain)
544 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
545 return true;
546 }
547
548 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
549}
550
551/// Return the top import module if it is implicit, nullptr otherwise.
552static 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;
566
567 StringRef ModuleName = TopImport->ModuleName;
568 assert(!ModuleName.empty() && "diagnostic options read before module name");
569
570 Module *M =
571 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
572 assert(M && "missing module");
573 return M;
574}
575
576bool 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&: *Diags, Opts: *DiagOpts, /*Report*/ReportDiags: false);
585
586 ModuleManager &ModuleMgr = Reader.getModuleManager();
587 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
588
589 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
590 if (!TopM)
591 return false;
592
593 Module *Importer = PP.getCurrentModule();
594
595 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
596 bool SystemHeaderWarningsInModule =
597 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
598 Element: Importer->Name);
599
600 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
601 // contains the union of their flags.
602 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, IsSystem: TopM->IsSystem,
603 SystemHeaderWarningsInModule, Complain);
604}
605
606/// Collect the macro definitions provided by the given preprocessor
607/// options.
608static void
609collectMacroDefinitions(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;
615
616 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
617 StringRef MacroName = MacroPair.first;
618 StringRef MacroBody = MacroPair.second;
619
620 // For an #undef'd macro, we only care about the name.
621 if (IsUndef) {
622 if (MacroNames && !Macros.count(Key: MacroName))
623 MacroNames->push_back(Elt: MacroName);
624
625 Macros[MacroName] = std::make_pair(x: "", y: true);
626 continue;
627 }
628
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(Chars: "\n\r");
635 MacroBody = MacroBody.substr(Start: 0, N: End);
636 }
637
638 if (MacroNames && !Macros.count(Key: MacroName))
639 MacroNames->push_back(Elt: MacroName);
640 Macros[MacroName] = std::make_pair(x&: MacroBody, y: false);
641 }
642}
643
644enum OptionValidation {
645 OptionValidateNone,
646 OptionValidateContradictions,
647 OptionValidateStrictMatches,
648};
649
650/// Check the preprocessor options deserialized from the control block
651/// against the preprocessor options in an existing preprocessor.
652///
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.
660static 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, Macros&: ASTFileMacros);
670 MacroDefinitionsMap ExistingMacros;
671 SmallVector<StringRef, 4> ExistingMacroNames;
672 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
673 MacroNames: &ExistingMacroNames);
674
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";
678
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];
683
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(Key: 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;
693 }
694 return true;
695 }
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?
699
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';
710 }
711 continue;
712 }
713
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;
720 }
721 return true;
722 }
723
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(I: Known);
728 continue;
729 }
730
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;
735 }
736 return true;
737 }
738
739 // Leave the <command line> file and return to <built-in>.
740 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
741
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;
748 }
749 return true;
750 }
751 }
752 }
753
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;
759 }
760 return true;
761 }
762
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;
769 }
770 return true;
771 }
772
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];
776
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;
785 }
786
787 if (File == ExistingPPOpts.ImplicitPCHInclude)
788 continue;
789
790 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
791 continue;
792
793 SuggestedPredefines += "#include \"";
794 SuggestedPredefines += File;
795 SuggestedPredefines += "\"\n";
796 }
797
798 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
799 StringRef File = ExistingPPOpts.MacroIncludes[I];
800 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
801 continue;
802
803 SuggestedPredefines += "#__include_macros \"";
804 SuggestedPredefines += File;
805 SuggestedPredefines += "\"\n##\n";
806 }
807
808 return false;
809}
810
811bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
812 bool ReadMacros, bool Complain,
813 std::string &SuggestedPredefines) {
814 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
815
816 return checkPreprocessorOptions(
817 PPOpts, ExistingPPOpts, ReadMacros, Diags: Complain ? &Reader.Diags : nullptr,
818 FileMgr&: PP.getFileManager(), SuggestedPredefines, LangOpts: PP.getLangOpts());
819}
820
821bool SimpleASTReaderListener::ReadPreprocessorOptions(
822 const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
823 std::string &SuggestedPredefines) {
824 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(), ReadMacros,
825 Diags: nullptr, FileMgr&: PP.getFileManager(),
826 SuggestedPredefines, LangOpts: PP.getLangOpts(),
827 Validation: OptionValidateNone);
828}
829
830/// Check the header search options deserialized from the control block
831/// against the header search options in an existing preprocessor.
832///
833/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
834static 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;
847 }
848 }
849
850 return false;
851}
852
853bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
854 StringRef SpecificModuleCachePath,
855 bool Complain) {
856 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
857 ExistingModuleCachePath: PP.getHeaderSearchInfo().getModuleCachePath(),
858 Diags: Complain ? &Reader.Diags : nullptr,
859 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts());
860}
861
862void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
863 PP.setCounterValue(Value);
864}
865
866//===----------------------------------------------------------------------===//
867// AST reader implementation
868//===----------------------------------------------------------------------===//
869
870static uint64_t readULEB(const unsigned char *&P) {
871 unsigned Length = 0;
872 const char *Error = nullptr;
873
874 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
875 if (Error)
876 llvm::report_fatal_error(reason: Error);
877 P += Length;
878 return Val;
879}
880
881/// Read ULEB-encoded key length and data length.
882static std::pair<unsigned, unsigned>
883readULEBKeyDataLength(const unsigned char *&P) {
884 unsigned KeyLen = readULEB(P);
885 if ((unsigned)KeyLen != KeyLen)
886 llvm::report_fatal_error(reason: "key too large");
887
888 unsigned DataLen = readULEB(P);
889 if ((unsigned)DataLen != DataLen)
890 llvm::report_fatal_error(reason: "data too large");
891
892 return std::make_pair(x&: KeyLen, y&: DataLen);
893}
894
895void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
896 bool TakeOwnership) {
897 DeserializationListener = Listener;
898 OwnsDeserializationListener = TakeOwnership;
899}
900
901unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
902 return serialization::ComputeHash(Sel);
903}
904
905std::pair<unsigned, unsigned>
906ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
907 return readULEBKeyDataLength(P&: d);
908}
909
910ASTSelectorLookupTrait::internal_key_type
911ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
912 using namespace llvm::support;
913
914 SelectorTable &SelTable = Reader.getContext().Selectors;
915 unsigned N =
916 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(memory&: d);
917 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
918 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d));
919 if (N == 0)
920 return SelTable.getNullarySelector(ID: FirstII);
921 else if (N == 1)
922 return SelTable.getUnarySelector(ID: FirstII);
923
924 SmallVector<IdentifierInfo *, 16> Args;
925 Args.push_back(Elt: FirstII);
926 for (unsigned I = 1; I != N; ++I)
927 Args.push_back(Elt: Reader.getLocalIdentifier(
928 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d)));
929
930 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
931}
932
933ASTSelectorLookupTrait::data_type
934ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
935 unsigned DataLen) {
936 using namespace llvm::support;
937
938 data_type Result;
939
940 Result.ID = Reader.getGlobalSelectorID(
941 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d));
942 unsigned FullInstanceBits =
943 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(memory&: d);
944 unsigned FullFactoryBits =
945 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(memory&: 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;
952
953 // Load instance methods
954 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
955 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
956 F,
957 LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d)))
958 Result.Instance.push_back(Elt: Method);
959 }
960
961 // Load factory methods
962 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
963 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
964 F,
965 LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d)))
966 Result.Factory.push_back(Elt: Method);
967 }
968
969 return Result;
970}
971
972unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
973 return llvm::djbHash(Buffer: a);
974}
975
976std::pair<unsigned, unsigned>
977ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
978 return readULEBKeyDataLength(P&: d);
979}
980
981ASTIdentifierLookupTraitBase::internal_key_type
982ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
983 assert(n >= 2 && d[n-1] == '\0');
984 return StringRef((const char*) d, n-1);
985}
986
987/// Whether the given identifier is "interesting".
988static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
989 bool IsModule) {
990 bool IsInteresting =
991 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
992 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
993 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
994 return II.hadMacroDefinition() || II.isPoisoned() ||
995 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
996 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
997 II.getFETokenInfo());
998}
999
1000static bool readBit(unsigned &Bits) {
1001 bool Value = Bits & 0x1;
1002 Bits >>= 1;
1003 return Value;
1004}
1005
1006IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1007 using namespace llvm::support;
1008
1009 unsigned RawID =
1010 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1011 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1012}
1013
1014static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
1015 if (!II.isFromAST()) {
1016 II.setIsFromAST();
1017 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1018 if (isInterestingIdentifier(Reader, II, IsModule))
1019 II.setChangedSinceDeserialization();
1020 }
1021}
1022
1023IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1024 const unsigned char* d,
1025 unsigned DataLen) {
1026 using namespace llvm::support;
1027
1028 unsigned RawID =
1029 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1030 bool IsInteresting = RawID & 0x01;
1031
1032 // Wipe out the "is interesting" bit.
1033 RawID = RawID >> 1;
1034
1035 // Build the IdentifierInfo and link the identifier ID with it.
1036 IdentifierInfo *II = KnownII;
1037 if (!II) {
1038 II = &Reader.getIdentifierTable().getOwn(Name: k);
1039 KnownII = II;
1040 }
1041 markIdentifierFromAST(Reader, II&: *II);
1042 Reader.markIdentifierUpToDate(II);
1043
1044 IdentID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1045 if (!IsInteresting) {
1046 // For uninteresting identifiers, there's nothing else to do. Just notify
1047 // the reader that we've finished loading this identifier.
1048 Reader.SetIdentifierInfo(ID, II);
1049 return II;
1050 }
1051
1052 unsigned ObjCOrBuiltinID =
1053 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(memory&: d);
1054 unsigned Bits =
1055 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(memory&: d);
1056 bool CPlusPlusOperatorKeyword = readBit(Bits);
1057 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1058 bool Poisoned = readBit(Bits);
1059 bool ExtensionToken = readBit(Bits);
1060 bool HadMacroDefinition = readBit(Bits);
1061
1062 assert(Bits == 0 && "Extra bits in the identifier?");
1063 DataLen -= 8;
1064
1065 // Set or check the various bits in the IdentifierInfo structure.
1066 // Token IDs are read-only.
1067 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1068 II->revertTokenIDToIdentifier();
1069 if (!F.isModule())
1070 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1071 assert(II->isExtensionToken() == ExtensionToken &&
1072 "Incorrect extension token flag");
1073 (void)ExtensionToken;
1074 if (Poisoned)
1075 II->setIsPoisoned(true);
1076 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1077 "Incorrect C++ operator keyword flag");
1078 (void)CPlusPlusOperatorKeyword;
1079
1080 // If this identifier is a macro, deserialize the macro
1081 // definition.
1082 if (HadMacroDefinition) {
1083 uint32_t MacroDirectivesOffset =
1084 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1085 DataLen -= 4;
1086
1087 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1088 }
1089
1090 Reader.SetIdentifierInfo(ID, II);
1091
1092 // Read all of the declarations visible at global scope with this
1093 // name.
1094 if (DataLen > 0) {
1095 SmallVector<uint32_t, 4> DeclIDs;
1096 for (; DataLen > 0; DataLen -= 4)
1097 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1098 F,
1099 LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d)));
1100 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1101 }
1102
1103 return II;
1104}
1105
1106DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1107 : Kind(Name.getNameKind()) {
1108 switch (Kind) {
1109 case DeclarationName::Identifier:
1110 Data = (uint64_t)Name.getAsIdentifierInfo();
1111 break;
1112 case DeclarationName::ObjCZeroArgSelector:
1113 case DeclarationName::ObjCOneArgSelector:
1114 case DeclarationName::ObjCMultiArgSelector:
1115 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1116 break;
1117 case DeclarationName::CXXOperatorName:
1118 Data = Name.getCXXOverloadedOperator();
1119 break;
1120 case DeclarationName::CXXLiteralOperatorName:
1121 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1122 break;
1123 case DeclarationName::CXXDeductionGuideName:
1124 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1125 ->getDeclName().getAsIdentifierInfo();
1126 break;
1127 case DeclarationName::CXXConstructorName:
1128 case DeclarationName::CXXDestructorName:
1129 case DeclarationName::CXXConversionFunctionName:
1130 case DeclarationName::CXXUsingDirective:
1131 Data = 0;
1132 break;
1133 }
1134}
1135
1136unsigned DeclarationNameKey::getHash() const {
1137 llvm::FoldingSetNodeID ID;
1138 ID.AddInteger(I: Kind);
1139
1140 switch (Kind) {
1141 case DeclarationName::Identifier:
1142 case DeclarationName::CXXLiteralOperatorName:
1143 case DeclarationName::CXXDeductionGuideName:
1144 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1145 break;
1146 case DeclarationName::ObjCZeroArgSelector:
1147 case DeclarationName::ObjCOneArgSelector:
1148 case DeclarationName::ObjCMultiArgSelector:
1149 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1150 break;
1151 case DeclarationName::CXXOperatorName:
1152 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1153 break;
1154 case DeclarationName::CXXConstructorName:
1155 case DeclarationName::CXXDestructorName:
1156 case DeclarationName::CXXConversionFunctionName:
1157 case DeclarationName::CXXUsingDirective:
1158 break;
1159 }
1160
1161 return ID.ComputeHash();
1162}
1163
1164ModuleFile *
1165ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1166 using namespace llvm::support;
1167
1168 uint32_t ModuleFileID =
1169 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1170 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1171}
1172
1173std::pair<unsigned, unsigned>
1174ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1175 return readULEBKeyDataLength(P&: d);
1176}
1177
1178ASTDeclContextNameLookupTrait::internal_key_type
1179ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1180 using namespace llvm::support;
1181
1182 auto Kind = (DeclarationName::NameKind)*d++;
1183 uint64_t Data;
1184 switch (Kind) {
1185 case DeclarationName::Identifier:
1186 case DeclarationName::CXXLiteralOperatorName:
1187 case DeclarationName::CXXDeductionGuideName:
1188 Data = (uint64_t)Reader.getLocalIdentifier(
1189 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d));
1190 break;
1191 case DeclarationName::ObjCZeroArgSelector:
1192 case DeclarationName::ObjCOneArgSelector:
1193 case DeclarationName::ObjCMultiArgSelector:
1194 Data =
1195 (uint64_t)Reader
1196 .getLocalSelector(
1197 M&: F,
1198 LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(
1199 memory&: d))
1200 .getAsOpaquePtr();
1201 break;
1202 case DeclarationName::CXXOperatorName:
1203 Data = *d++; // OverloadedOperatorKind
1204 break;
1205 case DeclarationName::CXXConstructorName:
1206 case DeclarationName::CXXDestructorName:
1207 case DeclarationName::CXXConversionFunctionName:
1208 case DeclarationName::CXXUsingDirective:
1209 Data = 0;
1210 break;
1211 }
1212
1213 return DeclarationNameKey(Kind, Data);
1214}
1215
1216void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1217 const unsigned char *d,
1218 unsigned DataLen,
1219 data_type_builder &Val) {
1220 using namespace llvm::support;
1221
1222 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1223 uint32_t LocalID =
1224 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1225 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID));
1226 }
1227}
1228
1229bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1230 BitstreamCursor &Cursor,
1231 uint64_t Offset,
1232 DeclContext *DC) {
1233 assert(Offset != 0);
1234
1235 SavedStreamPosition SavedPosition(Cursor);
1236 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1237 Error(Err: std::move(Err));
1238 return true;
1239 }
1240
1241 RecordData Record;
1242 StringRef Blob;
1243 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1244 if (!MaybeCode) {
1245 Error(Err: MaybeCode.takeError());
1246 return true;
1247 }
1248 unsigned Code = MaybeCode.get();
1249
1250 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1251 if (!MaybeRecCode) {
1252 Error(Err: MaybeRecCode.takeError());
1253 return true;
1254 }
1255 unsigned RecCode = MaybeRecCode.get();
1256 if (RecCode != DECL_CONTEXT_LEXICAL) {
1257 Error(Msg: "Expected lexical block");
1258 return true;
1259 }
1260
1261 assert(!isa<TranslationUnitDecl>(DC) &&
1262 "expected a TU_UPDATE_LEXICAL record for TU");
1263 // If we are handling a C++ class template instantiation, we can see multiple
1264 // lexical updates for the same record. It's important that we select only one
1265 // of them, so that field numbering works properly. Just pick the first one we
1266 // see.
1267 auto &Lex = LexicalDecls[DC];
1268 if (!Lex.first) {
1269 Lex = std::make_pair(
1270 x: &M, y: llvm::ArrayRef(
1271 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1272 Blob.data()),
1273 Blob.size() / 4));
1274 }
1275 DC->setHasExternalLexicalStorage(true);
1276 return false;
1277}
1278
1279bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1280 BitstreamCursor &Cursor,
1281 uint64_t Offset,
1282 DeclID ID) {
1283 assert(Offset != 0);
1284
1285 SavedStreamPosition SavedPosition(Cursor);
1286 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1287 Error(Err: std::move(Err));
1288 return true;
1289 }
1290
1291 RecordData Record;
1292 StringRef Blob;
1293 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1294 if (!MaybeCode) {
1295 Error(Err: MaybeCode.takeError());
1296 return true;
1297 }
1298 unsigned Code = MaybeCode.get();
1299
1300 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1301 if (!MaybeRecCode) {
1302 Error(Err: MaybeRecCode.takeError());
1303 return true;
1304 }
1305 unsigned RecCode = MaybeRecCode.get();
1306 if (RecCode != DECL_CONTEXT_VISIBLE) {
1307 Error(Msg: "Expected visible lookup table block");
1308 return true;
1309 }
1310
1311 // We can't safely determine the primary context yet, so delay attaching the
1312 // lookup table until we're done with recursive deserialization.
1313 auto *Data = (const unsigned char*)Blob.data();
1314 PendingVisibleUpdates[ID].push_back(Elt: PendingVisibleUpdate{.Mod: &M, .Data: Data});
1315 return false;
1316}
1317
1318void ASTReader::Error(StringRef Msg) const {
1319 Error(diag::err_fe_pch_malformed, Msg);
1320 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1321 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1322 Diag(diag::note_module_cache_path)
1323 << PP.getHeaderSearchInfo().getModuleCachePath();
1324 }
1325}
1326
1327void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1328 StringRef Arg3) const {
1329 if (Diags.isDiagnosticInFlight())
1330 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1331 else
1332 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1333}
1334
1335void ASTReader::Error(llvm::Error &&Err) const {
1336 llvm::Error RemainingErr =
1337 handleErrors(E: std::move(Err), Hs: [this](const DiagnosticError &E) {
1338 auto Diag = E.getDiagnostic().second;
1339
1340 // Ideally we'd just emit it, but have to handle a possible in-flight
1341 // diagnostic. Note that the location is currently ignored as well.
1342 auto NumArgs = Diag.getStorage()->NumDiagArgs;
1343 assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1344 StringRef Arg1, Arg2, Arg3;
1345 switch (NumArgs) {
1346 case 3:
1347 Arg3 = Diag.getStringArg(I: 2);
1348 [[fallthrough]];
1349 case 2:
1350 Arg2 = Diag.getStringArg(I: 1);
1351 [[fallthrough]];
1352 case 1:
1353 Arg1 = Diag.getStringArg(I: 0);
1354 }
1355 Error(DiagID: Diag.getDiagID(), Arg1, Arg2, Arg3);
1356 });
1357 if (RemainingErr)
1358 Error(Msg: toString(E: std::move(RemainingErr)));
1359}
1360
1361//===----------------------------------------------------------------------===//
1362// Source Manager Deserialization
1363//===----------------------------------------------------------------------===//
1364
1365/// Read the line table in the source manager block.
1366void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1367 unsigned Idx = 0;
1368 LineTableInfo &LineTable = SourceMgr.getLineTable();
1369
1370 // Parse the file names
1371 std::map<int, int> FileIDs;
1372 FileIDs[-1] = -1; // For unspecified filenames.
1373 for (unsigned I = 0; Record[Idx]; ++I) {
1374 // Extract the file name
1375 auto Filename = ReadPath(F, Record, Idx);
1376 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1377 }
1378 ++Idx;
1379
1380 // Parse the line entries
1381 std::vector<LineEntry> Entries;
1382 while (Idx < Record.size()) {
1383 FileID FID = ReadFileID(F, Record, Idx);
1384
1385 // Extract the line entries
1386 unsigned NumEntries = Record[Idx++];
1387 assert(NumEntries && "no line entries for file ID");
1388 Entries.clear();
1389 Entries.reserve(n: NumEntries);
1390 for (unsigned I = 0; I != NumEntries; ++I) {
1391 unsigned FileOffset = Record[Idx++];
1392 unsigned LineNo = Record[Idx++];
1393 int FilenameID = FileIDs[Record[Idx++]];
1394 SrcMgr::CharacteristicKind FileKind
1395 = (SrcMgr::CharacteristicKind)Record[Idx++];
1396 unsigned IncludeOffset = Record[Idx++];
1397 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1398 FileKind, IncludeOffset));
1399 }
1400 LineTable.AddEntry(FID, Entries);
1401 }
1402}
1403
1404/// Read a source manager block
1405llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1406 using namespace SrcMgr;
1407
1408 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1409
1410 // Set the source-location entry cursor to the current position in
1411 // the stream. This cursor will be used to read the contents of the
1412 // source manager block initially, and then lazily read
1413 // source-location entries as needed.
1414 SLocEntryCursor = F.Stream;
1415
1416 // The stream itself is going to skip over the source manager block.
1417 if (llvm::Error Err = F.Stream.SkipBlock())
1418 return Err;
1419
1420 // Enter the source manager block.
1421 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1422 return Err;
1423 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1424
1425 RecordData Record;
1426 while (true) {
1427 Expected<llvm::BitstreamEntry> MaybeE =
1428 SLocEntryCursor.advanceSkippingSubblocks();
1429 if (!MaybeE)
1430 return MaybeE.takeError();
1431 llvm::BitstreamEntry E = MaybeE.get();
1432
1433 switch (E.Kind) {
1434 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1435 case llvm::BitstreamEntry::Error:
1436 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1437 Fmt: "malformed block record in AST file");
1438 case llvm::BitstreamEntry::EndBlock:
1439 return llvm::Error::success();
1440 case llvm::BitstreamEntry::Record:
1441 // The interesting case.
1442 break;
1443 }
1444
1445 // Read a record.
1446 Record.clear();
1447 StringRef Blob;
1448 Expected<unsigned> MaybeRecord =
1449 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1450 if (!MaybeRecord)
1451 return MaybeRecord.takeError();
1452 switch (MaybeRecord.get()) {
1453 default: // Default behavior: ignore.
1454 break;
1455
1456 case SM_SLOC_FILE_ENTRY:
1457 case SM_SLOC_BUFFER_ENTRY:
1458 case SM_SLOC_EXPANSION_ENTRY:
1459 // Once we hit one of the source location entries, we're done.
1460 return llvm::Error::success();
1461 }
1462 }
1463}
1464
1465llvm::Expected<SourceLocation::UIntTy>
1466ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1467 BitstreamCursor &Cursor = F->SLocEntryCursor;
1468 SavedStreamPosition SavedPosition(Cursor);
1469 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1470 F->SLocEntryOffsets[Index]))
1471 return std::move(Err);
1472
1473 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1474 if (!MaybeEntry)
1475 return MaybeEntry.takeError();
1476
1477 llvm::BitstreamEntry Entry = MaybeEntry.get();
1478 if (Entry.Kind != llvm::BitstreamEntry::Record)
1479 return llvm::createStringError(
1480 EC: std::errc::illegal_byte_sequence,
1481 Fmt: "incorrectly-formatted source location entry in AST file");
1482
1483 RecordData Record;
1484 StringRef Blob;
1485 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1486 if (!MaybeSLOC)
1487 return MaybeSLOC.takeError();
1488
1489 switch (MaybeSLOC.get()) {
1490 default:
1491 return llvm::createStringError(
1492 EC: std::errc::illegal_byte_sequence,
1493 Fmt: "incorrectly-formatted source location entry in AST file");
1494 case SM_SLOC_FILE_ENTRY:
1495 case SM_SLOC_BUFFER_ENTRY:
1496 case SM_SLOC_EXPANSION_ENTRY:
1497 return F->SLocEntryBaseOffset + Record[0];
1498 }
1499}
1500
1501int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1502 auto SLocMapI =
1503 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1504 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1505 "Corrupted global sloc offset map");
1506 ModuleFile *F = SLocMapI->second;
1507
1508 bool Invalid = false;
1509
1510 auto It = llvm::upper_bound(
1511 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1512 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1513 int ID = F->SLocEntryBaseID + LocalIndex;
1514 std::size_t Index = -ID - 2;
1515 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1516 assert(!SourceMgr.SLocEntryLoaded[Index]);
1517 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1518 if (!MaybeEntryOffset) {
1519 Error(Err: MaybeEntryOffset.takeError());
1520 Invalid = true;
1521 return true;
1522 }
1523 SourceMgr.LoadedSLocEntryTable[Index] =
1524 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1525 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1526 }
1527 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1528 });
1529
1530 if (Invalid)
1531 return 0;
1532
1533 // The iterator points to the first entry with start offset greater than the
1534 // offset of interest. The previous entry must contain the offset of interest.
1535 return F->SLocEntryBaseID + *std::prev(x: It);
1536}
1537
1538bool ASTReader::ReadSLocEntry(int ID) {
1539 if (ID == 0)
1540 return false;
1541
1542 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1543 Error(Msg: "source location entry ID out-of-range for AST file");
1544 return true;
1545 }
1546
1547 // Local helper to read the (possibly-compressed) buffer data following the
1548 // entry record.
1549 auto ReadBuffer = [this](
1550 BitstreamCursor &SLocEntryCursor,
1551 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1552 RecordData Record;
1553 StringRef Blob;
1554 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1555 if (!MaybeCode) {
1556 Error(Err: MaybeCode.takeError());
1557 return nullptr;
1558 }
1559 unsigned Code = MaybeCode.get();
1560
1561 Expected<unsigned> MaybeRecCode =
1562 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1563 if (!MaybeRecCode) {
1564 Error(Err: MaybeRecCode.takeError());
1565 return nullptr;
1566 }
1567 unsigned RecCode = MaybeRecCode.get();
1568
1569 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1570 // Inspect the first byte to differentiate zlib (\x78) and zstd
1571 // (little-endian 0xFD2FB528).
1572 const llvm::compression::Format F =
1573 Blob.size() > 0 && Blob.data()[0] == 0x78
1574 ? llvm::compression::Format::Zlib
1575 : llvm::compression::Format::Zstd;
1576 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1577 Error(Msg: Reason);
1578 return nullptr;
1579 }
1580 SmallVector<uint8_t, 0> Decompressed;
1581 if (llvm::Error E = llvm::compression::decompress(
1582 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1583 Error(Msg: "could not decompress embedded file contents: " +
1584 llvm::toString(E: std::move(E)));
1585 return nullptr;
1586 }
1587 return llvm::MemoryBuffer::getMemBufferCopy(
1588 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1589 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1590 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1591 } else {
1592 Error(Msg: "AST record has invalid code");
1593 return nullptr;
1594 }
1595 };
1596
1597 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1598 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1599 BitNo: F->SLocEntryOffsetsBase +
1600 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1601 Error(Err: std::move(Err));
1602 return true;
1603 }
1604
1605 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1606 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1607
1608 ++NumSLocEntriesRead;
1609 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1610 if (!MaybeEntry) {
1611 Error(Err: MaybeEntry.takeError());
1612 return true;
1613 }
1614 llvm::BitstreamEntry Entry = MaybeEntry.get();
1615
1616 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1617 Error(Msg: "incorrectly-formatted source location entry in AST file");
1618 return true;
1619 }
1620
1621 RecordData Record;
1622 StringRef Blob;
1623 Expected<unsigned> MaybeSLOC =
1624 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1625 if (!MaybeSLOC) {
1626 Error(Err: MaybeSLOC.takeError());
1627 return true;
1628 }
1629 switch (MaybeSLOC.get()) {
1630 default:
1631 Error(Msg: "incorrectly-formatted source location entry in AST file");
1632 return true;
1633
1634 case SM_SLOC_FILE_ENTRY: {
1635 // We will detect whether a file changed and return 'Failure' for it, but
1636 // we will also try to fail gracefully by setting up the SLocEntry.
1637 unsigned InputID = Record[4];
1638 InputFile IF = getInputFile(F&: *F, ID: InputID);
1639 OptionalFileEntryRef File = IF.getFile();
1640 bool OverriddenBuffer = IF.isOverridden();
1641
1642 // Note that we only check if a File was returned. If it was out-of-date
1643 // we have complained but we will continue creating a FileID to recover
1644 // gracefully.
1645 if (!File)
1646 return true;
1647
1648 SourceLocation IncludeLoc = ReadSourceLocation(ModuleFile&: *F, Raw: Record[1]);
1649 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1650 // This is the module's main file.
1651 IncludeLoc = getImportLocation(F);
1652 }
1653 SrcMgr::CharacteristicKind
1654 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1655 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
1656 LoadedOffset: BaseOffset + Record[0]);
1657 SrcMgr::FileInfo &FileInfo =
1658 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1659 FileInfo.NumCreatedFIDs = Record[5];
1660 if (Record[3])
1661 FileInfo.setHasLineDirectives();
1662
1663 unsigned NumFileDecls = Record[7];
1664 if (NumFileDecls && ContextObj) {
1665 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1666 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1667 FileDeclIDs[FID] =
1668 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1669 }
1670
1671 const SrcMgr::ContentCache &ContentCache =
1672 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
1673 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1674 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1675 !ContentCache.getBufferIfLoaded()) {
1676 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1677 if (!Buffer)
1678 return true;
1679 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
1680 }
1681
1682 break;
1683 }
1684
1685 case SM_SLOC_BUFFER_ENTRY: {
1686 const char *Name = Blob.data();
1687 unsigned Offset = Record[0];
1688 SrcMgr::CharacteristicKind
1689 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1690 SourceLocation IncludeLoc = ReadSourceLocation(ModuleFile&: *F, Raw: Record[1]);
1691 if (IncludeLoc.isInvalid() && F->isModule()) {
1692 IncludeLoc = getImportLocation(F);
1693 }
1694
1695 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1696 if (!Buffer)
1697 return true;
1698 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
1699 LoadedOffset: BaseOffset + Offset, IncludeLoc);
1700 if (Record[3]) {
1701 auto &FileInfo =
1702 const_cast<SrcMgr::FileInfo &>(SourceMgr.getSLocEntry(FID).getFile());
1703 FileInfo.setHasLineDirectives();
1704 }
1705 break;
1706 }
1707
1708 case SM_SLOC_EXPANSION_ENTRY: {
1709 LocSeq::State Seq;
1710 SourceLocation SpellingLoc = ReadSourceLocation(ModuleFile&: *F, Raw: Record[1], Seq);
1711 SourceLocation ExpansionBegin = ReadSourceLocation(ModuleFile&: *F, Raw: Record[2], Seq);
1712 SourceLocation ExpansionEnd = ReadSourceLocation(ModuleFile&: *F, Raw: Record[3], Seq);
1713 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
1714 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
1715 LoadedOffset: BaseOffset + Record[0]);
1716 break;
1717 }
1718 }
1719
1720 return false;
1721}
1722
1723std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1724 if (ID == 0)
1725 return std::make_pair(x: SourceLocation(), y: "");
1726
1727 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1728 Error(Msg: "source location entry ID out-of-range for AST file");
1729 return std::make_pair(x: SourceLocation(), y: "");
1730 }
1731
1732 // Find which module file this entry lands in.
1733 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
1734 if (!M->isModule())
1735 return std::make_pair(x: SourceLocation(), y: "");
1736
1737 // FIXME: Can we map this down to a particular submodule? That would be
1738 // ideal.
1739 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
1740}
1741
1742/// Find the location where the module F is imported.
1743SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1744 if (F->ImportLoc.isValid())
1745 return F->ImportLoc;
1746
1747 // Otherwise we have a PCH. It's considered to be "imported" at the first
1748 // location of its includer.
1749 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1750 // Main file is the importer.
1751 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1752 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
1753 }
1754 return F->ImportedBy[0]->FirstLoc;
1755}
1756
1757/// Enter a subblock of the specified BlockID with the specified cursor. Read
1758/// the abbreviations that are at the top of the block and then leave the cursor
1759/// pointing into the block.
1760llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1761 unsigned BlockID,
1762 uint64_t *StartOfBlockOffset) {
1763 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1764 return Err;
1765
1766 if (StartOfBlockOffset)
1767 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1768
1769 while (true) {
1770 uint64_t Offset = Cursor.GetCurrentBitNo();
1771 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1772 if (!MaybeCode)
1773 return MaybeCode.takeError();
1774 unsigned Code = MaybeCode.get();
1775
1776 // We expect all abbrevs to be at the start of the block.
1777 if (Code != llvm::bitc::DEFINE_ABBREV) {
1778 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
1779 return Err;
1780 return llvm::Error::success();
1781 }
1782 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1783 return Err;
1784 }
1785}
1786
1787Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
1788 unsigned &Idx) {
1789 Token Tok;
1790 Tok.startToken();
1791 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
1792 Tok.setKind((tok::TokenKind)Record[Idx++]);
1793 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1794
1795 if (Tok.isAnnotation()) {
1796 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
1797 switch (Tok.getKind()) {
1798 case tok::annot_pragma_loop_hint: {
1799 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1800 Info->PragmaName = ReadToken(M, Record, Idx);
1801 Info->Option = ReadToken(M, Record, Idx);
1802 unsigned NumTokens = Record[Idx++];
1803 SmallVector<Token, 4> Toks;
1804 Toks.reserve(N: NumTokens);
1805 for (unsigned I = 0; I < NumTokens; ++I)
1806 Toks.push_back(Elt: ReadToken(M, Record, Idx));
1807 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
1808 Tok.setAnnotationValue(static_cast<void *>(Info));
1809 break;
1810 }
1811 case tok::annot_pragma_pack: {
1812 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
1813 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
1814 auto SlotLabel = ReadString(Record, Idx);
1815 Info->SlotLabel =
1816 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
1817 Info->Alignment = ReadToken(M, Record, Idx);
1818 Tok.setAnnotationValue(static_cast<void *>(Info));
1819 break;
1820 }
1821 // Some annotation tokens do not use the PtrData field.
1822 case tok::annot_pragma_openmp:
1823 case tok::annot_pragma_openmp_end:
1824 case tok::annot_pragma_unused:
1825 case tok::annot_pragma_openacc:
1826 case tok::annot_pragma_openacc_end:
1827 break;
1828 default:
1829 llvm_unreachable("missing deserialization code for annotation token");
1830 }
1831 } else {
1832 Tok.setLength(Record[Idx++]);
1833 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
1834 Tok.setIdentifierInfo(II);
1835 }
1836 return Tok;
1837}
1838
1839MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1840 BitstreamCursor &Stream = F.MacroCursor;
1841
1842 // Keep track of where we are in the stream, then jump back there
1843 // after reading this macro.
1844 SavedStreamPosition SavedPosition(Stream);
1845
1846 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
1847 // FIXME this drops errors on the floor.
1848 consumeError(Err: std::move(Err));
1849 return nullptr;
1850 }
1851 RecordData Record;
1852 SmallVector<IdentifierInfo*, 16> MacroParams;
1853 MacroInfo *Macro = nullptr;
1854 llvm::MutableArrayRef<Token> MacroTokens;
1855
1856 while (true) {
1857 // Advance to the next record, but if we get to the end of the block, don't
1858 // pop it (removing all the abbreviations from the cursor) since we want to
1859 // be able to reseek within the block and read entries.
1860 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1861 Expected<llvm::BitstreamEntry> MaybeEntry =
1862 Stream.advanceSkippingSubblocks(Flags);
1863 if (!MaybeEntry) {
1864 Error(Err: MaybeEntry.takeError());
1865 return Macro;
1866 }
1867 llvm::BitstreamEntry Entry = MaybeEntry.get();
1868
1869 switch (Entry.Kind) {
1870 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1871 case llvm::BitstreamEntry::Error:
1872 Error(Msg: "malformed block record in AST file");
1873 return Macro;
1874 case llvm::BitstreamEntry::EndBlock:
1875 return Macro;
1876 case llvm::BitstreamEntry::Record:
1877 // The interesting case.
1878 break;
1879 }
1880
1881 // Read a record.
1882 Record.clear();
1883 PreprocessorRecordTypes RecType;
1884 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
1885 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1886 else {
1887 Error(Err: MaybeRecType.takeError());
1888 return Macro;
1889 }
1890 switch (RecType) {
1891 case PP_MODULE_MACRO:
1892 case PP_MACRO_DIRECTIVE_HISTORY:
1893 return Macro;
1894
1895 case PP_MACRO_OBJECT_LIKE:
1896 case PP_MACRO_FUNCTION_LIKE: {
1897 // If we already have a macro, that means that we've hit the end
1898 // of the definition of the macro we were looking for. We're
1899 // done.
1900 if (Macro)
1901 return Macro;
1902
1903 unsigned NextIndex = 1; // Skip identifier ID.
1904 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
1905 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
1906 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
1907 MI->setIsUsed(Record[NextIndex++]);
1908 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1909 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
1910 PPAllocator&: PP.getPreprocessorAllocator());
1911 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1912 // Decode function-like macro info.
1913 bool isC99VarArgs = Record[NextIndex++];
1914 bool isGNUVarArgs = Record[NextIndex++];
1915 bool hasCommaPasting = Record[NextIndex++];
1916 MacroParams.clear();
1917 unsigned NumArgs = Record[NextIndex++];
1918 for (unsigned i = 0; i != NumArgs; ++i)
1919 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
1920
1921 // Install function-like macro info.
1922 MI->setIsFunctionLike();
1923 if (isC99VarArgs) MI->setIsC99Varargs();
1924 if (isGNUVarArgs) MI->setIsGNUVarargs();
1925 if (hasCommaPasting) MI->setHasCommaPasting();
1926 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
1927 }
1928
1929 // Remember that we saw this macro last so that we add the tokens that
1930 // form its body to it.
1931 Macro = MI;
1932
1933 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1934 Record[NextIndex]) {
1935 // We have a macro definition. Register the association
1936 PreprocessedEntityID
1937 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
1938 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1939 PreprocessingRecord::PPEntityID PPID =
1940 PPRec.getPPEntityID(Index: GlobalID - 1, /*isLoaded=*/true);
1941 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1942 Val: PPRec.getPreprocessedEntity(PPID));
1943 if (PPDef)
1944 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
1945 }
1946
1947 ++NumMacrosRead;
1948 break;
1949 }
1950
1951 case PP_TOKEN: {
1952 // If we see a TOKEN before a PP_MACRO_*, then the file is
1953 // erroneous, just pretend we didn't see this.
1954 if (!Macro) break;
1955 if (MacroTokens.empty()) {
1956 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
1957 return Macro;
1958 }
1959
1960 unsigned Idx = 0;
1961 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
1962 MacroTokens = MacroTokens.drop_front();
1963 break;
1964 }
1965 }
1966 }
1967}
1968
1969PreprocessedEntityID
1970ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1971 unsigned LocalID) const {
1972 if (!M.ModuleOffsetMap.empty())
1973 ReadModuleOffsetMap(F&: M);
1974
1975 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1976 I = M.PreprocessedEntityRemap.find(K: LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1977 assert(I != M.PreprocessedEntityRemap.end()
1978 && "Invalid index into preprocessed entity index remap");
1979
1980 return LocalID + I->second;
1981}
1982
1983const FileEntry *HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
1984 FileManager &FileMgr = Reader.getFileManager();
1985 if (!Key.Imported) {
1986 if (auto File = FileMgr.getFile(Filename: Key.Filename))
1987 return *File;
1988 return nullptr;
1989 }
1990
1991 std::string Resolved = std::string(Key.Filename);
1992 Reader.ResolveImportedPath(M, Filename&: Resolved);
1993 if (auto File = FileMgr.getFile(Filename: Resolved))
1994 return *File;
1995 return nullptr;
1996}
1997
1998unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1999 return llvm::hash_combine(args: ikey.Size, args: ikey.ModTime);
2000}
2001
2002HeaderFileInfoTrait::internal_key_type
2003HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2004 internal_key_type ikey = {.Size: ekey.getSize(),
2005 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2006 .Filename: ekey.getName(), /*Imported*/ false};
2007 return ikey;
2008}
2009
2010bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2011 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2012 return false;
2013
2014 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2015 return true;
2016
2017 // Determine whether the actual files are equivalent.
2018 const FileEntry *FEA = getFile(Key: a);
2019 const FileEntry *FEB = getFile(Key: b);
2020 return FEA && FEA == FEB;
2021}
2022
2023std::pair<unsigned, unsigned>
2024HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2025 return readULEBKeyDataLength(P&: d);
2026}
2027
2028HeaderFileInfoTrait::internal_key_type
2029HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2030 using namespace llvm::support;
2031
2032 internal_key_type ikey;
2033 ikey.Size =
2034 off_t(endian::readNext<uint64_t, llvm::endianness::little, unaligned>(memory&: d));
2035 ikey.ModTime = time_t(
2036 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(memory&: d));
2037 ikey.Filename = (const char *)d;
2038 ikey.Imported = true;
2039 return ikey;
2040}
2041
2042HeaderFileInfoTrait::data_type
2043HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2044 unsigned DataLen) {
2045 using namespace llvm::support;
2046
2047 const unsigned char *End = d + DataLen;
2048 HeaderFileInfo HFI;
2049 unsigned Flags = *d++;
2050
2051 bool Included = (Flags >> 6) & 0x01;
2052 if (Included)
2053 if (const FileEntry *FE = getFile(Key: key))
2054 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2055 // deserialize this header file info again.
2056 Reader.getPreprocessor().getIncludedFiles().insert(V: FE);
2057
2058 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2059 HFI.isImport |= (Flags >> 5) & 0x01;
2060 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2061 HFI.DirInfo = (Flags >> 1) & 0x07;
2062 HFI.IndexHeaderMapHeader = Flags & 0x01;
2063 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
2064 M, LocalID: endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d));
2065 if (unsigned FrameworkOffset =
2066 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d)) {
2067 // The framework offset is 1 greater than the actual offset,
2068 // since 0 is used as an indicator for "no framework name".
2069 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
2070 HFI.Framework = HS->getUniqueFrameworkName(Framework: FrameworkName);
2071 }
2072
2073 assert((End - d) % 4 == 0 &&
2074 "Wrong data length in HeaderFileInfo deserialization");
2075 while (d != End) {
2076 uint32_t LocalSMID =
2077 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
2078 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2079 LocalSMID >>= 3;
2080
2081 // This header is part of a module. Associate it with the module to enable
2082 // implicit module import.
2083 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2084 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2085 FileManager &FileMgr = Reader.getFileManager();
2086 ModuleMap &ModMap =
2087 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2088
2089 std::string Filename = std::string(key.Filename);
2090 if (key.Imported)
2091 Reader.ResolveImportedPath(M, Filename);
2092 if (auto FE = FileMgr.getOptionalFileRef(Filename)) {
2093 // FIXME: NameAsWritten
2094 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2095 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2096 }
2097 HFI.isModuleHeader |= ModuleMap::isModular(Role: HeaderRole);
2098 }
2099
2100 // This HeaderFileInfo was externally loaded.
2101 HFI.External = true;
2102 HFI.IsValid = true;
2103 return HFI;
2104}
2105
2106void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2107 uint32_t MacroDirectivesOffset) {
2108 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2109 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2110}
2111
2112void ASTReader::ReadDefinedMacros() {
2113 // Note that we are loading defined macros.
2114 Deserializing Macros(this);
2115
2116 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2117 BitstreamCursor &MacroCursor = I.MacroCursor;
2118
2119 // If there was no preprocessor block, skip this file.
2120 if (MacroCursor.getBitcodeBytes().empty())
2121 continue;
2122
2123 BitstreamCursor Cursor = MacroCursor;
2124 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2125 Error(Err: std::move(Err));
2126 return;
2127 }
2128
2129 RecordData Record;
2130 while (true) {
2131 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2132 if (!MaybeE) {
2133 Error(Err: MaybeE.takeError());
2134 return;
2135 }
2136 llvm::BitstreamEntry E = MaybeE.get();
2137
2138 switch (E.Kind) {
2139 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2140 case llvm::BitstreamEntry::Error:
2141 Error(Msg: "malformed block record in AST file");
2142 return;
2143 case llvm::BitstreamEntry::EndBlock:
2144 goto NextCursor;
2145
2146 case llvm::BitstreamEntry::Record: {
2147 Record.clear();
2148 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2149 if (!MaybeRecord) {
2150 Error(Err: MaybeRecord.takeError());
2151 return;
2152 }
2153 switch (MaybeRecord.get()) {
2154 default: // Default behavior: ignore.
2155 break;
2156
2157 case PP_MACRO_OBJECT_LIKE:
2158 case PP_MACRO_FUNCTION_LIKE: {
2159 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2160 if (II->isOutOfDate())
2161 updateOutOfDateIdentifier(II&: *II);
2162 break;
2163 }
2164
2165 case PP_TOKEN:
2166 // Ignore tokens.
2167 break;
2168 }
2169 break;
2170 }
2171 }
2172 }
2173 NextCursor: ;
2174 }
2175}
2176
2177namespace {
2178
2179 /// Visitor class used to look up identifirs in an AST file.
2180 class IdentifierLookupVisitor {
2181 StringRef Name;
2182 unsigned NameHash;
2183 unsigned PriorGeneration;
2184 unsigned &NumIdentifierLookups;
2185 unsigned &NumIdentifierLookupHits;
2186 IdentifierInfo *Found = nullptr;
2187
2188 public:
2189 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2190 unsigned &NumIdentifierLookups,
2191 unsigned &NumIdentifierLookupHits)
2192 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2193 PriorGeneration(PriorGeneration),
2194 NumIdentifierLookups(NumIdentifierLookups),
2195 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2196
2197 bool operator()(ModuleFile &M) {
2198 // If we've already searched this module file, skip it now.
2199 if (M.Generation <= PriorGeneration)
2200 return true;
2201
2202 ASTIdentifierLookupTable *IdTable
2203 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2204 if (!IdTable)
2205 return false;
2206
2207 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2208 Found);
2209 ++NumIdentifierLookups;
2210 ASTIdentifierLookupTable::iterator Pos =
2211 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2212 if (Pos == IdTable->end())
2213 return false;
2214
2215 // Dereferencing the iterator has the effect of building the
2216 // IdentifierInfo node and populating it with the various
2217 // declarations it needs.
2218 ++NumIdentifierLookupHits;
2219 Found = *Pos;
2220 return true;
2221 }
2222
2223 // Retrieve the identifier info found within the module
2224 // files.
2225 IdentifierInfo *getIdentifierInfo() const { return Found; }
2226 };
2227
2228} // namespace
2229
2230void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2231 // Note that we are loading an identifier.
2232 Deserializing AnIdentifier(this);
2233
2234 unsigned PriorGeneration = 0;
2235 if (getContext().getLangOpts().Modules)
2236 PriorGeneration = IdentifierGeneration[&II];
2237
2238 // If there is a global index, look there first to determine which modules
2239 // provably do not have any results for this identifier.
2240 GlobalModuleIndex::HitSet Hits;
2241 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2242 if (!loadGlobalIndex()) {
2243 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2244 HitsPtr = &Hits;
2245 }
2246 }
2247
2248 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2249 NumIdentifierLookups,
2250 NumIdentifierLookupHits);
2251 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2252 markIdentifierUpToDate(II: &II);
2253}
2254
2255void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2256 if (!II)
2257 return;
2258
2259 II->setOutOfDate(false);
2260
2261 // Update the generation for this identifier.
2262 if (getContext().getLangOpts().Modules)
2263 IdentifierGeneration[II] = getGeneration();
2264}
2265
2266void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2267 const PendingMacroInfo &PMInfo) {
2268 ModuleFile &M = *PMInfo.M;
2269
2270 BitstreamCursor &Cursor = M.MacroCursor;
2271 SavedStreamPosition SavedPosition(Cursor);
2272 if (llvm::Error Err =
2273 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2274 Error(Err: std::move(Err));
2275 return;
2276 }
2277
2278 struct ModuleMacroRecord {
2279 SubmoduleID SubModID;
2280 MacroInfo *MI;
2281 SmallVector<SubmoduleID, 8> Overrides;
2282 };
2283 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2284
2285 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2286 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2287 // macro histroy.
2288 RecordData Record;
2289 while (true) {
2290 Expected<llvm::BitstreamEntry> MaybeEntry =
2291 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2292 if (!MaybeEntry) {
2293 Error(Err: MaybeEntry.takeError());
2294 return;
2295 }
2296 llvm::BitstreamEntry Entry = MaybeEntry.get();
2297
2298 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2299 Error(Msg: "malformed block record in AST file");
2300 return;
2301 }
2302
2303 Record.clear();
2304 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2305 if (!MaybePP) {
2306 Error(Err: MaybePP.takeError());
2307 return;
2308 }
2309 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2310 case PP_MACRO_DIRECTIVE_HISTORY:
2311 break;
2312
2313 case PP_MODULE_MACRO: {
2314 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2315 auto &Info = ModuleMacros.back();
2316 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[0]);
2317 Info.MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[1]));
2318 for (int I = 2, N = Record.size(); I != N; ++I)
2319 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2320 continue;
2321 }
2322
2323 default:
2324 Error(Msg: "malformed block record in AST file");
2325 return;
2326 }
2327
2328 // We found the macro directive history; that's the last record
2329 // for this macro.
2330 break;
2331 }
2332
2333 // Module macros are listed in reverse dependency order.
2334 {
2335 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2336 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2337 for (auto &MMR : ModuleMacros) {
2338 Overrides.clear();
2339 for (unsigned ModID : MMR.Overrides) {
2340 Module *Mod = getSubmodule(GlobalID: ModID);
2341 auto *Macro = PP.getModuleMacro(Mod, II);
2342 assert(Macro && "missing definition for overridden macro");
2343 Overrides.push_back(Elt: Macro);
2344 }
2345
2346 bool Inserted = false;
2347 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2348 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2349 }
2350 }
2351
2352 // Don't read the directive history for a module; we don't have anywhere
2353 // to put it.
2354 if (M.isModule())
2355 return;
2356
2357 // Deserialize the macro directives history in reverse source-order.
2358 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2359 unsigned Idx = 0, N = Record.size();
2360 while (Idx < N) {
2361 MacroDirective *MD = nullptr;
2362 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2363 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2364 switch (K) {
2365 case MacroDirective::MD_Define: {
2366 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2367 MD = PP.AllocateDefMacroDirective(MI, Loc);
2368 break;
2369 }
2370 case MacroDirective::MD_Undefine:
2371 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2372 break;
2373 case MacroDirective::MD_Visibility:
2374 bool isPublic = Record[Idx++];
2375 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2376 break;
2377 }
2378
2379 if (!Latest)
2380 Latest = MD;
2381 if (Earliest)
2382 Earliest->setPrevious(MD);
2383 Earliest = MD;
2384 }
2385
2386 if (Latest)
2387 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2388}
2389
2390bool ASTReader::shouldDisableValidationForFile(
2391 const serialization::ModuleFile &M) const {
2392 if (DisableValidationKind == DisableValidationForModuleKind::None)
2393 return false;
2394
2395 // If a PCH is loaded and validation is disabled for PCH then disable
2396 // validation for the PCH and the modules it loads.
2397 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2398
2399 switch (K) {
2400 case MK_MainFile:
2401 case MK_Preamble:
2402 case MK_PCH:
2403 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2404 case MK_ImplicitModule:
2405 case MK_ExplicitModule:
2406 case MK_PrebuiltModule:
2407 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2408 }
2409
2410 return false;
2411}
2412
2413InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2414 // If this ID is bogus, just return an empty input file.
2415 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2416 return InputFileInfo();
2417
2418 // If we've already loaded this input file, return it.
2419 if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2420 return F.InputFileInfosLoaded[ID - 1];
2421
2422 // Go find this input file.
2423 BitstreamCursor &Cursor = F.InputFilesCursor;
2424 SavedStreamPosition SavedPosition(Cursor);
2425 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2426 F.InputFileOffsets[ID - 1])) {
2427 // FIXME this drops errors on the floor.
2428 consumeError(Err: std::move(Err));
2429 }
2430
2431 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2432 if (!MaybeCode) {
2433 // FIXME this drops errors on the floor.
2434 consumeError(Err: MaybeCode.takeError());
2435 }
2436 unsigned Code = MaybeCode.get();
2437 RecordData Record;
2438 StringRef Blob;
2439
2440 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2441 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2442 "invalid record type for input file");
2443 else {
2444 // FIXME this drops errors on the floor.
2445 consumeError(Err: Maybe.takeError());
2446 }
2447
2448 assert(Record[0] == ID && "Bogus stored ID or offset");
2449 InputFileInfo R;
2450 R.StoredSize = static_cast<off_t>(Record[1]);
2451 R.StoredTime = static_cast<time_t>(Record[2]);
2452 R.Overridden = static_cast<bool>(Record[3]);
2453 R.Transient = static_cast<bool>(Record[4]);
2454 R.TopLevel = static_cast<bool>(Record[5]);
2455 R.ModuleMap = static_cast<bool>(Record[6]);
2456 std::tie(args&: R.FilenameAsRequested, args&: R.Filename) = [&]() {
2457 uint16_t AsRequestedLength = Record[7];
2458
2459 std::string NameAsRequested = Blob.substr(Start: 0, N: AsRequestedLength).str();
2460 std::string Name = Blob.substr(Start: AsRequestedLength).str();
2461
2462 ResolveImportedPath(M&: F, Filename&: NameAsRequested);
2463 ResolveImportedPath(M&: F, Filename&: Name);
2464
2465 if (Name.empty())
2466 Name = NameAsRequested;
2467
2468 return std::make_pair(x: std::move(NameAsRequested), y: std::move(Name));
2469 }();
2470
2471 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2472 if (!MaybeEntry) // FIXME this drops errors on the floor.
2473 consumeError(Err: MaybeEntry.takeError());
2474 llvm::BitstreamEntry Entry = MaybeEntry.get();
2475 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2476 "expected record type for input file hash");
2477
2478 Record.clear();
2479 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2480 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2481 "invalid record type for input file hash");
2482 else {
2483 // FIXME this drops errors on the floor.
2484 consumeError(Err: Maybe.takeError());
2485 }
2486 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2487 static_cast<uint64_t>(Record[0]);
2488
2489 // Note that we've loaded this input file info.
2490 F.InputFileInfosLoaded[ID - 1] = R;
2491 return R;
2492}
2493
2494static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2495InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2496 // If this ID is bogus, just return an empty input file.
2497 if (ID == 0 || ID > F.InputFilesLoaded.size())
2498 return InputFile();
2499
2500 // If we've already loaded this input file, return it.
2501 if (F.InputFilesLoaded[ID-1].getFile())
2502 return F.InputFilesLoaded[ID-1];
2503
2504 if (F.InputFilesLoaded[ID-1].isNotFound())
2505 return InputFile();
2506
2507 // Go find this input file.
2508 BitstreamCursor &Cursor = F.InputFilesCursor;
2509 SavedStreamPosition SavedPosition(Cursor);
2510 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2511 F.InputFileOffsets[ID - 1])) {
2512 // FIXME this drops errors on the floor.
2513 consumeError(Err: std::move(Err));
2514 }
2515
2516 InputFileInfo FI = getInputFileInfo(F, ID);
2517 off_t StoredSize = FI.StoredSize;
2518 time_t StoredTime = FI.StoredTime;
2519 bool Overridden = FI.Overridden;
2520 bool Transient = FI.Transient;
2521 StringRef Filename = FI.FilenameAsRequested;
2522 uint64_t StoredContentHash = FI.ContentHash;
2523
2524 // For standard C++ modules, we don't need to check the inputs.
2525 bool SkipChecks = F.StandardCXXModule;
2526
2527 const HeaderSearchOptions &HSOpts =
2528 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2529
2530 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2531 // modules.
2532 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2533 SkipChecks = false;
2534 Overridden = false;
2535 }
2536
2537 auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);
2538
2539 // For an overridden file, create a virtual file with the stored
2540 // size/timestamp.
2541 if ((Overridden || Transient || SkipChecks) && !File)
2542 File = FileMgr.getVirtualFileRef(Filename, Size: StoredSize, ModificationTime: StoredTime);
2543
2544 if (!File) {
2545 if (Complain) {
2546 std::string ErrorStr = "could not find file '";
2547 ErrorStr += Filename;
2548 ErrorStr += "' referenced by AST file '";
2549 ErrorStr += F.FileName;
2550 ErrorStr += "'";
2551 Error(Msg: ErrorStr);
2552 }
2553 // Record that we didn't find the file.
2554 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2555 return InputFile();
2556 }
2557
2558 // Check if there was a request to override the contents of the file
2559 // that was part of the precompiled header. Overriding such a file
2560 // can lead to problems when lexing using the source locations from the
2561 // PCH.
2562 SourceManager &SM = getSourceManager();
2563 // FIXME: Reject if the overrides are different.
2564 if ((!Overridden && !Transient) && !SkipChecks &&
2565 SM.isFileOverridden(File: *File)) {
2566 if (Complain)
2567 Error(diag::err_fe_pch_file_overridden, Filename);
2568
2569 // After emitting the diagnostic, bypass the overriding file to recover
2570 // (this creates a separate FileEntry).
2571 File = SM.bypassFileContentsOverride(File: *File);
2572 if (!File) {
2573 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2574 return InputFile();
2575 }
2576 }
2577
2578 struct Change {
2579 enum ModificationKind {
2580 Size,
2581 ModTime,
2582 Content,
2583 None,
2584 } Kind;
2585 std::optional<int64_t> Old = std::nullopt;
2586 std::optional<int64_t> New = std::nullopt;
2587 };
2588 auto HasInputContentChanged = [&](Change OriginalChange) {
2589 assert(ValidateASTInputFilesContent &&
2590 "We should only check the content of the inputs with "
2591 "ValidateASTInputFilesContent enabled.");
2592
2593 if (StoredContentHash == static_cast<uint64_t>(llvm::hash_code(-1)))
2594 return OriginalChange;
2595
2596 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2597 if (!MemBuffOrError) {
2598 if (!Complain)
2599 return OriginalChange;
2600 std::string ErrorStr = "could not get buffer for file '";
2601 ErrorStr += File->getName();
2602 ErrorStr += "'";
2603 Error(Msg: ErrorStr);
2604 return OriginalChange;
2605 }
2606
2607 // FIXME: hash_value is not guaranteed to be stable!
2608 auto ContentHash = hash_value(S: MemBuffOrError.get()->getBuffer());
2609 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2610 return Change{.Kind: Change::None};
2611
2612 return Change{.Kind: Change::Content};
2613 };
2614 auto HasInputFileChanged = [&]() {
2615 if (StoredSize != File->getSize())
2616 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2617 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2618 StoredTime != File->getModificationTime()) {
2619 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2620 .New: File->getModificationTime()};
2621
2622 // In case the modification time changes but not the content,
2623 // accept the cached file as legit.
2624 if (ValidateASTInputFilesContent)
2625 return HasInputContentChanged(MTimeChange);
2626
2627 return MTimeChange;
2628 }
2629 return Change{.Kind: Change::None};
2630 };
2631
2632 bool IsOutOfDate = false;
2633 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2634 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2635 // enabled, it is better to check the contents of the inputs. Since we can't
2636 // get correct modified time information for inputs from overriden inputs.
2637 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2638 F.StandardCXXModule && FileChange.Kind == Change::None)
2639 FileChange = HasInputContentChanged(FileChange);
2640
2641 // For an overridden file, there is nothing to validate.
2642 if (!Overridden && FileChange.Kind != Change::None) {
2643 if (Complain && !Diags.isDiagnosticInFlight()) {
2644 // Build a list of the PCH imports that got us here (in reverse).
2645 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2646 while (!ImportStack.back()->ImportedBy.empty())
2647 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
2648
2649 // The top-level PCH is stale.
2650 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2651 Diag(diag::err_fe_ast_file_modified)
2652 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2653 << TopLevelPCHName << FileChange.Kind
2654 << (FileChange.Old && FileChange.New)
2655 << llvm::itostr(FileChange.Old.value_or(0))
2656 << llvm::itostr(FileChange.New.value_or(0));
2657
2658 // Print the import stack.
2659 if (ImportStack.size() > 1) {
2660 Diag(diag::note_pch_required_by)
2661 << Filename << ImportStack[0]->FileName;
2662 for (unsigned I = 1; I < ImportStack.size(); ++I)
2663 Diag(diag::note_pch_required_by)
2664 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2665 }
2666
2667 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2668 }
2669
2670 IsOutOfDate = true;
2671 }
2672 // FIXME: If the file is overridden and we've already opened it,
2673 // issue an error (or split it into a separate FileEntry).
2674
2675 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2676
2677 // Note that we've loaded this input file.
2678 F.InputFilesLoaded[ID-1] = IF;
2679 return IF;
2680}
2681
2682/// If we are loading a relocatable PCH or module file, and the filename
2683/// is not an absolute path, add the system or module root to the beginning of
2684/// the file name.
2685void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2686 // Resolve relative to the base directory, if we have one.
2687 if (!M.BaseDirectory.empty())
2688 return ResolveImportedPath(Filename, Prefix: M.BaseDirectory);
2689}
2690
2691void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2692 if (Filename.empty() || llvm::sys::path::is_absolute(path: Filename) ||
2693 Filename == "<built-in>" || Filename == "<command line>")
2694 return;
2695
2696 SmallString<128> Buffer;
2697 llvm::sys::path::append(path&: Buffer, a: Prefix, b: Filename);
2698 Filename.assign(first: Buffer.begin(), last: Buffer.end());
2699}
2700
2701static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2702 switch (ARR) {
2703 case ASTReader::Failure: return true;
2704 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2705 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2706 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2707 case ASTReader::ConfigurationMismatch:
2708 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2709 case ASTReader::HadErrors: return true;
2710 case ASTReader::Success: return false;
2711 }
2712
2713 llvm_unreachable("unknown ASTReadResult");
2714}
2715
2716ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2717 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2718 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2719 std::string &SuggestedPredefines) {
2720 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
2721 // FIXME this drops errors on the floor.
2722 consumeError(Err: std::move(Err));
2723 return Failure;
2724 }
2725
2726 // Read all of the records in the options block.
2727 RecordData Record;
2728 ASTReadResult Result = Success;
2729 while (true) {
2730 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2731 if (!MaybeEntry) {
2732 // FIXME this drops errors on the floor.
2733 consumeError(Err: MaybeEntry.takeError());
2734 return Failure;
2735 }
2736 llvm::BitstreamEntry Entry = MaybeEntry.get();
2737
2738 switch (Entry.Kind) {
2739 case llvm::BitstreamEntry::Error:
2740 case llvm::BitstreamEntry::SubBlock:
2741 return Failure;
2742
2743 case llvm::BitstreamEntry::EndBlock:
2744 return Result;
2745
2746 case llvm::BitstreamEntry::Record:
2747 // The interesting case.
2748 break;
2749 }
2750
2751 // Read and process a record.
2752 Record.clear();
2753 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2754 if (!MaybeRecordType) {
2755 // FIXME this drops errors on the floor.
2756 consumeError(Err: MaybeRecordType.takeError());
2757 return Failure;
2758 }
2759 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2760 case LANGUAGE_OPTIONS: {
2761 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2762 if (ParseLanguageOptions(Record, Complain, Listener,
2763 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
2764 Result = ConfigurationMismatch;
2765 break;
2766 }
2767
2768 case TARGET_OPTIONS: {
2769 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2770 if (ParseTargetOptions(Record, Complain, Listener,
2771 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
2772 Result = ConfigurationMismatch;
2773 break;
2774 }
2775
2776 case FILE_SYSTEM_OPTIONS: {
2777 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2778 if (!AllowCompatibleConfigurationMismatch &&
2779 ParseFileSystemOptions(Record, Complain, Listener))
2780 Result = ConfigurationMismatch;
2781 break;
2782 }
2783
2784 case HEADER_SEARCH_OPTIONS: {
2785 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2786 if (!AllowCompatibleConfigurationMismatch &&
2787 ParseHeaderSearchOptions(Record, Complain, Listener))
2788 Result = ConfigurationMismatch;
2789 break;
2790 }
2791
2792 case PREPROCESSOR_OPTIONS:
2793 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2794 if (!AllowCompatibleConfigurationMismatch &&
2795 ParsePreprocessorOptions(Record, Complain, Listener,
2796 SuggestedPredefines))
2797 Result = ConfigurationMismatch;
2798 break;
2799 }
2800 }
2801}
2802
2803ASTReader::ASTReadResult
2804ASTReader::ReadControlBlock(ModuleFile &F,
2805 SmallVectorImpl<ImportedModule> &Loaded,
2806 const ModuleFile *ImportedBy,
2807 unsigned ClientLoadCapabilities) {
2808 BitstreamCursor &Stream = F.Stream;
2809
2810 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
2811 Error(Err: std::move(Err));
2812 return Failure;
2813 }
2814
2815 // Lambda to read the unhashed control block the first time it's called.
2816 //
2817 // For PCM files, the unhashed control block cannot be read until after the
2818 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2819 // need to look ahead before reading the IMPORTS record. For consistency,
2820 // this block is always read somehow (see BitstreamEntry::EndBlock).
2821 bool HasReadUnhashedControlBlock = false;
2822 auto readUnhashedControlBlockOnce = [&]() {
2823 if (!HasReadUnhashedControlBlock) {
2824 HasReadUnhashedControlBlock = true;
2825 if (ASTReadResult Result =
2826 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
2827 return Result;
2828 }
2829 return Success;
2830 };
2831
2832 bool DisableValidation = shouldDisableValidationForFile(M: F);
2833
2834 // Read all of the records and blocks in the control block.
2835 RecordData Record;
2836 unsigned NumInputs = 0;
2837 unsigned NumUserInputs = 0;
2838 StringRef BaseDirectoryAsWritten;
2839 while (true) {
2840 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2841 if (!MaybeEntry) {
2842 Error(Err: MaybeEntry.takeError());
2843 return Failure;
2844 }
2845 llvm::BitstreamEntry Entry = MaybeEntry.get();
2846
2847 switch (Entry.Kind) {
2848 case llvm::BitstreamEntry::Error:
2849 Error(Msg: "malformed block record in AST file");
2850 return Failure;
2851 case llvm::BitstreamEntry::EndBlock: {
2852 // Validate the module before returning. This call catches an AST with
2853 // no module name and no imports.
2854 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2855 return Result;
2856
2857 // Validate input files.
2858 const HeaderSearchOptions &HSOpts =
2859 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2860
2861 // All user input files reside at the index range [0, NumUserInputs), and
2862 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2863 // loaded module files, ignore missing inputs.
2864 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2865 F.Kind != MK_PrebuiltModule) {
2866 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2867
2868 // If we are reading a module, we will create a verification timestamp,
2869 // so we verify all input files. Otherwise, verify only user input
2870 // files.
2871
2872 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2873 if (HSOpts.ModulesValidateOncePerBuildSession &&
2874 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
2875 F.Kind == MK_ImplicitModule)
2876 N = NumUserInputs;
2877
2878 for (unsigned I = 0; I < N; ++I) {
2879 InputFile IF = getInputFile(F, ID: I+1, Complain);
2880 if (!IF.getFile() || IF.isOutOfDate())
2881 return OutOfDate;
2882 }
2883 }
2884
2885 if (Listener)
2886 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind);
2887
2888 if (Listener && Listener->needsInputFileVisitation()) {
2889 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2890 : NumUserInputs;
2891 for (unsigned I = 0; I < N; ++I) {
2892 bool IsSystem = I >= NumUserInputs;
2893 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
2894 Listener->visitInputFile(
2895 Filename: FI.FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
2896 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
2897 }
2898 }
2899
2900 return Success;
2901 }
2902
2903 case llvm::BitstreamEntry::SubBlock:
2904 switch (Entry.ID) {
2905 case INPUT_FILES_BLOCK_ID:
2906 F.InputFilesCursor = Stream;
2907 if (llvm::Error Err = Stream.SkipBlock()) {
2908 Error(Err: std::move(Err));
2909 return Failure;
2910 }
2911 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
2912 Error(Msg: "malformed block record in AST file");
2913 return Failure;
2914 }
2915 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
2916 continue;
2917
2918 case OPTIONS_BLOCK_ID:
2919 // If we're reading the first module for this group, check its options
2920 // are compatible with ours. For modules it imports, no further checking
2921 // is required, because we checked them when we built it.
2922 if (Listener && !ImportedBy) {
2923 // Should we allow the configuration of the module file to differ from
2924 // the configuration of the current translation unit in a compatible
2925 // way?
2926 //
2927 // FIXME: Allow this for files explicitly specified with -include-pch.
2928 bool AllowCompatibleConfigurationMismatch =
2929 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2930
2931 ASTReadResult Result =
2932 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2933 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
2934 SuggestedPredefines);
2935 if (Result == Failure) {
2936 Error(Msg: "malformed block record in AST file");
2937 return Result;
2938 }
2939
2940 if (DisableValidation ||
2941 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2942 Result = Success;
2943
2944 // If we can't load the module, exit early since we likely
2945 // will rebuild the module anyway. The stream may be in the
2946 // middle of a block.
2947 if (Result != Success)
2948 return Result;
2949 } else if (llvm::Error Err = Stream.SkipBlock()) {
2950 Error(Err: std::move(Err));
2951 return Failure;
2952 }
2953 continue;
2954
2955 default:
2956 if (llvm::Error Err = Stream.SkipBlock()) {
2957 Error(Err: std::move(Err));
2958 return Failure;
2959 }
2960 continue;
2961 }
2962
2963 case llvm::BitstreamEntry::Record:
2964 // The interesting case.
2965 break;
2966 }
2967
2968 // Read and process a record.
2969 Record.clear();
2970 StringRef Blob;
2971 Expected<unsigned> MaybeRecordType =
2972 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
2973 if (!MaybeRecordType) {
2974 Error(Err: MaybeRecordType.takeError());
2975 return Failure;
2976 }
2977 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2978 case METADATA: {
2979 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2980 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2981 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2982 : diag::err_pch_version_too_new);
2983 return VersionMismatch;
2984 }
2985
2986 bool hasErrors = Record[7];
2987 if (hasErrors && !DisableValidation) {
2988 // If requested by the caller and the module hasn't already been read
2989 // or compiled, mark modules on error as out-of-date.
2990 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2991 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
2992 return OutOfDate;
2993
2994 if (!AllowASTWithCompilerErrors) {
2995 Diag(diag::err_pch_with_compiler_errors);
2996 return HadErrors;
2997 }
2998 }
2999 if (hasErrors) {
3000 Diags.ErrorOccurred = true;
3001 Diags.UncompilableErrorOccurred = true;
3002 Diags.UnrecoverableErrorOccurred = true;
3003 }
3004
3005 F.RelocatablePCH = Record[4];
3006 // Relative paths in a relocatable PCH are relative to our sysroot.
3007 if (F.RelocatablePCH)
3008 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3009
3010 F.StandardCXXModule = Record[5];
3011
3012 F.HasTimestamps = Record[6];
3013
3014 const std::string &CurBranch = getClangFullRepositoryVersion();
3015 StringRef ASTBranch = Blob;
3016 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3017 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3018 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
3019 return VersionMismatch;
3020 }
3021 break;
3022 }
3023
3024 case IMPORTS: {
3025 // Validate the AST before processing any imports (otherwise, untangling
3026 // them can be error-prone and expensive). A module will have a name and
3027 // will already have been validated, but this catches the PCH case.
3028 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3029 return Result;
3030
3031 // Load each of the imported PCH files.
3032 unsigned Idx = 0, N = Record.size();
3033 while (Idx < N) {
3034 // Read information about the AST file.
3035 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3036 // Whether we're importing a standard c++ module.
3037 bool IsImportingStdCXXModule = Record[Idx++];
3038 // The import location will be the local one for now; we will adjust
3039 // all import locations of module imports after the global source
3040 // location info are setup, in ReadAST.
3041 SourceLocation ImportLoc =
3042 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3043 off_t StoredSize = !IsImportingStdCXXModule ? (off_t)Record[Idx++] : 0;
3044 time_t StoredModTime =
3045 !IsImportingStdCXXModule ? (time_t)Record[Idx++] : 0;
3046
3047 ASTFileSignature StoredSignature;
3048 if (!IsImportingStdCXXModule) {
3049 auto FirstSignatureByte = Record.begin() + Idx;
3050 StoredSignature = ASTFileSignature::create(
3051 First: FirstSignatureByte, Last: FirstSignatureByte + ASTFileSignature::size);
3052 Idx += ASTFileSignature::size;
3053 }
3054
3055 std::string ImportedName = ReadString(Record, Idx);
3056 std::string ImportedFile;
3057
3058 // For prebuilt and explicit modules first consult the file map for
3059 // an override. Note that here we don't search prebuilt module
3060 // directories if we're not importing standard c++ module, only the
3061 // explicit name to file mappings. Also, we will still verify the
3062 // size/signature making sure it is essentially the same file but
3063 // perhaps in a different location.
3064 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3065 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3066 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3067
3068 // For C++20 Modules, we won't record the path to the imported modules
3069 // in the BMI
3070 if (!IsImportingStdCXXModule) {
3071 if (ImportedFile.empty()) {
3072 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3073 // ModuleCache as when writing.
3074 ImportedFile = ReadPath(BaseDirectory: BaseDirectoryAsWritten, Record, Idx);
3075 } else
3076 SkipPath(Record, Idx);
3077 } else if (ImportedFile.empty()) {
3078 Diag(clang::diag::err_failed_to_find_module_file) << ImportedName;
3079 return Missing;
3080 }
3081
3082 // If our client can't cope with us being out of date, we can't cope with
3083 // our dependency being missing.
3084 unsigned Capabilities = ClientLoadCapabilities;
3085 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3086 Capabilities &= ~ARR_Missing;
3087
3088 // Load the AST file.
3089 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3090 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3091 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3092
3093 // If we diagnosed a problem, produce a backtrace.
3094 bool recompilingFinalized =
3095 Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
3096 getModuleManager().getModuleCache().isPCMFinal(Filename: F.FileName);
3097 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
3098 Diag(diag::note_module_file_imported_by)
3099 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3100 if (recompilingFinalized)
3101 Diag(diag::note_module_file_conflict);
3102
3103 switch (Result) {
3104 case Failure: return Failure;
3105 // If we have to ignore the dependency, we'll have to ignore this too.
3106 case Missing:
3107 case OutOfDate: return OutOfDate;
3108 case VersionMismatch: return VersionMismatch;
3109 case ConfigurationMismatch: return ConfigurationMismatch;
3110 case HadErrors: return HadErrors;
3111 case Success: break;
3112 }
3113 }
3114 break;
3115 }
3116
3117 case ORIGINAL_FILE:
3118 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3119 F.ActualOriginalSourceFileName = std::string(Blob);
3120 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
3121 ResolveImportedPath(M&: F, Filename&: F.OriginalSourceFileName);
3122 break;
3123
3124 case ORIGINAL_FILE_ID:
3125 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3126 break;
3127
3128 case MODULE_NAME:
3129 F.ModuleName = std::string(Blob);
3130 Diag(diag::remark_module_import)
3131 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3132 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3133 if (Listener)
3134 Listener->ReadModuleName(ModuleName: F.ModuleName);
3135
3136 // Validate the AST as soon as we have a name so we can exit early on
3137 // failure.
3138 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3139 return Result;
3140
3141 break;
3142
3143 case MODULE_DIRECTORY: {
3144 // Save the BaseDirectory as written in the PCM for computing the module
3145 // filename for the ModuleCache.
3146 BaseDirectoryAsWritten = Blob;
3147 assert(!F.ModuleName.empty() &&
3148 "MODULE_DIRECTORY found before MODULE_NAME");
3149 F.BaseDirectory = std::string(Blob);
3150 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3151 break;
3152 // If we've already loaded a module map file covering this module, we may
3153 // have a better path for it (relative to the current build).
3154 Module *M = PP.getHeaderSearchInfo().lookupModule(
3155 ModuleName: F.ModuleName, ImportLoc: SourceLocation(), /*AllowSearch*/ true,
3156 /*AllowExtraModuleMapSearch*/ true);
3157 if (M && M->Directory) {
3158 // If we're implicitly loading a module, the base directory can't
3159 // change between the build and use.
3160 // Don't emit module relocation error if we have -fno-validate-pch
3161 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3162 DisableValidationForModuleKind::Module) &&
3163 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3164 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3165 if (!BuildDir || *BuildDir != M->Directory) {
3166 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3167 Diag(diag::err_imported_module_relocated)
3168 << F.ModuleName << Blob << M->Directory->getName();
3169 return OutOfDate;
3170 }
3171 }
3172 F.BaseDirectory = std::string(M->Directory->getName());
3173 }
3174 break;
3175 }
3176
3177 case MODULE_MAP_FILE:
3178 if (ASTReadResult Result =
3179 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3180 return Result;
3181 break;
3182
3183 case INPUT_FILE_OFFSETS:
3184 NumInputs = Record[0];
3185 NumUserInputs = Record[1];
3186 F.InputFileOffsets =
3187 (const llvm::support::unaligned_uint64_t *)Blob.data();
3188 F.InputFilesLoaded.resize(new_size: NumInputs);
3189 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3190 F.NumUserInputFiles = NumUserInputs;
3191 break;
3192 }
3193 }
3194}
3195
3196llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3197 unsigned ClientLoadCapabilities) {
3198 BitstreamCursor &Stream = F.Stream;
3199
3200 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3201 return Err;
3202 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3203
3204 // Read all of the records and blocks for the AST file.
3205 RecordData Record;
3206 while (true) {
3207 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3208 if (!MaybeEntry)
3209 return MaybeEntry.takeError();
3210 llvm::BitstreamEntry Entry = MaybeEntry.get();
3211
3212 switch (Entry.Kind) {
3213 case llvm::BitstreamEntry::Error:
3214 return llvm::createStringError(
3215 EC: std::errc::illegal_byte_sequence,
3216 Fmt: "error at end of module block in AST file");
3217 case llvm::BitstreamEntry::EndBlock:
3218 // Outside of C++, we do not store a lookup map for the translation unit.
3219 // Instead, mark it as needing a lookup map to be built if this module
3220 // contains any declarations lexically within it (which it always does!).
3221 // This usually has no cost, since we very rarely need the lookup map for
3222 // the translation unit outside C++.
3223 if (ASTContext *Ctx = ContextObj) {
3224 DeclContext *DC = Ctx->getTranslationUnitDecl();
3225 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3226 DC->setMustBuildLookupTable();
3227 }
3228
3229 return llvm::Error::success();
3230 case llvm::BitstreamEntry::SubBlock:
3231 switch (Entry.ID) {
3232 case DECLTYPES_BLOCK_ID:
3233 // We lazily load the decls block, but we want to set up the
3234 // DeclsCursor cursor to point into it. Clone our current bitcode
3235 // cursor to it, enter the block and read the abbrevs in that block.
3236 // With the main cursor, we just skip over it.
3237 F.DeclsCursor = Stream;
3238 if (llvm::Error Err = Stream.SkipBlock())
3239 return Err;
3240 if (llvm::Error Err = ReadBlockAbbrevs(
3241 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3242 return Err;
3243 break;
3244
3245 case PREPROCESSOR_BLOCK_ID:
3246 F.MacroCursor = Stream;
3247 if (!PP.getExternalSource())
3248 PP.setExternalSource(this);
3249
3250 if (llvm::Error Err = Stream.SkipBlock())
3251 return Err;
3252 if (llvm::Error Err =
3253 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3254 return Err;
3255 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3256 break;
3257
3258 case PREPROCESSOR_DETAIL_BLOCK_ID:
3259 F.PreprocessorDetailCursor = Stream;
3260
3261 if (llvm::Error Err = Stream.SkipBlock()) {
3262 return Err;
3263 }
3264 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3265 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3266 return Err;
3267 F.PreprocessorDetailStartOffset
3268 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3269
3270 if (!PP.getPreprocessingRecord())
3271 PP.createPreprocessingRecord();
3272 if (!PP.getPreprocessingRecord()->getExternalSource())
3273 PP.getPreprocessingRecord()->SetExternalSource(*this);
3274 break;
3275
3276 case SOURCE_MANAGER_BLOCK_ID:
3277 if (llvm::Error Err = ReadSourceManagerBlock(F))
3278 return Err;
3279 break;
3280
3281 case SUBMODULE_BLOCK_ID:
3282 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3283 return Err;
3284 break;
3285
3286 case COMMENTS_BLOCK_ID: {
3287 BitstreamCursor C = Stream;
3288
3289 if (llvm::Error Err = Stream.SkipBlock())
3290 return Err;
3291 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3292 return Err;
3293 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3294 break;
3295 }
3296
3297 default:
3298 if (llvm::Error Err = Stream.SkipBlock())
3299 return Err;
3300 break;
3301 }
3302 continue;
3303
3304 case llvm::BitstreamEntry::Record:
3305 // The interesting case.
3306 break;
3307 }
3308
3309 // Read and process a record.
3310 Record.clear();
3311 StringRef Blob;
3312 Expected<unsigned> MaybeRecordType =
3313 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3314 if (!MaybeRecordType)
3315 return MaybeRecordType.takeError();
3316 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3317
3318 // If we're not loading an AST context, we don't care about most records.
3319 if (!ContextObj) {
3320 switch (RecordType) {
3321 case IDENTIFIER_TABLE:
3322 case IDENTIFIER_OFFSET:
3323 case INTERESTING_IDENTIFIERS:
3324 case STATISTICS:
3325 case PP_ASSUME_NONNULL_LOC:
3326 case PP_CONDITIONAL_STACK:
3327 case PP_COUNTER_VALUE:
3328 case SOURCE_LOCATION_OFFSETS:
3329 case MODULE_OFFSET_MAP:
3330 case SOURCE_MANAGER_LINE_TABLE:
3331 case PPD_ENTITIES_OFFSETS:
3332 case HEADER_SEARCH_TABLE:
3333 case IMPORTED_MODULES:
3334 case MACRO_OFFSET:
3335 break;
3336 default:
3337 continue;
3338 }
3339 }
3340
3341 switch (RecordType) {
3342 default: // Default behavior: ignore.
3343 break;
3344
3345 case TYPE_OFFSET: {
3346 if (F.LocalNumTypes != 0)
3347 return llvm::createStringError(
3348 EC: std::errc::illegal_byte_sequence,
3349 Fmt: "duplicate TYPE_OFFSET record in AST file");
3350 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3351 F.LocalNumTypes = Record[0];
3352 unsigned LocalBaseTypeIndex = Record[1];
3353 F.BaseTypeIndex = getTotalNumTypes();
3354
3355 if (F.LocalNumTypes > 0) {
3356 // Introduce the global -> local mapping for types within this module.
3357 GlobalTypeMap.insert(Val: std::make_pair(x: getTotalNumTypes(), y: &F));
3358
3359 // Introduce the local -> global mapping for types within this module.
3360 F.TypeRemap.insertOrReplace(
3361 Val: std::make_pair(x&: LocalBaseTypeIndex,
3362 y: F.BaseTypeIndex - LocalBaseTypeIndex));
3363
3364 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3365 }
3366 break;
3367 }
3368
3369 case DECL_OFFSET: {
3370 if (F.LocalNumDecls != 0)
3371 return llvm::createStringError(
3372 EC: std::errc::illegal_byte_sequence,
3373 Fmt: "duplicate DECL_OFFSET record in AST file");
3374 F.DeclOffsets = (const DeclOffset *)Blob.data();
3375 F.LocalNumDecls = Record[0];
3376 unsigned LocalBaseDeclID = Record[1];
3377 F.BaseDeclID = getTotalNumDecls();
3378
3379 if (F.LocalNumDecls > 0) {
3380 // Introduce the global -> local mapping for declarations within this
3381 // module.
3382 GlobalDeclMap.insert(
3383 Val: std::make_pair(x: getTotalNumDecls() + NUM_PREDEF_DECL_IDS, y: &F));
3384
3385 // Introduce the local -> global mapping for declarations within this
3386 // module.
3387 F.DeclRemap.insertOrReplace(
3388 Val: std::make_pair(x&: LocalBaseDeclID, y: F.BaseDeclID - LocalBaseDeclID));
3389
3390 // Introduce the global -> local mapping for declarations within this
3391 // module.
3392 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3393
3394 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3395 }
3396 break;
3397 }
3398
3399 case TU_UPDATE_LEXICAL: {
3400 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3401 LexicalContents Contents(
3402 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3403 Blob.data()),
3404 static_cast<unsigned int>(Blob.size() / 4));
3405 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3406 TU->setHasExternalLexicalStorage(true);
3407 break;
3408 }
3409
3410 case UPDATE_VISIBLE: {
3411 unsigned Idx = 0;
3412 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3413 auto *Data = (const unsigned char*)Blob.data();
3414 PendingVisibleUpdates[ID].push_back(Elt: PendingVisibleUpdate{.Mod: &F, .Data: Data});
3415 // If we've already loaded the decl, perform the updates when we finish
3416 // loading this block.
3417 if (Decl *D = GetExistingDecl(ID))
3418 PendingUpdateRecords.push_back(
3419 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3420 break;
3421 }
3422
3423 case IDENTIFIER_TABLE:
3424 F.IdentifierTableData =
3425 reinterpret_cast<const unsigned char *>(Blob.data());
3426 if (Record[0]) {
3427 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3428 Buckets: F.IdentifierTableData + Record[0],
3429 Payload: F.IdentifierTableData + sizeof(uint32_t),
3430 Base: F.IdentifierTableData,
3431 InfoObj: ASTIdentifierLookupTrait(*this, F));
3432
3433 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3434 }
3435 break;
3436
3437 case IDENTIFIER_OFFSET: {
3438 if (F.LocalNumIdentifiers != 0)
3439 return llvm::createStringError(
3440 EC: std::errc::illegal_byte_sequence,
3441 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
3442 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3443 F.LocalNumIdentifiers = Record[0];
3444 unsigned LocalBaseIdentifierID = Record[1];
3445 F.BaseIdentifierID = getTotalNumIdentifiers();
3446
3447 if (F.LocalNumIdentifiers > 0) {
3448 // Introduce the global -> local mapping for identifiers within this
3449 // module.
3450 GlobalIdentifierMap.insert(Val: std::make_pair(x: getTotalNumIdentifiers() + 1,
3451 y: &F));
3452
3453 // Introduce the local -> global mapping for identifiers within this
3454 // module.
3455 F.IdentifierRemap.insertOrReplace(
3456 Val: std::make_pair(x&: LocalBaseIdentifierID,
3457 y: F.BaseIdentifierID - LocalBaseIdentifierID));
3458
3459 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
3460 + F.LocalNumIdentifiers);
3461 }
3462 break;
3463 }
3464
3465 case INTERESTING_IDENTIFIERS:
3466 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
3467 break;
3468
3469 case EAGERLY_DESERIALIZED_DECLS:
3470 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3471 // about "interesting" decls (for instance, if we're building a module).
3472 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3473 EagerlyDeserializedDecls.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3474 break;
3475
3476 case MODULAR_CODEGEN_DECLS:
3477 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3478 // them (ie: if we're not codegenerating this module).
3479 if (F.Kind == MK_MainFile ||
3480 getContext().getLangOpts().BuildingPCHWithObjectFile)
3481 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3482 EagerlyDeserializedDecls.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3483 break;
3484
3485 case SPECIAL_TYPES:
3486 if (SpecialTypes.empty()) {
3487 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3488 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
3489 break;
3490 }
3491
3492 if (SpecialTypes.size() != Record.size())
3493 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3494 Fmt: "invalid special-types record");
3495
3496 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3497 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
3498 if (!SpecialTypes[I])
3499 SpecialTypes[I] = ID;
3500 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3501 // merge step?
3502 }
3503 break;
3504
3505 case STATISTICS:
3506 TotalNumStatements += Record[0];
3507 TotalNumMacros += Record[1];
3508 TotalLexicalDeclContexts += Record[2];
3509 TotalVisibleDeclContexts += Record[3];
3510 break;
3511
3512 case UNUSED_FILESCOPED_DECLS:
3513 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3514 UnusedFileScopedDecls.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3515 break;
3516
3517 case DELEGATING_CTORS:
3518 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3519 DelegatingCtorDecls.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3520 break;
3521
3522 case WEAK_UNDECLARED_IDENTIFIERS:
3523 if (Record.size() % 3 != 0)
3524 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3525 Fmt: "invalid weak identifiers record");
3526
3527 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3528 // files. This isn't the way to do it :)
3529 WeakUndeclaredIdentifiers.clear();
3530
3531 // Translate the weak, undeclared identifiers into global IDs.
3532 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3533 WeakUndeclaredIdentifiers.push_back(
3534 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3535 WeakUndeclaredIdentifiers.push_back(
3536 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3537 WeakUndeclaredIdentifiers.push_back(
3538 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3539 }
3540 break;
3541
3542 case SELECTOR_OFFSETS: {
3543 F.SelectorOffsets = (const uint32_t *)Blob.data();
3544 F.LocalNumSelectors = Record[0];
3545 unsigned LocalBaseSelectorID = Record[1];
3546 F.BaseSelectorID = getTotalNumSelectors();
3547
3548 if (F.LocalNumSelectors > 0) {
3549 // Introduce the global -> local mapping for selectors within this
3550 // module.
3551 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
3552
3553 // Introduce the local -> global mapping for selectors within this
3554 // module.
3555 F.SelectorRemap.insertOrReplace(
3556 Val: std::make_pair(x&: LocalBaseSelectorID,
3557 y: F.BaseSelectorID - LocalBaseSelectorID));
3558
3559 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
3560 }
3561 break;
3562 }
3563
3564 case METHOD_POOL:
3565 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3566 if (Record[0])
3567 F.SelectorLookupTable
3568 = ASTSelectorLookupTable::Create(
3569 Buckets: F.SelectorLookupTableData + Record[0],
3570 Base: F.SelectorLookupTableData,
3571 InfoObj: ASTSelectorLookupTrait(*this, F));
3572 TotalNumMethodPoolEntries += Record[1];
3573 break;
3574
3575 case REFERENCED_SELECTOR_POOL:
3576 if (!Record.empty()) {
3577 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3578 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
3579 LocalID: Record[Idx++]));
3580 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
3581 getRawEncoding());
3582 }
3583 }
3584 break;
3585
3586 case PP_ASSUME_NONNULL_LOC: {
3587 unsigned Idx = 0;
3588 if (!Record.empty())
3589 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3590 ReadSourceLocation(ModuleFile&: F, Record, Idx));
3591 break;
3592 }
3593
3594 case PP_CONDITIONAL_STACK:
3595 if (!Record.empty()) {
3596 unsigned Idx = 0, End = Record.size() - 1;
3597 bool ReachedEOFWhileSkipping = Record[Idx++];
3598 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3599 if (ReachedEOFWhileSkipping) {
3600 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3601 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3602 bool FoundNonSkipPortion = Record[Idx++];
3603 bool FoundElse = Record[Idx++];
3604 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3605 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
3606 args&: FoundElse, args&: ElseLoc);
3607 }
3608 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3609 while (Idx < End) {
3610 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3611 bool WasSkipping = Record[Idx++];
3612 bool FoundNonSkip = Record[Idx++];
3613 bool FoundElse = Record[Idx++];
3614 ConditionalStack.push_back(
3615 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
3616 }
3617 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
3618 }
3619 break;
3620
3621 case PP_COUNTER_VALUE:
3622 if (!Record.empty() && Listener)
3623 Listener->ReadCounter(M: F, Value: Record[0]);
3624 break;
3625
3626 case FILE_SORTED_DECLS:
3627 F.FileSortedDecls = (const DeclID *)Blob.data();
3628 F.NumFileSortedDecls = Record[0];
3629 break;
3630
3631 case SOURCE_LOCATION_OFFSETS: {
3632 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3633 F.LocalNumSLocEntries = Record[0];
3634 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3635 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3636 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
3637 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
3638 TotalSize: SLocSpaceSize);
3639 if (!F.SLocEntryBaseID) {
3640 if (!Diags.isDiagnosticInFlight()) {
3641 Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3642 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
3643 }
3644 return llvm::createStringError(EC: std::errc::invalid_argument,
3645 Fmt: "ran out of source locations");
3646 }
3647 // Make our entry in the range map. BaseID is negative and growing, so
3648 // we invert it. Because we invert it, though, we need the other end of
3649 // the range.
3650 unsigned RangeStart =
3651 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3652 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
3653 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
3654
3655 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3656 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3657 GlobalSLocOffsetMap.insert(
3658 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3659 - SLocSpaceSize,y: &F));
3660
3661 // Initialize the remapping table.
3662 // Invalid stays invalid.
3663 F.SLocRemap.insertOrReplace(Val: std::make_pair(x: 0U, y: 0));
3664 // This module. Base was 2 when being compiled.
3665 F.SLocRemap.insertOrReplace(Val: std::make_pair(
3666 x: 2U, y: static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3667
3668 TotalNumSLocEntries += F.LocalNumSLocEntries;
3669 break;
3670 }
3671
3672 case MODULE_OFFSET_MAP:
3673 F.ModuleOffsetMap = Blob;
3674 break;
3675
3676 case SOURCE_MANAGER_LINE_TABLE:
3677 ParseLineTable(F, Record);
3678 break;
3679
3680 case EXT_VECTOR_DECLS:
3681 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3682 ExtVectorDecls.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3683 break;
3684
3685 case VTABLE_USES:
3686 if (Record.size() % 3 != 0)
3687 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3688 Fmt: "Invalid VTABLE_USES record");
3689
3690 // Later tables overwrite earlier ones.
3691 // FIXME: Modules will have some trouble with this. This is clearly not
3692 // the right way to do this.
3693 VTableUses.clear();
3694
3695 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3696 VTableUses.push_back(Elt: getGlobalDeclID(F, LocalID: Record[Idx++]));
3697 VTableUses.push_back(
3698 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding());
3699 VTableUses.push_back(Elt: Record[Idx++]);
3700 }
3701 break;
3702
3703 case PENDING_IMPLICIT_INSTANTIATIONS:
3704 if (PendingInstantiations.size() % 2 != 0)
3705 return llvm::createStringError(
3706 EC: std::errc::illegal_byte_sequence,
3707 Fmt: "Invalid existing PendingInstantiations");
3708
3709 if (Record.size() % 2 != 0)
3710 return llvm::createStringError(
3711 EC: std::errc::illegal_byte_sequence,
3712 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3713
3714 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3715 PendingInstantiations.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I++]));
3716 PendingInstantiations.push_back(
3717 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3718 }
3719 break;
3720
3721 case SEMA_DECL_REFS:
3722 if (Record.size() != 3)
3723 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3724 Fmt: "Invalid SEMA_DECL_REFS block");
3725 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3726 SemaDeclRefs.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3727 break;
3728
3729 case PPD_ENTITIES_OFFSETS: {
3730 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3731 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3732 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3733
3734 unsigned LocalBasePreprocessedEntityID = Record[0];
3735
3736 unsigned StartingID;
3737 if (!PP.getPreprocessingRecord())
3738 PP.createPreprocessingRecord();
3739 if (!PP.getPreprocessingRecord()->getExternalSource())
3740 PP.getPreprocessingRecord()->SetExternalSource(*this);
3741 StartingID
3742 = PP.getPreprocessingRecord()
3743 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
3744 F.BasePreprocessedEntityID = StartingID;
3745
3746 if (F.NumPreprocessedEntities > 0) {
3747 // Introduce the global -> local mapping for preprocessed entities in
3748 // this module.
3749 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
3750
3751 // Introduce the local -> global mapping for preprocessed entities in
3752 // this module.
3753 F.PreprocessedEntityRemap.insertOrReplace(
3754 Val: std::make_pair(x&: LocalBasePreprocessedEntityID,
3755 y: F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3756 }
3757
3758 break;
3759 }
3760
3761 case PPD_SKIPPED_RANGES: {
3762 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3763 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3764 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3765
3766 if (!PP.getPreprocessingRecord())
3767 PP.createPreprocessingRecord();
3768 if (!PP.getPreprocessingRecord()->getExternalSource())
3769 PP.getPreprocessingRecord()->SetExternalSource(*this);
3770 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3771 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
3772
3773 if (F.NumPreprocessedSkippedRanges > 0)
3774 GlobalSkippedRangeMap.insert(
3775 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
3776 break;
3777 }
3778
3779 case DECL_UPDATE_OFFSETS:
3780 if (Record.size() % 2 != 0)
3781 return llvm::createStringError(
3782 EC: std::errc::illegal_byte_sequence,
3783 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
3784 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3785 GlobalDeclID ID = getGlobalDeclID(F, LocalID: Record[I]);
3786 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I + 1]));
3787
3788 // If we've already loaded the decl, perform the updates when we finish
3789 // loading this block.
3790 if (Decl *D = GetExistingDecl(ID))
3791 PendingUpdateRecords.push_back(
3792 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3793 }
3794 break;
3795
3796 case OBJC_CATEGORIES_MAP:
3797 if (F.LocalNumObjCCategoriesInMap != 0)
3798 return llvm::createStringError(
3799 EC: std::errc::illegal_byte_sequence,
3800 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
3801
3802 F.LocalNumObjCCategoriesInMap = Record[0];
3803 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3804 break;
3805
3806 case OBJC_CATEGORIES:
3807 F.ObjCCategories.swap(RHS&: Record);
3808 break;
3809
3810 case CUDA_SPECIAL_DECL_REFS:
3811 // Later tables overwrite earlier ones.
3812 // FIXME: Modules will have trouble with this.
3813 CUDASpecialDeclRefs.clear();
3814 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3815 CUDASpecialDeclRefs.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3816 break;
3817
3818 case HEADER_SEARCH_TABLE:
3819 F.HeaderFileInfoTableData = Blob.data();
3820 F.LocalNumHeaderFileInfos = Record[1];
3821 if (Record[0]) {
3822 F.HeaderFileInfoTable
3823 = HeaderFileInfoLookupTable::Create(
3824 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3825 Base: (const unsigned char *)F.HeaderFileInfoTableData,
3826 InfoObj: HeaderFileInfoTrait(*this, F,
3827 &PP.getHeaderSearchInfo(),
3828 Blob.data() + Record[2]));
3829
3830 PP.getHeaderSearchInfo().SetExternalSource(this);
3831 if (!PP.getHeaderSearchInfo().getExternalLookup())
3832 PP.getHeaderSearchInfo().SetExternalLookup(this);
3833 }
3834 break;
3835
3836 case FP_PRAGMA_OPTIONS:
3837 // Later tables overwrite earlier ones.
3838 FPPragmaOptions.swap(RHS&: Record);
3839 break;
3840
3841 case OPENCL_EXTENSIONS:
3842 for (unsigned I = 0, E = Record.size(); I != E; ) {
3843 auto Name = ReadString(Record, Idx&: I);
3844 auto &OptInfo = OpenCLExtensions.OptMap[Name];
3845 OptInfo.Supported = Record[I++] != 0;
3846 OptInfo.Enabled = Record[I++] != 0;
3847 OptInfo.WithPragma = Record[I++] != 0;
3848 OptInfo.Avail = Record[I++];
3849 OptInfo.Core = Record[I++];
3850 OptInfo.Opt = Record[I++];
3851 }
3852 break;
3853
3854 case TENTATIVE_DEFINITIONS:
3855 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3856 TentativeDefinitions.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3857 break;
3858
3859 case KNOWN_NAMESPACES:
3860 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3861 KnownNamespaces.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I]));
3862 break;
3863
3864 case UNDEFINED_BUT_USED:
3865 if (UndefinedButUsed.size() % 2 != 0)
3866 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3867 Fmt: "Invalid existing UndefinedButUsed");
3868
3869 if (Record.size() % 2 != 0)
3870 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3871 Fmt: "invalid undefined-but-used record");
3872 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3873 UndefinedButUsed.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I++]));
3874 UndefinedButUsed.push_back(
3875 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3876 }
3877 break;
3878
3879 case DELETE_EXPRS_TO_ANALYZE:
3880 for (unsigned I = 0, N = Record.size(); I != N;) {
3881 DelayedDeleteExprs.push_back(Elt: getGlobalDeclID(F, LocalID: Record[I++]));
3882 const uint64_t Count = Record[I++];
3883 DelayedDeleteExprs.push_back(Elt: Count);
3884 for (uint64_t C = 0; C < Count; ++C) {
3885 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3886 bool IsArrayForm = Record[I++] == 1;
3887 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
3888 }
3889 }
3890 break;
3891
3892 case IMPORTED_MODULES:
3893 if (!F.isModule()) {
3894 // If we aren't loading a module (which has its own exports), make
3895 // all of the imported modules visible.
3896 // FIXME: Deal with macros-only imports.
3897 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3898 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
3899 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
3900 if (GlobalID) {
3901 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
3902 if (DeserializationListener)
3903 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
3904 }
3905 }
3906 }
3907 break;
3908
3909 case MACRO_OFFSET: {
3910 if (F.LocalNumMacros != 0)
3911 return llvm::createStringError(
3912 EC: std::errc::illegal_byte_sequence,
3913 Fmt: "duplicate MACRO_OFFSET record in AST file");
3914 F.MacroOffsets = (const uint32_t *)Blob.data();
3915 F.LocalNumMacros = Record[0];
3916 unsigned LocalBaseMacroID = Record[1];
3917 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3918 F.BaseMacroID = getTotalNumMacros();
3919
3920 if (F.LocalNumMacros > 0) {
3921 // Introduce the global -> local mapping for macros within this module.
3922 GlobalMacroMap.insert(Val: std::make_pair(x: getTotalNumMacros() + 1, y: &F));
3923
3924 // Introduce the local -> global mapping for macros within this module.
3925 F.MacroRemap.insertOrReplace(
3926 Val: std::make_pair(x&: LocalBaseMacroID,
3927 y: F.BaseMacroID - LocalBaseMacroID));
3928
3929 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
3930 }
3931 break;
3932 }
3933
3934 case LATE_PARSED_TEMPLATE:
3935 LateParsedTemplates.emplace_back(
3936 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
3937 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
3938 break;
3939
3940 case OPTIMIZE_PRAGMA_OPTIONS:
3941 if (Record.size() != 1)
3942 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3943 Fmt: "invalid pragma optimize record");
3944 OptimizeOffPragmaLocation = ReadSourceLocation(ModuleFile&: F, Raw: Record[0]);
3945 break;
3946
3947 case MSSTRUCT_PRAGMA_OPTIONS:
3948 if (Record.size() != 1)
3949 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3950 Fmt: "invalid pragma ms_struct record");
3951 PragmaMSStructState = Record[0];
3952 break;
3953
3954 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3955 if (Record.size() != 2)
3956 return llvm::createStringError(
3957 EC: std::errc::illegal_byte_sequence,
3958 Fmt: "invalid pragma pointers to members record");
3959 PragmaMSPointersToMembersState = Record[0];
3960 PointersToMembersPragmaLocation = ReadSourceLocation(ModuleFile&: F, Raw: Record[1]);
3961 break;
3962
3963 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3964 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3965 UnusedLocalTypedefNameCandidates.push_back(
3966 Elt: getGlobalDeclID(F, LocalID: Record[I]));
3967 break;
3968
3969 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3970 if (Record.size() != 1)
3971 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3972 Fmt: "invalid cuda pragma options record");
3973 ForceCUDAHostDeviceDepth = Record[0];
3974 break;
3975
3976 case ALIGN_PACK_PRAGMA_OPTIONS: {
3977 if (Record.size() < 3)
3978 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3979 Fmt: "invalid pragma pack record");
3980 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
3981 PragmaAlignPackCurrentLocation = ReadSourceLocation(ModuleFile&: F, Raw: Record[1]);
3982 unsigned NumStackEntries = Record[2];
3983 unsigned Idx = 3;
3984 // Reset the stack when importing a new module.
3985 PragmaAlignPackStack.clear();
3986 for (unsigned I = 0; I < NumStackEntries; ++I) {
3987 PragmaAlignPackStackEntry Entry;
3988 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
3989 Entry.Location = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
3990 Entry.PushLocation = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
3991 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
3992 Entry.SlotLabel = PragmaAlignPackStrings.back();
3993 PragmaAlignPackStack.push_back(Elt: Entry);
3994 }
3995 break;
3996 }
3997
3998 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3999 if (Record.size() < 3)
4000 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4001 Fmt: "invalid pragma float control record");
4002 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4003 FpPragmaCurrentLocation = ReadSourceLocation(ModuleFile&: F, Raw: Record[1]);
4004 unsigned NumStackEntries = Record[2];
4005 unsigned Idx = 3;
4006 // Reset the stack when importing a new module.
4007 FpPragmaStack.clear();
4008 for (unsigned I = 0; I < NumStackEntries; ++I) {
4009 FpPragmaStackEntry Entry;
4010 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4011 Entry.Location = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
4012 Entry.PushLocation = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
4013 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4014 Entry.SlotLabel = FpPragmaStrings.back();
4015 FpPragmaStack.push_back(Elt: Entry);
4016 }
4017 break;
4018 }
4019
4020 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4021 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4022 DeclsToCheckForDeferredDiags.insert(X: getGlobalDeclID(F, LocalID: Record[I]));
4023 break;
4024 }
4025 }
4026}
4027
4028void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4029 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4030
4031 // Additional remapping information.
4032 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4033 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4034 F.ModuleOffsetMap = StringRef();
4035
4036 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
4037 if (F.SLocRemap.find(K: 0) == F.SLocRemap.end()) {
4038 F.SLocRemap.insert(Val: std::make_pair(x: 0U, y: 0));
4039 F.SLocRemap.insert(Val: std::make_pair(x: 2U, y: 1));
4040 }
4041
4042 // Continuous range maps we may be updating in our module.
4043 using SLocRemapBuilder =
4044 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
4045 2>::Builder;
4046 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4047 SLocRemapBuilder SLocRemap(F.SLocRemap);
4048 RemapBuilder IdentifierRemap(F.IdentifierRemap);
4049 RemapBuilder MacroRemap(F.MacroRemap);
4050 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4051 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4052 RemapBuilder SelectorRemap(F.SelectorRemap);
4053 RemapBuilder DeclRemap(F.DeclRemap);
4054 RemapBuilder TypeRemap(F.TypeRemap);
4055
4056 while (Data < DataEnd) {
4057 // FIXME: Looking up dependency modules by filename is horrible. Let's
4058 // start fixing this with prebuilt, explicit and implicit modules and see
4059 // how it goes...
4060 using namespace llvm::support;
4061 ModuleKind Kind = static_cast<ModuleKind>(
4062 endian::readNext<uint8_t, llvm::endianness::little, unaligned>(memory&: Data));
4063 uint16_t Len =
4064 endian::readNext<uint16_t, llvm::endianness::little, unaligned>(memory&: Data);
4065 StringRef Name = StringRef((const char*)Data, Len);
4066 Data += Len;
4067 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4068 Kind == MK_ImplicitModule
4069 ? ModuleMgr.lookupByModuleName(ModName: Name)
4070 : ModuleMgr.lookupByFileName(FileName: Name));
4071 if (!OM) {
4072 std::string Msg =
4073 "SourceLocation remap refers to unknown module, cannot find ";
4074 Msg.append(str: std::string(Name));
4075 Error(Msg);
4076 return;
4077 }
4078
4079 SourceLocation::UIntTy SLocOffset =
4080 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4081 uint32_t IdentifierIDOffset =
4082 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4083 uint32_t MacroIDOffset =
4084 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4085 uint32_t PreprocessedEntityIDOffset =
4086 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4087 uint32_t SubmoduleIDOffset =
4088 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4089 uint32_t SelectorIDOffset =
4090 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4091 uint32_t DeclIDOffset =
4092 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4093 uint32_t TypeIndexOffset =
4094 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: Data);
4095
4096 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4097 RemapBuilder &Remap) {
4098 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4099 if (Offset != None)
4100 Remap.insert(Val: std::make_pair(x&: Offset,
4101 y: static_cast<int>(BaseOffset - Offset)));
4102 };
4103
4104 constexpr SourceLocation::UIntTy SLocNone =
4105 std::numeric_limits<SourceLocation::UIntTy>::max();
4106 if (SLocOffset != SLocNone)
4107 SLocRemap.insert(Val: std::make_pair(
4108 x&: SLocOffset, y: static_cast<SourceLocation::IntTy>(
4109 OM->SLocEntryBaseOffset - SLocOffset)));
4110
4111 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
4112 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4113 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4114 PreprocessedEntityRemap);
4115 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4116 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4117 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
4118 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
4119
4120 // Global -> local mappings.
4121 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
4122 }
4123}
4124
4125ASTReader::ASTReadResult
4126ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4127 const ModuleFile *ImportedBy,
4128 unsigned ClientLoadCapabilities) {
4129 unsigned Idx = 0;
4130 F.ModuleMapPath = ReadPath(F, Record, Idx);
4131
4132 // Try to resolve ModuleName in the current header search context and
4133 // verify that it is found in the same module map file as we saved. If the
4134 // top-level AST file is a main file, skip this check because there is no
4135 // usable header search context.
4136 assert(!F.ModuleName.empty() &&
4137 "MODULE_NAME should come before MODULE_MAP_FILE");
4138 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4139 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4140 // An implicitly-loaded module file should have its module listed in some
4141 // module map file that we've already loaded.
4142 Module *M =
4143 PP.getHeaderSearchInfo().lookupModule(ModuleName: F.ModuleName, ImportLoc: F.ImportLoc);
4144 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4145 OptionalFileEntryRef ModMap =
4146 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4147 // Don't emit module relocation error if we have -fno-validate-pch
4148 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4149 DisableValidationForModuleKind::Module) &&
4150 !ModMap) {
4151 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4152 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4153 // This module was defined by an imported (explicit) module.
4154 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
4155 << ASTFE->getName();
4156 } else {
4157 // This module was built with a different module map.
4158 Diag(diag::err_imported_module_not_found)
4159 << F.ModuleName << F.FileName
4160 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4161 << !ImportedBy;
4162 // In case it was imported by a PCH, there's a chance the user is
4163 // just missing to include the search path to the directory containing
4164 // the modulemap.
4165 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4166 Diag(diag::note_imported_by_pch_module_not_found)
4167 << llvm::sys::path::parent_path(F.ModuleMapPath);
4168 }
4169 }
4170 return OutOfDate;
4171 }
4172
4173 assert(M && M->Name == F.ModuleName && "found module with different name");
4174
4175 // Check the primary module map file.
4176 auto StoredModMap = FileMgr.getFile(Filename: F.ModuleMapPath);
4177 if (!StoredModMap || *StoredModMap != ModMap) {
4178 assert(ModMap && "found module is missing module map file");
4179 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4180 "top-level import should be verified");
4181 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4182 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4183 Diag(diag::err_imported_module_modmap_changed)
4184 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4185 << ModMap->getName() << F.ModuleMapPath << NotImported;
4186 return OutOfDate;
4187 }
4188
4189 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4190 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4191 // FIXME: we should use input files rather than storing names.
4192 std::string Filename = ReadPath(F, Record, Idx);
4193 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4194 if (!SF) {
4195 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4196 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4197 return OutOfDate;
4198 }
4199 AdditionalStoredMaps.insert(V: *SF);
4200 }
4201
4202 // Check any additional module map files (e.g. module.private.modulemap)
4203 // that are not in the pcm.
4204 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4205 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4206 // Remove files that match
4207 // Note: SmallPtrSet::erase is really remove
4208 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4209 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4210 Diag(diag::err_module_different_modmap)
4211 << F.ModuleName << /*new*/0 << ModMap.getName();
4212 return OutOfDate;
4213 }
4214 }
4215 }
4216
4217 // Check any additional module map files that are in the pcm, but not
4218 // found in header search. Cases that match are already removed.
4219 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4220 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4221 Diag(diag::err_module_different_modmap)
4222 << F.ModuleName << /*not new*/1 << ModMap.getName();
4223 return OutOfDate;
4224 }
4225 }
4226
4227 if (Listener)
4228 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4229 return Success;
4230}
4231
4232/// Move the given method to the back of the global list of methods.
4233static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4234 // Find the entry for this selector in the method pool.
4235 Sema::GlobalMethodPool::iterator Known
4236 = S.MethodPool.find(Sel: Method->getSelector());
4237 if (Known == S.MethodPool.end())
4238 return;
4239
4240 // Retrieve the appropriate method list.
4241 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4242 : Known->second.second;
4243 bool Found = false;
4244 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4245 if (!Found) {
4246 if (List->getMethod() == Method) {
4247 Found = true;
4248 } else {
4249 // Keep searching.
4250 continue;
4251 }
4252 }
4253
4254 if (List->getNext())
4255 List->setMethod(List->getNext()->getMethod());
4256 else
4257 List->setMethod(Method);
4258 }
4259}
4260
4261void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4262 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4263 for (Decl *D : Names) {
4264 bool wasHidden = !D->isUnconditionallyVisible();
4265 D->setVisibleDespiteOwningModule();
4266
4267 if (wasHidden && SemaObj) {
4268 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4269 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4270 }
4271 }
4272 }
4273}
4274
4275void ASTReader::makeModuleVisible(Module *Mod,
4276 Module::NameVisibilityKind NameVisibility,
4277 SourceLocation ImportLoc) {
4278 llvm::SmallPtrSet<Module *, 4> Visited;
4279 SmallVector<Module *, 4> Stack;
4280 Stack.push_back(Elt: Mod);
4281 while (!Stack.empty()) {
4282 Mod = Stack.pop_back_val();
4283
4284 if (NameVisibility <= Mod->NameVisibility) {
4285 // This module already has this level of visibility (or greater), so
4286 // there is nothing more to do.
4287 continue;
4288 }
4289
4290 if (Mod->isUnimportable()) {
4291 // Modules that aren't importable cannot be made visible.
4292 continue;
4293 }
4294
4295 // Update the module's name visibility.
4296 Mod->NameVisibility = NameVisibility;
4297
4298 // If we've already deserialized any names from this module,
4299 // mark them as visible.
4300 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4301 if (Hidden != HiddenNamesMap.end()) {
4302 auto HiddenNames = std::move(*Hidden);
4303 HiddenNamesMap.erase(I: Hidden);
4304 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4305 assert(!HiddenNamesMap.contains(Mod) &&
4306 "making names visible added hidden names");
4307 }
4308
4309 // Push any exported modules onto the stack to be marked as visible.
4310 SmallVector<Module *, 16> Exports;
4311 Mod->getExportedModules(Exported&: Exports);
4312 for (SmallVectorImpl<Module *>::iterator
4313 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4314 Module *Exported = *I;
4315 if (Visited.insert(Ptr: Exported).second)
4316 Stack.push_back(Elt: Exported);
4317 }
4318 }
4319}
4320
4321/// We've merged the definition \p MergedDef into the existing definition
4322/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4323/// visible.
4324void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4325 NamedDecl *MergedDef) {
4326 if (!Def->isUnconditionallyVisible()) {
4327 // If MergedDef is visible or becomes visible, make the definition visible.
4328 if (MergedDef->isUnconditionallyVisible())
4329 Def->setVisibleDespiteOwningModule();
4330 else {
4331 getContext().mergeDefinitionIntoModule(
4332 ND: Def, M: MergedDef->getImportedOwningModule(),
4333 /*NotifyListeners*/ false);
4334 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4335 }
4336 }
4337}
4338
4339bool ASTReader::loadGlobalIndex() {
4340 if (GlobalIndex)
4341 return false;
4342
4343 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4344 !PP.getLangOpts().Modules)
4345 return true;
4346
4347 // Try to load the global index.
4348 TriedLoadingGlobalIndex = true;
4349 StringRef ModuleCachePath
4350 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4351 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4352 GlobalModuleIndex::readIndex(Path: ModuleCachePath);
4353 if (llvm::Error Err = std::move(Result.second)) {
4354 assert(!Result.first);
4355 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4356 return true;
4357 }
4358
4359 GlobalIndex.reset(p: Result.first);
4360 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4361 return false;
4362}
4363
4364bool ASTReader::isGlobalIndexUnavailable() const {
4365 return PP.getLangOpts().Modules && UseGlobalIndex &&
4366 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4367}
4368
4369static void updateModuleTimestamp(ModuleFile &MF) {
4370 // Overwrite the timestamp file contents so that file's mtime changes.
4371 std::string TimestampFilename = MF.getTimestampFilename();
4372 std::error_code EC;
4373 llvm::raw_fd_ostream OS(TimestampFilename, EC,
4374 llvm::sys::fs::OF_TextWithCRLF);
4375 if (EC)
4376 return;
4377 OS << "Timestamp file\n";
4378 OS.close();
4379 OS.clear_error(); // Avoid triggering a fatal error.
4380}
4381
4382/// Given a cursor at the start of an AST file, scan ahead and drop the
4383/// cursor into the start of the given block ID, returning false on success and
4384/// true on failure.
4385static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4386 while (true) {
4387 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4388 if (!MaybeEntry) {
4389 // FIXME this drops errors on the floor.
4390 consumeError(Err: MaybeEntry.takeError());
4391 return true;
4392 }
4393 llvm::BitstreamEntry Entry = MaybeEntry.get();
4394
4395 switch (Entry.Kind) {
4396 case llvm::BitstreamEntry::Error:
4397 case llvm::BitstreamEntry::EndBlock:
4398 return true;
4399
4400 case llvm::BitstreamEntry::Record:
4401 // Ignore top-level records.
4402 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4403 break;
4404 else {
4405 // FIXME this drops errors on the floor.
4406 consumeError(Err: Skipped.takeError());
4407 return true;
4408 }
4409
4410 case llvm::BitstreamEntry::SubBlock:
4411 if (Entry.ID == BlockID) {
4412 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4413 // FIXME this drops the error on the floor.
4414 consumeError(Err: std::move(Err));
4415 return true;
4416 }
4417 // Found it!
4418 return false;
4419 }
4420
4421 if (llvm::Error Err = Cursor.SkipBlock()) {
4422 // FIXME this drops the error on the floor.
4423 consumeError(Err: std::move(Err));
4424 return true;
4425 }
4426 }
4427 }
4428}
4429
4430ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4431 SourceLocation ImportLoc,
4432 unsigned ClientLoadCapabilities,
4433 ModuleFile **NewLoadedModuleFile) {
4434 llvm::TimeTraceScope scope("ReadAST", FileName);
4435
4436 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4437 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4438 CurrentDeserializingModuleKind, Type);
4439
4440 // Defer any pending actions until we get to the end of reading the AST file.
4441 Deserializing AnASTFile(this);
4442
4443 // Bump the generation number.
4444 unsigned PreviousGeneration = 0;
4445 if (ContextObj)
4446 PreviousGeneration = incrementGeneration(C&: *ContextObj);
4447
4448 unsigned NumModules = ModuleMgr.size();
4449 SmallVector<ImportedModule, 4> Loaded;
4450 if (ASTReadResult ReadResult =
4451 ReadASTCore(FileName, Type, ImportLoc,
4452 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
4453 ClientLoadCapabilities)) {
4454 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
4455
4456 // If we find that any modules are unusable, the global index is going
4457 // to be out-of-date. Just remove it.
4458 GlobalIndex.reset();
4459 ModuleMgr.setGlobalIndex(nullptr);
4460 return ReadResult;
4461 }
4462
4463 if (NewLoadedModuleFile && !Loaded.empty())
4464 *NewLoadedModuleFile = Loaded.back().Mod;
4465
4466 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4467 // remove modules from this point. Various fields are updated during reading
4468 // the AST block and removing the modules would result in dangling pointers.
4469 // They are generally only incidentally dereferenced, ie. a binary search
4470 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4471 // be dereferenced but it wouldn't actually be used.
4472
4473 // Load the AST blocks of all of the modules that we loaded. We can still
4474 // hit errors parsing the ASTs at this point.
4475 for (ImportedModule &M : Loaded) {
4476 ModuleFile &F = *M.Mod;
4477 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4478
4479 // Read the AST block.
4480 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4481 Error(Err: std::move(Err));
4482 return Failure;
4483 }
4484
4485 // The AST block should always have a definition for the main module.
4486 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4487 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4488 return Failure;
4489 }
4490
4491 // Read the extension blocks.
4492 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
4493 if (llvm::Error Err = ReadExtensionBlock(F)) {
4494 Error(Err: std::move(Err));
4495 return Failure;
4496 }
4497 }
4498
4499 // Once read, set the ModuleFile bit base offset and update the size in
4500 // bits of all files we've seen.
4501 F.GlobalBitOffset = TotalModulesSizeInBits;
4502 TotalModulesSizeInBits += F.SizeInBits;
4503 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
4504 }
4505
4506 // Preload source locations and interesting indentifiers.
4507 for (ImportedModule &M : Loaded) {
4508 ModuleFile &F = *M.Mod;
4509
4510 // Map the original source file ID into the ID space of the current
4511 // compilation.
4512 if (F.OriginalSourceFileID.isValid())
4513 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
4514
4515 for (auto Offset : F.PreloadIdentifierOffsets) {
4516 const unsigned char *Data = F.IdentifierTableData + Offset;
4517
4518 ASTIdentifierLookupTrait Trait(*this, F);
4519 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
4520 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
4521
4522 IdentifierInfo *II;
4523 if (!PP.getLangOpts().CPlusPlus) {
4524 // Identifiers present in both the module file and the importing
4525 // instance are marked out-of-date so that they can be deserialized
4526 // on next use via ASTReader::updateOutOfDateIdentifier().
4527 // Identifiers present in the module file but not in the importing
4528 // instance are ignored for now, preventing growth of the identifier
4529 // table. They will be deserialized on first use via ASTReader::get().
4530 auto It = PP.getIdentifierTable().find(Key);
4531 if (It == PP.getIdentifierTable().end())
4532 continue;
4533 II = It->second;
4534 } else {
4535 // With C++ modules, not many identifiers are considered interesting.
4536 // All identifiers in the module file can be placed into the identifier
4537 // table of the importing instance and marked as out-of-date. This makes
4538 // ASTReader::get() a no-op, and deserialization will take place on
4539 // first/next use via ASTReader::updateOutOfDateIdentifier().
4540 II = &PP.getIdentifierTable().getOwn(Name: Key);
4541 }
4542
4543 II->setOutOfDate(true);
4544
4545 // Mark this identifier as being from an AST file so that we can track
4546 // whether we need to serialize it.
4547 markIdentifierFromAST(Reader&: *this, II&: *II);
4548
4549 // Associate the ID with the identifier so that the writer can reuse it.
4550 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
4551 SetIdentifierInfo(ID, II);
4552 }
4553 }
4554
4555 // Builtins and library builtins have already been initialized. Mark all
4556 // identifiers as out-of-date, so that they are deserialized on first use.
4557 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4558 for (auto &Id : PP.getIdentifierTable())
4559 Id.second->setOutOfDate(true);
4560
4561 // Mark selectors as out of date.
4562 for (const auto &Sel : SelectorGeneration)
4563 SelectorOutOfDate[Sel.first] = true;
4564
4565 // Setup the import locations and notify the module manager that we've
4566 // committed to these module files.
4567 for (ImportedModule &M : Loaded) {
4568 ModuleFile &F = *M.Mod;
4569
4570 ModuleMgr.moduleFileAccepted(MF: &F);
4571
4572 // Set the import location.
4573 F.DirectImportLoc = ImportLoc;
4574 // FIXME: We assume that locations from PCH / preamble do not need
4575 // any translation.
4576 if (!M.ImportedBy)
4577 F.ImportLoc = M.ImportLoc;
4578 else
4579 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
4580 }
4581
4582 // Resolve any unresolved module exports.
4583 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4584 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4585 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: *Unresolved.File,LocalID: Unresolved.ID);
4586 Module *ResolvedMod = getSubmodule(GlobalID);
4587
4588 switch (Unresolved.Kind) {
4589 case UnresolvedModuleRef::Conflict:
4590 if (ResolvedMod) {
4591 Module::Conflict Conflict;
4592 Conflict.Other = ResolvedMod;
4593 Conflict.Message = Unresolved.String.str();
4594 Unresolved.Mod->Conflicts.push_back(x: Conflict);
4595 }
4596 continue;
4597
4598 case UnresolvedModuleRef::Import:
4599 if (ResolvedMod)
4600 Unresolved.Mod->Imports.insert(X: ResolvedMod);
4601 continue;
4602
4603 case UnresolvedModuleRef::Affecting:
4604 if (ResolvedMod)
4605 Unresolved.Mod->AffectingClangModules.insert(X: ResolvedMod);
4606 continue;
4607
4608 case UnresolvedModuleRef::Export:
4609 if (ResolvedMod || Unresolved.IsWildcard)
4610 Unresolved.Mod->Exports.push_back(
4611 Elt: Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4612 continue;
4613 }
4614 }
4615 UnresolvedModuleRefs.clear();
4616
4617 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4618 // Might be unnecessary as use declarations are only used to build the
4619 // module itself.
4620
4621 if (ContextObj)
4622 InitializeContext();
4623
4624 if (SemaObj)
4625 UpdateSema();
4626
4627 if (DeserializationListener)
4628 DeserializationListener->ReaderInitialized(Reader: this);
4629
4630 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4631 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4632 // If this AST file is a precompiled preamble, then set the
4633 // preamble file ID of the source manager to the file source file
4634 // from which the preamble was built.
4635 if (Type == MK_Preamble) {
4636 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4637 } else if (Type == MK_MainFile) {
4638 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4639 }
4640 }
4641
4642 // For any Objective-C class definitions we have already loaded, make sure
4643 // that we load any additional categories.
4644 if (ContextObj) {
4645 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4646 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
4647 D: ObjCClassesLoaded[I],
4648 PreviousGeneration);
4649 }
4650 }
4651
4652 HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
4653 if (HSOpts.ModulesValidateOncePerBuildSession) {
4654 // Now we are certain that the module and all modules it depends on are
4655 // up-to-date. For implicitly-built module files, ensure the corresponding
4656 // timestamp files are up-to-date in this build session.
4657 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4658 ImportedModule &M = Loaded[I];
4659 if (M.Mod->Kind == MK_ImplicitModule &&
4660 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4661 updateModuleTimestamp(MF&: *M.Mod);
4662 }
4663 }
4664
4665 return Success;
4666}
4667
4668static ASTFileSignature readASTFileSignature(StringRef PCH);
4669
4670/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4671static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4672 // FIXME checking magic headers is done in other places such as
4673 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4674 // always done the same. Unify it all with a helper.
4675 if (!Stream.canSkipToPos(pos: 4))
4676 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4677 Fmt: "file too small to contain AST file magic");
4678 for (unsigned C : {'C', 'P', 'C', 'H'})
4679 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
4680 if (Res.get() != C)
4681 return llvm::createStringError(
4682 EC: std::errc::illegal_byte_sequence,
4683 Fmt: "file doesn't start with AST file magic");
4684 } else
4685 return Res.takeError();
4686 return llvm::Error::success();
4687}
4688
4689static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4690 switch (Kind) {
4691 case MK_PCH:
4692 return 0; // PCH
4693 case MK_ImplicitModule:
4694 case MK_ExplicitModule:
4695 case MK_PrebuiltModule:
4696 return 1; // module
4697 case MK_MainFile:
4698 case MK_Preamble:
4699 return 2; // main source file
4700 }
4701 llvm_unreachable("unknown module kind");
4702}
4703
4704ASTReader::ASTReadResult
4705ASTReader::ReadASTCore(StringRef FileName,
4706 ModuleKind Type,
4707 SourceLocation ImportLoc,
4708 ModuleFile *ImportedBy,
4709 SmallVectorImpl<ImportedModule> &Loaded,
4710 off_t ExpectedSize, time_t ExpectedModTime,
4711 ASTFileSignature ExpectedSignature,
4712 unsigned ClientLoadCapabilities) {
4713 ModuleFile *M;
4714 std::string ErrorStr;
4715 ModuleManager::AddModuleResult AddResult
4716 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4717 Generation: getGeneration(), ExpectedSize, ExpectedModTime,
4718 ExpectedSignature, ReadSignature: readASTFileSignature,
4719 Module&: M, ErrorStr);
4720
4721 switch (AddResult) {
4722 case ModuleManager::AlreadyLoaded:
4723 Diag(diag::remark_module_import)
4724 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4725 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4726 return Success;
4727
4728 case ModuleManager::NewlyLoaded:
4729 // Load module file below.
4730 break;
4731
4732 case ModuleManager::Missing:
4733 // The module file was missing; if the client can handle that, return
4734 // it.
4735 if (ClientLoadCapabilities & ARR_Missing)
4736 return Missing;
4737
4738 // Otherwise, return an error.
4739 Diag(diag::err_ast_file_not_found)
4740 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4741 << ErrorStr;
4742 return Failure;
4743
4744 case ModuleManager::OutOfDate:
4745 // We couldn't load the module file because it is out-of-date. If the
4746 // client can handle out-of-date, return it.
4747 if (ClientLoadCapabilities & ARR_OutOfDate)
4748 return OutOfDate;
4749
4750 // Otherwise, return an error.
4751 Diag(diag::err_ast_file_out_of_date)
4752 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4753 << ErrorStr;
4754 return Failure;
4755 }
4756
4757 assert(M && "Missing module file");
4758
4759 bool ShouldFinalizePCM = false;
4760 auto FinalizeOrDropPCM = llvm::make_scope_exit(F: [&]() {
4761 auto &MC = getModuleManager().getModuleCache();
4762 if (ShouldFinalizePCM)
4763 MC.finalizePCM(Filename: FileName);
4764 else
4765 MC.tryToDropPCM(Filename: FileName);
4766 });
4767 ModuleFile &F = *M;
4768 BitstreamCursor &Stream = F.Stream;
4769 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
4770 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4771
4772 // Sniff for the signature.
4773 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4774 Diag(diag::err_ast_file_invalid)
4775 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4776 return Failure;
4777 }
4778
4779 // This is used for compatibility with older PCH formats.
4780 bool HaveReadControlBlock = false;
4781 while (true) {
4782 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4783 if (!MaybeEntry) {
4784 Error(Err: MaybeEntry.takeError());
4785 return Failure;
4786 }
4787 llvm::BitstreamEntry Entry = MaybeEntry.get();
4788
4789 switch (Entry.Kind) {
4790 case llvm::BitstreamEntry::Error:
4791 case llvm::BitstreamEntry::Record:
4792 case llvm::BitstreamEntry::EndBlock:
4793 Error(Msg: "invalid record at top-level of AST file");
4794 return Failure;
4795
4796 case llvm::BitstreamEntry::SubBlock:
4797 break;
4798 }
4799
4800 switch (Entry.ID) {
4801 case CONTROL_BLOCK_ID:
4802 HaveReadControlBlock = true;
4803 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4804 case Success:
4805 // Check that we didn't try to load a non-module AST file as a module.
4806 //
4807 // FIXME: Should we also perform the converse check? Loading a module as
4808 // a PCH file sort of works, but it's a bit wonky.
4809 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4810 Type == MK_PrebuiltModule) &&
4811 F.ModuleName.empty()) {
4812 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4813 if (Result != OutOfDate ||
4814 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4815 Diag(diag::err_module_file_not_module) << FileName;
4816 return Result;
4817 }
4818 break;
4819
4820 case Failure: return Failure;
4821 case Missing: return Missing;
4822 case OutOfDate: return OutOfDate;
4823 case VersionMismatch: return VersionMismatch;
4824 case ConfigurationMismatch: return ConfigurationMismatch;
4825 case HadErrors: return HadErrors;
4826 }
4827 break;
4828
4829 case AST_BLOCK_ID:
4830 if (!HaveReadControlBlock) {
4831 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4832 Diag(diag::err_pch_version_too_old);
4833 return VersionMismatch;
4834 }
4835
4836 // Record that we've loaded this module.
4837 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
4838 ShouldFinalizePCM = true;
4839 return Success;
4840
4841 default:
4842 if (llvm::Error Err = Stream.SkipBlock()) {
4843 Error(Err: std::move(Err));
4844 return Failure;
4845 }
4846 break;
4847 }
4848 }
4849
4850 llvm_unreachable("unexpected break; expected return");
4851}
4852
4853ASTReader::ASTReadResult
4854ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4855 unsigned ClientLoadCapabilities) {
4856 const HeaderSearchOptions &HSOpts =
4857 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4858 bool AllowCompatibleConfigurationMismatch =
4859 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4860 bool DisableValidation = shouldDisableValidationForFile(M: F);
4861
4862 ASTReadResult Result = readUnhashedControlBlockImpl(
4863 F: &F, StreamData: F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4864 Listener: Listener.get(),
4865 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4866
4867 // If F was directly imported by another module, it's implicitly validated by
4868 // the importing module.
4869 if (DisableValidation || WasImportedBy ||
4870 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4871 return Success;
4872
4873 if (Result == Failure) {
4874 Error(Msg: "malformed block record in AST file");
4875 return Failure;
4876 }
4877
4878 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4879 // If this module has already been finalized in the ModuleCache, we're stuck
4880 // with it; we can only load a single version of each module.
4881 //
4882 // This can happen when a module is imported in two contexts: in one, as a
4883 // user module; in another, as a system module (due to an import from
4884 // another module marked with the [system] flag). It usually indicates a
4885 // bug in the module map: this module should also be marked with [system].
4886 //
4887 // If -Wno-system-headers (the default), and the first import is as a
4888 // system module, then validation will fail during the as-user import,
4889 // since -Werror flags won't have been validated. However, it's reasonable
4890 // to treat this consistently as a system module.
4891 //
4892 // If -Wsystem-headers, the PCM on disk was built with
4893 // -Wno-system-headers, and the first import is as a user module, then
4894 // validation will fail during the as-system import since the PCM on disk
4895 // doesn't guarantee that -Werror was respected. However, the -Werror
4896 // flags were checked during the initial as-user import.
4897 if (getModuleManager().getModuleCache().isPCMFinal(Filename: F.FileName)) {
4898 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4899 return Success;
4900 }
4901 }
4902
4903 return Result;
4904}
4905
4906ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4907 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4908 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4909 bool ValidateDiagnosticOptions) {
4910 // Initialize a stream.
4911 BitstreamCursor Stream(StreamData);
4912
4913 // Sniff for the signature.
4914 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4915 // FIXME this drops the error on the floor.
4916 consumeError(Err: std::move(Err));
4917 return Failure;
4918 }
4919
4920 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4921 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
4922 return Failure;
4923
4924 // Read all of the records in the options block.
4925 RecordData Record;
4926 ASTReadResult Result = Success;
4927 while (true) {
4928 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4929 if (!MaybeEntry) {
4930 // FIXME this drops the error on the floor.
4931 consumeError(Err: MaybeEntry.takeError());
4932 return Failure;
4933 }
4934 llvm::BitstreamEntry Entry = MaybeEntry.get();
4935
4936 switch (Entry.Kind) {
4937 case llvm::BitstreamEntry::Error:
4938 case llvm::BitstreamEntry::SubBlock:
4939 return Failure;
4940
4941 case llvm::BitstreamEntry::EndBlock:
4942 return Result;
4943
4944 case llvm::BitstreamEntry::Record:
4945 // The interesting case.
4946 break;
4947 }
4948
4949 // Read and process a record.
4950 Record.clear();
4951 StringRef Blob;
4952 Expected<unsigned> MaybeRecordType =
4953 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
4954 if (!MaybeRecordType) {
4955 // FIXME this drops the error.
4956 return Failure;
4957 }
4958 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4959 case SIGNATURE:
4960 if (F) {
4961 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
4962 assert(F->Signature != ASTFileSignature::createDummy() &&
4963 "Dummy AST file signature not backpatched in ASTWriter.");
4964 }
4965 break;
4966 case AST_BLOCK_HASH:
4967 if (F) {
4968 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
4969 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
4970 "Dummy AST block hash not backpatched in ASTWriter.");
4971 }
4972 break;
4973 case DIAGNOSTIC_OPTIONS: {
4974 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4975 if (Listener && ValidateDiagnosticOptions &&
4976 !AllowCompatibleConfigurationMismatch &&
4977 ParseDiagnosticOptions(Record, Complain, Listener&: *Listener))
4978 Result = OutOfDate; // Don't return early. Read the signature.
4979 break;
4980 }
4981 case HEADER_SEARCH_PATHS: {
4982 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
4983 if (Listener && !AllowCompatibleConfigurationMismatch &&
4984 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
4985 Result = ConfigurationMismatch;
4986 break;
4987 }
4988 case DIAG_PRAGMA_MAPPINGS:
4989 if (!F)
4990 break;
4991 if (F->PragmaDiagMappings.empty())
4992 F->PragmaDiagMappings.swap(RHS&: Record);
4993 else
4994 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
4995 From: Record.begin(), To: Record.end());
4996 break;
4997 case HEADER_SEARCH_ENTRY_USAGE:
4998 if (F)
4999 F->SearchPathUsage = ReadBitVector(Record, Blob);
5000 break;
5001 case VFS_USAGE:
5002 if (F)
5003 F->VFSUsage = ReadBitVector(Record, Blob);
5004 break;
5005 }
5006 }
5007}
5008
5009/// Parse a record and blob containing module file extension metadata.
5010static bool parseModuleFileExtensionMetadata(
5011 const SmallVectorImpl<uint64_t> &Record,
5012 StringRef Blob,
5013 ModuleFileExtensionMetadata &Metadata) {
5014 if (Record.size() < 4) return true;
5015
5016 Metadata.MajorVersion = Record[0];
5017 Metadata.MinorVersion = Record[1];
5018
5019 unsigned BlockNameLen = Record[2];
5020 unsigned UserInfoLen = Record[3];
5021
5022 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5023
5024 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5025 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5026 Blob.data() + BlockNameLen + UserInfoLen);
5027 return false;
5028}
5029
5030llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5031 BitstreamCursor &Stream = F.Stream;
5032
5033 RecordData Record;
5034 while (true) {
5035 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5036 if (!MaybeEntry)
5037 return MaybeEntry.takeError();
5038 llvm::BitstreamEntry Entry = MaybeEntry.get();
5039
5040 switch (Entry.Kind) {
5041 case llvm::BitstreamEntry::SubBlock:
5042 if (llvm::Error Err = Stream.SkipBlock())
5043 return Err;
5044 continue;
5045 case llvm::BitstreamEntry::EndBlock:
5046 return llvm::Error::success();
5047 case llvm::BitstreamEntry::Error:
5048 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5049 Fmt: "malformed block record in AST file");
5050 case llvm::BitstreamEntry::Record:
5051 break;
5052 }
5053
5054 Record.clear();
5055 StringRef Blob;
5056 Expected<unsigned> MaybeRecCode =
5057 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5058 if (!MaybeRecCode)
5059 return MaybeRecCode.takeError();
5060 switch (MaybeRecCode.get()) {
5061 case EXTENSION_METADATA: {
5062 ModuleFileExtensionMetadata Metadata;
5063 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5064 return llvm::createStringError(
5065 EC: std::errc::illegal_byte_sequence,
5066 Fmt: "malformed EXTENSION_METADATA in AST file");
5067
5068 // Find a module file extension with this block name.
5069 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5070 if (Known == ModuleFileExtensions.end()) break;
5071
5072 // Form a reader.
5073 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5074 Mod&: F, Stream)) {
5075 F.ExtensionReaders.push_back(x: std::move(Reader));
5076 }
5077
5078 break;
5079 }
5080 }
5081 }
5082
5083 return llvm::Error::success();
5084}
5085
5086void ASTReader::InitializeContext() {
5087 assert(ContextObj && "no context to initialize");
5088 ASTContext &Context = *ContextObj;
5089
5090 // If there's a listener, notify them that we "read" the translation unit.
5091 if (DeserializationListener)
5092 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
5093 Context.getTranslationUnitDecl());
5094
5095 // FIXME: Find a better way to deal with collisions between these
5096 // built-in types. Right now, we just ignore the problem.
5097
5098 // Load the special types.
5099 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5100 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5101 if (!Context.CFConstantStringTypeDecl)
5102 Context.setCFConstantStringType(GetType(ID: String));
5103 }
5104
5105 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5106 QualType FileType = GetType(ID: File);
5107 if (FileType.isNull()) {
5108 Error(Msg: "FILE type is NULL");
5109 return;
5110 }
5111
5112 if (!Context.FILEDecl) {
5113 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5114 Context.setFILEDecl(Typedef->getDecl());
5115 else {
5116 const TagType *Tag = FileType->getAs<TagType>();
5117 if (!Tag) {
5118 Error(Msg: "Invalid FILE type in AST file");
5119 return;
5120 }
5121 Context.setFILEDecl(Tag->getDecl());
5122 }
5123 }
5124 }
5125
5126 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5127 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5128 if (Jmp_bufType.isNull()) {
5129 Error(Msg: "jmp_buf type is NULL");
5130 return;
5131 }
5132
5133 if (!Context.jmp_bufDecl) {
5134 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5135 Context.setjmp_bufDecl(Typedef->getDecl());
5136 else {
5137 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5138 if (!Tag) {
5139 Error(Msg: "Invalid jmp_buf type in AST file");
5140 return;
5141 }
5142 Context.setjmp_bufDecl(Tag->getDecl());
5143 }
5144 }
5145 }
5146
5147 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5148 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5149 if (Sigjmp_bufType.isNull()) {
5150 Error(Msg: "sigjmp_buf type is NULL");
5151 return;
5152 }
5153
5154 if (!Context.sigjmp_bufDecl) {
5155 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5156 Context.setsigjmp_bufDecl(Typedef->getDecl());
5157 else {
5158 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5159 assert(Tag && "Invalid sigjmp_buf type in AST file");
5160 Context.setsigjmp_bufDecl(Tag->getDecl());
5161 }
5162 }
5163 }
5164
5165 if (unsigned ObjCIdRedef
5166 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5167 if (Context.ObjCIdRedefinitionType.isNull())
5168 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5169 }
5170
5171 if (unsigned ObjCClassRedef
5172 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5173 if (Context.ObjCClassRedefinitionType.isNull())
5174 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5175 }
5176
5177 if (unsigned ObjCSelRedef
5178 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5179 if (Context.ObjCSelRedefinitionType.isNull())
5180 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5181 }
5182
5183 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5184 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5185 if (Ucontext_tType.isNull()) {
5186 Error(Msg: "ucontext_t type is NULL");
5187 return;
5188 }
5189
5190 if (!Context.ucontext_tDecl) {
5191 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5192 Context.setucontext_tDecl(Typedef->getDecl());
5193 else {
5194 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5195 assert(Tag && "Invalid ucontext_t type in AST file");
5196 Context.setucontext_tDecl(Tag->getDecl());
5197 }
5198 }
5199 }
5200 }
5201
5202 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5203
5204 // If there were any CUDA special declarations, deserialize them.
5205 if (!CUDASpecialDeclRefs.empty()) {
5206 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5207 Context.setcudaConfigureCallDecl(
5208 cast<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5209 }
5210
5211 // Re-export any modules that were imported by a non-module AST file.
5212 // FIXME: This does not make macro-only imports visible again.
5213 for (auto &Import : PendingImportedModules) {
5214 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5215 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5216 /*ImportLoc=*/Import.ImportLoc);
5217 if (Import.ImportLoc.isValid())
5218 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5219 // This updates visibility for Preprocessor only. For Sema, which can be
5220 // nullptr here, we do the same later, in UpdateSema().
5221 }
5222 }
5223
5224 // Hand off these modules to Sema.
5225 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5226 PendingImportedModules.clear();
5227}
5228
5229void ASTReader::finalizeForWriting() {
5230 // Nothing to do for now.
5231}
5232
5233/// Reads and return the signature record from \p PCH's control block, or
5234/// else returns 0.
5235static ASTFileSignature readASTFileSignature(StringRef PCH) {
5236 BitstreamCursor Stream(PCH);
5237 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5238 // FIXME this drops the error on the floor.
5239 consumeError(Err: std::move(Err));
5240 return ASTFileSignature();
5241 }
5242
5243 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5244 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5245 return ASTFileSignature();
5246
5247 // Scan for SIGNATURE inside the diagnostic options block.
5248 ASTReader::RecordData Record;
5249 while (true) {
5250 Expected<llvm::BitstreamEntry> MaybeEntry =
5251 Stream.advanceSkippingSubblocks();
5252 if (!MaybeEntry) {
5253 // FIXME this drops the error on the floor.
5254 consumeError(Err: MaybeEntry.takeError());
5255 return ASTFileSignature();
5256 }
5257 llvm::BitstreamEntry Entry = MaybeEntry.get();
5258
5259 if (Entry.Kind != llvm::BitstreamEntry::Record)
5260 return ASTFileSignature();
5261
5262 Record.clear();
5263 StringRef Blob;
5264 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5265 if (!MaybeRecord) {
5266 // FIXME this drops the error on the floor.
5267 consumeError(Err: MaybeRecord.takeError());
5268 return ASTFileSignature();
5269 }
5270 if (SIGNATURE == MaybeRecord.get()) {
5271 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5272 assert(Signature != ASTFileSignature::createDummy() &&
5273 "Dummy AST file signature not backpatched in ASTWriter.");
5274 return Signature;
5275 }
5276 }
5277}
5278
5279/// Retrieve the name of the original source file name
5280/// directly from the AST file, without actually loading the AST
5281/// file.
5282std::string ASTReader::getOriginalSourceFile(
5283 const std::string &ASTFileName, FileManager &FileMgr,
5284 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5285 // Open the AST file.
5286 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5287 /*RequiresNullTerminator=*/false);
5288 if (!Buffer) {
5289 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5290 << ASTFileName << Buffer.getError().message();
5291 return std::string();
5292 }
5293
5294 // Initialize the stream
5295 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5296
5297 // Sniff for the signature.
5298 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5299 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5300 return std::string();
5301 }
5302
5303 // Scan for the CONTROL_BLOCK_ID block.
5304 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5305 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5306 return std::string();
5307 }
5308
5309 // Scan for ORIGINAL_FILE inside the control block.
5310 RecordData Record;
5311 while (true) {
5312 Expected<llvm::BitstreamEntry> MaybeEntry =
5313 Stream.advanceSkippingSubblocks();
5314 if (!MaybeEntry) {
5315 // FIXME this drops errors on the floor.
5316 consumeError(Err: MaybeEntry.takeError());
5317 return std::string();
5318 }
5319 llvm::BitstreamEntry Entry = MaybeEntry.get();
5320
5321 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5322 return std::string();
5323
5324 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5325 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5326 return std::string();
5327 }
5328
5329 Record.clear();
5330 StringRef Blob;
5331 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5332 if (!MaybeRecord) {
5333 // FIXME this drops the errors on the floor.
5334 consumeError(Err: MaybeRecord.takeError());
5335 return std::string();
5336 }
5337 if (ORIGINAL_FILE == MaybeRecord.get())
5338 return Blob.str();
5339 }
5340}
5341
5342namespace {
5343
5344 class SimplePCHValidator : public ASTReaderListener {
5345 const LangOptions &ExistingLangOpts;
5346 const TargetOptions &ExistingTargetOpts;
5347 const PreprocessorOptions &ExistingPPOpts;
5348 std::string ExistingModuleCachePath;
5349 FileManager &FileMgr;
5350 bool StrictOptionMatches;
5351
5352 public:
5353 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5354 const TargetOptions &ExistingTargetOpts,
5355 const PreprocessorOptions &ExistingPPOpts,
5356 StringRef ExistingModuleCachePath, FileManager &FileMgr,
5357 bool StrictOptionMatches)
5358 : ExistingLangOpts(ExistingLangOpts),
5359 ExistingTargetOpts(ExistingTargetOpts),
5360 ExistingPPOpts(ExistingPPOpts),
5361 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5362 StrictOptionMatches(StrictOptionMatches) {}
5363
5364 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5365 bool AllowCompatibleDifferences) override {
5366 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, Diags: nullptr,
5367 AllowCompatibleDifferences);
5368 }
5369
5370 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5371 bool AllowCompatibleDifferences) override {
5372 return checkTargetOptions(TargetOpts: ExistingTargetOpts, ExistingTargetOpts: TargetOpts, Diags: nullptr,
5373 AllowCompatibleDifferences);
5374 }
5375
5376 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5377 StringRef SpecificModuleCachePath,
5378 bool Complain) override {
5379 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5380 ExistingModuleCachePath, Diags: nullptr,
5381 LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts);
5382 }
5383
5384 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5385 bool ReadMacros, bool Complain,
5386 std::string &SuggestedPredefines) override {
5387 return checkPreprocessorOptions(
5388 PPOpts, ExistingPPOpts, ReadMacros, /*Diags=*/nullptr, FileMgr,
5389 SuggestedPredefines, LangOpts: ExistingLangOpts,
5390 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5391 : OptionValidateContradictions);
5392 }
5393 };
5394
5395} // namespace
5396
5397bool ASTReader::readASTFileControlBlock(
5398 StringRef Filename, FileManager &FileMgr,
5399 const InMemoryModuleCache &ModuleCache,
5400 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5401 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5402 unsigned ClientLoadCapabilities) {
5403 // Open the AST file.
5404 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5405 llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
5406 if (!Buffer) {
5407 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5408 // read again later, but we do not have the context here to determine if it
5409 // is safe to change the result of InMemoryModuleCache::getPCMState().
5410
5411 // FIXME: This allows use of the VFS; we do not allow use of the
5412 // VFS when actually loading a module.
5413 auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5414 if (!BufferOrErr)
5415 return true;
5416 OwnedBuffer = std::move(*BufferOrErr);
5417 Buffer = OwnedBuffer.get();
5418 }
5419
5420 // Initialize the stream
5421 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
5422 BitstreamCursor Stream(Bytes);
5423
5424 // Sniff for the signature.
5425 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5426 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5427 return true;
5428 }
5429
5430 // Scan for the CONTROL_BLOCK_ID block.
5431 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
5432 return true;
5433
5434 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5435 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5436 bool NeedsImports = Listener.needsImportVisitation();
5437 BitstreamCursor InputFilesCursor;
5438 uint64_t InputFilesOffsetBase = 0;
5439
5440 RecordData Record;
5441 std::string ModuleDir;
5442 bool DoneWithControlBlock = false;
5443 while (!DoneWithControlBlock) {
5444 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5445 if (!MaybeEntry) {
5446 // FIXME this drops the error on the floor.
5447 consumeError(Err: MaybeEntry.takeError());
5448 return true;
5449 }
5450 llvm::BitstreamEntry Entry = MaybeEntry.get();
5451
5452 switch (Entry.Kind) {
5453 case llvm::BitstreamEntry::SubBlock: {
5454 switch (Entry.ID) {
5455 case OPTIONS_BLOCK_ID: {
5456 std::string IgnoredSuggestedPredefines;
5457 if (ReadOptionsBlock(Stream, ClientLoadCapabilities,
5458 /*AllowCompatibleConfigurationMismatch*/ false,
5459 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
5460 return true;
5461 break;
5462 }
5463
5464 case INPUT_FILES_BLOCK_ID:
5465 InputFilesCursor = Stream;
5466 if (llvm::Error Err = Stream.SkipBlock()) {
5467 // FIXME this drops the error on the floor.
5468 consumeError(Err: std::move(Err));
5469 return true;
5470 }
5471 if (NeedsInputFiles &&
5472 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
5473 return true;
5474 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5475 break;
5476
5477 default:
5478 if (llvm::Error Err = Stream.SkipBlock()) {
5479 // FIXME this drops the error on the floor.
5480 consumeError(Err: std::move(Err));
5481 return true;
5482 }
5483 break;
5484 }
5485
5486 continue;
5487 }
5488
5489 case llvm::BitstreamEntry::EndBlock:
5490 DoneWithControlBlock = true;
5491 break;
5492
5493 case llvm::BitstreamEntry::Error:
5494 return true;
5495
5496 case llvm::BitstreamEntry::Record:
5497 break;
5498 }
5499
5500 if (DoneWithControlBlock) break;
5501
5502 Record.clear();
5503 StringRef Blob;
5504 Expected<unsigned> MaybeRecCode =
5505 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5506 if (!MaybeRecCode) {
5507 // FIXME this drops the error.
5508 return Failure;
5509 }
5510 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5511 case METADATA:
5512 if (Record[0] != VERSION_MAJOR)
5513 return true;
5514 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
5515 return true;
5516 break;
5517 case MODULE_NAME:
5518 Listener.ReadModuleName(ModuleName: Blob);
5519 break;
5520 case MODULE_DIRECTORY:
5521 ModuleDir = std::string(Blob);
5522 break;
5523 case MODULE_MAP_FILE: {
5524 unsigned Idx = 0;
5525 auto Path = ReadString(Record, Idx);
5526 ResolveImportedPath(Filename&: Path, Prefix: ModuleDir);
5527 Listener.ReadModuleMapFile(ModuleMapPath: Path);
5528 break;
5529 }
5530 case INPUT_FILE_OFFSETS: {
5531 if (!NeedsInputFiles)
5532 break;
5533
5534 unsigned NumInputFiles = Record[0];
5535 unsigned NumUserFiles = Record[1];
5536 const llvm::support::unaligned_uint64_t *InputFileOffs =
5537 (const llvm::support::unaligned_uint64_t *)Blob.data();
5538 for (unsigned I = 0; I != NumInputFiles; ++I) {
5539 // Go find this input file.
5540 bool isSystemFile = I >= NumUserFiles;
5541
5542 if (isSystemFile && !NeedsSystemInputFiles)
5543 break; // the rest are system input files
5544
5545 BitstreamCursor &Cursor = InputFilesCursor;
5546 SavedStreamPosition SavedPosition(Cursor);
5547 if (llvm::Error Err =
5548 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
5549 // FIXME this drops errors on the floor.
5550 consumeError(Err: std::move(Err));
5551 }
5552
5553 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5554 if (!MaybeCode) {
5555 // FIXME this drops errors on the floor.
5556 consumeError(Err: MaybeCode.takeError());
5557 }
5558 unsigned Code = MaybeCode.get();
5559
5560 RecordData Record;
5561 StringRef Blob;
5562 bool shouldContinue = false;
5563 Expected<unsigned> MaybeRecordType =
5564 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
5565 if (!MaybeRecordType) {
5566 // FIXME this drops errors on the floor.
5567 consumeError(Err: MaybeRecordType.takeError());
5568 }
5569 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5570 case INPUT_FILE_HASH:
5571 break;
5572 case INPUT_FILE:
5573 bool Overridden = static_cast<bool>(Record[3]);
5574 std::string Filename = std::string(Blob);
5575 ResolveImportedPath(Filename, Prefix: ModuleDir);
5576 shouldContinue = Listener.visitInputFile(
5577 Filename, isSystem: isSystemFile, isOverridden: Overridden, /*IsExplicitModule*/isExplicitModule: false);
5578 break;
5579 }
5580 if (!shouldContinue)
5581 break;
5582 }
5583 break;
5584 }
5585
5586 case IMPORTS: {
5587 if (!NeedsImports)
5588 break;
5589
5590 unsigned Idx = 0, N = Record.size();
5591 while (Idx < N) {
5592 // Read information about the AST file.
5593
5594 // Skip Kind
5595 Idx++;
5596 bool IsStandardCXXModule = Record[Idx++];
5597
5598 // Skip ImportLoc
5599 Idx++;
5600
5601 // In C++20 Modules, we don't record the path to imported
5602 // modules in the BMI files.
5603 if (IsStandardCXXModule) {
5604 std::string ModuleName = ReadString(Record, Idx);
5605 Listener.visitImport(ModuleName, /*Filename=*/"");
5606 continue;
5607 }
5608
5609 // Skip Size, ModTime and Signature
5610 Idx += 1 + 1 + ASTFileSignature::size;
5611 std::string ModuleName = ReadString(Record, Idx);
5612 std::string Filename = ReadString(Record, Idx);
5613 ResolveImportedPath(Filename, Prefix: ModuleDir);
5614 Listener.visitImport(ModuleName, Filename);
5615 }
5616 break;
5617 }
5618
5619 default:
5620 // No other validation to perform.
5621 break;
5622 }
5623 }
5624
5625 // Look for module file extension blocks, if requested.
5626 if (FindModuleFileExtensions) {
5627 BitstreamCursor SavedStream = Stream;
5628 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
5629 bool DoneWithExtensionBlock = false;
5630 while (!DoneWithExtensionBlock) {
5631 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5632 if (!MaybeEntry) {
5633 // FIXME this drops the error.
5634 return true;
5635 }
5636 llvm::BitstreamEntry Entry = MaybeEntry.get();
5637
5638 switch (Entry.Kind) {
5639 case llvm::BitstreamEntry::SubBlock:
5640 if (llvm::Error Err = Stream.SkipBlock()) {
5641 // FIXME this drops the error on the floor.
5642 consumeError(Err: std::move(Err));
5643 return true;
5644 }
5645 continue;
5646
5647 case llvm::BitstreamEntry::EndBlock:
5648 DoneWithExtensionBlock = true;
5649 continue;
5650
5651 case llvm::BitstreamEntry::Error:
5652 return true;
5653
5654 case llvm::BitstreamEntry::Record:
5655 break;
5656 }
5657
5658 Record.clear();
5659 StringRef Blob;
5660 Expected<unsigned> MaybeRecCode =
5661 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5662 if (!MaybeRecCode) {
5663 // FIXME this drops the error.
5664 return true;
5665 }
5666 switch (MaybeRecCode.get()) {
5667 case EXTENSION_METADATA: {
5668 ModuleFileExtensionMetadata Metadata;
5669 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5670 return true;
5671
5672 Listener.readModuleFileExtension(Metadata);
5673 break;
5674 }
5675 }
5676 }
5677 }
5678 Stream = SavedStream;
5679 }
5680
5681 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5682 if (readUnhashedControlBlockImpl(
5683 F: nullptr, StreamData: Bytes, ClientLoadCapabilities,
5684 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
5685 ValidateDiagnosticOptions) != Success)
5686 return true;
5687
5688 return false;
5689}
5690
5691bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5692 const InMemoryModuleCache &ModuleCache,
5693 const PCHContainerReader &PCHContainerRdr,
5694 const LangOptions &LangOpts,
5695 const TargetOptions &TargetOpts,
5696 const PreprocessorOptions &PPOpts,
5697 StringRef ExistingModuleCachePath,
5698 bool RequireStrictOptionMatches) {
5699 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5700 ExistingModuleCachePath, FileMgr,
5701 RequireStrictOptionMatches);
5702 return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
5703 PCHContainerRdr,
5704 /*FindModuleFileExtensions=*/false, Listener&: validator,
5705 /*ValidateDiagnosticOptions=*/true);
5706}
5707
5708llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5709 unsigned ClientLoadCapabilities) {
5710 // Enter the submodule block.
5711 if (llvm::Error Err = F.Stream.EnterSubBlock(BlockID: SUBMODULE_BLOCK_ID))
5712 return Err;
5713
5714 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5715 bool First = true;
5716 Module *CurrentModule = nullptr;
5717 RecordData Record;
5718 while (true) {
5719 Expected<llvm::BitstreamEntry> MaybeEntry =
5720 F.Stream.advanceSkippingSubblocks();
5721 if (!MaybeEntry)
5722 return MaybeEntry.takeError();
5723 llvm::BitstreamEntry Entry = MaybeEntry.get();
5724
5725 switch (Entry.Kind) {
5726 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5727 case llvm::BitstreamEntry::Error:
5728 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5729 Fmt: "malformed block record in AST file");
5730 case llvm::BitstreamEntry::EndBlock:
5731 return llvm::Error::success();
5732 case llvm::BitstreamEntry::Record:
5733 // The interesting case.
5734 break;
5735 }
5736
5737 // Read a record.
5738 StringRef Blob;
5739 Record.clear();
5740 Expected<unsigned> MaybeKind = F.Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5741 if (!MaybeKind)
5742 return MaybeKind.takeError();
5743 unsigned Kind = MaybeKind.get();
5744
5745 if ((Kind == SUBMODULE_METADATA) != First)
5746 return llvm::createStringError(
5747 EC: std::errc::illegal_byte_sequence,
5748 Fmt: "submodule metadata record should be at beginning of block");
5749 First = false;
5750
5751 // Submodule information is only valid if we have a current module.
5752 // FIXME: Should we error on these cases?
5753 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5754 Kind != SUBMODULE_DEFINITION)
5755 continue;
5756
5757 switch (Kind) {
5758 default: // Default behavior: ignore.
5759 break;
5760
5761 case SUBMODULE_DEFINITION: {
5762 if (Record.size() < 13)
5763 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5764 Fmt: "malformed module definition");
5765
5766 StringRef Name = Blob;
5767 unsigned Idx = 0;
5768 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
5769 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
5770 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5771 SourceLocation DefinitionLoc = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
5772 bool IsFramework = Record[Idx++];
5773 bool IsExplicit = Record[Idx++];
5774 bool IsSystem = Record[Idx++];
5775 bool IsExternC = Record[Idx++];
5776 bool InferSubmodules = Record[Idx++];
5777 bool InferExplicitSubmodules = Record[Idx++];
5778 bool InferExportWildcard = Record[Idx++];
5779 bool ConfigMacrosExhaustive = Record[Idx++];
5780 bool ModuleMapIsPrivate = Record[Idx++];
5781 bool NamedModuleHasInit = Record[Idx++];
5782
5783 Module *ParentModule = nullptr;
5784 if (Parent)
5785 ParentModule = getSubmodule(GlobalID: Parent);
5786
5787 // Retrieve this (sub)module from the module map, creating it if
5788 // necessary.
5789 CurrentModule =
5790 ModMap.findOrCreateModule(Name, Parent: ParentModule, IsFramework, IsExplicit)
5791 .first;
5792
5793 // FIXME: Call ModMap.setInferredModuleAllowedBy()
5794
5795 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5796 if (GlobalIndex >= SubmodulesLoaded.size() ||
5797 SubmodulesLoaded[GlobalIndex])
5798 return llvm::createStringError(EC: std::errc::invalid_argument,
5799 Fmt: "too many submodules");
5800
5801 if (!ParentModule) {
5802 if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
5803 // Don't emit module relocation error if we have -fno-validate-pch
5804 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5805 DisableValidationForModuleKind::Module) &&
5806 CurFile != F.File) {
5807 auto ConflictError =
5808 PartialDiagnostic(diag::err_module_file_conflict,
5809 ContextObj->DiagAllocator)
5810 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5811 << F.File.getName();
5812 return DiagnosticError::create(Loc: CurrentImportLoc, Diag: ConflictError);
5813 }
5814 }
5815
5816 F.DidReadTopLevelSubmodule = true;
5817 CurrentModule->setASTFile(F.File);
5818 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5819 }
5820
5821 CurrentModule->Kind = Kind;
5822 CurrentModule->DefinitionLoc = DefinitionLoc;
5823 CurrentModule->Signature = F.Signature;
5824 CurrentModule->IsFromModuleFile = true;
5825 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5826 CurrentModule->IsExternC = IsExternC;
5827 CurrentModule->InferSubmodules = InferSubmodules;
5828 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5829 CurrentModule->InferExportWildcard = InferExportWildcard;
5830 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5831 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5832 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
5833 if (DeserializationListener)
5834 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
5835
5836 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5837
5838 // Clear out data that will be replaced by what is in the module file.
5839 CurrentModule->LinkLibraries.clear();
5840 CurrentModule->ConfigMacros.clear();
5841 CurrentModule->UnresolvedConflicts.clear();
5842 CurrentModule->Conflicts.clear();
5843
5844 // The module is available unless it's missing a requirement; relevant
5845 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5846 // Missing headers that were present when the module was built do not
5847 // make it unavailable -- if we got this far, this must be an explicitly
5848 // imported module file.
5849 CurrentModule->Requirements.clear();
5850 CurrentModule->MissingHeaders.clear();
5851 CurrentModule->IsUnimportable =
5852 ParentModule && ParentModule->IsUnimportable;
5853 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5854 break;
5855 }
5856
5857 case SUBMODULE_UMBRELLA_HEADER: {
5858 // FIXME: This doesn't work for framework modules as `Filename` is the
5859 // name as written in the module file and does not include
5860 // `Headers/`, so this path will never exist.
5861 std::string Filename = std::string(Blob);
5862 ResolveImportedPath(M&: F, Filename);
5863 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) {
5864 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
5865 // FIXME: NameAsWritten
5866 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
5867 }
5868 // Note that it's too late at this point to return out of date if the
5869 // name from the PCM doesn't match up with the one in the module map,
5870 // but also quite unlikely since we will have already checked the
5871 // modification time and size of the module map file itself.
5872 }
5873 break;
5874 }
5875
5876 case SUBMODULE_HEADER:
5877 case SUBMODULE_EXCLUDED_HEADER:
5878 case SUBMODULE_PRIVATE_HEADER:
5879 // We lazily associate headers with their modules via the HeaderInfo table.
5880 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5881 // of complete filenames or remove it entirely.
5882 break;
5883
5884 case SUBMODULE_TEXTUAL_HEADER:
5885 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5886 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5887 // them here.
5888 break;
5889
5890 case SUBMODULE_TOPHEADER: {
5891 std::string HeaderName(Blob);
5892 ResolveImportedPath(M&: F, Filename&: HeaderName);
5893 CurrentModule->addTopHeaderFilename(Filename: HeaderName);
5894 break;
5895 }
5896
5897 case SUBMODULE_UMBRELLA_DIR: {
5898 // See comments in SUBMODULE_UMBRELLA_HEADER
5899 std::string Dirname = std::string(Blob);
5900 ResolveImportedPath(M&: F, Filename&: Dirname);
5901 if (auto Umbrella =
5902 PP.getFileManager().getOptionalDirectoryRef(DirName: Dirname)) {
5903 if (!CurrentModule->getUmbrellaDirAsWritten()) {
5904 // FIXME: NameAsWritten
5905 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
5906 }
5907 }
5908 break;
5909 }
5910
5911 case SUBMODULE_METADATA: {
5912 F.BaseSubmoduleID = getTotalNumSubmodules();
5913 F.LocalNumSubmodules = Record[0];
5914 unsigned LocalBaseSubmoduleID = Record[1];
5915 if (F.LocalNumSubmodules > 0) {
5916 // Introduce the global -> local mapping for submodules within this
5917 // module.
5918 GlobalSubmoduleMap.insert(Val: std::make_pair(x: getTotalNumSubmodules()+1,y: &F));
5919
5920 // Introduce the local -> global mapping for submodules within this
5921 // module.
5922 F.SubmoduleRemap.insertOrReplace(
5923 Val: std::make_pair(x&: LocalBaseSubmoduleID,
5924 y: F.BaseSubmoduleID - LocalBaseSubmoduleID));
5925
5926 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
5927 }
5928 break;
5929 }
5930
5931 case SUBMODULE_IMPORTS:
5932 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5933 UnresolvedModuleRef Unresolved;
5934 Unresolved.File = &F;
5935 Unresolved.Mod = CurrentModule;
5936 Unresolved.ID = Record[Idx];
5937 Unresolved.Kind = UnresolvedModuleRef::Import;
5938 Unresolved.IsWildcard = false;
5939 UnresolvedModuleRefs.push_back(Elt: Unresolved);
5940 }
5941 break;
5942
5943 case SUBMODULE_AFFECTING_MODULES:
5944 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5945 UnresolvedModuleRef Unresolved;
5946 Unresolved.File = &F;
5947 Unresolved.Mod = CurrentModule;
5948 Unresolved.ID = Record[Idx];
5949 Unresolved.Kind = UnresolvedModuleRef::Affecting;
5950 Unresolved.IsWildcard = false;
5951 UnresolvedModuleRefs.push_back(Elt: Unresolved);
5952 }
5953 break;
5954
5955 case SUBMODULE_EXPORTS:
5956 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5957 UnresolvedModuleRef Unresolved;
5958 Unresolved.File = &F;
5959 Unresolved.Mod = CurrentModule;
5960 Unresolved.ID = Record[Idx];
5961 Unresolved.Kind = UnresolvedModuleRef::Export;
5962 Unresolved.IsWildcard = Record[Idx + 1];
5963 UnresolvedModuleRefs.push_back(Elt: Unresolved);
5964 }
5965
5966 // Once we've loaded the set of exports, there's no reason to keep
5967 // the parsed, unresolved exports around.
5968 CurrentModule->UnresolvedExports.clear();
5969 break;
5970
5971 case SUBMODULE_REQUIRES:
5972 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
5973 Target: PP.getTargetInfo());
5974 break;
5975
5976 case SUBMODULE_LINK_LIBRARY:
5977 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
5978 CurrentModule->LinkLibraries.push_back(
5979 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
5980 break;
5981
5982 case SUBMODULE_CONFIG_MACRO:
5983 CurrentModule->ConfigMacros.push_back(x: Blob.str());
5984 break;
5985
5986 case SUBMODULE_CONFLICT: {
5987 UnresolvedModuleRef Unresolved;
5988 Unresolved.File = &F;
5989 Unresolved.Mod = CurrentModule;
5990 Unresolved.ID = Record[0];
5991 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5992 Unresolved.IsWildcard = false;
5993 Unresolved.String = Blob;
5994 UnresolvedModuleRefs.push_back(Elt: Unresolved);
5995 break;
5996 }
5997
5998 case SUBMODULE_INITIALIZERS: {
5999 if (!ContextObj)
6000 break;
6001 SmallVector<uint32_t, 16> Inits;
6002 for (auto &ID : Record)
6003 Inits.push_back(Elt: getGlobalDeclID(F, LocalID: ID));
6004 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6005 break;
6006 }
6007
6008 case SUBMODULE_EXPORT_AS:
6009 CurrentModule->ExportAsModule = Blob.str();
6010 ModMap.addLinkAsDependency(Mod: CurrentModule);
6011 break;
6012 }
6013 }
6014}
6015
6016/// Parse the record that corresponds to a LangOptions data
6017/// structure.
6018///
6019/// This routine parses the language options from the AST file and then gives
6020/// them to the AST listener if one is set.
6021///
6022/// \returns true if the listener deems the file unacceptable, false otherwise.
6023bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6024 bool Complain,
6025 ASTReaderListener &Listener,
6026 bool AllowCompatibleDifferences) {
6027 LangOptions LangOpts;
6028 unsigned Idx = 0;
6029#define LANGOPT(Name, Bits, Default, Description) \
6030 LangOpts.Name = Record[Idx++];
6031#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
6032 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6033#include "clang/Basic/LangOptions.def"
6034#define SANITIZER(NAME, ID) \
6035 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6036#include "clang/Basic/Sanitizers.def"
6037
6038 for (unsigned N = Record[Idx++]; N; --N)
6039 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6040
6041 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6042 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6043 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6044
6045 LangOpts.CurrentModule = ReadString(Record, Idx);
6046
6047 // Comment options.
6048 for (unsigned N = Record[Idx++]; N; --N) {
6049 LangOpts.CommentOpts.BlockCommandNames.push_back(
6050 x: ReadString(Record, Idx));
6051 }
6052 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6053
6054 // OpenMP offloading options.
6055 for (unsigned N = Record[Idx++]; N; --N) {
6056 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6057 }
6058
6059 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6060
6061 return Listener.ReadLanguageOptions(LangOpts, Complain,
6062 AllowCompatibleDifferences);
6063}
6064
6065bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
6066 ASTReaderListener &Listener,
6067 bool AllowCompatibleDifferences) {
6068 unsigned Idx = 0;
6069 TargetOptions TargetOpts;
6070 TargetOpts.Triple = ReadString(Record, Idx);
6071 TargetOpts.CPU = ReadString(Record, Idx);
6072 TargetOpts.TuneCPU = ReadString(Record, Idx);
6073 TargetOpts.ABI = ReadString(Record, Idx);
6074 for (unsigned N = Record[Idx++]; N; --N) {
6075 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6076 }
6077 for (unsigned N = Record[Idx++]; N; --N) {
6078 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6079 }
6080
6081 return Listener.ReadTargetOptions(TargetOpts, Complain,
6082 AllowCompatibleDifferences);
6083}
6084
6085bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
6086 ASTReaderListener &Listener) {
6087 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
6088 unsigned Idx = 0;
6089#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
6090#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6091 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
6092#include "clang/Basic/DiagnosticOptions.def"
6093
6094 for (unsigned N = Record[Idx++]; N; --N)
6095 DiagOpts->Warnings.push_back(x: ReadString(Record, Idx));
6096 for (unsigned N = Record[Idx++]; N; --N)
6097 DiagOpts->Remarks.push_back(x: ReadString(Record, Idx));
6098
6099 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
6100}
6101
6102bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6103 ASTReaderListener &Listener) {
6104 FileSystemOptions FSOpts;
6105 unsigned Idx = 0;
6106 FSOpts.WorkingDir = ReadString(Record, Idx);
6107 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6108}
6109
6110bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6111 bool Complain,
6112 ASTReaderListener &Listener) {
6113 HeaderSearchOptions HSOpts;
6114 unsigned Idx = 0;
6115 HSOpts.Sysroot = ReadString(Record, Idx);
6116
6117 HSOpts.ResourceDir = ReadString(Record, Idx);
6118 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6119 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6120 HSOpts.DisableModuleHash = Record[Idx++];
6121 HSOpts.ImplicitModuleMaps = Record[Idx++];
6122 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6123 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6124 HSOpts.UseBuiltinIncludes = Record[Idx++];
6125 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6126 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6127 HSOpts.UseLibcxx = Record[Idx++];
6128 std::string SpecificModuleCachePath = ReadString(Record, Idx);
6129
6130 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
6131 Complain);
6132}
6133
6134bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6135 ASTReaderListener &Listener) {
6136 HeaderSearchOptions HSOpts;
6137 unsigned Idx = 0;
6138
6139 // Include entries.
6140 for (unsigned N = Record[Idx++]; N; --N) {
6141 std::string Path = ReadString(Record, Idx);
6142 frontend::IncludeDirGroup Group
6143 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6144 bool IsFramework = Record[Idx++];
6145 bool IgnoreSysRoot = Record[Idx++];
6146 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6147 args&: IgnoreSysRoot);
6148 }
6149
6150 // System header prefixes.
6151 for (unsigned N = Record[Idx++]; N; --N) {
6152 std::string Prefix = ReadString(Record, Idx);
6153 bool IsSystemHeader = Record[Idx++];
6154 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6155 }
6156
6157 // VFS overlay files.
6158 for (unsigned N = Record[Idx++]; N; --N) {
6159 std::string VFSOverlayFile = ReadString(Record, Idx);
6160 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6161 }
6162
6163 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6164}
6165
6166bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6167 bool Complain,
6168 ASTReaderListener &Listener,
6169 std::string &SuggestedPredefines) {
6170 PreprocessorOptions PPOpts;
6171 unsigned Idx = 0;
6172
6173 // Macro definitions/undefs
6174 bool ReadMacros = Record[Idx++];
6175 if (ReadMacros) {
6176 for (unsigned N = Record[Idx++]; N; --N) {
6177 std::string Macro = ReadString(Record, Idx);
6178 bool IsUndef = Record[Idx++];
6179 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6180 }
6181 }
6182
6183 // Includes
6184 for (unsigned N = Record[Idx++]; N; --N) {
6185 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6186 }
6187
6188 // Macro Includes
6189 for (unsigned N = Record[Idx++]; N; --N) {
6190 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6191 }
6192
6193 PPOpts.UsePredefines = Record[Idx++];
6194 PPOpts.DetailedRecord = Record[Idx++];
6195 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6196 PPOpts.ObjCXXARCStandardLibrary =
6197 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6198 SuggestedPredefines.clear();
6199 return Listener.ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
6200 SuggestedPredefines);
6201}
6202
6203std::pair<ModuleFile *, unsigned>
6204ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6205 GlobalPreprocessedEntityMapType::iterator
6206 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6207 assert(I != GlobalPreprocessedEntityMap.end() &&
6208 "Corrupted global preprocessed entity map");
6209 ModuleFile *M = I->second;
6210 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6211 return std::make_pair(x&: M, y&: LocalIndex);
6212}
6213
6214llvm::iterator_range<PreprocessingRecord::iterator>
6215ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6216 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6217 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6218 count: Mod.NumPreprocessedEntities);
6219
6220 return llvm::make_range(x: PreprocessingRecord::iterator(),
6221 y: PreprocessingRecord::iterator());
6222}
6223
6224bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6225 unsigned int ClientLoadCapabilities) {
6226 return ClientLoadCapabilities & ARR_OutOfDate &&
6227 !getModuleManager().getModuleCache().isPCMFinal(Filename: ModuleFileName);
6228}
6229
6230llvm::iterator_range<ASTReader::ModuleDeclIterator>
6231ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6232 return llvm::make_range(
6233 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6234 y: ModuleDeclIterator(this, &Mod,
6235 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6236}
6237
6238SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6239 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6240 assert(I != GlobalSkippedRangeMap.end() &&
6241 "Corrupted global skipped range map");
6242 ModuleFile *M = I->second;
6243 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6244 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6245 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6246 SourceRange Range(TranslateSourceLocation(ModuleFile&: *M, Loc: RawRange.getBegin()),
6247 TranslateSourceLocation(ModuleFile&: *M, Loc: RawRange.getEnd()));
6248 assert(Range.isValid());
6249 return Range;
6250}
6251
6252PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6253 PreprocessedEntityID PPID = Index+1;
6254 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6255 ModuleFile &M = *PPInfo.first;
6256 unsigned LocalIndex = PPInfo.second;
6257 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6258
6259 if (!PP.getPreprocessingRecord()) {
6260 Error(Msg: "no preprocessing record");
6261 return nullptr;
6262 }
6263
6264 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6265 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6266 BitNo: M.MacroOffsetsBase + PPOffs.BitOffset)) {
6267 Error(Err: std::move(Err));
6268 return nullptr;
6269 }
6270
6271 Expected<llvm::BitstreamEntry> MaybeEntry =
6272 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6273 if (!MaybeEntry) {
6274 Error(Err: MaybeEntry.takeError());
6275 return nullptr;
6276 }
6277 llvm::BitstreamEntry Entry = MaybeEntry.get();
6278
6279 if (Entry.Kind != llvm::BitstreamEntry::Record)
6280 return nullptr;
6281
6282 // Read the record.
6283 SourceRange Range(TranslateSourceLocation(ModuleFile&: M, Loc: PPOffs.getBegin()),
6284 TranslateSourceLocation(ModuleFile&: M, Loc: PPOffs.getEnd()));
6285 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6286 StringRef Blob;
6287 RecordData Record;
6288 Expected<unsigned> MaybeRecType =
6289 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6290 if (!MaybeRecType) {
6291 Error(Err: MaybeRecType.takeError());
6292 return nullptr;
6293 }
6294 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6295 case PPD_MACRO_EXPANSION: {
6296 bool isBuiltin = Record[0];
6297 IdentifierInfo *Name = nullptr;
6298 MacroDefinitionRecord *Def = nullptr;
6299 if (isBuiltin)
6300 Name = getLocalIdentifier(M, LocalID: Record[1]);
6301 else {
6302 PreprocessedEntityID GlobalID =
6303 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6304 Def = cast<MacroDefinitionRecord>(
6305 Val: PPRec.getLoadedPreprocessedEntity(Index: GlobalID - 1));
6306 }
6307
6308 MacroExpansion *ME;
6309 if (isBuiltin)
6310 ME = new (PPRec) MacroExpansion(Name, Range);
6311 else
6312 ME = new (PPRec) MacroExpansion(Def, Range);
6313
6314 return ME;
6315 }
6316
6317 case PPD_MACRO_DEFINITION: {
6318 // Decode the identifier info and then check again; if the macro is
6319 // still defined and associated with the identifier,
6320 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6321 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6322
6323 if (DeserializationListener)
6324 DeserializationListener->MacroDefinitionRead(PPID, MD);
6325
6326 return MD;
6327 }
6328
6329 case PPD_INCLUSION_DIRECTIVE: {
6330 const char *FullFileNameStart = Blob.data() + Record[0];
6331 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6332 OptionalFileEntryRef File;
6333 if (!FullFileName.empty())
6334 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
6335
6336 // FIXME: Stable encoding
6337 InclusionDirective::InclusionKind Kind
6338 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6339 InclusionDirective *ID
6340 = new (PPRec) InclusionDirective(PPRec, Kind,
6341 StringRef(Blob.data(), Record[0]),
6342 Record[1], Record[3],
6343 File,
6344 Range);
6345 return ID;
6346 }
6347 }
6348
6349 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6350}
6351
6352/// Find the next module that contains entities and return the ID
6353/// of the first entry.
6354///
6355/// \param SLocMapI points at a chunk of a module that contains no
6356/// preprocessed entities or the entities it contains are not the ones we are
6357/// looking for.
6358PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6359 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6360 ++SLocMapI;
6361 for (GlobalSLocOffsetMapType::const_iterator
6362 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6363 ModuleFile &M = *SLocMapI->second;
6364 if (M.NumPreprocessedEntities)
6365 return M.BasePreprocessedEntityID;
6366 }
6367
6368 return getTotalNumPreprocessedEntities();
6369}
6370
6371namespace {
6372
6373struct PPEntityComp {
6374 const ASTReader &Reader;
6375 ModuleFile &M;
6376
6377 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6378
6379 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6380 SourceLocation LHS = getLoc(PPE: L);
6381 SourceLocation RHS = getLoc(PPE: R);
6382 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6383 }
6384
6385 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6386 SourceLocation LHS = getLoc(PPE: L);
6387 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6388 }
6389
6390 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6391 SourceLocation RHS = getLoc(PPE: R);
6392 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6393 }
6394
6395 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6396 return Reader.TranslateSourceLocation(ModuleFile&: M, Loc: PPE.getBegin());
6397 }
6398};
6399
6400} // namespace
6401
6402PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6403 bool EndsAfter) const {
6404 if (SourceMgr.isLocalSourceLocation(Loc))
6405 return getTotalNumPreprocessedEntities();
6406
6407 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6408 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6409 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6410 "Corrupted global sloc offset map");
6411
6412 if (SLocMapI->second->NumPreprocessedEntities == 0)
6413 return findNextPreprocessedEntity(SLocMapI);
6414
6415 ModuleFile &M = *SLocMapI->second;
6416
6417 using pp_iterator = const PPEntityOffset *;
6418
6419 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6420 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6421
6422 size_t Count = M.NumPreprocessedEntities;
6423 size_t Half;
6424 pp_iterator First = pp_begin;
6425 pp_iterator PPI;
6426
6427 if (EndsAfter) {
6428 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
6429 comp: PPEntityComp(*this, M));
6430 } else {
6431 // Do a binary search manually instead of using std::lower_bound because
6432 // The end locations of entities may be unordered (when a macro expansion
6433 // is inside another macro argument), but for this case it is not important
6434 // whether we get the first macro expansion or its containing macro.
6435 while (Count > 0) {
6436 Half = Count / 2;
6437 PPI = First;
6438 std::advance(i&: PPI, n: Half);
6439 if (SourceMgr.isBeforeInTranslationUnit(
6440 LHS: TranslateSourceLocation(ModuleFile&: M, Loc: PPI->getEnd()), RHS: Loc)) {
6441 First = PPI;
6442 ++First;
6443 Count = Count - Half - 1;
6444 } else
6445 Count = Half;
6446 }
6447 }
6448
6449 if (PPI == pp_end)
6450 return findNextPreprocessedEntity(SLocMapI);
6451
6452 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6453}
6454
6455/// Returns a pair of [Begin, End) indices of preallocated
6456/// preprocessed entities that \arg Range encompasses.
6457std::pair<unsigned, unsigned>
6458 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6459 if (Range.isInvalid())
6460 return std::make_pair(x: 0,y: 0);
6461 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6462
6463 PreprocessedEntityID BeginID =
6464 findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
6465 PreprocessedEntityID EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
6466 return std::make_pair(x&: BeginID, y&: EndID);
6467}
6468
6469/// Optionally returns true or false if the preallocated preprocessed
6470/// entity with index \arg Index came from file \arg FID.
6471std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6472 FileID FID) {
6473 if (FID.isInvalid())
6474 return false;
6475
6476 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6477 ModuleFile &M = *PPInfo.first;
6478 unsigned LocalIndex = PPInfo.second;
6479 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6480
6481 SourceLocation Loc = TranslateSourceLocation(ModuleFile&: M, Loc: PPOffs.getBegin());
6482 if (Loc.isInvalid())
6483 return false;
6484
6485 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
6486 return true;
6487 else
6488 return false;
6489}
6490
6491namespace {
6492
6493 /// Visitor used to search for information about a header file.
6494 class HeaderFileInfoVisitor {
6495 FileEntryRef FE;
6496 std::optional<HeaderFileInfo> HFI;
6497
6498 public:
6499 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
6500
6501 bool operator()(ModuleFile &M) {
6502 HeaderFileInfoLookupTable *Table
6503 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6504 if (!Table)
6505 return false;
6506
6507 // Look in the on-disk hash table for an entry for this file name.
6508 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
6509 if (Pos == Table->end())
6510 return false;
6511
6512 HFI = *Pos;
6513 return true;
6514 }
6515
6516 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6517 };
6518
6519} // namespace
6520
6521HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
6522 HeaderFileInfoVisitor Visitor(FE);
6523 ModuleMgr.visit(Visitor);
6524 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6525 return *HFI;
6526
6527 return HeaderFileInfo();
6528}
6529
6530void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6531 using DiagState = DiagnosticsEngine::DiagState;
6532 SmallVector<DiagState *, 32> DiagStates;
6533
6534 for (ModuleFile &F : ModuleMgr) {
6535 unsigned Idx = 0;
6536 auto &Record = F.PragmaDiagMappings;
6537 if (Record.empty())
6538 continue;
6539
6540 DiagStates.clear();
6541
6542 auto ReadDiagState = [&](const DiagState &BasedOn,
6543 bool IncludeNonPragmaStates) {
6544 unsigned BackrefID = Record[Idx++];
6545 if (BackrefID != 0)
6546 return DiagStates[BackrefID - 1];
6547
6548 // A new DiagState was created here.
6549 Diag.DiagStates.push_back(x: BasedOn);
6550 DiagState *NewState = &Diag.DiagStates.back();
6551 DiagStates.push_back(Elt: NewState);
6552 unsigned Size = Record[Idx++];
6553 assert(Idx + Size * 2 <= Record.size() &&
6554 "Invalid data, not enough diag/map pairs");
6555 while (Size--) {
6556 unsigned DiagID = Record[Idx++];
6557 DiagnosticMapping NewMapping =
6558 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
6559 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6560 continue;
6561
6562 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
6563
6564 // If this mapping was specified as a warning but the severity was
6565 // upgraded due to diagnostic settings, simulate the current diagnostic
6566 // settings (and use a warning).
6567 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6568 NewMapping.setSeverity(diag::Severity::Warning);
6569 NewMapping.setUpgradedFromWarning(false);
6570 }
6571
6572 Mapping = NewMapping;
6573 }
6574 return NewState;
6575 };
6576
6577 // Read the first state.
6578 DiagState *FirstState;
6579 if (F.Kind == MK_ImplicitModule) {
6580 // Implicitly-built modules are reused with different diagnostic
6581 // settings. Use the initial diagnostic state from Diag to simulate this
6582 // compilation's diagnostic settings.
6583 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6584 DiagStates.push_back(Elt: FirstState);
6585
6586 // Skip the initial diagnostic state from the serialized module.
6587 assert(Record[1] == 0 &&
6588 "Invalid data, unexpected backref in initial state");
6589 Idx = 3 + Record[2] * 2;
6590 assert(Idx < Record.size() &&
6591 "Invalid data, not enough state change pairs in initial state");
6592 } else if (F.isModule()) {
6593 // For an explicit module, preserve the flags from the module build
6594 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6595 // -Wblah flags.
6596 unsigned Flags = Record[Idx++];
6597 DiagState Initial;
6598 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6599 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6600 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6601 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6602 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6603 Initial.ExtBehavior = (diag::Severity)Flags;
6604 FirstState = ReadDiagState(Initial, true);
6605
6606 assert(F.OriginalSourceFileID.isValid());
6607
6608 // Set up the root buffer of the module to start with the initial
6609 // diagnostic state of the module itself, to cover files that contain no
6610 // explicit transitions (for which we did not serialize anything).
6611 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6612 .StateTransitions.push_back(Elt: {FirstState, 0});
6613 } else {
6614 // For prefix ASTs, start with whatever the user configured on the
6615 // command line.
6616 Idx++; // Skip flags.
6617 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
6618 }
6619
6620 // Read the state transitions.
6621 unsigned NumLocations = Record[Idx++];
6622 while (NumLocations--) {
6623 assert(Idx < Record.size() &&
6624 "Invalid data, missing pragma diagnostic states");
6625 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
6626 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6627 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6628 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6629 unsigned Transitions = Record[Idx++];
6630
6631 // Note that we don't need to set up Parent/ParentOffset here, because
6632 // we won't be changing the diagnostic state within imported FileIDs
6633 // (other than perhaps appending to the main source file, which has no
6634 // parent).
6635 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6636 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
6637 for (unsigned I = 0; I != Transitions; ++I) {
6638 unsigned Offset = Record[Idx++];
6639 auto *State = ReadDiagState(*FirstState, false);
6640 F.StateTransitions.push_back(Elt: {State, Offset});
6641 }
6642 }
6643
6644 // Read the final state.
6645 assert(Idx < Record.size() &&
6646 "Invalid data, missing final pragma diagnostic state");
6647 SourceLocation CurStateLoc = ReadSourceLocation(ModuleFile&: F, Raw: Record[Idx++]);
6648 auto *CurState = ReadDiagState(*FirstState, false);
6649
6650 if (!F.isModule()) {
6651 Diag.DiagStatesByLoc.CurDiagState = CurState;
6652 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6653
6654 // Preserve the property that the imaginary root file describes the
6655 // current state.
6656 FileID NullFile;
6657 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6658 if (T.empty())
6659 T.push_back(Elt: {CurState, 0});
6660 else
6661 T[0].State = CurState;
6662 }
6663
6664 // Don't try to read these mappings again.
6665 Record.clear();
6666 }
6667}
6668
6669/// Get the correct cursor and offset for loading a type.
6670ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6671 GlobalTypeMapType::iterator I = GlobalTypeMap.find(K: Index);
6672 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6673 ModuleFile *M = I->second;
6674 return RecordLocation(
6675 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6676 M->DeclsBlockStartOffset);
6677}
6678
6679static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6680 switch (code) {
6681#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6682 case TYPE_##CODE_ID: return Type::CLASS_ID;
6683#include "clang/Serialization/TypeBitCodes.def"
6684 default:
6685 return std::nullopt;
6686 }
6687}
6688
6689/// Read and return the type with the given index..
6690///
6691/// The index is the type ID, shifted and minus the number of predefs. This
6692/// routine actually reads the record corresponding to the type at the given
6693/// location. It is a helper routine for GetType, which deals with reading type
6694/// IDs.
6695QualType ASTReader::readTypeRecord(unsigned Index) {
6696 assert(ContextObj && "reading type with no AST context");
6697 ASTContext &Context = *ContextObj;
6698 RecordLocation Loc = TypeCursorForIndex(Index);
6699 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6700
6701 // Keep track of where we are in the stream, then jump back there
6702 // after reading this type.
6703 SavedStreamPosition SavedPosition(DeclsCursor);
6704
6705 ReadingKindTracker ReadingKind(Read_Type, *this);
6706
6707 // Note that we are loading a type record.
6708 Deserializing AType(this);
6709
6710 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
6711 Error(Err: std::move(Err));
6712 return QualType();
6713 }
6714 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6715 if (!RawCode) {
6716 Error(Err: RawCode.takeError());
6717 return QualType();
6718 }
6719
6720 ASTRecordReader Record(*this, *Loc.F);
6721 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
6722 if (!Code) {
6723 Error(Err: Code.takeError());
6724 return QualType();
6725 }
6726 if (Code.get() == TYPE_EXT_QUAL) {
6727 QualType baseType = Record.readQualType();
6728 Qualifiers quals = Record.readQualifiers();
6729 return Context.getQualifiedType(T: baseType, Qs: quals);
6730 }
6731
6732 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
6733 if (!maybeClass) {
6734 Error(Msg: "Unexpected code for type");
6735 return QualType();
6736 }
6737
6738 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6739 return TypeReader.read(*maybeClass);
6740}
6741
6742namespace clang {
6743
6744class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6745 using LocSeq = SourceLocationSequence;
6746
6747 ASTRecordReader &Reader;
6748 LocSeq *Seq;
6749
6750 SourceLocation readSourceLocation() { return Reader.readSourceLocation(Seq); }
6751 SourceRange readSourceRange() { return Reader.readSourceRange(Seq); }
6752
6753 TypeSourceInfo *GetTypeSourceInfo() {
6754 return Reader.readTypeSourceInfo();
6755 }
6756
6757 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6758 return Reader.readNestedNameSpecifierLoc();
6759 }
6760
6761 Attr *ReadAttr() {
6762 return Reader.readAttr();
6763 }
6764
6765public:
6766 TypeLocReader(ASTRecordReader &Reader, LocSeq *Seq)
6767 : Reader(Reader), Seq(Seq) {}
6768
6769 // We want compile-time assurance that we've enumerated all of
6770 // these, so unfortunately we have to declare them first, then
6771 // define them out-of-line.
6772#define ABSTRACT_TYPELOC(CLASS, PARENT)
6773#define TYPELOC(CLASS, PARENT) \
6774 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6775#include "clang/AST/TypeLocNodes.def"
6776
6777 void VisitFunctionTypeLoc(FunctionTypeLoc);
6778 void VisitArrayTypeLoc(ArrayTypeLoc);
6779};
6780
6781} // namespace clang
6782
6783void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6784 // nothing to do
6785}
6786
6787void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6788 TL.setBuiltinLoc(readSourceLocation());
6789 if (TL.needsExtraLocalData()) {
6790 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6791 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6792 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6793 TL.setModeAttr(Reader.readInt());
6794 }
6795}
6796
6797void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6798 TL.setNameLoc(readSourceLocation());
6799}
6800
6801void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6802 TL.setStarLoc(readSourceLocation());
6803}
6804
6805void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6806 // nothing to do
6807}
6808
6809void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6810 // nothing to do
6811}
6812
6813void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6814 TL.setExpansionLoc(readSourceLocation());
6815}
6816
6817void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6818 TL.setCaretLoc(readSourceLocation());
6819}
6820
6821void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6822 TL.setAmpLoc(readSourceLocation());
6823}
6824
6825void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6826 TL.setAmpAmpLoc(readSourceLocation());
6827}
6828
6829void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6830 TL.setStarLoc(readSourceLocation());
6831 TL.setClassTInfo(GetTypeSourceInfo());
6832}
6833
6834void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6835 TL.setLBracketLoc(readSourceLocation());
6836 TL.setRBracketLoc(readSourceLocation());
6837 if (Reader.readBool())
6838 TL.setSizeExpr(Reader.readExpr());
6839 else
6840 TL.setSizeExpr(nullptr);
6841}
6842
6843void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6844 VisitArrayTypeLoc(TL);
6845}
6846
6847void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6848 VisitArrayTypeLoc(TL);
6849}
6850
6851void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6852 VisitArrayTypeLoc(TL);
6853}
6854
6855void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6856 DependentSizedArrayTypeLoc TL) {
6857 VisitArrayTypeLoc(TL);
6858}
6859
6860void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6861 DependentAddressSpaceTypeLoc TL) {
6862
6863 TL.setAttrNameLoc(readSourceLocation());
6864 TL.setAttrOperandParensRange(readSourceRange());
6865 TL.setAttrExprOperand(Reader.readExpr());
6866}
6867
6868void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6869 DependentSizedExtVectorTypeLoc TL) {
6870 TL.setNameLoc(readSourceLocation());
6871}
6872
6873void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6874 TL.setNameLoc(readSourceLocation());
6875}
6876
6877void TypeLocReader::VisitDependentVectorTypeLoc(
6878 DependentVectorTypeLoc TL) {
6879 TL.setNameLoc(readSourceLocation());
6880}
6881
6882void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6883 TL.setNameLoc(readSourceLocation());
6884}
6885
6886void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6887 TL.setAttrNameLoc(readSourceLocation());
6888 TL.setAttrOperandParensRange(readSourceRange());
6889 TL.setAttrRowOperand(Reader.readExpr());
6890 TL.setAttrColumnOperand(Reader.readExpr());
6891}
6892
6893void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6894 DependentSizedMatrixTypeLoc TL) {
6895 TL.setAttrNameLoc(readSourceLocation());
6896 TL.setAttrOperandParensRange(readSourceRange());
6897 TL.setAttrRowOperand(Reader.readExpr());
6898 TL.setAttrColumnOperand(Reader.readExpr());
6899}
6900
6901void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6902 TL.setLocalRangeBegin(readSourceLocation());
6903 TL.setLParenLoc(readSourceLocation());
6904 TL.setRParenLoc(readSourceLocation());
6905 TL.setExceptionSpecRange(readSourceRange());
6906 TL.setLocalRangeEnd(readSourceLocation());
6907 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6908 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
6909 }
6910}
6911
6912void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6913 VisitFunctionTypeLoc(TL);
6914}
6915
6916void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6917 VisitFunctionTypeLoc(TL);
6918}
6919
6920void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6921 TL.setNameLoc(readSourceLocation());
6922}
6923
6924void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6925 TL.setNameLoc(readSourceLocation());
6926}
6927
6928void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6929 TL.setNameLoc(readSourceLocation());
6930}
6931
6932void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6933 TL.setTypeofLoc(readSourceLocation());
6934 TL.setLParenLoc(readSourceLocation());
6935 TL.setRParenLoc(readSourceLocation());
6936}
6937
6938void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6939 TL.setTypeofLoc(readSourceLocation());
6940 TL.setLParenLoc(readSourceLocation());
6941 TL.setRParenLoc(readSourceLocation());
6942 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
6943}
6944
6945void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6946 TL.setDecltypeLoc(readSourceLocation());
6947 TL.setRParenLoc(readSourceLocation());
6948}
6949
6950void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
6951 TL.setEllipsisLoc(readSourceLocation());
6952}
6953
6954void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6955 TL.setKWLoc(readSourceLocation());
6956 TL.setLParenLoc(readSourceLocation());
6957 TL.setRParenLoc(readSourceLocation());
6958 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6959}
6960
6961ConceptReference *ASTRecordReader::readConceptReference() {
6962 auto NNS = readNestedNameSpecifierLoc();
6963 auto TemplateKWLoc = readSourceLocation();
6964 auto ConceptNameLoc = readDeclarationNameInfo();
6965 auto FoundDecl = readDeclAs<NamedDecl>();
6966 auto NamedConcept = readDeclAs<ConceptDecl>();
6967 auto *CR = ConceptReference::Create(
6968 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
6969 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
6970 return CR;
6971}
6972
6973void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6974 TL.setNameLoc(readSourceLocation());
6975 if (Reader.readBool())
6976 TL.setConceptReference(Reader.readConceptReference());
6977 if (Reader.readBool())
6978 TL.setRParenLoc(readSourceLocation());
6979}
6980
6981void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6982 DeducedTemplateSpecializationTypeLoc TL) {
6983 TL.setTemplateNameLoc(readSourceLocation());
6984}
6985
6986void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6987 TL.setNameLoc(readSourceLocation());
6988}
6989
6990void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6991 TL.setNameLoc(readSourceLocation());
6992}
6993
6994void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6995 TL.setAttr(ReadAttr());
6996}
6997
6998void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
6999 // Nothing to do.
7000}
7001
7002void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7003 TL.setNameLoc(readSourceLocation());
7004}
7005
7006void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7007 SubstTemplateTypeParmTypeLoc TL) {
7008 TL.setNameLoc(readSourceLocation());
7009}
7010
7011void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7012 SubstTemplateTypeParmPackTypeLoc TL) {
7013 TL.setNameLoc(readSourceLocation());
7014}
7015
7016void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7017 TemplateSpecializationTypeLoc TL) {
7018 TL.setTemplateKeywordLoc(readSourceLocation());
7019 TL.setTemplateNameLoc(readSourceLocation());
7020 TL.setLAngleLoc(readSourceLocation());
7021 TL.setRAngleLoc(readSourceLocation());
7022 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7023 TL.setArgLocInfo(i,
7024 AI: Reader.readTemplateArgumentLocInfo(
7025 Kind: TL.getTypePtr()->template_arguments()[i].getKind()));
7026}
7027
7028void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7029 TL.setLParenLoc(readSourceLocation());
7030 TL.setRParenLoc(readSourceLocation());
7031}
7032
7033void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7034 TL.setElaboratedKeywordLoc(readSourceLocation());
7035 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7036}
7037
7038void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7039 TL.setNameLoc(readSourceLocation());
7040}
7041
7042void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7043 TL.setElaboratedKeywordLoc(readSourceLocation());
7044 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7045 TL.setNameLoc(readSourceLocation());
7046}
7047
7048void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7049 DependentTemplateSpecializationTypeLoc TL) {
7050 TL.setElaboratedKeywordLoc(readSourceLocation());
7051 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7052 TL.setTemplateKeywordLoc(readSourceLocation());
7053 TL.setTemplateNameLoc(readSourceLocation());
7054 TL.setLAngleLoc(readSourceLocation());
7055 TL.setRAngleLoc(readSourceLocation());
7056 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7057 TL.setArgLocInfo(i: I,
7058 AI: Reader.readTemplateArgumentLocInfo(
7059 Kind: TL.getTypePtr()->template_arguments()[I].getKind()));
7060}
7061
7062void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7063 TL.setEllipsisLoc(readSourceLocation());
7064}
7065
7066void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7067 TL.setNameLoc(readSourceLocation());
7068 TL.setNameEndLoc(readSourceLocation());
7069}
7070
7071void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7072 if (TL.getNumProtocols()) {
7073 TL.setProtocolLAngleLoc(readSourceLocation());
7074 TL.setProtocolRAngleLoc(readSourceLocation());
7075 }
7076 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7077 TL.setProtocolLoc(i, Loc: readSourceLocation());
7078}
7079
7080void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7081 TL.setHasBaseTypeAsWritten(Reader.readBool());
7082 TL.setTypeArgsLAngleLoc(readSourceLocation());
7083 TL.setTypeArgsRAngleLoc(readSourceLocation());
7084 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7085 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7086 TL.setProtocolLAngleLoc(readSourceLocation());
7087 TL.setProtocolRAngleLoc(readSourceLocation());
7088 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7089 TL.setProtocolLoc(i, Loc: readSourceLocation());
7090}
7091
7092void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7093 TL.setStarLoc(readSourceLocation());
7094}
7095
7096void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7097 TL.setKWLoc(readSourceLocation());
7098 TL.setLParenLoc(readSourceLocation());
7099 TL.setRParenLoc(readSourceLocation());
7100}
7101
7102void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7103 TL.setKWLoc(readSourceLocation());
7104}
7105
7106void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7107 TL.setNameLoc(readSourceLocation());
7108}
7109void TypeLocReader::VisitDependentBitIntTypeLoc(
7110 clang::DependentBitIntTypeLoc TL) {
7111 TL.setNameLoc(readSourceLocation());
7112}
7113
7114void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) {
7115 LocSeq::State Seq(ParentSeq);
7116 TypeLocReader TLR(*this, Seq);
7117 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7118 TLR.Visit(TyLoc: TL);
7119}
7120
7121TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7122 QualType InfoTy = readType();
7123 if (InfoTy.isNull())
7124 return nullptr;
7125
7126 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7127 readTypeLoc(TL: TInfo->getTypeLoc());
7128 return TInfo;
7129}
7130
7131QualType ASTReader::GetType(TypeID ID) {
7132 assert(ContextObj && "reading type with no AST context");
7133 ASTContext &Context = *ContextObj;
7134
7135 unsigned FastQuals = ID & Qualifiers::FastMask;
7136 unsigned Index = ID >> Qualifiers::FastWidth;
7137
7138 if (Index < NUM_PREDEF_TYPE_IDS) {
7139 QualType T;
7140 switch ((PredefinedTypeIDs)Index) {
7141 case PREDEF_TYPE_LAST_ID:
7142 // We should never use this one.
7143 llvm_unreachable("Invalid predefined type");
7144 break;
7145 case PREDEF_TYPE_NULL_ID:
7146 return QualType();
7147 case PREDEF_TYPE_VOID_ID:
7148 T = Context.VoidTy;
7149 break;
7150 case PREDEF_TYPE_BOOL_ID:
7151 T = Context.BoolTy;
7152 break;
7153 case PREDEF_TYPE_CHAR_U_ID:
7154 case PREDEF_TYPE_CHAR_S_ID:
7155 // FIXME: Check that the signedness of CharTy is correct!
7156 T = Context.CharTy;
7157 break;
7158 case PREDEF_TYPE_UCHAR_ID:
7159 T = Context.UnsignedCharTy;
7160 break;
7161 case PREDEF_TYPE_USHORT_ID:
7162 T = Context.UnsignedShortTy;
7163 break;
7164 case PREDEF_TYPE_UINT_ID:
7165 T = Context.UnsignedIntTy;
7166 break;
7167 case PREDEF_TYPE_ULONG_ID:
7168 T = Context.UnsignedLongTy;
7169 break;
7170 case PREDEF_TYPE_ULONGLONG_ID:
7171 T = Context.UnsignedLongLongTy;
7172 break;
7173 case PREDEF_TYPE_UINT128_ID:
7174 T = Context.UnsignedInt128Ty;
7175 break;
7176 case PREDEF_TYPE_SCHAR_ID:
7177 T = Context.SignedCharTy;
7178 break;
7179 case PREDEF_TYPE_WCHAR_ID:
7180 T = Context.WCharTy;
7181 break;
7182 case PREDEF_TYPE_SHORT_ID:
7183 T = Context.ShortTy;
7184 break;
7185 case PREDEF_TYPE_INT_ID:
7186 T = Context.IntTy;
7187 break;
7188 case PREDEF_TYPE_LONG_ID:
7189 T = Context.LongTy;
7190 break;
7191 case PREDEF_TYPE_LONGLONG_ID:
7192 T = Context.LongLongTy;
7193 break;
7194 case PREDEF_TYPE_INT128_ID:
7195 T = Context.Int128Ty;
7196 break;
7197 case PREDEF_TYPE_BFLOAT16_ID:
7198 T = Context.BFloat16Ty;
7199 break;
7200 case PREDEF_TYPE_HALF_ID:
7201 T = Context.HalfTy;
7202 break;
7203 case PREDEF_TYPE_FLOAT_ID:
7204 T = Context.FloatTy;
7205 break;
7206 case PREDEF_TYPE_DOUBLE_ID:
7207 T = Context.DoubleTy;
7208 break;
7209 case PREDEF_TYPE_LONGDOUBLE_ID:
7210 T = Context.LongDoubleTy;
7211 break;
7212 case PREDEF_TYPE_SHORT_ACCUM_ID:
7213 T = Context.ShortAccumTy;
7214 break;
7215 case PREDEF_TYPE_ACCUM_ID:
7216 T = Context.AccumTy;
7217 break;
7218 case PREDEF_TYPE_LONG_ACCUM_ID:
7219 T = Context.LongAccumTy;
7220 break;
7221 case PREDEF_TYPE_USHORT_ACCUM_ID:
7222 T = Context.UnsignedShortAccumTy;
7223 break;
7224 case PREDEF_TYPE_UACCUM_ID:
7225 T = Context.UnsignedAccumTy;
7226 break;
7227 case PREDEF_TYPE_ULONG_ACCUM_ID:
7228 T = Context.UnsignedLongAccumTy;
7229 break;
7230 case PREDEF_TYPE_SHORT_FRACT_ID:
7231 T = Context.ShortFractTy;
7232 break;
7233 case PREDEF_TYPE_FRACT_ID:
7234 T = Context.FractTy;
7235 break;
7236 case PREDEF_TYPE_LONG_FRACT_ID:
7237 T = Context.LongFractTy;
7238 break;
7239 case PREDEF_TYPE_USHORT_FRACT_ID:
7240 T = Context.UnsignedShortFractTy;
7241 break;
7242 case PREDEF_TYPE_UFRACT_ID:
7243 T = Context.UnsignedFractTy;
7244 break;
7245 case PREDEF_TYPE_ULONG_FRACT_ID:
7246 T = Context.UnsignedLongFractTy;
7247 break;
7248 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7249 T = Context.SatShortAccumTy;
7250 break;
7251 case PREDEF_TYPE_SAT_ACCUM_ID:
7252 T = Context.SatAccumTy;
7253 break;
7254 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7255 T = Context.SatLongAccumTy;
7256 break;
7257 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7258 T = Context.SatUnsignedShortAccumTy;
7259 break;
7260 case PREDEF_TYPE_SAT_UACCUM_ID:
7261 T = Context.SatUnsignedAccumTy;
7262 break;
7263 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7264 T = Context.SatUnsignedLongAccumTy;
7265 break;
7266 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7267 T = Context.SatShortFractTy;
7268 break;
7269 case PREDEF_TYPE_SAT_FRACT_ID:
7270 T = Context.SatFractTy;
7271 break;
7272 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7273 T = Context.SatLongFractTy;
7274 break;
7275 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7276 T = Context.SatUnsignedShortFractTy;
7277 break;
7278 case PREDEF_TYPE_SAT_UFRACT_ID:
7279 T = Context.SatUnsignedFractTy;
7280 break;
7281 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7282 T = Context.SatUnsignedLongFractTy;
7283 break;
7284 case PREDEF_TYPE_FLOAT16_ID:
7285 T = Context.Float16Ty;
7286 break;
7287 case PREDEF_TYPE_FLOAT128_ID:
7288 T = Context.Float128Ty;
7289 break;
7290 case PREDEF_TYPE_IBM128_ID:
7291 T = Context.Ibm128Ty;
7292 break;
7293 case PREDEF_TYPE_OVERLOAD_ID:
7294 T = Context.OverloadTy;
7295 break;
7296 case PREDEF_TYPE_BOUND_MEMBER:
7297 T = Context.BoundMemberTy;
7298 break;
7299 case PREDEF_TYPE_PSEUDO_OBJECT:
7300 T = Context.PseudoObjectTy;
7301 break;
7302 case PREDEF_TYPE_DEPENDENT_ID:
7303 T = Context.DependentTy;
7304 break;
7305 case PREDEF_TYPE_UNKNOWN_ANY:
7306 T = Context.UnknownAnyTy;
7307 break;
7308 case PREDEF_TYPE_NULLPTR_ID:
7309 T = Context.NullPtrTy;
7310 break;
7311 case PREDEF_TYPE_CHAR8_ID:
7312 T = Context.Char8Ty;
7313 break;
7314 case PREDEF_TYPE_CHAR16_ID:
7315 T = Context.Char16Ty;
7316 break;
7317 case PREDEF_TYPE_CHAR32_ID:
7318 T = Context.Char32Ty;
7319 break;
7320 case PREDEF_TYPE_OBJC_ID:
7321 T = Context.ObjCBuiltinIdTy;
7322 break;
7323 case PREDEF_TYPE_OBJC_CLASS:
7324 T = Context.ObjCBuiltinClassTy;
7325 break;
7326 case PREDEF_TYPE_OBJC_SEL:
7327 T = Context.ObjCBuiltinSelTy;
7328 break;
7329#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7330 case PREDEF_TYPE_##Id##_ID: \
7331 T = Context.SingletonId; \
7332 break;
7333#include "clang/Basic/OpenCLImageTypes.def"
7334#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7335 case PREDEF_TYPE_##Id##_ID: \
7336 T = Context.Id##Ty; \
7337 break;
7338#include "clang/Basic/OpenCLExtensionTypes.def"
7339 case PREDEF_TYPE_SAMPLER_ID:
7340 T = Context.OCLSamplerTy;
7341 break;
7342 case PREDEF_TYPE_EVENT_ID:
7343 T = Context.OCLEventTy;
7344 break;
7345 case PREDEF_TYPE_CLK_EVENT_ID:
7346 T = Context.OCLClkEventTy;
7347 break;
7348 case PREDEF_TYPE_QUEUE_ID:
7349 T = Context.OCLQueueTy;
7350 break;
7351 case PREDEF_TYPE_RESERVE_ID_ID:
7352 T = Context.OCLReserveIDTy;
7353 break;
7354 case PREDEF_TYPE_AUTO_DEDUCT:
7355 T = Context.getAutoDeductType();
7356 break;
7357 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7358 T = Context.getAutoRRefDeductType();
7359 break;
7360 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7361 T = Context.ARCUnbridgedCastTy;
7362 break;
7363 case PREDEF_TYPE_BUILTIN_FN:
7364 T = Context.BuiltinFnTy;
7365 break;
7366 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7367 T = Context.IncompleteMatrixIdxTy;
7368 break;
7369 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7370 T = Context.OMPArraySectionTy;
7371 break;
7372 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7373 T = Context.OMPArraySectionTy;
7374 break;
7375 case PREDEF_TYPE_OMP_ITERATOR:
7376 T = Context.OMPIteratorTy;
7377 break;
7378#define SVE_TYPE(Name, Id, SingletonId) \
7379 case PREDEF_TYPE_##Id##_ID: \
7380 T = Context.SingletonId; \
7381 break;
7382#include "clang/Basic/AArch64SVEACLETypes.def"
7383#define PPC_VECTOR_TYPE(Name, Id, Size) \
7384 case PREDEF_TYPE_##Id##_ID: \
7385 T = Context.Id##Ty; \
7386 break;
7387#include "clang/Basic/PPCTypes.def"
7388#define RVV_TYPE(Name, Id, SingletonId) \
7389 case PREDEF_TYPE_##Id##_ID: \
7390 T = Context.SingletonId; \
7391 break;
7392#include "clang/Basic/RISCVVTypes.def"
7393#define WASM_TYPE(Name, Id, SingletonId) \
7394 case PREDEF_TYPE_##Id##_ID: \
7395 T = Context.SingletonId; \
7396 break;
7397#include "clang/Basic/WebAssemblyReferenceTypes.def"
7398 }
7399
7400 assert(!T.isNull() && "Unknown predefined type");
7401 return T.withFastQualifiers(TQs: FastQuals);
7402 }
7403
7404 Index -= NUM_PREDEF_TYPE_IDS;
7405 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7406 if (TypesLoaded[Index].isNull()) {
7407 TypesLoaded[Index] = readTypeRecord(Index);
7408 if (TypesLoaded[Index].isNull())
7409 return QualType();
7410
7411 TypesLoaded[Index]->setFromAST();
7412 if (DeserializationListener)
7413 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
7414 T: TypesLoaded[Index]);
7415 }
7416
7417 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
7418}
7419
7420QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7421 return GetType(ID: getGlobalTypeID(F, LocalID));
7422}
7423
7424serialization::TypeID
7425ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7426 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7427 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7428
7429 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7430 return LocalID;
7431
7432 if (!F.ModuleOffsetMap.empty())
7433 ReadModuleOffsetMap(F);
7434
7435 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7436 = F.TypeRemap.find(K: LocalIndex - NUM_PREDEF_TYPE_IDS);
7437 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7438
7439 unsigned GlobalIndex = LocalIndex + I->second;
7440 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7441}
7442
7443TemplateArgumentLocInfo
7444ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7445 switch (Kind) {
7446 case TemplateArgument::Expression:
7447 return readExpr();
7448 case TemplateArgument::Type:
7449 return readTypeSourceInfo();
7450 case TemplateArgument::Template: {
7451 NestedNameSpecifierLoc QualifierLoc =
7452 readNestedNameSpecifierLoc();
7453 SourceLocation TemplateNameLoc = readSourceLocation();
7454 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7455 TemplateNameLoc, SourceLocation());
7456 }
7457 case TemplateArgument::TemplateExpansion: {
7458 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7459 SourceLocation TemplateNameLoc = readSourceLocation();
7460 SourceLocation EllipsisLoc = readSourceLocation();
7461 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7462 TemplateNameLoc, EllipsisLoc);
7463 }
7464 case TemplateArgument::Null:
7465 case TemplateArgument::Integral:
7466 case TemplateArgument::Declaration:
7467 case TemplateArgument::NullPtr:
7468 case TemplateArgument::StructuralValue:
7469 case TemplateArgument::Pack:
7470 // FIXME: Is this right?
7471 return TemplateArgumentLocInfo();
7472 }
7473 llvm_unreachable("unexpected template argument loc");
7474}
7475
7476TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7477 TemplateArgument Arg = readTemplateArgument();
7478
7479 if (Arg.getKind() == TemplateArgument::Expression) {
7480 if (readBool()) // bool InfoHasSameExpr.
7481 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7482 }
7483 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
7484}
7485
7486void ASTRecordReader::readTemplateArgumentListInfo(
7487 TemplateArgumentListInfo &Result) {
7488 Result.setLAngleLoc(readSourceLocation());
7489 Result.setRAngleLoc(readSourceLocation());
7490 unsigned NumArgsAsWritten = readInt();
7491 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7492 Result.addArgument(Loc: readTemplateArgumentLoc());
7493}
7494
7495const ASTTemplateArgumentListInfo *
7496ASTRecordReader::readASTTemplateArgumentListInfo() {
7497 TemplateArgumentListInfo Result;
7498 readTemplateArgumentListInfo(Result);
7499 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
7500}
7501
7502Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7503 return GetDecl(ID);
7504}
7505
7506void ASTReader::CompleteRedeclChain(const Decl *D) {
7507 if (NumCurrentElementsDeserializing) {
7508 // We arrange to not care about the complete redeclaration chain while we're
7509 // deserializing. Just remember that the AST has marked this one as complete
7510 // but that it's not actually complete yet, so we know we still need to
7511 // complete it later.
7512 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
7513 return;
7514 }
7515
7516 if (!D->getDeclContext()) {
7517 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7518 return;
7519 }
7520
7521 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7522
7523 // If this is a named declaration, complete it by looking it up
7524 // within its context.
7525 //
7526 // FIXME: Merging a function definition should merge
7527 // all mergeable entities within it.
7528 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
7529 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
7530 if (!getContext().getLangOpts().CPlusPlus &&
7531 isa<TranslationUnitDecl>(Val: DC)) {
7532 // Outside of C++, we don't have a lookup table for the TU, so update
7533 // the identifier instead. (For C++ modules, we don't store decls
7534 // in the serialized identifier table, so we do the lookup in the TU.)
7535 auto *II = Name.getAsIdentifierInfo();
7536 assert(II && "non-identifier name in C?");
7537 if (II->isOutOfDate())
7538 updateOutOfDateIdentifier(II&: *II);
7539 } else
7540 DC->lookup(Name);
7541 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
7542 // Find all declarations of this kind from the relevant context.
7543 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
7544 auto *DC = cast<DeclContext>(Val: DCDecl);
7545 SmallVector<Decl*, 8> Decls;
7546 FindExternalLexicalDecls(
7547 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7548 }
7549 }
7550 }
7551
7552 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D))
7553 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7554 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D))
7555 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7556 if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
7557 if (auto *Template = FD->getPrimaryTemplate())
7558 Template->LoadLazySpecializations();
7559 }
7560}
7561
7562CXXCtorInitializer **
7563ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7564 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
7565 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7566 SavedStreamPosition SavedPosition(Cursor);
7567 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
7568 Error(Err: std::move(Err));
7569 return nullptr;
7570 }
7571 ReadingKindTracker ReadingKind(Read_Decl, *this);
7572 Deserializing D(this);
7573
7574 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7575 if (!MaybeCode) {
7576 Error(Err: MaybeCode.takeError());
7577 return nullptr;
7578 }
7579 unsigned Code = MaybeCode.get();
7580
7581 ASTRecordReader Record(*this, *Loc.F);
7582 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
7583 if (!MaybeRecCode) {
7584 Error(Err: MaybeRecCode.takeError());
7585 return nullptr;
7586 }
7587 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7588 Error(Msg: "malformed AST file: missing C++ ctor initializers");
7589 return nullptr;
7590 }
7591
7592 return Record.readCXXCtorInitializers();
7593}
7594
7595CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7596 assert(ContextObj && "reading base specifiers with no AST context");
7597 ASTContext &Context = *ContextObj;
7598
7599 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
7600 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7601 SavedStreamPosition SavedPosition(Cursor);
7602 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
7603 Error(Err: std::move(Err));
7604 return nullptr;
7605 }
7606 ReadingKindTracker ReadingKind(Read_Decl, *this);
7607 Deserializing D(this);
7608
7609 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7610 if (!MaybeCode) {
7611 Error(Err: MaybeCode.takeError());
7612 return nullptr;
7613 }
7614 unsigned Code = MaybeCode.get();
7615
7616 ASTRecordReader Record(*this, *Loc.F);
7617 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
7618 if (!MaybeRecCode) {
7619 Error(Err: MaybeCode.takeError());
7620 return nullptr;
7621 }
7622 unsigned RecCode = MaybeRecCode.get();
7623
7624 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7625 Error(Msg: "malformed AST file: missing C++ base specifiers");
7626 return nullptr;
7627 }
7628
7629 unsigned NumBases = Record.readInt();
7630 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
7631 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7632 for (unsigned I = 0; I != NumBases; ++I)
7633 Bases[I] = Record.readCXXBaseSpecifier();
7634 return Bases;
7635}
7636
7637serialization::DeclID
7638ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7639 if (LocalID < NUM_PREDEF_DECL_IDS)
7640 return LocalID;
7641
7642 if (!F.ModuleOffsetMap.empty())
7643 ReadModuleOffsetMap(F);
7644
7645 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7646 = F.DeclRemap.find(K: LocalID - NUM_PREDEF_DECL_IDS);
7647 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7648
7649 return LocalID + I->second;
7650}
7651
7652bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7653 ModuleFile &M) const {
7654 // Predefined decls aren't from any module.
7655 if (ID < NUM_PREDEF_DECL_IDS)
7656 return false;
7657
7658 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7659 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7660}
7661
7662ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7663 if (!D->isFromASTFile())
7664 return nullptr;
7665 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(K: D->getGlobalID());
7666 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7667 return I->second;
7668}
7669
7670SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7671 if (ID < NUM_PREDEF_DECL_IDS)
7672 return SourceLocation();
7673
7674 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7675
7676 if (Index > DeclsLoaded.size()) {
7677 Error(Msg: "declaration ID out-of-range for AST file");
7678 return SourceLocation();
7679 }
7680
7681 if (Decl *D = DeclsLoaded[Index])
7682 return D->getLocation();
7683
7684 SourceLocation Loc;
7685 DeclCursorForID(ID, Location&: Loc);
7686 return Loc;
7687}
7688
7689static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7690 switch (ID) {
7691 case PREDEF_DECL_NULL_ID:
7692 return nullptr;
7693
7694 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7695 return Context.getTranslationUnitDecl();
7696
7697 case PREDEF_DECL_OBJC_ID_ID:
7698 return Context.getObjCIdDecl();
7699
7700 case PREDEF_DECL_OBJC_SEL_ID:
7701 return Context.getObjCSelDecl();
7702
7703 case PREDEF_DECL_OBJC_CLASS_ID:
7704 return Context.getObjCClassDecl();
7705
7706 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7707 return Context.getObjCProtocolDecl();
7708
7709 case PREDEF_DECL_INT_128_ID:
7710 return Context.getInt128Decl();
7711
7712 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7713 return Context.getUInt128Decl();
7714
7715 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7716 return Context.getObjCInstanceTypeDecl();
7717
7718 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7719 return Context.getBuiltinVaListDecl();
7720
7721 case PREDEF_DECL_VA_LIST_TAG:
7722 return Context.getVaListTagDecl();
7723
7724 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7725 return Context.getBuiltinMSVaListDecl();
7726
7727 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7728 return Context.getMSGuidTagDecl();
7729
7730 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7731 return Context.getExternCContextDecl();
7732
7733 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7734 return Context.getMakeIntegerSeqDecl();
7735
7736 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7737 return Context.getCFConstantStringDecl();
7738
7739 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7740 return Context.getCFConstantStringTagDecl();
7741
7742 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7743 return Context.getTypePackElementDecl();
7744 }
7745 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7746}
7747
7748Decl *ASTReader::GetExistingDecl(DeclID ID) {
7749 assert(ContextObj && "reading decl with no AST context");
7750 if (ID < NUM_PREDEF_DECL_IDS) {
7751 Decl *D = getPredefinedDecl(Context&: *ContextObj, ID: (PredefinedDeclIDs)ID);
7752 if (D) {
7753 // Track that we have merged the declaration with ID \p ID into the
7754 // pre-existing predefined declaration \p D.
7755 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7756 if (Merged.empty())
7757 Merged.push_back(Elt: ID);
7758 }
7759 return D;
7760 }
7761
7762 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7763
7764 if (Index >= DeclsLoaded.size()) {
7765 assert(0 && "declaration ID out-of-range for AST file");
7766 Error(Msg: "declaration ID out-of-range for AST file");
7767 return nullptr;
7768 }
7769
7770 return DeclsLoaded[Index];
7771}
7772
7773Decl *ASTReader::GetDecl(DeclID ID) {
7774 if (ID < NUM_PREDEF_DECL_IDS)
7775 return GetExistingDecl(ID);
7776
7777 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7778
7779 if (Index >= DeclsLoaded.size()) {
7780 assert(0 && "declaration ID out-of-range for AST file");
7781 Error(Msg: "declaration ID out-of-range for AST file");
7782 return nullptr;
7783 }
7784
7785 if (!DeclsLoaded[Index]) {
7786 ReadDeclRecord(ID);
7787 if (DeserializationListener)
7788 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
7789 }
7790
7791 return DeclsLoaded[Index];
7792}
7793
7794DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7795 DeclID GlobalID) {
7796 if (GlobalID < NUM_PREDEF_DECL_IDS)
7797 return GlobalID;
7798
7799 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(K: GlobalID);
7800 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7801 ModuleFile *Owner = I->second;
7802
7803 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7804 = M.GlobalToLocalDeclIDs.find(Val: Owner);
7805 if (Pos == M.GlobalToLocalDeclIDs.end())
7806 return 0;
7807
7808 return GlobalID - Owner->BaseDeclID + Pos->second;
7809}
7810
7811serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7812 const RecordData &Record,
7813 unsigned &Idx) {
7814 if (Idx >= Record.size()) {
7815 Error(Msg: "Corrupted AST file");
7816 return 0;
7817 }
7818
7819 return getGlobalDeclID(F, LocalID: Record[Idx++]);
7820}
7821
7822/// Resolve the offset of a statement into a statement.
7823///
7824/// This operation will read a new statement from the external
7825/// source each time it is called, and is meant to be used via a
7826/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7827Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7828 // Switch case IDs are per Decl.
7829 ClearSwitchCaseIDs();
7830
7831 // Offset here is a global offset across the entire chain.
7832 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
7833 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7834 Error(Err: std::move(Err));
7835 return nullptr;
7836 }
7837 assert(NumCurrentElementsDeserializing == 0 &&
7838 "should not be called while already deserializing");
7839 Deserializing D(this);
7840 return ReadStmtFromStream(F&: *Loc.F);
7841}
7842
7843void ASTReader::FindExternalLexicalDecls(
7844 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7845 SmallVectorImpl<Decl *> &Decls) {
7846 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7847
7848 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7849 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7850 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7851 auto K = (Decl::Kind)+LexicalDecls[I];
7852 if (!IsKindWeWant(K))
7853 continue;
7854
7855 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7856
7857 // Don't add predefined declarations to the lexical context more
7858 // than once.
7859 if (ID < NUM_PREDEF_DECL_IDS) {
7860 if (PredefsVisited[ID])
7861 continue;
7862
7863 PredefsVisited[ID] = true;
7864 }
7865
7866 if (Decl *D = GetLocalDecl(F&: *M, LocalID: ID)) {
7867 assert(D->getKind() == K && "wrong kind for lexical decl");
7868 if (!DC->isDeclInLexicalTraversal(D))
7869 Decls.push_back(Elt: D);
7870 }
7871 }
7872 };
7873
7874 if (isa<TranslationUnitDecl>(Val: DC)) {
7875 for (const auto &Lexical : TULexicalDecls)
7876 Visit(Lexical.first, Lexical.second);
7877 } else {
7878 auto I = LexicalDecls.find(Val: DC);
7879 if (I != LexicalDecls.end())
7880 Visit(I->second.first, I->second.second);
7881 }
7882
7883 ++NumLexicalDeclContextsRead;
7884}
7885
7886namespace {
7887
7888class DeclIDComp {
7889 ASTReader &Reader;
7890 ModuleFile &Mod;
7891
7892public:
7893 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7894
7895 bool operator()(LocalDeclID L, LocalDeclID R) const {
7896 SourceLocation LHS = getLocation(ID: L);
7897 SourceLocation RHS = getLocation(ID: R);
7898 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7899 }
7900
7901 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7902 SourceLocation RHS = getLocation(ID: R);
7903 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7904 }
7905
7906 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7907 SourceLocation LHS = getLocation(ID: L);
7908 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7909 }
7910
7911 SourceLocation getLocation(LocalDeclID ID) const {
7912 return Reader.getSourceManager().getFileLoc(
7913 Loc: Reader.getSourceLocationForDeclID(ID: Reader.getGlobalDeclID(F&: Mod, LocalID: ID)));
7914 }
7915};
7916
7917} // namespace
7918
7919void ASTReader::FindFileRegionDecls(FileID File,
7920 unsigned Offset, unsigned Length,
7921 SmallVectorImpl<Decl *> &Decls) {
7922 SourceManager &SM = getSourceManager();
7923
7924 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
7925 if (I == FileDeclIDs.end())
7926 return;
7927
7928 FileDeclsInfo &DInfo = I->second;
7929 if (DInfo.Decls.empty())
7930 return;
7931
7932 SourceLocation
7933 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
7934 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
7935
7936 DeclIDComp DIDComp(*this, *DInfo.Mod);
7937 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7938 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
7939 if (BeginIt != DInfo.Decls.begin())
7940 --BeginIt;
7941
7942 // If we are pointing at a top-level decl inside an objc container, we need
7943 // to backtrack until we find it otherwise we will fail to report that the
7944 // region overlaps with an objc container.
7945 while (BeginIt != DInfo.Decls.begin() &&
7946 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod, LocalID: *BeginIt))
7947 ->isTopLevelDeclInObjCContainer())
7948 --BeginIt;
7949
7950 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7951 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
7952 if (EndIt != DInfo.Decls.end())
7953 ++EndIt;
7954
7955 for (ArrayRef<serialization::LocalDeclID>::iterator
7956 DIt = BeginIt; DIt != EndIt; ++DIt)
7957 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod, LocalID: *DIt)));
7958}
7959
7960bool
7961ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7962 DeclarationName Name) {
7963 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7964 "DeclContext has no visible decls in storage");
7965 if (!Name)
7966 return false;
7967
7968 auto It = Lookups.find(Val: DC);
7969 if (It == Lookups.end())
7970 return false;
7971
7972 Deserializing LookupResults(this);
7973
7974 // Load the list of declarations.
7975 SmallVector<NamedDecl *, 64> Decls;
7976 llvm::SmallPtrSet<NamedDecl *, 8> Found;
7977 for (DeclID ID : It->second.Table.find(EKey: Name)) {
7978 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
7979 if (ND->getDeclName() == Name && Found.insert(Ptr: ND).second)
7980 Decls.push_back(Elt: ND);
7981 }
7982
7983 ++NumVisibleDeclContextsRead;
7984 SetExternalVisibleDeclsForName(DC, Name, Decls);
7985 return !Decls.empty();
7986}
7987
7988void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7989 if (!DC->hasExternalVisibleStorage())
7990 return;
7991
7992 auto It = Lookups.find(Val: DC);
7993 assert(It != Lookups.end() &&
7994 "have external visible storage but no lookup tables");
7995
7996 DeclsMap Decls;
7997
7998 for (DeclID ID : It->second.Table.findAll()) {
7999 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8000 Decls[ND->getDeclName()].push_back(Elt: ND);
8001 }
8002
8003 ++NumVisibleDeclContextsRead;
8004
8005 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8006 SetExternalVisibleDeclsForName(DC, Name: I->first, Decls: I->second);
8007 }
8008 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8009}
8010
8011const serialization::reader::DeclContextLookupTable *
8012ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8013 auto I = Lookups.find(Val: Primary);
8014 return I == Lookups.end() ? nullptr : &I->second;
8015}
8016
8017/// Under non-PCH compilation the consumer receives the objc methods
8018/// before receiving the implementation, and codegen depends on this.
8019/// We simulate this by deserializing and passing to consumer the methods of the
8020/// implementation before passing the deserialized implementation decl.
8021static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8022 ASTConsumer *Consumer) {
8023 assert(ImplD && Consumer);
8024
8025 for (auto *I : ImplD->methods())
8026 Consumer->HandleInterestingDecl(DeclGroupRef(I));
8027
8028 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
8029}
8030
8031void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8032 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
8033 PassObjCImplDeclToConsumer(ImplD, Consumer);
8034 else
8035 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
8036}
8037
8038void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8039 this->Consumer = Consumer;
8040
8041 if (Consumer)
8042 PassInterestingDeclsToConsumer();
8043
8044 if (DeserializationListener)
8045 DeserializationListener->ReaderInitialized(Reader: this);
8046}
8047
8048void ASTReader::PrintStats() {
8049 std::fprintf(stderr, format: "*** AST File Statistics:\n");
8050
8051 unsigned NumTypesLoaded =
8052 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
8053 unsigned NumDeclsLoaded =
8054 DeclsLoaded.size() -
8055 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
8056 unsigned NumIdentifiersLoaded =
8057 IdentifiersLoaded.size() -
8058 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
8059 unsigned NumMacrosLoaded =
8060 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
8061 unsigned NumSelectorsLoaded =
8062 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
8063
8064 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8065 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
8066 NumSLocEntriesRead, TotalNumSLocEntries,
8067 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8068 if (!TypesLoaded.empty())
8069 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
8070 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8071 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8072 if (!DeclsLoaded.empty())
8073 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
8074 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8075 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8076 if (!IdentifiersLoaded.empty())
8077 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
8078 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8079 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8080 if (!MacrosLoaded.empty())
8081 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8082 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8083 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8084 if (!SelectorsLoaded.empty())
8085 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
8086 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8087 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8088 if (TotalNumStatements)
8089 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
8090 NumStatementsRead, TotalNumStatements,
8091 ((float)NumStatementsRead/TotalNumStatements * 100));
8092 if (TotalNumMacros)
8093 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8094 NumMacrosRead, TotalNumMacros,
8095 ((float)NumMacrosRead/TotalNumMacros * 100));
8096 if (TotalLexicalDeclContexts)
8097 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
8098 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8099 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8100 * 100));
8101 if (TotalVisibleDeclContexts)
8102 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
8103 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8104 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8105 * 100));
8106 if (TotalNumMethodPoolEntries)
8107 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
8108 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8109 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8110 * 100));
8111 if (NumMethodPoolLookups)
8112 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
8113 NumMethodPoolHits, NumMethodPoolLookups,
8114 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8115 if (NumMethodPoolTableLookups)
8116 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
8117 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8118 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8119 * 100.0));
8120 if (NumIdentifierLookupHits)
8121 std::fprintf(stderr,
8122 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
8123 NumIdentifierLookupHits, NumIdentifierLookups,
8124 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8125
8126 if (GlobalIndex) {
8127 std::fprintf(stderr, format: "\n");
8128 GlobalIndex->printStats();
8129 }
8130
8131 std::fprintf(stderr, format: "\n");
8132 dump();
8133 std::fprintf(stderr, format: "\n");
8134}
8135
8136template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8137LLVM_DUMP_METHOD static void
8138dumpModuleIDMap(StringRef Name,
8139 const ContinuousRangeMap<Key, ModuleFile *,
8140 InitialCapacity> &Map) {
8141 if (Map.begin() == Map.end())
8142 return;
8143
8144 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8145
8146 llvm::errs() << Name << ":\n";
8147 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8148 I != IEnd; ++I) {
8149 llvm::errs() << " " << I->first << " -> " << I->second->FileName
8150 << "\n";
8151 }
8152}
8153
8154LLVM_DUMP_METHOD void ASTReader::dump() {
8155 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8156 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
8157 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
8158 dumpModuleIDMap(Name: "Global type map", Map: GlobalTypeMap);
8159 dumpModuleIDMap(Name: "Global declaration map", Map: GlobalDeclMap);
8160 dumpModuleIDMap(Name: "Global identifier map", Map: GlobalIdentifierMap);
8161 dumpModuleIDMap(Name: "Global macro map", Map: GlobalMacroMap);
8162 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
8163 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
8164 dumpModuleIDMap(Name: "Global preprocessed entity map",
8165 Map: GlobalPreprocessedEntityMap);
8166
8167 llvm::errs() << "\n*** PCH/Modules Loaded:";
8168 for (ModuleFile &M : ModuleMgr)
8169 M.dump();
8170}
8171
8172/// Return the amount of memory used by memory buffers, breaking down
8173/// by heap-backed versus mmap'ed memory.
8174void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8175 for (ModuleFile &I : ModuleMgr) {
8176 if (llvm::MemoryBuffer *buf = I.Buffer) {
8177 size_t bytes = buf->getBufferSize();
8178 switch (buf->getBufferKind()) {
8179 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8180 sizes.malloc_bytes += bytes;
8181 break;
8182 case llvm::MemoryBuffer::MemoryBuffer_MMap:
8183 sizes.mmap_bytes += bytes;
8184 break;
8185 }
8186 }
8187 }
8188}
8189
8190void ASTReader::InitializeSema(Sema &S) {
8191 SemaObj = &S;
8192 S.addExternalSource(E: this);
8193
8194 // Makes sure any declarations that were deserialized "too early"
8195 // still get added to the identifier's declaration chains.
8196 for (uint64_t ID : PreloadedDeclIDs) {
8197 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
8198 pushExternalDeclIntoScope(D, Name: D->getDeclName());
8199 }
8200 PreloadedDeclIDs.clear();
8201
8202 // FIXME: What happens if these are changed by a module import?
8203 if (!FPPragmaOptions.empty()) {
8204 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8205 FPOptionsOverride NewOverrides =
8206 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
8207 SemaObj->CurFPFeatures =
8208 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
8209 }
8210
8211 SemaObj->OpenCLFeatures = OpenCLExtensions;
8212
8213 UpdateSema();
8214}
8215
8216void ASTReader::UpdateSema() {
8217 assert(SemaObj && "no Sema to update");
8218
8219 // Load the offsets of the declarations that Sema references.
8220 // They will be lazily deserialized when needed.
8221 if (!SemaDeclRefs.empty()) {
8222 assert(SemaDeclRefs.size() % 3 == 0);
8223 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8224 if (!SemaObj->StdNamespace)
8225 SemaObj->StdNamespace = SemaDeclRefs[I];
8226 if (!SemaObj->StdBadAlloc)
8227 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8228 if (!SemaObj->StdAlignValT)
8229 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8230 }
8231 SemaDeclRefs.clear();
8232 }
8233
8234 // Update the state of pragmas. Use the same API as if we had encountered the
8235 // pragma in the source.
8236 if(OptimizeOffPragmaLocation.isValid())
8237 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
8238 if (PragmaMSStructState != -1)
8239 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
8240 if (PointersToMembersPragmaLocation.isValid()) {
8241 SemaObj->ActOnPragmaMSPointersToMembers(
8242 Kind: (LangOptions::PragmaMSPointersToMembersKind)
8243 PragmaMSPointersToMembersState,
8244 PragmaLoc: PointersToMembersPragmaLocation);
8245 }
8246 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8247
8248 if (PragmaAlignPackCurrentValue) {
8249 // The bottom of the stack might have a default value. It must be adjusted
8250 // to the current value to ensure that the packing state is preserved after
8251 // popping entries that were included/imported from a PCH/module.
8252 bool DropFirst = false;
8253 if (!PragmaAlignPackStack.empty() &&
8254 PragmaAlignPackStack.front().Location.isInvalid()) {
8255 assert(PragmaAlignPackStack.front().Value ==
8256 SemaObj->AlignPackStack.DefaultValue &&
8257 "Expected a default alignment value");
8258 SemaObj->AlignPackStack.Stack.emplace_back(
8259 Args&: PragmaAlignPackStack.front().SlotLabel,
8260 Args&: SemaObj->AlignPackStack.CurrentValue,
8261 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
8262 Args&: PragmaAlignPackStack.front().PushLocation);
8263 DropFirst = true;
8264 }
8265 for (const auto &Entry :
8266 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
8267 SemaObj->AlignPackStack.Stack.emplace_back(
8268 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
8269 }
8270 if (PragmaAlignPackCurrentLocation.isInvalid()) {
8271 assert(*PragmaAlignPackCurrentValue ==
8272 SemaObj->AlignPackStack.DefaultValue &&
8273 "Expected a default align and pack value");
8274 // Keep the current values.
8275 } else {
8276 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
8277 SemaObj->AlignPackStack.CurrentPragmaLocation =
8278 PragmaAlignPackCurrentLocation;
8279 }
8280 }
8281 if (FpPragmaCurrentValue) {
8282 // The bottom of the stack might have a default value. It must be adjusted
8283 // to the current value to ensure that fp-pragma state is preserved after
8284 // popping entries that were included/imported from a PCH/module.
8285 bool DropFirst = false;
8286 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
8287 assert(FpPragmaStack.front().Value ==
8288 SemaObj->FpPragmaStack.DefaultValue &&
8289 "Expected a default pragma float_control value");
8290 SemaObj->FpPragmaStack.Stack.emplace_back(
8291 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
8292 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
8293 Args&: FpPragmaStack.front().PushLocation);
8294 DropFirst = true;
8295 }
8296 for (const auto &Entry :
8297 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
8298 SemaObj->FpPragmaStack.Stack.emplace_back(
8299 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
8300 if (FpPragmaCurrentLocation.isInvalid()) {
8301 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8302 "Expected a default pragma float_control value");
8303 // Keep the current values.
8304 } else {
8305 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8306 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8307 }
8308 }
8309
8310 // For non-modular AST files, restore visiblity of modules.
8311 for (auto &Import : PendingImportedModulesSema) {
8312 if (Import.ImportLoc.isInvalid())
8313 continue;
8314 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
8315 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
8316 }
8317 }
8318 PendingImportedModulesSema.clear();
8319}
8320
8321IdentifierInfo *ASTReader::get(StringRef Name) {
8322 // Note that we are loading an identifier.
8323 Deserializing AnIdentifier(this);
8324
8325 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8326 NumIdentifierLookups,
8327 NumIdentifierLookupHits);
8328
8329 // We don't need to do identifier table lookups in C++ modules (we preload
8330 // all interesting declarations, and don't need to use the scope for name
8331 // lookups). Perform the lookup in PCH files, though, since we don't build
8332 // a complete initial identifier table if we're carrying on from a PCH.
8333 if (PP.getLangOpts().CPlusPlus) {
8334 for (auto *F : ModuleMgr.pch_modules())
8335 if (Visitor(*F))
8336 break;
8337 } else {
8338 // If there is a global index, look there first to determine which modules
8339 // provably do not have any results for this identifier.
8340 GlobalModuleIndex::HitSet Hits;
8341 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8342 if (!loadGlobalIndex()) {
8343 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8344 HitsPtr = &Hits;
8345 }
8346 }
8347
8348 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
8349 }
8350
8351 IdentifierInfo *II = Visitor.getIdentifierInfo();
8352 markIdentifierUpToDate(II);
8353 return II;
8354}
8355
8356namespace clang {
8357
8358 /// An identifier-lookup iterator that enumerates all of the
8359 /// identifiers stored within a set of AST files.
8360 class ASTIdentifierIterator : public IdentifierIterator {
8361 /// The AST reader whose identifiers are being enumerated.
8362 const ASTReader &Reader;
8363
8364 /// The current index into the chain of AST files stored in
8365 /// the AST reader.
8366 unsigned Index;
8367
8368 /// The current position within the identifier lookup table
8369 /// of the current AST file.
8370 ASTIdentifierLookupTable::key_iterator Current;
8371
8372 /// The end position within the identifier lookup table of
8373 /// the current AST file.
8374 ASTIdentifierLookupTable::key_iterator End;
8375
8376 /// Whether to skip any modules in the ASTReader.
8377 bool SkipModules;
8378
8379 public:
8380 explicit ASTIdentifierIterator(const ASTReader &Reader,
8381 bool SkipModules = false);
8382
8383 StringRef Next() override;
8384 };
8385
8386} // namespace clang
8387
8388ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8389 bool SkipModules)
8390 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8391}
8392
8393StringRef ASTIdentifierIterator::Next() {
8394 while (Current == End) {
8395 // If we have exhausted all of our AST files, we're done.
8396 if (Index == 0)
8397 return StringRef();
8398
8399 --Index;
8400 ModuleFile &F = Reader.ModuleMgr[Index];
8401 if (SkipModules && F.isModule())
8402 continue;
8403
8404 ASTIdentifierLookupTable *IdTable =
8405 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8406 Current = IdTable->key_begin();
8407 End = IdTable->key_end();
8408 }
8409
8410 // We have any identifiers remaining in the current AST file; return
8411 // the next one.
8412 StringRef Result = *Current;
8413 ++Current;
8414 return Result;
8415}
8416
8417namespace {
8418
8419/// A utility for appending two IdentifierIterators.
8420class ChainedIdentifierIterator : public IdentifierIterator {
8421 std::unique_ptr<IdentifierIterator> Current;
8422 std::unique_ptr<IdentifierIterator> Queued;
8423
8424public:
8425 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8426 std::unique_ptr<IdentifierIterator> Second)
8427 : Current(std::move(First)), Queued(std::move(Second)) {}
8428
8429 StringRef Next() override {
8430 if (!Current)
8431 return StringRef();
8432
8433 StringRef result = Current->Next();
8434 if (!result.empty())
8435 return result;
8436
8437 // Try the queued iterator, which may itself be empty.
8438 Current.reset();
8439 std::swap(x&: Current, y&: Queued);
8440 return Next();
8441 }
8442};
8443
8444} // namespace
8445
8446IdentifierIterator *ASTReader::getIdentifiers() {
8447 if (!loadGlobalIndex()) {
8448 std::unique_ptr<IdentifierIterator> ReaderIter(
8449 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8450 std::unique_ptr<IdentifierIterator> ModulesIter(
8451 GlobalIndex->createIdentifierIterator());
8452 return new ChainedIdentifierIterator(std::move(ReaderIter),
8453 std::move(ModulesIter));
8454 }
8455
8456 return new ASTIdentifierIterator(*this);
8457}
8458
8459namespace clang {
8460namespace serialization {
8461
8462 class ReadMethodPoolVisitor {
8463 ASTReader &Reader;
8464 Selector Sel;
8465 unsigned PriorGeneration;
8466 unsigned InstanceBits = 0;
8467 unsigned FactoryBits = 0;
8468 bool InstanceHasMoreThanOneDecl = false;
8469 bool FactoryHasMoreThanOneDecl = false;
8470 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8471 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8472
8473 public:
8474 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8475 unsigned PriorGeneration)
8476 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8477
8478 bool operator()(ModuleFile &M) {
8479 if (!M.SelectorLookupTable)
8480 return false;
8481
8482 // If we've already searched this module file, skip it now.
8483 if (M.Generation <= PriorGeneration)
8484 return true;
8485
8486 ++Reader.NumMethodPoolTableLookups;
8487 ASTSelectorLookupTable *PoolTable
8488 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8489 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8490 if (Pos == PoolTable->end())
8491 return false;
8492
8493 ++Reader.NumMethodPoolTableHits;
8494 ++Reader.NumSelectorsRead;
8495 // FIXME: Not quite happy with the statistics here. We probably should
8496 // disable this tracking when called via LoadSelector.
8497 // Also, should entries without methods count as misses?
8498 ++Reader.NumMethodPoolEntriesRead;
8499 ASTSelectorLookupTrait::data_type Data = *Pos;
8500 if (Reader.DeserializationListener)
8501 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8502
8503 // Append methods in the reverse order, so that later we can process them
8504 // in the order they appear in the source code by iterating through
8505 // the vector in the reverse order.
8506 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
8507 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
8508 InstanceBits = Data.InstanceBits;
8509 FactoryBits = Data.FactoryBits;
8510 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8511 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8512 return false;
8513 }
8514
8515 /// Retrieve the instance methods found by this visitor.
8516 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8517 return InstanceMethods;
8518 }
8519
8520 /// Retrieve the instance methods found by this visitor.
8521 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8522 return FactoryMethods;
8523 }
8524
8525 unsigned getInstanceBits() const { return InstanceBits; }
8526 unsigned getFactoryBits() const { return FactoryBits; }
8527
8528 bool instanceHasMoreThanOneDecl() const {
8529 return InstanceHasMoreThanOneDecl;
8530 }
8531
8532 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8533 };
8534
8535} // namespace serialization
8536} // namespace clang
8537
8538/// Add the given set of methods to the method list.
8539static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8540 ObjCMethodList &List) {
8541 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
8542 S.addMethodToGlobalList(List: &List, Method: M);
8543}
8544
8545void ASTReader::ReadMethodPool(Selector Sel) {
8546 // Get the selector generation and update it to the current generation.
8547 unsigned &Generation = SelectorGeneration[Sel];
8548 unsigned PriorGeneration = Generation;
8549 Generation = getGeneration();
8550 SelectorOutOfDate[Sel] = false;
8551
8552 // Search for methods defined with this selector.
8553 ++NumMethodPoolLookups;
8554 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8555 ModuleMgr.visit(Visitor);
8556
8557 if (Visitor.getInstanceMethods().empty() &&
8558 Visitor.getFactoryMethods().empty())
8559 return;
8560
8561 ++NumMethodPoolHits;
8562
8563 if (!getSema())
8564 return;
8565
8566 Sema &S = *getSema();
8567 Sema::GlobalMethodPool::iterator Pos =
8568 S.MethodPool.insert(Val: std::make_pair(x&: Sel, y: Sema::GlobalMethodPool::Lists()))
8569 .first;
8570
8571 Pos->second.first.setBits(Visitor.getInstanceBits());
8572 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8573 Pos->second.second.setBits(Visitor.getFactoryBits());
8574 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8575
8576 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8577 // when building a module we keep every method individually and may need to
8578 // update hasMoreThanOneDecl as we add the methods.
8579 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8580 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8581}
8582
8583void ASTReader::updateOutOfDateSelector(Selector Sel) {
8584 if (SelectorOutOfDate[Sel])
8585 ReadMethodPool(Sel);
8586}
8587
8588void ASTReader::ReadKnownNamespaces(
8589 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8590 Namespaces.clear();
8591
8592 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8593 if (NamespaceDecl *Namespace
8594 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
8595 Namespaces.push_back(Elt: Namespace);
8596 }
8597}
8598
8599void ASTReader::ReadUndefinedButUsed(
8600 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8601 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8602 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: UndefinedButUsed[Idx++]));
8603 SourceLocation Loc =
8604 SourceLocation::getFromRawEncoding(Encoding: UndefinedButUsed[Idx++]);
8605 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
8606 }
8607}
8608
8609void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8610 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8611 Exprs) {
8612 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8613 FieldDecl *FD = cast<FieldDecl>(Val: GetDecl(ID: DelayedDeleteExprs[Idx++]));
8614 uint64_t Count = DelayedDeleteExprs[Idx++];
8615 for (uint64_t C = 0; C < Count; ++C) {
8616 SourceLocation DeleteLoc =
8617 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
8618 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8619 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
8620 }
8621 }
8622}
8623
8624void ASTReader::ReadTentativeDefinitions(
8625 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8626 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8627 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
8628 if (Var)
8629 TentativeDefs.push_back(Elt: Var);
8630 }
8631 TentativeDefinitions.clear();
8632}
8633
8634void ASTReader::ReadUnusedFileScopedDecls(
8635 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8636 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8637 DeclaratorDecl *D
8638 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
8639 if (D)
8640 Decls.push_back(Elt: D);
8641 }
8642 UnusedFileScopedDecls.clear();
8643}
8644
8645void ASTReader::ReadDelegatingConstructors(
8646 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8647 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8648 CXXConstructorDecl *D
8649 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
8650 if (D)
8651 Decls.push_back(Elt: D);
8652 }
8653 DelegatingCtorDecls.clear();
8654}
8655
8656void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8657 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8658 TypedefNameDecl *D
8659 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
8660 if (D)
8661 Decls.push_back(Elt: D);
8662 }
8663 ExtVectorDecls.clear();
8664}
8665
8666void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8667 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8668 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8669 ++I) {
8670 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8671 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
8672 if (D)
8673 Decls.insert(X: D);
8674 }
8675 UnusedLocalTypedefNameCandidates.clear();
8676}
8677
8678void ASTReader::ReadDeclsToCheckForDeferredDiags(
8679 llvm::SmallSetVector<Decl *, 4> &Decls) {
8680 for (auto I : DeclsToCheckForDeferredDiags) {
8681 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
8682 if (D)
8683 Decls.insert(X: D);
8684 }
8685 DeclsToCheckForDeferredDiags.clear();
8686}
8687
8688void ASTReader::ReadReferencedSelectors(
8689 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8690 if (ReferencedSelectorsData.empty())
8691 return;
8692
8693 // If there are @selector references added them to its pool. This is for
8694 // implementation of -Wselector.
8695 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8696 unsigned I = 0;
8697 while (I < DataSize) {
8698 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
8699 SourceLocation SelLoc
8700 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
8701 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
8702 }
8703 ReferencedSelectorsData.clear();
8704}
8705
8706void ASTReader::ReadWeakUndeclaredIdentifiers(
8707 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8708 if (WeakUndeclaredIdentifiers.empty())
8709 return;
8710
8711 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8712 IdentifierInfo *WeakId
8713 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
8714 IdentifierInfo *AliasId
8715 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
8716 SourceLocation Loc =
8717 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
8718 WeakInfo WI(AliasId, Loc);
8719 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
8720 }
8721 WeakUndeclaredIdentifiers.clear();
8722}
8723
8724void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8725 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8726 ExternalVTableUse VT;
8727 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: VTableUses[Idx++]));
8728 VT.Location = SourceLocation::getFromRawEncoding(Encoding: VTableUses[Idx++]);
8729 VT.DefinitionRequired = VTableUses[Idx++];
8730 VTables.push_back(Elt: VT);
8731 }
8732
8733 VTableUses.clear();
8734}
8735
8736void ASTReader::ReadPendingInstantiations(
8737 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8738 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8739 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: PendingInstantiations[Idx++]));
8740 SourceLocation Loc
8741 = SourceLocation::getFromRawEncoding(Encoding: PendingInstantiations[Idx++]);
8742
8743 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
8744 }
8745 PendingInstantiations.clear();
8746}
8747
8748void ASTReader::ReadLateParsedTemplates(
8749 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8750 &LPTMap) {
8751 for (auto &LPT : LateParsedTemplates) {
8752 ModuleFile *FMod = LPT.first;
8753 RecordDataImpl &LateParsed = LPT.second;
8754 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8755 /* In loop */) {
8756 FunctionDecl *FD =
8757 cast<FunctionDecl>(Val: GetLocalDecl(F&: *FMod, LocalID: LateParsed[Idx++]));
8758
8759 auto LT = std::make_unique<LateParsedTemplate>();
8760 LT->D = GetLocalDecl(F&: *FMod, LocalID: LateParsed[Idx++]);
8761 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
8762
8763 ModuleFile *F = getOwningModuleFile(D: LT->D);
8764 assert(F && "No module");
8765
8766 unsigned TokN = LateParsed[Idx++];
8767 LT->Toks.reserve(N: TokN);
8768 for (unsigned T = 0; T < TokN; ++T)
8769 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
8770
8771 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
8772 }
8773 }
8774
8775 LateParsedTemplates.clear();
8776}
8777
8778void ASTReader::AssignedLambdaNumbering(const CXXRecordDecl *Lambda) {
8779 if (Lambda->getLambdaContextDecl()) {
8780 // Keep track of this lambda so it can be merged with another lambda that
8781 // is loaded later.
8782 LambdaDeclarationsForMerging.insert(
8783 {{Lambda->getLambdaContextDecl()->getCanonicalDecl(),
8784 Lambda->getLambdaIndexInContext()},
8785 const_cast<CXXRecordDecl *>(Lambda)});
8786 }
8787}
8788
8789void ASTReader::LoadSelector(Selector Sel) {
8790 // It would be complicated to avoid reading the methods anyway. So don't.
8791 ReadMethodPool(Sel);
8792}
8793
8794void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8795 assert(ID && "Non-zero identifier ID required");
8796 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8797 IdentifiersLoaded[ID - 1] = II;
8798 if (DeserializationListener)
8799 DeserializationListener->IdentifierRead(ID, II);
8800}
8801
8802/// Set the globally-visible declarations associated with the given
8803/// identifier.
8804///
8805/// If the AST reader is currently in a state where the given declaration IDs
8806/// cannot safely be resolved, they are queued until it is safe to resolve
8807/// them.
8808///
8809/// \param II an IdentifierInfo that refers to one or more globally-visible
8810/// declarations.
8811///
8812/// \param DeclIDs the set of declaration IDs with the name @p II that are
8813/// visible at global scope.
8814///
8815/// \param Decls if non-null, this vector will be populated with the set of
8816/// deserialized declarations. These declarations will not be pushed into
8817/// scope.
8818void
8819ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8820 const SmallVectorImpl<uint32_t> &DeclIDs,
8821 SmallVectorImpl<Decl *> *Decls) {
8822 if (NumCurrentElementsDeserializing && !Decls) {
8823 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
8824 return;
8825 }
8826
8827 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8828 if (!SemaObj) {
8829 // Queue this declaration so that it will be added to the
8830 // translation unit scope and identifier's declaration chain
8831 // once a Sema object is known.
8832 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
8833 continue;
8834 }
8835
8836 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
8837
8838 // If we're simply supposed to record the declarations, do so now.
8839 if (Decls) {
8840 Decls->push_back(D);
8841 continue;
8842 }
8843
8844 // Introduce this declaration into the translation-unit scope
8845 // and add it to the declaration chain for this identifier, so
8846 // that (unqualified) name lookup will find it.
8847 pushExternalDeclIntoScope(D, Name: II);
8848 }
8849}
8850
8851IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8852 if (ID == 0)
8853 return nullptr;
8854
8855 if (IdentifiersLoaded.empty()) {
8856 Error(Msg: "no identifier table in AST file");
8857 return nullptr;
8858 }
8859
8860 ID -= 1;
8861 if (!IdentifiersLoaded[ID]) {
8862 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(K: ID + 1);
8863 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8864 ModuleFile *M = I->second;
8865 unsigned Index = ID - M->BaseIdentifierID;
8866 const unsigned char *Data =
8867 M->IdentifierTableData + M->IdentifierOffsets[Index];
8868
8869 ASTIdentifierLookupTrait Trait(*this, *M);
8870 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
8871 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
8872 auto &II = PP.getIdentifierTable().get(Name: Key);
8873 IdentifiersLoaded[ID] = &II;
8874 markIdentifierFromAST(Reader&: *this, II);
8875 if (DeserializationListener)
8876 DeserializationListener->IdentifierRead(ID: ID + 1, II: &II);
8877 }
8878
8879 return IdentifiersLoaded[ID];
8880}
8881
8882IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8883 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
8884}
8885
8886IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8887 if (LocalID < NUM_PREDEF_IDENT_IDS)
8888 return LocalID;
8889
8890 if (!M.ModuleOffsetMap.empty())
8891 ReadModuleOffsetMap(F&: M);
8892
8893 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8894 = M.IdentifierRemap.find(K: LocalID - NUM_PREDEF_IDENT_IDS);
8895 assert(I != M.IdentifierRemap.end()
8896 && "Invalid index into identifier index remap");
8897
8898 return LocalID + I->second;
8899}
8900
8901MacroInfo *ASTReader::getMacro(MacroID ID) {
8902 if (ID == 0)
8903 return nullptr;
8904
8905 if (MacrosLoaded.empty()) {
8906 Error(Msg: "no macro table in AST file");
8907 return nullptr;
8908 }
8909
8910 ID -= NUM_PREDEF_MACRO_IDS;
8911 if (!MacrosLoaded[ID]) {
8912 GlobalMacroMapType::iterator I
8913 = GlobalMacroMap.find(K: ID + NUM_PREDEF_MACRO_IDS);
8914 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8915 ModuleFile *M = I->second;
8916 unsigned Index = ID - M->BaseMacroID;
8917 MacrosLoaded[ID] =
8918 ReadMacroRecord(F&: *M, Offset: M->MacroOffsetsBase + M->MacroOffsets[Index]);
8919
8920 if (DeserializationListener)
8921 DeserializationListener->MacroRead(ID: ID + NUM_PREDEF_MACRO_IDS,
8922 MI: MacrosLoaded[ID]);
8923 }
8924
8925 return MacrosLoaded[ID];
8926}
8927
8928MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8929 if (LocalID < NUM_PREDEF_MACRO_IDS)
8930 return LocalID;
8931
8932 if (!M.ModuleOffsetMap.empty())
8933 ReadModuleOffsetMap(F&: M);
8934
8935 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8936 = M.MacroRemap.find(K: LocalID - NUM_PREDEF_MACRO_IDS);
8937 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8938
8939 return LocalID + I->second;
8940}
8941
8942serialization::SubmoduleID
8943ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8944 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8945 return LocalID;
8946
8947 if (!M.ModuleOffsetMap.empty())
8948 ReadModuleOffsetMap(F&: M);
8949
8950 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8951 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
8952 assert(I != M.SubmoduleRemap.end()
8953 && "Invalid index into submodule index remap");
8954
8955 return LocalID + I->second;
8956}
8957
8958Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8959 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8960 assert(GlobalID == 0 && "Unhandled global submodule ID");
8961 return nullptr;
8962 }
8963
8964 if (GlobalID > SubmodulesLoaded.size()) {
8965 Error(Msg: "submodule ID out of range in AST file");
8966 return nullptr;
8967 }
8968
8969 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8970}
8971
8972Module *ASTReader::getModule(unsigned ID) {
8973 return getSubmodule(GlobalID: ID);
8974}
8975
8976ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) {
8977 if (ID & 1) {
8978 // It's a module, look it up by submodule ID.
8979 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
8980 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8981 } else {
8982 // It's a prefix (preamble, PCH, ...). Look it up by index.
8983 unsigned IndexFromEnd = ID >> 1;
8984 assert(IndexFromEnd && "got reference to unknown module file");
8985 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8986 }
8987}
8988
8989unsigned ASTReader::getModuleFileID(ModuleFile *M) {
8990 if (!M)
8991 return 1;
8992
8993 // For a file representing a module, use the submodule ID of the top-level
8994 // module as the file ID. For any other kind of file, the number of such
8995 // files loaded beforehand will be the same on reload.
8996 // FIXME: Is this true even if we have an explicit module file and a PCH?
8997 if (M->isModule())
8998 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8999
9000 auto PCHModules = getModuleManager().pch_modules();
9001 auto I = llvm::find(Range&: PCHModules, Val: M);
9002 assert(I != PCHModules.end() && "emitting reference to unknown file");
9003 return (I - PCHModules.end()) << 1;
9004}
9005
9006std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
9007 if (Module *M = getSubmodule(GlobalID: ID))
9008 return ASTSourceDescriptor(*M);
9009
9010 // If there is only a single PCH, return it instead.
9011 // Chained PCH are not supported.
9012 const auto &PCHChain = ModuleMgr.pch_modules();
9013 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
9014 ModuleFile &MF = ModuleMgr.getPrimaryModule();
9015 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
9016 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
9017 return ASTSourceDescriptor(ModuleName,
9018 llvm::sys::path::parent_path(path: MF.FileName),
9019 FileName, MF.Signature);
9020 }
9021 return std::nullopt;
9022}
9023
9024ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
9025 auto I = DefinitionSource.find(Val: FD);
9026 if (I == DefinitionSource.end())
9027 return EK_ReplyHazy;
9028 return I->second ? EK_Never : EK_Always;
9029}
9030
9031Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9032 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
9033}
9034
9035Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9036 if (ID == 0)
9037 return Selector();
9038
9039 if (ID > SelectorsLoaded.size()) {
9040 Error(Msg: "selector ID out of range in AST file");
9041 return Selector();
9042 }
9043
9044 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9045 // Load this selector from the selector table.
9046 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
9047 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9048 ModuleFile &M = *I->second;
9049 ASTSelectorLookupTrait Trait(*this, M);
9050 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9051 SelectorsLoaded[ID - 1] =
9052 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9053 if (DeserializationListener)
9054 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
9055 }
9056
9057 return SelectorsLoaded[ID - 1];
9058}
9059
9060Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9061 return DecodeSelector(ID);
9062}
9063
9064uint32_t ASTReader::GetNumExternalSelectors() {
9065 // ID 0 (the null selector) is considered an external selector.
9066 return getTotalNumSelectors() + 1;
9067}
9068
9069serialization::SelectorID
9070ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9071 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9072 return LocalID;
9073
9074 if (!M.ModuleOffsetMap.empty())
9075 ReadModuleOffsetMap(F&: M);
9076
9077 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9078 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
9079 assert(I != M.SelectorRemap.end()
9080 && "Invalid index into selector index remap");
9081
9082 return LocalID + I->second;
9083}
9084
9085DeclarationNameLoc
9086ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
9087 switch (Name.getNameKind()) {
9088 case DeclarationName::CXXConstructorName:
9089 case DeclarationName::CXXDestructorName:
9090 case DeclarationName::CXXConversionFunctionName:
9091 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
9092
9093 case DeclarationName::CXXOperatorName:
9094 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
9095
9096 case DeclarationName::CXXLiteralOperatorName:
9097 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
9098 Loc: readSourceLocation());
9099
9100 case DeclarationName::Identifier:
9101 case DeclarationName::ObjCZeroArgSelector:
9102 case DeclarationName::ObjCOneArgSelector:
9103 case DeclarationName::ObjCMultiArgSelector:
9104 case DeclarationName::CXXUsingDirective:
9105 case DeclarationName::CXXDeductionGuideName:
9106 break;
9107 }
9108 return DeclarationNameLoc();
9109}
9110
9111DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
9112 DeclarationNameInfo NameInfo;
9113 NameInfo.setName(readDeclarationName());
9114 NameInfo.setLoc(readSourceLocation());
9115 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
9116 return NameInfo;
9117}
9118
9119void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
9120 Info.QualifierLoc = readNestedNameSpecifierLoc();
9121 unsigned NumTPLists = readInt();
9122 Info.NumTemplParamLists = NumTPLists;
9123 if (NumTPLists) {
9124 Info.TemplParamLists =
9125 new (getContext()) TemplateParameterList *[NumTPLists];
9126 for (unsigned i = 0; i != NumTPLists; ++i)
9127 Info.TemplParamLists[i] = readTemplateParameterList();
9128 }
9129}
9130
9131TemplateParameterList *
9132ASTRecordReader::readTemplateParameterList() {
9133 SourceLocation TemplateLoc = readSourceLocation();
9134 SourceLocation LAngleLoc = readSourceLocation();
9135 SourceLocation RAngleLoc = readSourceLocation();
9136
9137 unsigned NumParams = readInt();
9138 SmallVector<NamedDecl *, 16> Params;
9139 Params.reserve(N: NumParams);
9140 while (NumParams--)
9141 Params.push_back(Elt: readDeclAs<NamedDecl>());
9142
9143 bool HasRequiresClause = readBool();
9144 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
9145
9146 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9147 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9148 return TemplateParams;
9149}
9150
9151void ASTRecordReader::readTemplateArgumentList(
9152 SmallVectorImpl<TemplateArgument> &TemplArgs,
9153 bool Canonicalize) {
9154 unsigned NumTemplateArgs = readInt();
9155 TemplArgs.reserve(N: NumTemplateArgs);
9156 while (NumTemplateArgs--)
9157 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
9158}
9159
9160/// Read a UnresolvedSet structure.
9161void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
9162 unsigned NumDecls = readInt();
9163 Set.reserve(C&: getContext(), N: NumDecls);
9164 while (NumDecls--) {
9165 DeclID ID = readDeclID();
9166 AccessSpecifier AS = (AccessSpecifier) readInt();
9167 Set.addLazyDecl(C&: getContext(), ID, AS);
9168 }
9169}
9170
9171CXXBaseSpecifier
9172ASTRecordReader::readCXXBaseSpecifier() {
9173 bool isVirtual = readBool();
9174 bool isBaseOfClass = readBool();
9175 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
9176 bool inheritConstructors = readBool();
9177 TypeSourceInfo *TInfo = readTypeSourceInfo();
9178 SourceRange Range = readSourceRange();
9179 SourceLocation EllipsisLoc = readSourceLocation();
9180 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9181 EllipsisLoc);
9182 Result.setInheritConstructors(inheritConstructors);
9183 return Result;
9184}
9185
9186CXXCtorInitializer **
9187ASTRecordReader::readCXXCtorInitializers() {
9188 ASTContext &Context = getContext();
9189 unsigned NumInitializers = readInt();
9190 assert(NumInitializers && "wrote ctor initializers but have no inits");
9191 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9192 for (unsigned i = 0; i != NumInitializers; ++i) {
9193 TypeSourceInfo *TInfo = nullptr;
9194 bool IsBaseVirtual = false;
9195 FieldDecl *Member = nullptr;
9196 IndirectFieldDecl *IndirectMember = nullptr;
9197
9198 CtorInitializerType Type = (CtorInitializerType) readInt();
9199 switch (Type) {
9200 case CTOR_INITIALIZER_BASE:
9201 TInfo = readTypeSourceInfo();
9202 IsBaseVirtual = readBool();
9203 break;
9204
9205 case CTOR_INITIALIZER_DELEGATING:
9206 TInfo = readTypeSourceInfo();
9207 break;
9208
9209 case CTOR_INITIALIZER_MEMBER:
9210 Member = readDeclAs<FieldDecl>();
9211 break;
9212
9213 case CTOR_INITIALIZER_INDIRECT_MEMBER:
9214 IndirectMember = readDeclAs<IndirectFieldDecl>();
9215 break;
9216 }
9217
9218 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
9219 Expr *Init = readExpr();
9220 SourceLocation LParenLoc = readSourceLocation();
9221 SourceLocation RParenLoc = readSourceLocation();
9222
9223 CXXCtorInitializer *BOMInit;
9224 if (Type == CTOR_INITIALIZER_BASE)
9225 BOMInit = new (Context)
9226 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9227 RParenLoc, MemberOrEllipsisLoc);
9228 else if (Type == CTOR_INITIALIZER_DELEGATING)
9229 BOMInit = new (Context)
9230 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9231 else if (Member)
9232 BOMInit = new (Context)
9233 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9234 Init, RParenLoc);
9235 else
9236 BOMInit = new (Context)
9237 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9238 LParenLoc, Init, RParenLoc);
9239
9240 if (/*IsWritten*/readBool()) {
9241 unsigned SourceOrder = readInt();
9242 BOMInit->setSourceOrder(SourceOrder);
9243 }
9244
9245 CtorInitializers[i] = BOMInit;
9246 }
9247
9248 return CtorInitializers;
9249}
9250
9251NestedNameSpecifierLoc
9252ASTRecordReader::readNestedNameSpecifierLoc() {
9253 ASTContext &Context = getContext();
9254 unsigned N = readInt();
9255 NestedNameSpecifierLocBuilder Builder;
9256 for (unsigned I = 0; I != N; ++I) {
9257 auto Kind = readNestedNameSpecifierKind();
9258 switch (Kind) {
9259 case NestedNameSpecifier::Identifier: {
9260 IdentifierInfo *II = readIdentifier();
9261 SourceRange Range = readSourceRange();
9262 Builder.Extend(Context, Identifier: II, IdentifierLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9263 break;
9264 }
9265
9266 case NestedNameSpecifier::Namespace: {
9267 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
9268 SourceRange Range = readSourceRange();
9269 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9270 break;
9271 }
9272
9273 case NestedNameSpecifier::NamespaceAlias: {
9274 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
9275 SourceRange Range = readSourceRange();
9276 Builder.Extend(Context, Alias, AliasLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9277 break;
9278 }
9279
9280 case NestedNameSpecifier::TypeSpec:
9281 case NestedNameSpecifier::TypeSpecWithTemplate: {
9282 bool Template = readBool();
9283 TypeSourceInfo *T = readTypeSourceInfo();
9284 if (!T)
9285 return NestedNameSpecifierLoc();
9286 SourceLocation ColonColonLoc = readSourceLocation();
9287
9288 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9289 Builder.Extend(Context,
9290 TemplateKWLoc: Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9291 TL: T->getTypeLoc(), ColonColonLoc);
9292 break;
9293 }
9294
9295 case NestedNameSpecifier::Global: {
9296 SourceLocation ColonColonLoc = readSourceLocation();
9297 Builder.MakeGlobal(Context, ColonColonLoc);
9298 break;
9299 }
9300
9301 case NestedNameSpecifier::Super: {
9302 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
9303 SourceRange Range = readSourceRange();
9304 Builder.MakeSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9305 break;
9306 }
9307 }
9308 }
9309
9310 return Builder.getWithLocInContext(Context);
9311}
9312
9313SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9314 unsigned &Idx, LocSeq *Seq) {
9315 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx, Seq);
9316 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx, Seq);
9317 return SourceRange(beg, end);
9318}
9319
9320llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
9321 const StringRef Blob) {
9322 unsigned Count = Record[0];
9323 const char *Byte = Blob.data();
9324 llvm::BitVector Ret = llvm::BitVector(Count, false);
9325 for (unsigned I = 0; I < Count; ++Byte)
9326 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
9327 if (*Byte & (1 << Bit))
9328 Ret[I] = true;
9329 return Ret;
9330}
9331
9332/// Read a floating-point value
9333llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9334 return llvm::APFloat(Sem, readAPInt());
9335}
9336
9337// Read a string
9338std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9339 unsigned Len = Record[Idx++];
9340 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9341 Idx += Len;
9342 return Result;
9343}
9344
9345std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9346 unsigned &Idx) {
9347 std::string Filename = ReadString(Record, Idx);
9348 ResolveImportedPath(M&: F, Filename);
9349 return Filename;
9350}
9351
9352std::string ASTReader::ReadPath(StringRef BaseDirectory,
9353 const RecordData &Record, unsigned &Idx) {
9354 std::string Filename = ReadString(Record, Idx);
9355 if (!BaseDirectory.empty())
9356 ResolveImportedPath(Filename, Prefix: BaseDirectory);
9357 return Filename;
9358}
9359
9360VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9361 unsigned &Idx) {
9362 unsigned Major = Record[Idx++];
9363 unsigned Minor = Record[Idx++];
9364 unsigned Subminor = Record[Idx++];
9365 if (Minor == 0)
9366 return VersionTuple(Major);
9367 if (Subminor == 0)
9368 return VersionTuple(Major, Minor - 1);
9369 return VersionTuple(Major, Minor - 1, Subminor - 1);
9370}
9371
9372CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9373 const RecordData &Record,
9374 unsigned &Idx) {
9375 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
9376 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
9377}
9378
9379DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9380 return Diag(Loc: CurrentImportLoc, DiagID);
9381}
9382
9383DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9384 return Diags.Report(Loc, DiagID);
9385}
9386
9387/// Retrieve the identifier table associated with the
9388/// preprocessor.
9389IdentifierTable &ASTReader::getIdentifierTable() {
9390 return PP.getIdentifierTable();
9391}
9392
9393/// Record that the given ID maps to the given switch-case
9394/// statement.
9395void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9396 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9397 "Already have a SwitchCase with this ID");
9398 (*CurrSwitchCaseStmts)[ID] = SC;
9399}
9400
9401/// Retrieve the switch-case statement with the given ID.
9402SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9403 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9404 return (*CurrSwitchCaseStmts)[ID];
9405}
9406
9407void ASTReader::ClearSwitchCaseIDs() {
9408 CurrSwitchCaseStmts->clear();
9409}
9410
9411void ASTReader::ReadComments() {
9412 ASTContext &Context = getContext();
9413 std::vector<RawComment *> Comments;
9414 for (SmallVectorImpl<std::pair<BitstreamCursor,
9415 serialization::ModuleFile *>>::iterator
9416 I = CommentsCursors.begin(),
9417 E = CommentsCursors.end();
9418 I != E; ++I) {
9419 Comments.clear();
9420 BitstreamCursor &Cursor = I->first;
9421 serialization::ModuleFile &F = *I->second;
9422 SavedStreamPosition SavedPosition(Cursor);
9423
9424 RecordData Record;
9425 while (true) {
9426 Expected<llvm::BitstreamEntry> MaybeEntry =
9427 Cursor.advanceSkippingSubblocks(
9428 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
9429 if (!MaybeEntry) {
9430 Error(Err: MaybeEntry.takeError());
9431 return;
9432 }
9433 llvm::BitstreamEntry Entry = MaybeEntry.get();
9434
9435 switch (Entry.Kind) {
9436 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9437 case llvm::BitstreamEntry::Error:
9438 Error(Msg: "malformed block record in AST file");
9439 return;
9440 case llvm::BitstreamEntry::EndBlock:
9441 goto NextCursor;
9442 case llvm::BitstreamEntry::Record:
9443 // The interesting case.
9444 break;
9445 }
9446
9447 // Read a record.
9448 Record.clear();
9449 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
9450 if (!MaybeComment) {
9451 Error(Err: MaybeComment.takeError());
9452 return;
9453 }
9454 switch ((CommentRecordTypes)MaybeComment.get()) {
9455 case COMMENTS_RAW_COMMENT: {
9456 unsigned Idx = 0;
9457 SourceRange SR = ReadSourceRange(F, Record, Idx);
9458 RawComment::CommentKind Kind =
9459 (RawComment::CommentKind) Record[Idx++];
9460 bool IsTrailingComment = Record[Idx++];
9461 bool IsAlmostTrailingComment = Record[Idx++];
9462 Comments.push_back(x: new (Context) RawComment(
9463 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9464 break;
9465 }
9466 }
9467 }
9468 NextCursor:
9469 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9470 FileToOffsetToComment;
9471 for (RawComment *C : Comments) {
9472 SourceLocation CommentLoc = C->getBeginLoc();
9473 if (CommentLoc.isValid()) {
9474 std::pair<FileID, unsigned> Loc =
9475 SourceMgr.getDecomposedLoc(Loc: CommentLoc);
9476 if (Loc.first.isValid())
9477 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
9478 }
9479 }
9480 }
9481}
9482
9483void ASTReader::visitInputFileInfos(
9484 serialization::ModuleFile &MF, bool IncludeSystem,
9485 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
9486 bool IsSystem)>
9487 Visitor) {
9488 unsigned NumUserInputs = MF.NumUserInputFiles;
9489 unsigned NumInputs = MF.InputFilesLoaded.size();
9490 assert(NumUserInputs <= NumInputs);
9491 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9492 for (unsigned I = 0; I < N; ++I) {
9493 bool IsSystem = I >= NumUserInputs;
9494 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
9495 Visitor(IFI, IsSystem);
9496 }
9497}
9498
9499void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9500 bool IncludeSystem, bool Complain,
9501 llvm::function_ref<void(const serialization::InputFile &IF,
9502 bool isSystem)> Visitor) {
9503 unsigned NumUserInputs = MF.NumUserInputFiles;
9504 unsigned NumInputs = MF.InputFilesLoaded.size();
9505 assert(NumUserInputs <= NumInputs);
9506 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9507 for (unsigned I = 0; I < N; ++I) {
9508 bool IsSystem = I >= NumUserInputs;
9509 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
9510 Visitor(IF, IsSystem);
9511 }
9512}
9513
9514void ASTReader::visitTopLevelModuleMaps(
9515 serialization::ModuleFile &MF,
9516 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
9517 unsigned NumInputs = MF.InputFilesLoaded.size();
9518 for (unsigned I = 0; I < NumInputs; ++I) {
9519 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
9520 if (IFI.TopLevel && IFI.ModuleMap)
9521 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
9522 Visitor(*FE);
9523 }
9524}
9525
9526void ASTReader::finishPendingActions() {
9527 while (
9528 !PendingIdentifierInfos.empty() || !PendingDeducedFunctionTypes.empty() ||
9529 !PendingDeducedVarTypes.empty() || !PendingIncompleteDeclChains.empty() ||
9530 !PendingDeclChains.empty() || !PendingMacroIDs.empty() ||
9531 !PendingDeclContextInfos.empty() || !PendingUpdateRecords.empty() ||
9532 !PendingObjCExtensionIvarRedeclarations.empty()) {
9533 // If any identifiers with corresponding top-level declarations have
9534 // been loaded, load those declarations now.
9535 using TopLevelDeclsMap =
9536 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9537 TopLevelDeclsMap TopLevelDecls;
9538
9539 while (!PendingIdentifierInfos.empty()) {
9540 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9541 SmallVector<uint32_t, 4> DeclIDs =
9542 std::move(PendingIdentifierInfos.back().second);
9543 PendingIdentifierInfos.pop_back();
9544
9545 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
9546 }
9547
9548 // Load each function type that we deferred loading because it was a
9549 // deduced type that might refer to a local type declared within itself.
9550 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
9551 auto *FD = PendingDeducedFunctionTypes[I].first;
9552 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
9553
9554 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
9555 // If we gave a function a deduced return type, remember that we need to
9556 // propagate that along the redeclaration chain.
9557 if (DT->isDeduced()) {
9558 PendingDeducedTypeUpdates.insert(
9559 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
9560 continue;
9561 }
9562
9563 // The function has undeduced DeduceType return type. We hope we can
9564 // find the deduced type by iterating the redecls in other modules
9565 // later.
9566 PendingUndeducedFunctionDecls.push_back(Elt: FD);
9567 continue;
9568 }
9569 }
9570 PendingDeducedFunctionTypes.clear();
9571
9572 // Load each variable type that we deferred loading because it was a
9573 // deduced type that might refer to a local type declared within itself.
9574 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
9575 auto *VD = PendingDeducedVarTypes[I].first;
9576 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
9577 }
9578 PendingDeducedVarTypes.clear();
9579
9580 // For each decl chain that we wanted to complete while deserializing, mark
9581 // it as "still needs to be completed".
9582 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9583 markIncompleteDeclChain(D: PendingIncompleteDeclChains[I]);
9584 }
9585 PendingIncompleteDeclChains.clear();
9586
9587 // Load pending declaration chains.
9588 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9589 loadPendingDeclChain(D: PendingDeclChains[I].first,
9590 LocalOffset: PendingDeclChains[I].second);
9591 PendingDeclChains.clear();
9592
9593 // Make the most recent of the top-level declarations visible.
9594 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9595 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9596 IdentifierInfo *II = TLD->first;
9597 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9598 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
9599 }
9600 }
9601
9602 // Load any pending macro definitions.
9603 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9604 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9605 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9606 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
9607 // Initialize the macro history from chained-PCHs ahead of module imports.
9608 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9609 ++IDIdx) {
9610 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9611 if (!Info.M->isModule())
9612 resolvePendingMacro(II, PMInfo: Info);
9613 }
9614 // Handle module imports.
9615 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9616 ++IDIdx) {
9617 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9618 if (Info.M->isModule())
9619 resolvePendingMacro(II, PMInfo: Info);
9620 }
9621 }
9622 PendingMacroIDs.clear();
9623
9624 // Wire up the DeclContexts for Decls that we delayed setting until
9625 // recursive loading is completed.
9626 while (!PendingDeclContextInfos.empty()) {
9627 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9628 PendingDeclContextInfos.pop_front();
9629 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
9630 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
9631 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
9632 }
9633
9634 // Perform any pending declaration updates.
9635 while (!PendingUpdateRecords.empty()) {
9636 auto Update = PendingUpdateRecords.pop_back_val();
9637 ReadingKindTracker ReadingKind(Read_Decl, *this);
9638 loadDeclUpdateRecords(Record&: Update);
9639 }
9640
9641 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
9642 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
9643 auto DuplicateIvars =
9644 PendingObjCExtensionIvarRedeclarations.back().second;
9645 llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
9646 StructuralEquivalenceContext Ctx(
9647 ExtensionsPair.first->getASTContext(),
9648 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
9649 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
9650 /*Complain =*/false,
9651 /*ErrorOnTagTypeMismatch =*/true);
9652 if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) {
9653 // Merge redeclared ivars with their predecessors.
9654 for (auto IvarPair : DuplicateIvars) {
9655 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
9656 // Change semantic DeclContext but keep the lexical one.
9657 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
9658 LexicalDC: Ivar->getLexicalDeclContext(),
9659 Ctx&: getContext());
9660 getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl());
9661 }
9662 // Invalidate duplicate extension and the cached ivar list.
9663 ExtensionsPair.first->setInvalidDecl();
9664 ExtensionsPair.second->getClassInterface()
9665 ->getDefinition()
9666 ->setIvarList(nullptr);
9667 } else {
9668 for (auto IvarPair : DuplicateIvars) {
9669 Diag(IvarPair.first->getLocation(),
9670 diag::err_duplicate_ivar_declaration)
9671 << IvarPair.first->getIdentifier();
9672 Diag(IvarPair.second->getLocation(), diag::note_previous_definition);
9673 }
9674 }
9675 PendingObjCExtensionIvarRedeclarations.pop_back();
9676 }
9677 }
9678
9679 // At this point, all update records for loaded decls are in place, so any
9680 // fake class definitions should have become real.
9681 assert(PendingFakeDefinitionData.empty() &&
9682 "faked up a class definition but never saw the real one");
9683
9684 // If we deserialized any C++ or Objective-C class definitions, any
9685 // Objective-C protocol definitions, or any redeclarable templates, make sure
9686 // that all redeclarations point to the definitions. Note that this can only
9687 // happen now, after the redeclaration chains have been fully wired.
9688 for (Decl *D : PendingDefinitions) {
9689 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
9690 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9691 // Make sure that the TagType points at the definition.
9692 const_cast<TagType*>(TagT)->decl = TD;
9693 }
9694
9695 if (auto RD = dyn_cast<CXXRecordDecl>(Val: D)) {
9696 for (auto *R = getMostRecentExistingDecl(RD); R;
9697 R = R->getPreviousDecl()) {
9698 assert((R == D) ==
9699 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9700 "declaration thinks it's the definition but it isn't");
9701 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9702 }
9703 }
9704
9705 continue;
9706 }
9707
9708 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
9709 // Make sure that the ObjCInterfaceType points at the definition.
9710 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
9711 ->Decl = ID;
9712
9713 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9714 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9715
9716 continue;
9717 }
9718
9719 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
9720 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9721 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9722
9723 continue;
9724 }
9725
9726 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
9727 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9728 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9729 }
9730 PendingDefinitions.clear();
9731
9732 // Load the bodies of any functions or methods we've encountered. We do
9733 // this now (delayed) so that we can be sure that the declaration chains
9734 // have been fully wired up (hasBody relies on this).
9735 // FIXME: We shouldn't require complete redeclaration chains here.
9736 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9737 PBEnd = PendingBodies.end();
9738 PB != PBEnd; ++PB) {
9739 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
9740 // For a function defined inline within a class template, force the
9741 // canonical definition to be the one inside the canonical definition of
9742 // the template. This ensures that we instantiate from a correct view
9743 // of the template.
9744 //
9745 // Sadly we can't do this more generally: we can't be sure that all
9746 // copies of an arbitrary class definition will have the same members
9747 // defined (eg, some member functions may not be instantiated, and some
9748 // special members may or may not have been implicitly defined).
9749 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9750 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9751 continue;
9752
9753 // FIXME: Check for =delete/=default?
9754 const FunctionDecl *Defn = nullptr;
9755 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
9756 FD->setLazyBody(PB->second);
9757 } else {
9758 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9759 mergeDefinitionVisibility(NonConstDefn, FD);
9760
9761 if (!FD->isLateTemplateParsed() &&
9762 !NonConstDefn->isLateTemplateParsed() &&
9763 // We only perform ODR checks for decls not in the explicit
9764 // global module fragment.
9765 !shouldSkipCheckingODR(FD) &&
9766 FD->getODRHash() != NonConstDefn->getODRHash()) {
9767 if (!isa<CXXMethodDecl>(Val: FD)) {
9768 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
9769 } else if (FD->getLexicalParent()->isFileContext() &&
9770 NonConstDefn->getLexicalParent()->isFileContext()) {
9771 // Only diagnose out-of-line method definitions. If they are
9772 // in class definitions, then an error will be generated when
9773 // processing the class bodies.
9774 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
9775 }
9776 }
9777 }
9778 continue;
9779 }
9780
9781 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
9782 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9783 MD->setLazyBody(PB->second);
9784 }
9785 PendingBodies.clear();
9786
9787 // Inform any classes that had members added that they now have more members.
9788 for (auto [RD, MD] : PendingAddedClassMembers) {
9789 RD->addedMember(D: MD);
9790 }
9791 PendingAddedClassMembers.clear();
9792
9793 // Do some cleanup.
9794 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9795 getContext().deduplicateMergedDefinitonsFor(ND);
9796 PendingMergedDefinitionsToDeduplicate.clear();
9797}
9798
9799void ASTReader::diagnoseOdrViolations() {
9800 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9801 PendingRecordOdrMergeFailures.empty() &&
9802 PendingFunctionOdrMergeFailures.empty() &&
9803 PendingEnumOdrMergeFailures.empty() &&
9804 PendingObjCInterfaceOdrMergeFailures.empty() &&
9805 PendingObjCProtocolOdrMergeFailures.empty())
9806 return;
9807
9808 // Trigger the import of the full definition of each class that had any
9809 // odr-merging problems, so we can produce better diagnostics for them.
9810 // These updates may in turn find and diagnose some ODR failures, so take
9811 // ownership of the set first.
9812 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9813 PendingOdrMergeFailures.clear();
9814 for (auto &Merge : OdrMergeFailures) {
9815 Merge.first->buildLookup();
9816 Merge.first->decls_begin();
9817 Merge.first->bases_begin();
9818 Merge.first->vbases_begin();
9819 for (auto &RecordPair : Merge.second) {
9820 auto *RD = RecordPair.first;
9821 RD->decls_begin();
9822 RD->bases_begin();
9823 RD->vbases_begin();
9824 }
9825 }
9826
9827 // Trigger the import of the full definition of each record in C/ObjC.
9828 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
9829 PendingRecordOdrMergeFailures.clear();
9830 for (auto &Merge : RecordOdrMergeFailures) {
9831 Merge.first->decls_begin();
9832 for (auto &D : Merge.second)
9833 D->decls_begin();
9834 }
9835
9836 // Trigger the import of the full interface definition.
9837 auto ObjCInterfaceOdrMergeFailures =
9838 std::move(PendingObjCInterfaceOdrMergeFailures);
9839 PendingObjCInterfaceOdrMergeFailures.clear();
9840 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
9841 Merge.first->decls_begin();
9842 for (auto &InterfacePair : Merge.second)
9843 InterfacePair.first->decls_begin();
9844 }
9845
9846 // Trigger the import of functions.
9847 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9848 PendingFunctionOdrMergeFailures.clear();
9849 for (auto &Merge : FunctionOdrMergeFailures) {
9850 Merge.first->buildLookup();
9851 Merge.first->decls_begin();
9852 Merge.first->getBody();
9853 for (auto &FD : Merge.second) {
9854 FD->buildLookup();
9855 FD->decls_begin();
9856 FD->getBody();
9857 }
9858 }
9859
9860 // Trigger the import of enums.
9861 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9862 PendingEnumOdrMergeFailures.clear();
9863 for (auto &Merge : EnumOdrMergeFailures) {
9864 Merge.first->decls_begin();
9865 for (auto &Enum : Merge.second) {
9866 Enum->decls_begin();
9867 }
9868 }
9869
9870 // Trigger the import of the full protocol definition.
9871 auto ObjCProtocolOdrMergeFailures =
9872 std::move(PendingObjCProtocolOdrMergeFailures);
9873 PendingObjCProtocolOdrMergeFailures.clear();
9874 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
9875 Merge.first->decls_begin();
9876 for (auto &ProtocolPair : Merge.second)
9877 ProtocolPair.first->decls_begin();
9878 }
9879
9880 // For each declaration from a merged context, check that the canonical
9881 // definition of that context also contains a declaration of the same
9882 // entity.
9883 //
9884 // Caution: this loop does things that might invalidate iterators into
9885 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9886 while (!PendingOdrMergeChecks.empty()) {
9887 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9888
9889 // FIXME: Skip over implicit declarations for now. This matters for things
9890 // like implicitly-declared special member functions. This isn't entirely
9891 // correct; we can end up with multiple unmerged declarations of the same
9892 // implicit entity.
9893 if (D->isImplicit())
9894 continue;
9895
9896 DeclContext *CanonDef = D->getDeclContext();
9897
9898 bool Found = false;
9899 const Decl *DCanon = D->getCanonicalDecl();
9900
9901 for (auto *RI : D->redecls()) {
9902 if (RI->getLexicalDeclContext() == CanonDef) {
9903 Found = true;
9904 break;
9905 }
9906 }
9907 if (Found)
9908 continue;
9909
9910 // Quick check failed, time to do the slow thing. Note, we can't just
9911 // look up the name of D in CanonDef here, because the member that is
9912 // in CanonDef might not be found by name lookup (it might have been
9913 // replaced by a more recent declaration in the lookup table), and we
9914 // can't necessarily find it in the redeclaration chain because it might
9915 // be merely mergeable, not redeclarable.
9916 llvm::SmallVector<const NamedDecl*, 4> Candidates;
9917 for (auto *CanonMember : CanonDef->decls()) {
9918 if (CanonMember->getCanonicalDecl() == DCanon) {
9919 // This can happen if the declaration is merely mergeable and not
9920 // actually redeclarable (we looked for redeclarations earlier).
9921 //
9922 // FIXME: We should be able to detect this more efficiently, without
9923 // pulling in all of the members of CanonDef.
9924 Found = true;
9925 break;
9926 }
9927 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9928 if (ND->getDeclName() == D->getDeclName())
9929 Candidates.push_back(ND);
9930 }
9931
9932 if (!Found) {
9933 // The AST doesn't like TagDecls becoming invalid after they've been
9934 // completed. We only really need to mark FieldDecls as invalid here.
9935 if (!isa<TagDecl>(Val: D))
9936 D->setInvalidDecl();
9937
9938 // Ensure we don't accidentally recursively enter deserialization while
9939 // we're producing our diagnostic.
9940 Deserializing RecursionGuard(this);
9941
9942 std::string CanonDefModule =
9943 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
9944 D: cast<Decl>(Val: CanonDef));
9945 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9946 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
9947 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9948
9949 if (Candidates.empty())
9950 Diag(cast<Decl>(CanonDef)->getLocation(),
9951 diag::note_module_odr_violation_no_possible_decls) << D;
9952 else {
9953 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9954 Diag(Candidates[I]->getLocation(),
9955 diag::note_module_odr_violation_possible_decl)
9956 << Candidates[I];
9957 }
9958
9959 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
9960 }
9961 }
9962
9963 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
9964 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
9965 ObjCInterfaceOdrMergeFailures.empty() &&
9966 ObjCProtocolOdrMergeFailures.empty())
9967 return;
9968
9969 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
9970 getPreprocessor().getLangOpts());
9971
9972 // Issue any pending ODR-failure diagnostics.
9973 for (auto &Merge : OdrMergeFailures) {
9974 // If we've already pointed out a specific problem with this class, don't
9975 // bother issuing a general "something's different" diagnostic.
9976 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9977 continue;
9978
9979 bool Diagnosed = false;
9980 CXXRecordDecl *FirstRecord = Merge.first;
9981 for (auto &RecordPair : Merge.second) {
9982 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
9983 SecondDD: RecordPair.second)) {
9984 Diagnosed = true;
9985 break;
9986 }
9987 }
9988
9989 if (!Diagnosed) {
9990 // All definitions are updates to the same declaration. This happens if a
9991 // module instantiates the declaration of a class template specialization
9992 // and two or more other modules instantiate its definition.
9993 //
9994 // FIXME: Indicate which modules had instantiations of this definition.
9995 // FIXME: How can this even happen?
9996 Diag(Merge.first->getLocation(),
9997 diag::err_module_odr_violation_different_instantiations)
9998 << Merge.first;
9999 }
10000 }
10001
10002 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
10003 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
10004 for (auto &Merge : RecordOdrMergeFailures) {
10005 // If we've already pointed out a specific problem with this class, don't
10006 // bother issuing a general "something's different" diagnostic.
10007 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10008 continue;
10009
10010 RecordDecl *FirstRecord = Merge.first;
10011 bool Diagnosed = false;
10012 for (auto *SecondRecord : Merge.second) {
10013 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
10014 Diagnosed = true;
10015 break;
10016 }
10017 }
10018 (void)Diagnosed;
10019 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10020 }
10021
10022 // Issue ODR failures diagnostics for functions.
10023 for (auto &Merge : FunctionOdrMergeFailures) {
10024 FunctionDecl *FirstFunction = Merge.first;
10025 bool Diagnosed = false;
10026 for (auto &SecondFunction : Merge.second) {
10027 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
10028 Diagnosed = true;
10029 break;
10030 }
10031 }
10032 (void)Diagnosed;
10033 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10034 }
10035
10036 // Issue ODR failures diagnostics for enums.
10037 for (auto &Merge : EnumOdrMergeFailures) {
10038 // If we've already pointed out a specific problem with this enum, don't
10039 // bother issuing a general "something's different" diagnostic.
10040 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10041 continue;
10042
10043 EnumDecl *FirstEnum = Merge.first;
10044 bool Diagnosed = false;
10045 for (auto &SecondEnum : Merge.second) {
10046 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
10047 Diagnosed = true;
10048 break;
10049 }
10050 }
10051 (void)Diagnosed;
10052 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10053 }
10054
10055 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10056 // If we've already pointed out a specific problem with this interface,
10057 // don't bother issuing a general "something's different" diagnostic.
10058 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10059 continue;
10060
10061 bool Diagnosed = false;
10062 ObjCInterfaceDecl *FirstID = Merge.first;
10063 for (auto &InterfacePair : Merge.second) {
10064 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
10065 SecondDD: InterfacePair.second)) {
10066 Diagnosed = true;
10067 break;
10068 }
10069 }
10070 (void)Diagnosed;
10071 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10072 }
10073
10074 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10075 // If we've already pointed out a specific problem with this protocol,
10076 // don't bother issuing a general "something's different" diagnostic.
10077 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10078 continue;
10079
10080 ObjCProtocolDecl *FirstProtocol = Merge.first;
10081 bool Diagnosed = false;
10082 for (auto &ProtocolPair : Merge.second) {
10083 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
10084 SecondDD: ProtocolPair.second)) {
10085 Diagnosed = true;
10086 break;
10087 }
10088 }
10089 (void)Diagnosed;
10090 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10091 }
10092}
10093
10094void ASTReader::StartedDeserializing() {
10095 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
10096 ReadTimer->startTimer();
10097}
10098
10099void ASTReader::FinishedDeserializing() {
10100 assert(NumCurrentElementsDeserializing &&
10101 "FinishedDeserializing not paired with StartedDeserializing");
10102 if (NumCurrentElementsDeserializing == 1) {
10103 // We decrease NumCurrentElementsDeserializing only after pending actions
10104 // are finished, to avoid recursively re-calling finishPendingActions().
10105 finishPendingActions();
10106 }
10107 --NumCurrentElementsDeserializing;
10108
10109 if (NumCurrentElementsDeserializing == 0) {
10110 // Propagate exception specification and deduced type updates along
10111 // redeclaration chains.
10112 //
10113 // We do this now rather than in finishPendingActions because we want to
10114 // be able to walk the complete redeclaration chains of the updated decls.
10115 while (!PendingExceptionSpecUpdates.empty() ||
10116 !PendingDeducedTypeUpdates.empty()) {
10117 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10118 PendingExceptionSpecUpdates.clear();
10119 for (auto Update : ESUpdates) {
10120 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10121 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10122 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10123 if (auto *Listener = getContext().getASTMutationListener())
10124 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
10125 for (auto *Redecl : Update.second->redecls())
10126 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
10127 }
10128
10129 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10130 PendingDeducedTypeUpdates.clear();
10131 for (auto Update : DTUpdates) {
10132 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10133 // FIXME: If the return type is already deduced, check that it matches.
10134 getContext().adjustDeducedFunctionResultType(FD: Update.first,
10135 ResultType: Update.second);
10136 }
10137
10138 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
10139 PendingUndeducedFunctionDecls.clear();
10140 // We hope we can find the deduced type for the functions by iterating
10141 // redeclarations in other modules.
10142 for (FunctionDecl *UndeducedFD : UDTUpdates)
10143 (void)UndeducedFD->getMostRecentDecl();
10144 }
10145
10146 if (ReadTimer)
10147 ReadTimer->stopTimer();
10148
10149 diagnoseOdrViolations();
10150
10151 // We are not in recursive loading, so it's safe to pass the "interesting"
10152 // decls to the consumer.
10153 if (Consumer)
10154 PassInterestingDeclsToConsumer();
10155 }
10156}
10157
10158void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
10159 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
10160 // Remove any fake results before adding any real ones.
10161 auto It = PendingFakeLookupResults.find(Key: II);
10162 if (It != PendingFakeLookupResults.end()) {
10163 for (auto *ND : It->second)
10164 SemaObj->IdResolver.RemoveDecl(D: ND);
10165 // FIXME: this works around module+PCH performance issue.
10166 // Rather than erase the result from the map, which is O(n), just clear
10167 // the vector of NamedDecls.
10168 It->second.clear();
10169 }
10170 }
10171
10172 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
10173 SemaObj->TUScope->AddDecl(D);
10174 } else if (SemaObj->TUScope) {
10175 // Adding the decl to IdResolver may have failed because it was already in
10176 // (even though it was not added in scope). If it is already in, make sure
10177 // it gets in the scope as well.
10178 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
10179 SemaObj->TUScope->AddDecl(D);
10180 }
10181}
10182
10183ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10184 ASTContext *Context,
10185 const PCHContainerReader &PCHContainerRdr,
10186 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
10187 StringRef isysroot,
10188 DisableValidationForModuleKind DisableValidationKind,
10189 bool AllowASTWithCompilerErrors,
10190 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
10191 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
10192 std::unique_ptr<llvm::Timer> ReadTimer)
10193 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
10194 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
10195 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
10196 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
10197 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
10198 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
10199 PCHContainerRdr, PP.getHeaderSearchInfo()),
10200 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
10201 DisableValidationKind(DisableValidationKind),
10202 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
10203 AllowConfigurationMismatch(AllowConfigurationMismatch),
10204 ValidateSystemInputs(ValidateSystemInputs),
10205 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
10206 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10207 SourceMgr.setExternalSLocEntrySource(this);
10208
10209 for (const auto &Ext : Extensions) {
10210 auto BlockName = Ext->getExtensionMetadata().BlockName;
10211 auto Known = ModuleFileExtensions.find(Key: BlockName);
10212 if (Known != ModuleFileExtensions.end()) {
10213 Diags.Report(diag::warn_duplicate_module_file_extension)
10214 << BlockName;
10215 continue;
10216 }
10217
10218 ModuleFileExtensions.insert(KV: {BlockName, Ext});
10219 }
10220}
10221
10222ASTReader::~ASTReader() {
10223 if (OwnsDeserializationListener)
10224 delete DeserializationListener;
10225}
10226
10227IdentifierResolver &ASTReader::getIdResolver() {
10228 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
10229}
10230
10231Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
10232 unsigned AbbrevID) {
10233 Idx = 0;
10234 Record.clear();
10235 return Cursor.readRecord(AbbrevID, Vals&: Record);
10236}
10237//===----------------------------------------------------------------------===//
10238//// OMPClauseReader implementation
10239////===----------------------------------------------------------------------===//
10240
10241// This has to be in namespace clang because it's friended by all
10242// of the OMP clauses.
10243namespace clang {
10244
10245class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
10246 ASTRecordReader &Record;
10247 ASTContext &Context;
10248
10249public:
10250 OMPClauseReader(ASTRecordReader &Record)
10251 : Record(Record), Context(Record.getContext()) {}
10252#define GEN_CLANG_CLAUSE_CLASS
10253#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
10254#include "llvm/Frontend/OpenMP/OMP.inc"
10255 OMPClause *readClause();
10256 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
10257 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
10258};
10259
10260} // end namespace clang
10261
10262OMPClause *ASTRecordReader::readOMPClause() {
10263 return OMPClauseReader(*this).readClause();
10264}
10265
10266OMPClause *OMPClauseReader::readClause() {
10267 OMPClause *C = nullptr;
10268 switch (llvm::omp::Clause(Record.readInt())) {
10269 case llvm::omp::OMPC_if:
10270 C = new (Context) OMPIfClause();
10271 break;
10272 case llvm::omp::OMPC_final:
10273 C = new (Context) OMPFinalClause();
10274 break;
10275 case llvm::omp::OMPC_num_threads:
10276 C = new (Context) OMPNumThreadsClause();
10277 break;
10278 case llvm::omp::OMPC_safelen:
10279 C = new (Context) OMPSafelenClause();
10280 break;
10281 case llvm::omp::OMPC_simdlen:
10282 C = new (Context) OMPSimdlenClause();
10283 break;
10284 case llvm::omp::OMPC_sizes: {
10285 unsigned NumSizes = Record.readInt();
10286 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
10287 break;
10288 }
10289 case llvm::omp::OMPC_full:
10290 C = OMPFullClause::CreateEmpty(C: Context);
10291 break;
10292 case llvm::omp::OMPC_partial:
10293 C = OMPPartialClause::CreateEmpty(C: Context);
10294 break;
10295 case llvm::omp::OMPC_allocator:
10296 C = new (Context) OMPAllocatorClause();
10297 break;
10298 case llvm::omp::OMPC_collapse:
10299 C = new (Context) OMPCollapseClause();
10300 break;
10301 case llvm::omp::OMPC_default:
10302 C = new (Context) OMPDefaultClause();
10303 break;
10304 case llvm::omp::OMPC_proc_bind:
10305 C = new (Context) OMPProcBindClause();
10306 break;
10307 case llvm::omp::OMPC_schedule:
10308 C = new (Context) OMPScheduleClause();
10309 break;
10310 case llvm::omp::OMPC_ordered:
10311 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
10312 break;
10313 case llvm::omp::OMPC_nowait:
10314 C = new (Context) OMPNowaitClause();
10315 break;
10316 case llvm::omp::OMPC_untied:
10317 C = new (Context) OMPUntiedClause();
10318 break;
10319 case llvm::omp::OMPC_mergeable:
10320 C = new (Context) OMPMergeableClause();
10321 break;
10322 case llvm::omp::OMPC_read:
10323 C = new (Context) OMPReadClause();
10324 break;
10325 case llvm::omp::OMPC_write:
10326 C = new (Context) OMPWriteClause();
10327 break;
10328 case llvm::omp::OMPC_update:
10329 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
10330 break;
10331 case llvm::omp::OMPC_capture:
10332 C = new (Context) OMPCaptureClause();
10333 break;
10334 case llvm::omp::OMPC_compare:
10335 C = new (Context) OMPCompareClause();
10336 break;
10337 case llvm::omp::OMPC_fail:
10338 C = new (Context) OMPFailClause();
10339 break;
10340 case llvm::omp::OMPC_seq_cst:
10341 C = new (Context) OMPSeqCstClause();
10342 break;
10343 case llvm::omp::OMPC_acq_rel:
10344 C = new (Context) OMPAcqRelClause();
10345 break;
10346 case llvm::omp::OMPC_acquire:
10347 C = new (Context) OMPAcquireClause();
10348 break;
10349 case llvm::omp::OMPC_release:
10350 C = new (Context) OMPReleaseClause();
10351 break;
10352 case llvm::omp::OMPC_relaxed:
10353 C = new (Context) OMPRelaxedClause();
10354 break;
10355 case llvm::omp::OMPC_weak:
10356 C = new (Context) OMPWeakClause();
10357 break;
10358 case llvm::omp::OMPC_threads:
10359 C = new (Context) OMPThreadsClause();
10360 break;
10361 case llvm::omp::OMPC_simd:
10362 C = new (Context) OMPSIMDClause();
10363 break;
10364 case llvm::omp::OMPC_nogroup:
10365 C = new (Context) OMPNogroupClause();
10366 break;
10367 case llvm::omp::OMPC_unified_address:
10368 C = new (Context) OMPUnifiedAddressClause();
10369 break;
10370 case llvm::omp::OMPC_unified_shared_memory:
10371 C = new (Context) OMPUnifiedSharedMemoryClause();
10372 break;
10373 case llvm::omp::OMPC_reverse_offload:
10374 C = new (Context) OMPReverseOffloadClause();
10375 break;
10376 case llvm::omp::OMPC_dynamic_allocators:
10377 C = new (Context) OMPDynamicAllocatorsClause();
10378 break;
10379 case llvm::omp::OMPC_atomic_default_mem_order:
10380 C = new (Context) OMPAtomicDefaultMemOrderClause();
10381 break;
10382 case llvm::omp::OMPC_at:
10383 C = new (Context) OMPAtClause();
10384 break;
10385 case llvm::omp::OMPC_severity:
10386 C = new (Context) OMPSeverityClause();
10387 break;
10388 case llvm::omp::OMPC_message:
10389 C = new (Context) OMPMessageClause();
10390 break;
10391 case llvm::omp::OMPC_private:
10392 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
10393 break;
10394 case llvm::omp::OMPC_firstprivate:
10395 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
10396 break;
10397 case llvm::omp::OMPC_lastprivate:
10398 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
10399 break;
10400 case llvm::omp::OMPC_shared:
10401 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
10402 break;
10403 case llvm::omp::OMPC_reduction: {
10404 unsigned N = Record.readInt();
10405 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
10406 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier: Modifier);
10407 break;
10408 }
10409 case llvm::omp::OMPC_task_reduction:
10410 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
10411 break;
10412 case llvm::omp::OMPC_in_reduction:
10413 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
10414 break;
10415 case llvm::omp::OMPC_linear:
10416 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
10417 break;
10418 case llvm::omp::OMPC_aligned:
10419 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
10420 break;
10421 case llvm::omp::OMPC_copyin:
10422 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
10423 break;
10424 case llvm::omp::OMPC_copyprivate:
10425 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
10426 break;
10427 case llvm::omp::OMPC_flush:
10428 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
10429 break;
10430 case llvm::omp::OMPC_depobj:
10431 C = OMPDepobjClause::CreateEmpty(C: Context);
10432 break;
10433 case llvm::omp::OMPC_depend: {
10434 unsigned NumVars = Record.readInt();
10435 unsigned NumLoops = Record.readInt();
10436 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
10437 break;
10438 }
10439 case llvm::omp::OMPC_device:
10440 C = new (Context) OMPDeviceClause();
10441 break;
10442 case llvm::omp::OMPC_map: {
10443 OMPMappableExprListSizeTy Sizes;
10444 Sizes.NumVars = Record.readInt();
10445 Sizes.NumUniqueDeclarations = Record.readInt();
10446 Sizes.NumComponentLists = Record.readInt();
10447 Sizes.NumComponents = Record.readInt();
10448 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
10449 break;
10450 }
10451 case llvm::omp::OMPC_num_teams:
10452 C = new (Context) OMPNumTeamsClause();
10453 break;
10454 case llvm::omp::OMPC_thread_limit:
10455 C = new (Context) OMPThreadLimitClause();
10456 break;
10457 case llvm::omp::OMPC_priority:
10458 C = new (Context) OMPPriorityClause();
10459 break;
10460 case llvm::omp::OMPC_grainsize:
10461 C = new (Context) OMPGrainsizeClause();
10462 break;
10463 case llvm::omp::OMPC_num_tasks:
10464 C = new (Context) OMPNumTasksClause();
10465 break;
10466 case llvm::omp::OMPC_hint:
10467 C = new (Context) OMPHintClause();
10468 break;
10469 case llvm::omp::OMPC_dist_schedule:
10470 C = new (Context) OMPDistScheduleClause();
10471 break;
10472 case llvm::omp::OMPC_defaultmap:
10473 C = new (Context) OMPDefaultmapClause();
10474 break;
10475 case llvm::omp::OMPC_to: {
10476 OMPMappableExprListSizeTy Sizes;
10477 Sizes.NumVars = Record.readInt();
10478 Sizes.NumUniqueDeclarations = Record.readInt();
10479 Sizes.NumComponentLists = Record.readInt();
10480 Sizes.NumComponents = Record.readInt();
10481 C = OMPToClause::CreateEmpty(C: Context, Sizes);
10482 break;
10483 }
10484 case llvm::omp::OMPC_from: {
10485 OMPMappableExprListSizeTy Sizes;
10486 Sizes.NumVars = Record.readInt();
10487 Sizes.NumUniqueDeclarations = Record.readInt();
10488 Sizes.NumComponentLists = Record.readInt();
10489 Sizes.NumComponents = Record.readInt();
10490 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
10491 break;
10492 }
10493 case llvm::omp::OMPC_use_device_ptr: {
10494 OMPMappableExprListSizeTy Sizes;
10495 Sizes.NumVars = Record.readInt();
10496 Sizes.NumUniqueDeclarations = Record.readInt();
10497 Sizes.NumComponentLists = Record.readInt();
10498 Sizes.NumComponents = Record.readInt();
10499 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
10500 break;
10501 }
10502 case llvm::omp::OMPC_use_device_addr: {
10503 OMPMappableExprListSizeTy Sizes;
10504 Sizes.NumVars = Record.readInt();
10505 Sizes.NumUniqueDeclarations = Record.readInt();
10506 Sizes.NumComponentLists = Record.readInt();
10507 Sizes.NumComponents = Record.readInt();
10508 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
10509 break;
10510 }
10511 case llvm::omp::OMPC_is_device_ptr: {
10512 OMPMappableExprListSizeTy Sizes;
10513 Sizes.NumVars = Record.readInt();
10514 Sizes.NumUniqueDeclarations = Record.readInt();
10515 Sizes.NumComponentLists = Record.readInt();
10516 Sizes.NumComponents = Record.readInt();
10517 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
10518 break;
10519 }
10520 case llvm::omp::OMPC_has_device_addr: {
10521 OMPMappableExprListSizeTy Sizes;
10522 Sizes.NumVars = Record.readInt();
10523 Sizes.NumUniqueDeclarations = Record.readInt();
10524 Sizes.NumComponentLists = Record.readInt();
10525 Sizes.NumComponents = Record.readInt();
10526 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
10527 break;
10528 }
10529 case llvm::omp::OMPC_allocate:
10530 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
10531 break;
10532 case llvm::omp::OMPC_nontemporal:
10533 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
10534 break;
10535 case llvm::omp::OMPC_inclusive:
10536 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
10537 break;
10538 case llvm::omp::OMPC_exclusive:
10539 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
10540 break;
10541 case llvm::omp::OMPC_order:
10542 C = new (Context) OMPOrderClause();
10543 break;
10544 case llvm::omp::OMPC_init:
10545 C = OMPInitClause::CreateEmpty(C: Context, N: Record.readInt());
10546 break;
10547 case llvm::omp::OMPC_use:
10548 C = new (Context) OMPUseClause();
10549 break;
10550 case llvm::omp::OMPC_destroy:
10551 C = new (Context) OMPDestroyClause();
10552 break;
10553 case llvm::omp::OMPC_novariants:
10554 C = new (Context) OMPNovariantsClause();
10555 break;
10556 case llvm::omp::OMPC_nocontext:
10557 C = new (Context) OMPNocontextClause();
10558 break;
10559 case llvm::omp::OMPC_detach:
10560 C = new (Context) OMPDetachClause();
10561 break;
10562 case llvm::omp::OMPC_uses_allocators:
10563 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
10564 break;
10565 case llvm::omp::OMPC_affinity:
10566 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
10567 break;
10568 case llvm::omp::OMPC_filter:
10569 C = new (Context) OMPFilterClause();
10570 break;
10571 case llvm::omp::OMPC_bind:
10572 C = OMPBindClause::CreateEmpty(C: Context);
10573 break;
10574 case llvm::omp::OMPC_align:
10575 C = new (Context) OMPAlignClause();
10576 break;
10577 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
10578 C = new (Context) OMPXDynCGroupMemClause();
10579 break;
10580 case llvm::omp::OMPC_doacross: {
10581 unsigned NumVars = Record.readInt();
10582 unsigned NumLoops = Record.readInt();
10583 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
10584 break;
10585 }
10586 case llvm::omp::OMPC_ompx_attribute:
10587 C = new (Context) OMPXAttributeClause();
10588 break;
10589 case llvm::omp::OMPC_ompx_bare:
10590 C = new (Context) OMPXBareClause();
10591 break;
10592#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
10593 case llvm::omp::Enum: \
10594 break;
10595#include "llvm/Frontend/OpenMP/OMPKinds.def"
10596 default:
10597 break;
10598 }
10599 assert(C && "Unknown OMPClause type");
10600
10601 Visit(C);
10602 C->setLocStart(Record.readSourceLocation());
10603 C->setLocEnd(Record.readSourceLocation());
10604
10605 return C;
10606}
10607
10608void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
10609 C->setPreInitStmt(Record.readSubStmt(),
10610 static_cast<OpenMPDirectiveKind>(Record.readInt()));
10611}
10612
10613void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
10614 VisitOMPClauseWithPreInit(C);
10615 C->setPostUpdateExpr(Record.readSubExpr());
10616}
10617
10618void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
10619 VisitOMPClauseWithPreInit(C);
10620 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
10621 C->setNameModifierLoc(Record.readSourceLocation());
10622 C->setColonLoc(Record.readSourceLocation());
10623 C->setCondition(Record.readSubExpr());
10624 C->setLParenLoc(Record.readSourceLocation());
10625}
10626
10627void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
10628 VisitOMPClauseWithPreInit(C);
10629 C->setCondition(Record.readSubExpr());
10630 C->setLParenLoc(Record.readSourceLocation());
10631}
10632
10633void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
10634 VisitOMPClauseWithPreInit(C);
10635 C->setNumThreads(Record.readSubExpr());
10636 C->setLParenLoc(Record.readSourceLocation());
10637}
10638
10639void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
10640 C->setSafelen(Record.readSubExpr());
10641 C->setLParenLoc(Record.readSourceLocation());
10642}
10643
10644void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
10645 C->setSimdlen(Record.readSubExpr());
10646 C->setLParenLoc(Record.readSourceLocation());
10647}
10648
10649void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
10650 for (Expr *&E : C->getSizesRefs())
10651 E = Record.readSubExpr();
10652 C->setLParenLoc(Record.readSourceLocation());
10653}
10654
10655void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
10656
10657void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
10658 C->setFactor(Record.readSubExpr());
10659 C->setLParenLoc(Record.readSourceLocation());
10660}
10661
10662void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
10663 C->setAllocator(Record.readExpr());
10664 C->setLParenLoc(Record.readSourceLocation());
10665}
10666
10667void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
10668 C->setNumForLoops(Record.readSubExpr());
10669 C->setLParenLoc(Record.readSourceLocation());
10670}
10671
10672void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
10673 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
10674 C->setLParenLoc(Record.readSourceLocation());
10675 C->setDefaultKindKwLoc(Record.readSourceLocation());
10676}
10677
10678void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
10679 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
10680 C->setLParenLoc(Record.readSourceLocation());
10681 C->setProcBindKindKwLoc(Record.readSourceLocation());
10682}
10683
10684void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
10685 VisitOMPClauseWithPreInit(C);
10686 C->setScheduleKind(
10687 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
10688 C->setFirstScheduleModifier(
10689 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10690 C->setSecondScheduleModifier(
10691 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10692 C->setChunkSize(Record.readSubExpr());
10693 C->setLParenLoc(Record.readSourceLocation());
10694 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
10695 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
10696 C->setScheduleKindLoc(Record.readSourceLocation());
10697 C->setCommaLoc(Record.readSourceLocation());
10698}
10699
10700void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
10701 C->setNumForLoops(Record.readSubExpr());
10702 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10703 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
10704 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10705 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
10706 C->setLParenLoc(Record.readSourceLocation());
10707}
10708
10709void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
10710 C->setEventHandler(Record.readSubExpr());
10711 C->setLParenLoc(Record.readSourceLocation());
10712}
10713
10714void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
10715
10716void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
10717
10718void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
10719
10720void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
10721
10722void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
10723
10724void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
10725 if (C->isExtended()) {
10726 C->setLParenLoc(Record.readSourceLocation());
10727 C->setArgumentLoc(Record.readSourceLocation());
10728 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
10729 }
10730}
10731
10732void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
10733
10734void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
10735
10736// Read the parameter of fail clause. This will have been saved when
10737// OMPClauseWriter is called.
10738void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
10739 C->setLParenLoc(Record.readSourceLocation());
10740 SourceLocation FailParameterLoc = Record.readSourceLocation();
10741 C->setFailParameterLoc(FailParameterLoc);
10742 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
10743 C->setFailParameter(CKind);
10744}
10745
10746void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
10747
10748void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
10749
10750void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
10751
10752void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
10753
10754void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
10755
10756void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
10757
10758void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
10759
10760void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
10761
10762void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
10763
10764void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
10765 unsigned NumVars = C->varlist_size();
10766 SmallVector<Expr *, 16> Vars;
10767 Vars.reserve(N: NumVars);
10768 for (unsigned I = 0; I != NumVars; ++I)
10769 Vars.push_back(Elt: Record.readSubExpr());
10770 C->setVarRefs(Vars);
10771 C->setIsTarget(Record.readBool());
10772 C->setIsTargetSync(Record.readBool());
10773 C->setLParenLoc(Record.readSourceLocation());
10774 C->setVarLoc(Record.readSourceLocation());
10775}
10776
10777void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
10778 C->setInteropVar(Record.readSubExpr());
10779 C->setLParenLoc(Record.readSourceLocation());
10780 C->setVarLoc(Record.readSourceLocation());
10781}
10782
10783void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
10784 C->setInteropVar(Record.readSubExpr());
10785 C->setLParenLoc(Record.readSourceLocation());
10786 C->setVarLoc(Record.readSourceLocation());
10787}
10788
10789void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
10790 VisitOMPClauseWithPreInit(C);
10791 C->setCondition(Record.readSubExpr());
10792 C->setLParenLoc(Record.readSourceLocation());
10793}
10794
10795void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
10796 VisitOMPClauseWithPreInit(C);
10797 C->setCondition(Record.readSubExpr());
10798 C->setLParenLoc(Record.readSourceLocation());
10799}
10800
10801void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
10802
10803void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
10804 OMPUnifiedSharedMemoryClause *) {}
10805
10806void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
10807
10808void
10809OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
10810}
10811
10812void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
10813 OMPAtomicDefaultMemOrderClause *C) {
10814 C->setAtomicDefaultMemOrderKind(
10815 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
10816 C->setLParenLoc(Record.readSourceLocation());
10817 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
10818}
10819
10820void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
10821 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
10822 C->setLParenLoc(Record.readSourceLocation());
10823 C->setAtKindKwLoc(Record.readSourceLocation());
10824}
10825
10826void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
10827 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
10828 C->setLParenLoc(Record.readSourceLocation());
10829 C->setSeverityKindKwLoc(Record.readSourceLocation());
10830}
10831
10832void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
10833 C->setMessageString(Record.readSubExpr());
10834 C->setLParenLoc(Record.readSourceLocation());
10835}
10836
10837void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
10838 C->setLParenLoc(Record.readSourceLocation());
10839 unsigned NumVars = C->varlist_size();
10840 SmallVector<Expr *, 16> Vars;
10841 Vars.reserve(N: NumVars);
10842 for (unsigned i = 0; i != NumVars; ++i)
10843 Vars.push_back(Elt: Record.readSubExpr());
10844 C->setVarRefs(Vars);
10845 Vars.clear();
10846 for (unsigned i = 0; i != NumVars; ++i)
10847 Vars.push_back(Elt: Record.readSubExpr());
10848 C->setPrivateCopies(Vars);
10849}
10850
10851void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
10852 VisitOMPClauseWithPreInit(C);
10853 C->setLParenLoc(Record.readSourceLocation());
10854 unsigned NumVars = C->varlist_size();
10855 SmallVector<Expr *, 16> Vars;
10856 Vars.reserve(N: NumVars);
10857 for (unsigned i = 0; i != NumVars; ++i)
10858 Vars.push_back(Elt: Record.readSubExpr());
10859 C->setVarRefs(Vars);
10860 Vars.clear();
10861 for (unsigned i = 0; i != NumVars; ++i)
10862 Vars.push_back(Elt: Record.readSubExpr());
10863 C->setPrivateCopies(Vars);
10864 Vars.clear();
10865 for (unsigned i = 0; i != NumVars; ++i)
10866 Vars.push_back(Elt: Record.readSubExpr());
10867 C->setInits(Vars);
10868}
10869
10870void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
10871 VisitOMPClauseWithPostUpdate(C);
10872 C->setLParenLoc(Record.readSourceLocation());
10873 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
10874 C->setKindLoc(Record.readSourceLocation());
10875 C->setColonLoc(Record.readSourceLocation());
10876 unsigned NumVars = C->varlist_size();
10877 SmallVector<Expr *, 16> Vars;
10878 Vars.reserve(N: NumVars);
10879 for (unsigned i = 0; i != NumVars; ++i)
10880 Vars.push_back(Elt: Record.readSubExpr());
10881 C->setVarRefs(Vars);
10882 Vars.clear();
10883 for (unsigned i = 0; i != NumVars; ++i)
10884 Vars.push_back(Elt: Record.readSubExpr());
10885 C->setPrivateCopies(Vars);
10886 Vars.clear();
10887 for (unsigned i = 0; i != NumVars; ++i)
10888 Vars.push_back(Elt: Record.readSubExpr());
10889 C->setSourceExprs(Vars);
10890 Vars.clear();
10891 for (unsigned i = 0; i != NumVars; ++i)
10892 Vars.push_back(Elt: Record.readSubExpr());
10893 C->setDestinationExprs(Vars);
10894 Vars.clear();
10895 for (unsigned i = 0; i != NumVars; ++i)
10896 Vars.push_back(Elt: Record.readSubExpr());
10897 C->setAssignmentOps(Vars);
10898}
10899
10900void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
10901 C->setLParenLoc(Record.readSourceLocation());
10902 unsigned NumVars = C->varlist_size();
10903 SmallVector<Expr *, 16> Vars;
10904 Vars.reserve(N: NumVars);
10905 for (unsigned i = 0; i != NumVars; ++i)
10906 Vars.push_back(Elt: Record.readSubExpr());
10907 C->setVarRefs(Vars);
10908}
10909
10910void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
10911 VisitOMPClauseWithPostUpdate(C);
10912 C->setLParenLoc(Record.readSourceLocation());
10913 C->setModifierLoc(Record.readSourceLocation());
10914 C->setColonLoc(Record.readSourceLocation());
10915 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10916 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10917 C->setQualifierLoc(NNSL);
10918 C->setNameInfo(DNI);
10919
10920 unsigned NumVars = C->varlist_size();
10921 SmallVector<Expr *, 16> Vars;
10922 Vars.reserve(N: NumVars);
10923 for (unsigned i = 0; i != NumVars; ++i)
10924 Vars.push_back(Elt: Record.readSubExpr());
10925 C->setVarRefs(Vars);
10926 Vars.clear();
10927 for (unsigned i = 0; i != NumVars; ++i)
10928 Vars.push_back(Elt: Record.readSubExpr());
10929 C->setPrivates(Vars);
10930 Vars.clear();
10931 for (unsigned i = 0; i != NumVars; ++i)
10932 Vars.push_back(Elt: Record.readSubExpr());
10933 C->setLHSExprs(Vars);
10934 Vars.clear();
10935 for (unsigned i = 0; i != NumVars; ++i)
10936 Vars.push_back(Elt: Record.readSubExpr());
10937 C->setRHSExprs(Vars);
10938 Vars.clear();
10939 for (unsigned i = 0; i != NumVars; ++i)
10940 Vars.push_back(Elt: Record.readSubExpr());
10941 C->setReductionOps(Vars);
10942 if (C->getModifier() == OMPC_REDUCTION_inscan) {
10943 Vars.clear();
10944 for (unsigned i = 0; i != NumVars; ++i)
10945 Vars.push_back(Elt: Record.readSubExpr());
10946 C->setInscanCopyOps(Vars);
10947 Vars.clear();
10948 for (unsigned i = 0; i != NumVars; ++i)
10949 Vars.push_back(Elt: Record.readSubExpr());
10950 C->setInscanCopyArrayTemps(Vars);
10951 Vars.clear();
10952 for (unsigned i = 0; i != NumVars; ++i)
10953 Vars.push_back(Elt: Record.readSubExpr());
10954 C->setInscanCopyArrayElems(Vars);
10955 }
10956}
10957
10958void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
10959 VisitOMPClauseWithPostUpdate(C);
10960 C->setLParenLoc(Record.readSourceLocation());
10961 C->setColonLoc(Record.readSourceLocation());
10962 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10963 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10964 C->setQualifierLoc(NNSL);
10965 C->setNameInfo(DNI);
10966
10967 unsigned NumVars = C->varlist_size();
10968 SmallVector<Expr *, 16> Vars;
10969 Vars.reserve(N: NumVars);
10970 for (unsigned I = 0; I != NumVars; ++I)
10971 Vars.push_back(Elt: Record.readSubExpr());
10972 C->setVarRefs(Vars);
10973 Vars.clear();
10974 for (unsigned I = 0; I != NumVars; ++I)
10975 Vars.push_back(Elt: Record.readSubExpr());
10976 C->setPrivates(Vars);
10977 Vars.clear();
10978 for (unsigned I = 0; I != NumVars; ++I)
10979 Vars.push_back(Elt: Record.readSubExpr());
10980 C->setLHSExprs(Vars);
10981 Vars.clear();
10982 for (unsigned I = 0; I != NumVars; ++I)
10983 Vars.push_back(Elt: Record.readSubExpr());
10984 C->setRHSExprs(Vars);
10985 Vars.clear();
10986 for (unsigned I = 0; I != NumVars; ++I)
10987 Vars.push_back(Elt: Record.readSubExpr());
10988 C->setReductionOps(Vars);
10989}
10990
10991void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
10992 VisitOMPClauseWithPostUpdate(C);
10993 C->setLParenLoc(Record.readSourceLocation());
10994 C->setColonLoc(Record.readSourceLocation());
10995 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10996 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10997 C->setQualifierLoc(NNSL);
10998 C->setNameInfo(DNI);
10999
11000 unsigned NumVars = C->varlist_size();
11001 SmallVector<Expr *, 16> Vars;
11002 Vars.reserve(N: NumVars);
11003 for (unsigned I = 0; I != NumVars; ++I)
11004 Vars.push_back(Elt: Record.readSubExpr());
11005 C->setVarRefs(Vars);
11006 Vars.clear();
11007 for (unsigned I = 0; I != NumVars; ++I)
11008 Vars.push_back(Elt: Record.readSubExpr());
11009 C->setPrivates(Vars);
11010 Vars.clear();
11011 for (unsigned I = 0; I != NumVars; ++I)
11012 Vars.push_back(Elt: Record.readSubExpr());
11013 C->setLHSExprs(Vars);
11014 Vars.clear();
11015 for (unsigned I = 0; I != NumVars; ++I)
11016 Vars.push_back(Elt: Record.readSubExpr());
11017 C->setRHSExprs(Vars);
11018 Vars.clear();
11019 for (unsigned I = 0; I != NumVars; ++I)
11020 Vars.push_back(Elt: Record.readSubExpr());
11021 C->setReductionOps(Vars);
11022 Vars.clear();
11023 for (unsigned I = 0; I != NumVars; ++I)
11024 Vars.push_back(Elt: Record.readSubExpr());
11025 C->setTaskgroupDescriptors(Vars);
11026}
11027
11028void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
11029 VisitOMPClauseWithPostUpdate(C);
11030 C->setLParenLoc(Record.readSourceLocation());
11031 C->setColonLoc(Record.readSourceLocation());
11032 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
11033 C->setModifierLoc(Record.readSourceLocation());
11034 unsigned NumVars = C->varlist_size();
11035 SmallVector<Expr *, 16> Vars;
11036 Vars.reserve(N: NumVars);
11037 for (unsigned i = 0; i != NumVars; ++i)
11038 Vars.push_back(Elt: Record.readSubExpr());
11039 C->setVarRefs(Vars);
11040 Vars.clear();
11041 for (unsigned i = 0; i != NumVars; ++i)
11042 Vars.push_back(Elt: Record.readSubExpr());
11043 C->setPrivates(Vars);
11044 Vars.clear();
11045 for (unsigned i = 0; i != NumVars; ++i)
11046 Vars.push_back(Elt: Record.readSubExpr());
11047 C->setInits(Vars);
11048 Vars.clear();
11049 for (unsigned i = 0; i != NumVars; ++i)
11050 Vars.push_back(Elt: Record.readSubExpr());
11051 C->setUpdates(Vars);
11052 Vars.clear();
11053 for (unsigned i = 0; i != NumVars; ++i)
11054 Vars.push_back(Elt: Record.readSubExpr());
11055 C->setFinals(Vars);
11056 C->setStep(Record.readSubExpr());
11057 C->setCalcStep(Record.readSubExpr());
11058 Vars.clear();
11059 for (unsigned I = 0; I != NumVars + 1; ++I)
11060 Vars.push_back(Elt: Record.readSubExpr());
11061 C->setUsedExprs(Vars);
11062}
11063
11064void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
11065 C->setLParenLoc(Record.readSourceLocation());
11066 C->setColonLoc(Record.readSourceLocation());
11067 unsigned NumVars = C->varlist_size();
11068 SmallVector<Expr *, 16> Vars;
11069 Vars.reserve(N: NumVars);
11070 for (unsigned i = 0; i != NumVars; ++i)
11071 Vars.push_back(Elt: Record.readSubExpr());
11072 C->setVarRefs(Vars);
11073 C->setAlignment(Record.readSubExpr());
11074}
11075
11076void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
11077 C->setLParenLoc(Record.readSourceLocation());
11078 unsigned NumVars = C->varlist_size();
11079 SmallVector<Expr *, 16> Exprs;
11080 Exprs.reserve(N: NumVars);
11081 for (unsigned i = 0; i != NumVars; ++i)
11082 Exprs.push_back(Elt: Record.readSubExpr());
11083 C->setVarRefs(Exprs);
11084 Exprs.clear();
11085 for (unsigned i = 0; i != NumVars; ++i)
11086 Exprs.push_back(Elt: Record.readSubExpr());
11087 C->setSourceExprs(Exprs);
11088 Exprs.clear();
11089 for (unsigned i = 0; i != NumVars; ++i)
11090 Exprs.push_back(Elt: Record.readSubExpr());
11091 C->setDestinationExprs(Exprs);
11092 Exprs.clear();
11093 for (unsigned i = 0; i != NumVars; ++i)
11094 Exprs.push_back(Elt: Record.readSubExpr());
11095 C->setAssignmentOps(Exprs);
11096}
11097
11098void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
11099 C->setLParenLoc(Record.readSourceLocation());
11100 unsigned NumVars = C->varlist_size();
11101 SmallVector<Expr *, 16> Exprs;
11102 Exprs.reserve(N: NumVars);
11103 for (unsigned i = 0; i != NumVars; ++i)
11104 Exprs.push_back(Elt: Record.readSubExpr());
11105 C->setVarRefs(Exprs);
11106 Exprs.clear();
11107 for (unsigned i = 0; i != NumVars; ++i)
11108 Exprs.push_back(Elt: Record.readSubExpr());
11109 C->setSourceExprs(Exprs);
11110 Exprs.clear();
11111 for (unsigned i = 0; i != NumVars; ++i)
11112 Exprs.push_back(Elt: Record.readSubExpr());
11113 C->setDestinationExprs(Exprs);
11114 Exprs.clear();
11115 for (unsigned i = 0; i != NumVars; ++i)
11116 Exprs.push_back(Elt: Record.readSubExpr());
11117 C->setAssignmentOps(Exprs);
11118}
11119
11120void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
11121 C->setLParenLoc(Record.readSourceLocation());
11122 unsigned NumVars = C->varlist_size();
11123 SmallVector<Expr *, 16> Vars;
11124 Vars.reserve(N: NumVars);
11125 for (unsigned i = 0; i != NumVars; ++i)
11126 Vars.push_back(Elt: Record.readSubExpr());
11127 C->setVarRefs(Vars);
11128}
11129
11130void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
11131 C->setDepobj(Record.readSubExpr());
11132 C->setLParenLoc(Record.readSourceLocation());
11133}
11134
11135void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
11136 C->setLParenLoc(Record.readSourceLocation());
11137 C->setModifier(Record.readSubExpr());
11138 C->setDependencyKind(
11139 static_cast<OpenMPDependClauseKind>(Record.readInt()));
11140 C->setDependencyLoc(Record.readSourceLocation());
11141 C->setColonLoc(Record.readSourceLocation());
11142 C->setOmpAllMemoryLoc(Record.readSourceLocation());
11143 unsigned NumVars = C->varlist_size();
11144 SmallVector<Expr *, 16> Vars;
11145 Vars.reserve(N: NumVars);
11146 for (unsigned I = 0; I != NumVars; ++I)
11147 Vars.push_back(Elt: Record.readSubExpr());
11148 C->setVarRefs(Vars);
11149 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11150 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
11151}
11152
11153void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
11154 VisitOMPClauseWithPreInit(C);
11155 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
11156 C->setDevice(Record.readSubExpr());
11157 C->setModifierLoc(Record.readSourceLocation());
11158 C->setLParenLoc(Record.readSourceLocation());
11159}
11160
11161void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
11162 C->setLParenLoc(Record.readSourceLocation());
11163 bool HasIteratorModifier = false;
11164 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
11165 C->setMapTypeModifier(
11166 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
11167 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
11168 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
11169 HasIteratorModifier = true;
11170 }
11171 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11172 C->setMapperIdInfo(Record.readDeclarationNameInfo());
11173 C->setMapType(
11174 static_cast<OpenMPMapClauseKind>(Record.readInt()));
11175 C->setMapLoc(Record.readSourceLocation());
11176 C->setColonLoc(Record.readSourceLocation());
11177 auto NumVars = C->varlist_size();
11178 auto UniqueDecls = C->getUniqueDeclarationsNum();
11179 auto TotalLists = C->getTotalComponentListNum();
11180 auto TotalComponents = C->getTotalComponentsNum();
11181
11182 SmallVector<Expr *, 16> Vars;
11183 Vars.reserve(N: NumVars);
11184 for (unsigned i = 0; i != NumVars; ++i)
11185 Vars.push_back(Elt: Record.readExpr());
11186 C->setVarRefs(Vars);
11187
11188 SmallVector<Expr *, 16> UDMappers;
11189 UDMappers.reserve(N: NumVars);
11190 for (unsigned I = 0; I < NumVars; ++I)
11191 UDMappers.push_back(Elt: Record.readExpr());
11192 C->setUDMapperRefs(UDMappers);
11193
11194 if (HasIteratorModifier)
11195 C->setIteratorModifier(Record.readExpr());
11196
11197 SmallVector<ValueDecl *, 16> Decls;
11198 Decls.reserve(N: UniqueDecls);
11199 for (unsigned i = 0; i < UniqueDecls; ++i)
11200 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11201 C->setUniqueDecls(Decls);
11202
11203 SmallVector<unsigned, 16> ListsPerDecl;
11204 ListsPerDecl.reserve(N: UniqueDecls);
11205 for (unsigned i = 0; i < UniqueDecls; ++i)
11206 ListsPerDecl.push_back(Elt: Record.readInt());
11207 C->setDeclNumLists(ListsPerDecl);
11208
11209 SmallVector<unsigned, 32> ListSizes;
11210 ListSizes.reserve(N: TotalLists);
11211 for (unsigned i = 0; i < TotalLists; ++i)
11212 ListSizes.push_back(Elt: Record.readInt());
11213 C->setComponentListSizes(ListSizes);
11214
11215 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11216 Components.reserve(N: TotalComponents);
11217 for (unsigned i = 0; i < TotalComponents; ++i) {
11218 Expr *AssociatedExprPr = Record.readExpr();
11219 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11220 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
11221 /*IsNonContiguous=*/Args: false);
11222 }
11223 C->setComponents(Components, ListSizes);
11224}
11225
11226void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
11227 C->setLParenLoc(Record.readSourceLocation());
11228 C->setColonLoc(Record.readSourceLocation());
11229 C->setAllocator(Record.readSubExpr());
11230 unsigned NumVars = C->varlist_size();
11231 SmallVector<Expr *, 16> Vars;
11232 Vars.reserve(N: NumVars);
11233 for (unsigned i = 0; i != NumVars; ++i)
11234 Vars.push_back(Elt: Record.readSubExpr());
11235 C->setVarRefs(Vars);
11236}
11237
11238void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
11239 VisitOMPClauseWithPreInit(C);
11240 C->setNumTeams(Record.readSubExpr());
11241 C->setLParenLoc(Record.readSourceLocation());
11242}
11243
11244void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
11245 VisitOMPClauseWithPreInit(C);
11246 C->setThreadLimit(Record.readSubExpr());
11247 C->setLParenLoc(Record.readSourceLocation());
11248}
11249
11250void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
11251 VisitOMPClauseWithPreInit(C);
11252 C->setPriority(Record.readSubExpr());
11253 C->setLParenLoc(Record.readSourceLocation());
11254}
11255
11256void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
11257 VisitOMPClauseWithPreInit(C);
11258 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
11259 C->setGrainsize(Record.readSubExpr());
11260 C->setModifierLoc(Record.readSourceLocation());
11261 C->setLParenLoc(Record.readSourceLocation());
11262}
11263
11264void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
11265 VisitOMPClauseWithPreInit(C);
11266 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
11267 C->setNumTasks(Record.readSubExpr());
11268 C->setModifierLoc(Record.readSourceLocation());
11269 C->setLParenLoc(Record.readSourceLocation());
11270}
11271
11272void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
11273 C->setHint(Record.readSubExpr());
11274 C->setLParenLoc(Record.readSourceLocation());
11275}
11276
11277void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
11278 VisitOMPClauseWithPreInit(C);
11279 C->setDistScheduleKind(
11280 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
11281 C->setChunkSize(Record.readSubExpr());
11282 C->setLParenLoc(Record.readSourceLocation());
11283 C->setDistScheduleKindLoc(Record.readSourceLocation());
11284 C->setCommaLoc(Record.readSourceLocation());
11285}
11286
11287void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
11288 C->setDefaultmapKind(
11289 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
11290 C->setDefaultmapModifier(
11291 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
11292 C->setLParenLoc(Record.readSourceLocation());
11293 C->setDefaultmapModifierLoc(Record.readSourceLocation());
11294 C->setDefaultmapKindLoc(Record.readSourceLocation());
11295}
11296
11297void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
11298 C->setLParenLoc(Record.readSourceLocation());
11299 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11300 C->setMotionModifier(
11301 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11302 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
11303 }
11304 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11305 C->setMapperIdInfo(Record.readDeclarationNameInfo());
11306 C->setColonLoc(Record.readSourceLocation());
11307 auto NumVars = C->varlist_size();
11308 auto UniqueDecls = C->getUniqueDeclarationsNum();
11309 auto TotalLists = C->getTotalComponentListNum();
11310 auto TotalComponents = C->getTotalComponentsNum();
11311
11312 SmallVector<Expr *, 16> Vars;
11313 Vars.reserve(N: NumVars);
11314 for (unsigned i = 0; i != NumVars; ++i)
11315 Vars.push_back(Elt: Record.readSubExpr());
11316 C->setVarRefs(Vars);
11317
11318 SmallVector<Expr *, 16> UDMappers;
11319 UDMappers.reserve(N: NumVars);
11320 for (unsigned I = 0; I < NumVars; ++I)
11321 UDMappers.push_back(Elt: Record.readSubExpr());
11322 C->setUDMapperRefs(UDMappers);
11323
11324 SmallVector<ValueDecl *, 16> Decls;
11325 Decls.reserve(N: UniqueDecls);
11326 for (unsigned i = 0; i < UniqueDecls; ++i)
11327 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11328 C->setUniqueDecls(Decls);
11329
11330 SmallVector<unsigned, 16> ListsPerDecl;
11331 ListsPerDecl.reserve(N: UniqueDecls);
11332 for (unsigned i = 0; i < UniqueDecls; ++i)
11333 ListsPerDecl.push_back(Elt: Record.readInt());
11334 C->setDeclNumLists(ListsPerDecl);
11335
11336 SmallVector<unsigned, 32> ListSizes;
11337 ListSizes.reserve(N: TotalLists);
11338 for (unsigned i = 0; i < TotalLists; ++i)
11339 ListSizes.push_back(Elt: Record.readInt());
11340 C->setComponentListSizes(ListSizes);
11341
11342 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11343 Components.reserve(N: TotalComponents);
11344 for (unsigned i = 0; i < TotalComponents; ++i) {
11345 Expr *AssociatedExprPr = Record.readSubExpr();
11346 bool IsNonContiguous = Record.readBool();
11347 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11348 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
11349 }
11350 C->setComponents(Components, ListSizes);
11351}
11352
11353void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
11354 C->setLParenLoc(Record.readSourceLocation());
11355 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11356 C->setMotionModifier(
11357 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11358 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
11359 }
11360 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11361 C->setMapperIdInfo(Record.readDeclarationNameInfo());
11362 C->setColonLoc(Record.readSourceLocation());
11363 auto NumVars = C->varlist_size();
11364 auto UniqueDecls = C->getUniqueDeclarationsNum();
11365 auto TotalLists = C->getTotalComponentListNum();
11366 auto TotalComponents = C->getTotalComponentsNum();
11367
11368 SmallVector<Expr *, 16> Vars;
11369 Vars.reserve(N: NumVars);
11370 for (unsigned i = 0; i != NumVars; ++i)
11371 Vars.push_back(Elt: Record.readSubExpr());
11372 C->setVarRefs(Vars);
11373
11374 SmallVector<Expr *, 16> UDMappers;
11375 UDMappers.reserve(N: NumVars);
11376 for (unsigned I = 0; I < NumVars; ++I)
11377 UDMappers.push_back(Elt: Record.readSubExpr());
11378 C->setUDMapperRefs(UDMappers);
11379
11380 SmallVector<ValueDecl *, 16> Decls;
11381 Decls.reserve(N: UniqueDecls);
11382 for (unsigned i = 0; i < UniqueDecls; ++i)
11383 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11384 C->setUniqueDecls(Decls);
11385
11386 SmallVector<unsigned, 16> ListsPerDecl;
11387 ListsPerDecl.reserve(N: UniqueDecls);
11388 for (unsigned i = 0; i < UniqueDecls; ++i)
11389 ListsPerDecl.push_back(Elt: Record.readInt());
11390 C->setDeclNumLists(ListsPerDecl);
11391
11392 SmallVector<unsigned, 32> ListSizes;
11393 ListSizes.reserve(N: TotalLists);
11394 for (unsigned i = 0; i < TotalLists; ++i)
11395 ListSizes.push_back(Elt: Record.readInt());
11396 C->setComponentListSizes(ListSizes);
11397
11398 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11399 Components.reserve(N: TotalComponents);
11400 for (unsigned i = 0; i < TotalComponents; ++i) {
11401 Expr *AssociatedExprPr = Record.readSubExpr();
11402 bool IsNonContiguous = Record.readBool();
11403 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11404 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
11405 }
11406 C->setComponents(Components, ListSizes);
11407}
11408
11409void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
11410 C->setLParenLoc(Record.readSourceLocation());
11411 auto NumVars = C->varlist_size();
11412 auto UniqueDecls = C->getUniqueDeclarationsNum();
11413 auto TotalLists = C->getTotalComponentListNum();
11414 auto TotalComponents = C->getTotalComponentsNum();
11415
11416 SmallVector<Expr *, 16> Vars;
11417 Vars.reserve(N: NumVars);
11418 for (unsigned i = 0; i != NumVars; ++i)
11419 Vars.push_back(Elt: Record.readSubExpr());
11420 C->setVarRefs(Vars);
11421 Vars.clear();
11422 for (unsigned i = 0; i != NumVars; ++i)
11423 Vars.push_back(Elt: Record.readSubExpr());
11424 C->setPrivateCopies(Vars);
11425 Vars.clear();
11426 for (unsigned i = 0; i != NumVars; ++i)
11427 Vars.push_back(Elt: Record.readSubExpr());
11428 C->setInits(Vars);
11429
11430 SmallVector<ValueDecl *, 16> Decls;
11431 Decls.reserve(N: UniqueDecls);
11432 for (unsigned i = 0; i < UniqueDecls; ++i)
11433 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11434 C->setUniqueDecls(Decls);
11435
11436 SmallVector<unsigned, 16> ListsPerDecl;
11437 ListsPerDecl.reserve(N: UniqueDecls);
11438 for (unsigned i = 0; i < UniqueDecls; ++i)
11439 ListsPerDecl.push_back(Elt: Record.readInt());
11440 C->setDeclNumLists(ListsPerDecl);
11441
11442 SmallVector<unsigned, 32> ListSizes;
11443 ListSizes.reserve(N: TotalLists);
11444 for (unsigned i = 0; i < TotalLists; ++i)
11445 ListSizes.push_back(Elt: Record.readInt());
11446 C->setComponentListSizes(ListSizes);
11447
11448 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11449 Components.reserve(N: TotalComponents);
11450 for (unsigned i = 0; i < TotalComponents; ++i) {
11451 auto *AssociatedExprPr = Record.readSubExpr();
11452 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11453 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
11454 /*IsNonContiguous=*/Args: false);
11455 }
11456 C->setComponents(Components, ListSizes);
11457}
11458
11459void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
11460 C->setLParenLoc(Record.readSourceLocation());
11461 auto NumVars = C->varlist_size();
11462 auto UniqueDecls = C->getUniqueDeclarationsNum();
11463 auto TotalLists = C->getTotalComponentListNum();
11464 auto TotalComponents = C->getTotalComponentsNum();
11465
11466 SmallVector<Expr *, 16> Vars;
11467 Vars.reserve(N: NumVars);
11468 for (unsigned i = 0; i != NumVars; ++i)
11469 Vars.push_back(Elt: Record.readSubExpr());
11470 C->setVarRefs(Vars);
11471
11472 SmallVector<ValueDecl *, 16> Decls;
11473 Decls.reserve(N: UniqueDecls);
11474 for (unsigned i = 0; i < UniqueDecls; ++i)
11475 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11476 C->setUniqueDecls(Decls);
11477
11478 SmallVector<unsigned, 16> ListsPerDecl;
11479 ListsPerDecl.reserve(N: UniqueDecls);
11480 for (unsigned i = 0; i < UniqueDecls; ++i)
11481 ListsPerDecl.push_back(Elt: Record.readInt());
11482 C->setDeclNumLists(ListsPerDecl);
11483
11484 SmallVector<unsigned, 32> ListSizes;
11485 ListSizes.reserve(N: TotalLists);
11486 for (unsigned i = 0; i < TotalLists; ++i)
11487 ListSizes.push_back(Elt: Record.readInt());
11488 C->setComponentListSizes(ListSizes);
11489
11490 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11491 Components.reserve(N: TotalComponents);
11492 for (unsigned i = 0; i < TotalComponents; ++i) {
11493 Expr *AssociatedExpr = Record.readSubExpr();
11494 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11495 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
11496 /*IsNonContiguous*/ Args: false);
11497 }
11498 C->setComponents(Components, ListSizes);
11499}
11500
11501void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
11502 C->setLParenLoc(Record.readSourceLocation());
11503 auto NumVars = C->varlist_size();
11504 auto UniqueDecls = C->getUniqueDeclarationsNum();
11505 auto TotalLists = C->getTotalComponentListNum();
11506 auto TotalComponents = C->getTotalComponentsNum();
11507
11508 SmallVector<Expr *, 16> Vars;
11509 Vars.reserve(N: NumVars);
11510 for (unsigned i = 0; i != NumVars; ++i)
11511 Vars.push_back(Elt: Record.readSubExpr());
11512 C->setVarRefs(Vars);
11513 Vars.clear();
11514
11515 SmallVector<ValueDecl *, 16> Decls;
11516 Decls.reserve(N: UniqueDecls);
11517 for (unsigned i = 0; i < UniqueDecls; ++i)
11518 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11519 C->setUniqueDecls(Decls);
11520
11521 SmallVector<unsigned, 16> ListsPerDecl;
11522 ListsPerDecl.reserve(N: UniqueDecls);
11523 for (unsigned i = 0; i < UniqueDecls; ++i)
11524 ListsPerDecl.push_back(Elt: Record.readInt());
11525 C->setDeclNumLists(ListsPerDecl);
11526
11527 SmallVector<unsigned, 32> ListSizes;
11528 ListSizes.reserve(N: TotalLists);
11529 for (unsigned i = 0; i < TotalLists; ++i)
11530 ListSizes.push_back(Elt: Record.readInt());
11531 C->setComponentListSizes(ListSizes);
11532
11533 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11534 Components.reserve(N: TotalComponents);
11535 for (unsigned i = 0; i < TotalComponents; ++i) {
11536 Expr *AssociatedExpr = Record.readSubExpr();
11537 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11538 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
11539 /*IsNonContiguous=*/Args: false);
11540 }
11541 C->setComponents(Components, ListSizes);
11542}
11543
11544void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
11545 C->setLParenLoc(Record.readSourceLocation());
11546 auto NumVars = C->varlist_size();
11547 auto UniqueDecls = C->getUniqueDeclarationsNum();
11548 auto TotalLists = C->getTotalComponentListNum();
11549 auto TotalComponents = C->getTotalComponentsNum();
11550
11551 SmallVector<Expr *, 16> Vars;
11552 Vars.reserve(N: NumVars);
11553 for (unsigned I = 0; I != NumVars; ++I)
11554 Vars.push_back(Elt: Record.readSubExpr());
11555 C->setVarRefs(Vars);
11556 Vars.clear();
11557
11558 SmallVector<ValueDecl *, 16> Decls;
11559 Decls.reserve(N: UniqueDecls);
11560 for (unsigned I = 0; I < UniqueDecls; ++I)
11561 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
11562 C->setUniqueDecls(Decls);
11563
11564 SmallVector<unsigned, 16> ListsPerDecl;
11565 ListsPerDecl.reserve(N: UniqueDecls);
11566 for (unsigned I = 0; I < UniqueDecls; ++I)
11567 ListsPerDecl.push_back(Elt: Record.readInt());
11568 C->setDeclNumLists(ListsPerDecl);
11569
11570 SmallVector<unsigned, 32> ListSizes;
11571 ListSizes.reserve(N: TotalLists);
11572 for (unsigned i = 0; i < TotalLists; ++i)
11573 ListSizes.push_back(Elt: Record.readInt());
11574 C->setComponentListSizes(ListSizes);
11575
11576 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11577 Components.reserve(N: TotalComponents);
11578 for (unsigned I = 0; I < TotalComponents; ++I) {
11579 Expr *AssociatedExpr = Record.readSubExpr();
11580 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11581 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
11582 /*IsNonContiguous=*/Args: false);
11583 }
11584 C->setComponents(Components, ListSizes);
11585}
11586
11587void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
11588 C->setLParenLoc(Record.readSourceLocation());
11589 unsigned NumVars = C->varlist_size();
11590 SmallVector<Expr *, 16> Vars;
11591 Vars.reserve(N: NumVars);
11592 for (unsigned i = 0; i != NumVars; ++i)
11593 Vars.push_back(Elt: Record.readSubExpr());
11594 C->setVarRefs(Vars);
11595 Vars.clear();
11596 Vars.reserve(N: NumVars);
11597 for (unsigned i = 0; i != NumVars; ++i)
11598 Vars.push_back(Elt: Record.readSubExpr());
11599 C->setPrivateRefs(Vars);
11600}
11601
11602void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
11603 C->setLParenLoc(Record.readSourceLocation());
11604 unsigned NumVars = C->varlist_size();
11605 SmallVector<Expr *, 16> Vars;
11606 Vars.reserve(N: NumVars);
11607 for (unsigned i = 0; i != NumVars; ++i)
11608 Vars.push_back(Elt: Record.readSubExpr());
11609 C->setVarRefs(Vars);
11610}
11611
11612void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
11613 C->setLParenLoc(Record.readSourceLocation());
11614 unsigned NumVars = C->varlist_size();
11615 SmallVector<Expr *, 16> Vars;
11616 Vars.reserve(N: NumVars);
11617 for (unsigned i = 0; i != NumVars; ++i)
11618 Vars.push_back(Elt: Record.readSubExpr());
11619 C->setVarRefs(Vars);
11620}
11621
11622void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
11623 C->setLParenLoc(Record.readSourceLocation());
11624 unsigned NumOfAllocators = C->getNumberOfAllocators();
11625 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
11626 Data.reserve(N: NumOfAllocators);
11627 for (unsigned I = 0; I != NumOfAllocators; ++I) {
11628 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
11629 D.Allocator = Record.readSubExpr();
11630 D.AllocatorTraits = Record.readSubExpr();
11631 D.LParenLoc = Record.readSourceLocation();
11632 D.RParenLoc = Record.readSourceLocation();
11633 }
11634 C->setAllocatorsData(Data);
11635}
11636
11637void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
11638 C->setLParenLoc(Record.readSourceLocation());
11639 C->setModifier(Record.readSubExpr());
11640 C->setColonLoc(Record.readSourceLocation());
11641 unsigned NumOfLocators = C->varlist_size();
11642 SmallVector<Expr *, 4> Locators;
11643 Locators.reserve(N: NumOfLocators);
11644 for (unsigned I = 0; I != NumOfLocators; ++I)
11645 Locators.push_back(Elt: Record.readSubExpr());
11646 C->setVarRefs(Locators);
11647}
11648
11649void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
11650 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
11651 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
11652 C->setLParenLoc(Record.readSourceLocation());
11653 C->setKindKwLoc(Record.readSourceLocation());
11654 C->setModifierKwLoc(Record.readSourceLocation());
11655}
11656
11657void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
11658 VisitOMPClauseWithPreInit(C);
11659 C->setThreadID(Record.readSubExpr());
11660 C->setLParenLoc(Record.readSourceLocation());
11661}
11662
11663void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
11664 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
11665 C->setLParenLoc(Record.readSourceLocation());
11666 C->setBindKindLoc(Record.readSourceLocation());
11667}
11668
11669void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
11670 C->setAlignment(Record.readExpr());
11671 C->setLParenLoc(Record.readSourceLocation());
11672}
11673
11674void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
11675 VisitOMPClauseWithPreInit(C);
11676 C->setSize(Record.readSubExpr());
11677 C->setLParenLoc(Record.readSourceLocation());
11678}
11679
11680void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
11681 C->setLParenLoc(Record.readSourceLocation());
11682 C->setDependenceType(
11683 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
11684 C->setDependenceLoc(Record.readSourceLocation());
11685 C->setColonLoc(Record.readSourceLocation());
11686 unsigned NumVars = C->varlist_size();
11687 SmallVector<Expr *, 16> Vars;
11688 Vars.reserve(N: NumVars);
11689 for (unsigned I = 0; I != NumVars; ++I)
11690 Vars.push_back(Elt: Record.readSubExpr());
11691 C->setVarRefs(Vars);
11692 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11693 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
11694}
11695
11696void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
11697 AttrVec Attrs;
11698 Record.readAttributes(Attrs);
11699 C->setAttrs(Attrs);
11700 C->setLocStart(Record.readSourceLocation());
11701 C->setLParenLoc(Record.readSourceLocation());
11702 C->setLocEnd(Record.readSourceLocation());
11703}
11704
11705void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
11706
11707OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
11708 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
11709 TI.Sets.resize(readUInt32());
11710 for (auto &Set : TI.Sets) {
11711 Set.Kind = readEnum<llvm::omp::TraitSet>();
11712 Set.Selectors.resize(readUInt32());
11713 for (auto &Selector : Set.Selectors) {
11714 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
11715 Selector.ScoreOrCondition = nullptr;
11716 if (readBool())
11717 Selector.ScoreOrCondition = readExprRef();
11718 Selector.Properties.resize(readUInt32());
11719 for (auto &Property : Selector.Properties)
11720 Property.Kind = readEnum<llvm::omp::TraitProperty>();
11721 }
11722 }
11723 return &TI;
11724}
11725
11726void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
11727 if (!Data)
11728 return;
11729 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
11730 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
11731 skipInts(N: 3);
11732 }
11733 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
11734 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
11735 Clauses[I] = readOMPClause();
11736 Data->setClauses(Clauses);
11737 if (Data->hasAssociatedStmt())
11738 Data->setAssociatedStmt(readStmt());
11739 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
11740 Data->getChildren()[I] = readStmt();
11741}
11742

source code of clang/lib/Serialization/ASTReader.cpp