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 "TemplateArgumentHasher.h"
16#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/ASTMutationListener.h"
19#include "clang/AST/ASTStructuralEquivalence.h"
20#include "clang/AST/ASTUnresolvedSet.h"
21#include "clang/AST/AbstractTypeReader.h"
22#include "clang/AST/Attr.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/DeclBase.h"
25#include "clang/AST/DeclCXX.h"
26#include "clang/AST/DeclFriend.h"
27#include "clang/AST/DeclGroup.h"
28#include "clang/AST/DeclObjC.h"
29#include "clang/AST/DeclOpenMP.h"
30#include "clang/AST/DeclTemplate.h"
31#include "clang/AST/DeclarationName.h"
32#include "clang/AST/Expr.h"
33#include "clang/AST/ExprCXX.h"
34#include "clang/AST/ExternalASTSource.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/ODRDiagsEmitter.h"
37#include "clang/AST/OpenACCClause.h"
38#include "clang/AST/OpenMPClause.h"
39#include "clang/AST/RawCommentList.h"
40#include "clang/AST/TemplateBase.h"
41#include "clang/AST/TemplateName.h"
42#include "clang/AST/Type.h"
43#include "clang/AST/TypeLoc.h"
44#include "clang/AST/TypeLocVisitor.h"
45#include "clang/AST/UnresolvedSet.h"
46#include "clang/Basic/ASTSourceDescriptor.h"
47#include "clang/Basic/CommentOptions.h"
48#include "clang/Basic/Diagnostic.h"
49#include "clang/Basic/DiagnosticIDs.h"
50#include "clang/Basic/DiagnosticOptions.h"
51#include "clang/Basic/DiagnosticSema.h"
52#include "clang/Basic/FileManager.h"
53#include "clang/Basic/FileSystemOptions.h"
54#include "clang/Basic/IdentifierTable.h"
55#include "clang/Basic/LLVM.h"
56#include "clang/Basic/LangOptions.h"
57#include "clang/Basic/Module.h"
58#include "clang/Basic/ObjCRuntime.h"
59#include "clang/Basic/OpenACCKinds.h"
60#include "clang/Basic/OpenMPKinds.h"
61#include "clang/Basic/OperatorKinds.h"
62#include "clang/Basic/PragmaKinds.h"
63#include "clang/Basic/Sanitizers.h"
64#include "clang/Basic/SourceLocation.h"
65#include "clang/Basic/SourceManager.h"
66#include "clang/Basic/SourceManagerInternals.h"
67#include "clang/Basic/Specifiers.h"
68#include "clang/Basic/TargetInfo.h"
69#include "clang/Basic/TargetOptions.h"
70#include "clang/Basic/TokenKinds.h"
71#include "clang/Basic/Version.h"
72#include "clang/Lex/HeaderSearch.h"
73#include "clang/Lex/HeaderSearchOptions.h"
74#include "clang/Lex/MacroInfo.h"
75#include "clang/Lex/ModuleMap.h"
76#include "clang/Lex/PreprocessingRecord.h"
77#include "clang/Lex/Preprocessor.h"
78#include "clang/Lex/PreprocessorOptions.h"
79#include "clang/Lex/Token.h"
80#include "clang/Sema/ObjCMethodList.h"
81#include "clang/Sema/Scope.h"
82#include "clang/Sema/Sema.h"
83#include "clang/Sema/SemaCUDA.h"
84#include "clang/Sema/SemaObjC.h"
85#include "clang/Sema/SemaOpenMP.h"
86#include "clang/Sema/SemaRISCV.h"
87#include "clang/Sema/Weak.h"
88#include "clang/Serialization/ASTBitCodes.h"
89#include "clang/Serialization/ASTDeserializationListener.h"
90#include "clang/Serialization/ASTRecordReader.h"
91#include "clang/Serialization/ContinuousRangeMap.h"
92#include "clang/Serialization/GlobalModuleIndex.h"
93#include "clang/Serialization/InMemoryModuleCache.h"
94#include "clang/Serialization/ModuleCache.h"
95#include "clang/Serialization/ModuleFile.h"
96#include "clang/Serialization/ModuleFileExtension.h"
97#include "clang/Serialization/ModuleManager.h"
98#include "clang/Serialization/PCHContainerOperations.h"
99#include "clang/Serialization/SerializationDiagnostic.h"
100#include "llvm/ADT/APFloat.h"
101#include "llvm/ADT/APInt.h"
102#include "llvm/ADT/ArrayRef.h"
103#include "llvm/ADT/DenseMap.h"
104#include "llvm/ADT/FoldingSet.h"
105#include "llvm/ADT/IntrusiveRefCntPtr.h"
106#include "llvm/ADT/STLExtras.h"
107#include "llvm/ADT/ScopeExit.h"
108#include "llvm/ADT/Sequence.h"
109#include "llvm/ADT/SmallPtrSet.h"
110#include "llvm/ADT/SmallVector.h"
111#include "llvm/ADT/StringExtras.h"
112#include "llvm/ADT/StringMap.h"
113#include "llvm/ADT/StringRef.h"
114#include "llvm/ADT/iterator_range.h"
115#include "llvm/Bitstream/BitstreamReader.h"
116#include "llvm/Support/Chrono.h"
117#include "llvm/Support/Compiler.h"
118#include "llvm/Support/Compression.h"
119#include "llvm/Support/DJB.h"
120#include "llvm/Support/Endian.h"
121#include "llvm/Support/Error.h"
122#include "llvm/Support/ErrorHandling.h"
123#include "llvm/Support/LEB128.h"
124#include "llvm/Support/MemoryBuffer.h"
125#include "llvm/Support/Path.h"
126#include "llvm/Support/SaveAndRestore.h"
127#include "llvm/Support/TimeProfiler.h"
128#include "llvm/Support/Timer.h"
129#include "llvm/Support/VersionTuple.h"
130#include "llvm/Support/VirtualFileSystem.h"
131#include "llvm/Support/raw_ostream.h"
132#include "llvm/TargetParser/Triple.h"
133#include <algorithm>
134#include <cassert>
135#include <cstddef>
136#include <cstdint>
137#include <cstdio>
138#include <ctime>
139#include <iterator>
140#include <limits>
141#include <map>
142#include <memory>
143#include <optional>
144#include <string>
145#include <system_error>
146#include <tuple>
147#include <utility>
148#include <vector>
149
150using namespace clang;
151using namespace clang::serialization;
152using namespace clang::serialization::reader;
153using llvm::BitstreamCursor;
154
155//===----------------------------------------------------------------------===//
156// ChainedASTReaderListener implementation
157//===----------------------------------------------------------------------===//
158
159bool
160ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
161 return First->ReadFullVersionInformation(FullVersion) ||
162 Second->ReadFullVersionInformation(FullVersion);
163}
164
165void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
166 First->ReadModuleName(ModuleName);
167 Second->ReadModuleName(ModuleName);
168}
169
170void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
171 First->ReadModuleMapFile(ModuleMapPath);
172 Second->ReadModuleMapFile(ModuleMapPath);
173}
174
175bool ChainedASTReaderListener::ReadLanguageOptions(
176 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
177 bool AllowCompatibleDifferences) {
178 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
179 AllowCompatibleDifferences) ||
180 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
181 AllowCompatibleDifferences);
182}
183
184bool ChainedASTReaderListener::ReadCodeGenOptions(
185 const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,
186 bool AllowCompatibleDifferences) {
187 return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
188 AllowCompatibleDifferences) ||
189 Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
190 AllowCompatibleDifferences);
191}
192
193bool ChainedASTReaderListener::ReadTargetOptions(
194 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
195 bool AllowCompatibleDifferences) {
196 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
197 AllowCompatibleDifferences) ||
198 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
199 AllowCompatibleDifferences);
200}
201
202bool ChainedASTReaderListener::ReadDiagnosticOptions(
203 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
204 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
205 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
206}
207
208bool
209ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
210 bool Complain) {
211 return First->ReadFileSystemOptions(FSOpts, Complain) ||
212 Second->ReadFileSystemOptions(FSOpts, Complain);
213}
214
215bool ChainedASTReaderListener::ReadHeaderSearchOptions(
216 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
217 StringRef ContextHash, bool Complain) {
218 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
219 Complain) ||
220 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
221 Complain);
222}
223
224bool ChainedASTReaderListener::ReadPreprocessorOptions(
225 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
226 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
227 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
228 Complain, SuggestedPredefines) ||
229 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
230 Complain, SuggestedPredefines);
231}
232
233void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
234 uint32_t Value) {
235 First->ReadCounter(M, Value);
236 Second->ReadCounter(M, Value);
237}
238
239bool ChainedASTReaderListener::needsInputFileVisitation() {
240 return First->needsInputFileVisitation() ||
241 Second->needsInputFileVisitation();
242}
243
244bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
245 return First->needsSystemInputFileVisitation() ||
246 Second->needsSystemInputFileVisitation();
247}
248
249void ChainedASTReaderListener::visitModuleFile(ModuleFileName Filename,
250 ModuleKind Kind,
251 bool DirectlyImported) {
252 First->visitModuleFile(Filename, Kind, DirectlyImported);
253 Second->visitModuleFile(Filename, Kind, DirectlyImported);
254}
255
256bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
257 bool isSystem,
258 bool isOverridden,
259 bool isExplicitModule) {
260 bool Continue = false;
261 if (First->needsInputFileVisitation() &&
262 (!isSystem || First->needsSystemInputFileVisitation()))
263 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
264 isExplicitModule);
265 if (Second->needsInputFileVisitation() &&
266 (!isSystem || Second->needsSystemInputFileVisitation()))
267 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
268 isExplicitModule);
269 return Continue;
270}
271
272void ChainedASTReaderListener::readModuleFileExtension(
273 const ModuleFileExtensionMetadata &Metadata) {
274 First->readModuleFileExtension(Metadata);
275 Second->readModuleFileExtension(Metadata);
276}
277
278//===----------------------------------------------------------------------===//
279// PCH validator implementation
280//===----------------------------------------------------------------------===//
281
282ASTReaderListener::~ASTReaderListener() = default;
283
284static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionFlagMismatch(
285 DiagnosticsEngine *Diags, StringRef Description, bool SerializedValue,
286 bool CurrentValue, StringRef ModuleFilename) {
287 if (!Diags)
288 return true;
289 return Diags->Report(DiagID: diag::err_ast_file_langopt_mismatch)
290 << Description << SerializedValue << CurrentValue << ModuleFilename;
291}
292
293static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionValueMismatch(
294 DiagnosticsEngine *Diags, StringRef Description, StringRef ModuleFilename) {
295 if (!Diags)
296 return true;
297 return Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
298 << Description << ModuleFilename;
299}
300
301/// Compare the given set of language options against an existing set of
302/// language options.
303///
304/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
305/// \param AllowCompatibleDifferences If true, differences between compatible
306/// language options will be permitted.
307///
308/// \returns true if the languagae options mis-match, false otherwise.
309static bool checkLanguageOptions(const LangOptions &LangOpts,
310 const LangOptions &ExistingLangOpts,
311 StringRef ModuleFilename,
312 DiagnosticsEngine *Diags,
313 bool AllowCompatibleDifferences = true) {
314 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
315 using CK = LangOptions::CompatibilityKind;
316
317#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
318 if constexpr (CK::Compatibility != CK::Benign) { \
319 if ((CK::Compatibility == CK::NotCompatible) || \
320 (CK::Compatibility == CK::Compatible && \
321 !AllowCompatibleDifferences)) { \
322 if (ExistingLangOpts.Name != LangOpts.Name) { \
323 if (Bits == 1) \
324 return diagnoseLanguageOptionFlagMismatch( \
325 Diags, Description, LangOpts.Name, ExistingLangOpts.Name, \
326 ModuleFilename); \
327 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
328 ModuleFilename); \
329 } \
330 } \
331 }
332
333#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
334 if constexpr (CK::Compatibility != CK::Benign) { \
335 if ((CK::Compatibility == CK::NotCompatible) || \
336 (CK::Compatibility == CK::Compatible && \
337 !AllowCompatibleDifferences)) { \
338 if (ExistingLangOpts.Name != LangOpts.Name) { \
339 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
340 ModuleFilename); \
341 } \
342 } \
343 }
344
345#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
346 if constexpr (CK::Compatibility != CK::Benign) { \
347 if ((CK::Compatibility == CK::NotCompatible) || \
348 (CK::Compatibility == CK::Compatible && \
349 !AllowCompatibleDifferences)) { \
350 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
351 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
352 ModuleFilename); \
353 } \
354 } \
355 }
356
357#include "clang/Basic/LangOptions.def"
358
359 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
360 return diagnoseLanguageOptionValueMismatch(Diags, Description: "module features",
361 ModuleFilename);
362 }
363
364 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
365 return diagnoseLanguageOptionValueMismatch(
366 Diags, Description: "target Objective-C runtime", ModuleFilename);
367 }
368
369 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
370 LangOpts.CommentOpts.BlockCommandNames) {
371 return diagnoseLanguageOptionValueMismatch(Diags, Description: "block command names",
372 ModuleFilename);
373 }
374
375 // Sanitizer feature mismatches are treated as compatible differences. If
376 // compatible differences aren't allowed, we still only want to check for
377 // mismatches of non-modular sanitizers (the only ones which can affect AST
378 // generation).
379 if (!AllowCompatibleDifferences) {
380 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
381 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
382 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
383 ExistingSanitizers.clear(K: ModularSanitizers);
384 ImportedSanitizers.clear(K: ModularSanitizers);
385 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
386 const std::string Flag = "-fsanitize=";
387 if (Diags) {
388#define SANITIZER(NAME, ID) \
389 { \
390 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
391 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
392 if (InExistingModule != InImportedModule) \
393 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
394 << InExistingModule << ModuleFilename << (Flag + NAME); \
395 }
396#include "clang/Basic/Sanitizers.def"
397 }
398 return true;
399 }
400 }
401
402 return false;
403}
404
405static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
406 const CodeGenOptions &ExistingCGOpts,
407 StringRef ModuleFilename,
408 DiagnosticsEngine *Diags,
409 bool AllowCompatibleDifferences = true) {
410 // FIXME: Specify and print a description for each option instead of the name.
411 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
412 using CK = CodeGenOptions::CompatibilityKind;
413#define CODEGENOPT(Name, Bits, Default, Compatibility) \
414 if constexpr (CK::Compatibility != CK::Benign) { \
415 if ((CK::Compatibility == CK::NotCompatible) || \
416 (CK::Compatibility == CK::Compatible && \
417 !AllowCompatibleDifferences)) { \
418 if (ExistingCGOpts.Name != CGOpts.Name) { \
419 if (Diags) { \
420 if (Bits == 1) \
421 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
422 << #Name << CGOpts.Name << ExistingCGOpts.Name \
423 << ModuleFilename; \
424 else \
425 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
426 << #Name << ModuleFilename; \
427 } \
428 return true; \
429 } \
430 } \
431 }
432
433#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
434 if constexpr (CK::Compatibility != CK::Benign) { \
435 if ((CK::Compatibility == CK::NotCompatible) || \
436 (CK::Compatibility == CK::Compatible && \
437 !AllowCompatibleDifferences)) { \
438 if (ExistingCGOpts.Name != CGOpts.Name) { \
439 if (Diags) \
440 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
441 << #Name << ModuleFilename; \
442 return true; \
443 } \
444 } \
445 }
446#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
447 if constexpr (CK::Compatibility != CK::Benign) { \
448 if ((CK::Compatibility == CK::NotCompatible) || \
449 (CK::Compatibility == CK::Compatible && \
450 !AllowCompatibleDifferences)) { \
451 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
452 if (Diags) \
453 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
454 << #Name << ModuleFilename; \
455 return true; \
456 } \
457 } \
458 }
459#define DEBUGOPT(Name, Bits, Default, Compatibility)
460#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
461#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
462#include "clang/Basic/CodeGenOptions.def"
463
464 return false;
465}
466
467static std::vector<std::string>
468accumulateFeaturesAsWritten(std::vector<std::string> FeaturesAsWritten) {
469 llvm::erase_if(C&: FeaturesAsWritten, P: [](const std::string &S) {
470 return S.empty() || (S[0] != '+' && S[0] != '-');
471 });
472 llvm::stable_sort(Range&: FeaturesAsWritten,
473 C: [](const std::string &A, const std::string &B) {
474 return A.substr(pos: 1) < B.substr(pos: 1);
475 });
476 auto NewRend =
477 std::unique(first: FeaturesAsWritten.rbegin(), last: FeaturesAsWritten.rend(),
478 binary_pred: [](const std::string &A, const std::string &B) {
479 return A.substr(pos: 1) == B.substr(pos: 1);
480 });
481 // Because we are operating on reverse iterators, the duplicate elements
482 // are actually at the beginning.
483 FeaturesAsWritten.erase(first: FeaturesAsWritten.begin(), last: NewRend.base());
484 return FeaturesAsWritten;
485}
486
487/// Compare the given set of target options against an existing set of
488/// target options.
489///
490/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
491///
492/// \returns true if the target options mis-match, false otherwise.
493static bool checkTargetOptions(const TargetOptions &TargetOpts,
494 const TargetOptions &ExistingTargetOpts,
495 StringRef ModuleFilename,
496 DiagnosticsEngine *Diags,
497 bool AllowCompatibleDifferences = true) {
498#define CHECK_TARGET_OPT(Field, Name) \
499 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
500 if (Diags) \
501 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
502 << ModuleFilename << Name << TargetOpts.Field \
503 << ExistingTargetOpts.Field; \
504 return true; \
505 }
506
507 // The triple and ABI must match exactly.
508 CHECK_TARGET_OPT(Triple, "target");
509 CHECK_TARGET_OPT(ABI, "target ABI");
510
511 // We can tolerate different CPUs in many cases, notably when one CPU
512 // supports a strict superset of another. When allowing compatible
513 // differences skip this check.
514 if (!AllowCompatibleDifferences) {
515 CHECK_TARGET_OPT(CPU, "target CPU");
516 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
517 }
518
519#undef CHECK_TARGET_OPT
520
521 // Compare feature sets.
522 // Alternatively, we could be diffing TargetOpts.Features, but that would
523 // clutter the output with implied features.
524 std::vector<std::string> ExistingFeatures =
525 accumulateFeaturesAsWritten(FeaturesAsWritten: ExistingTargetOpts.FeaturesAsWritten);
526 std::vector<std::string> ReadFeatures =
527 accumulateFeaturesAsWritten(FeaturesAsWritten: TargetOpts.FeaturesAsWritten);
528
529 // We compute the set difference in both directions explicitly so that we can
530 // diagnose the differences differently.
531 auto FeatureLess = [](StringRef A, StringRef B) {
532 return A.substr(Start: 1) < B.substr(Start: 1);
533 };
534
535 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
536 std::set_difference(first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(),
537 first2: ReadFeatures.begin(), last2: ReadFeatures.end(),
538 result: std::back_inserter(x&: UnmatchedExistingFeatures),
539 comp: FeatureLess);
540 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
541 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
542 result: std::back_inserter(x&: UnmatchedReadFeatures), comp: FeatureLess);
543
544 // If we are allowing compatible differences and the read feature set is
545 // a strict subset of the existing feature set, there is nothing to diagnose.
546 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
547 return false;
548
549 if (Diags) {
550 for (StringRef Feature : UnmatchedReadFeatures)
551 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
552 << /* is-existing-feature */ false << ModuleFilename << Feature;
553 for (StringRef Feature : UnmatchedExistingFeatures)
554 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
555 << /* is-existing-feature */ true << ModuleFilename << Feature;
556 }
557
558 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
559}
560
561bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
562 StringRef ModuleFilename, bool Complain,
563 bool AllowCompatibleDifferences) {
564 const LangOptions &ExistingLangOpts = PP.getLangOpts();
565 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
566 Diags: Complain ? &Reader.Diags : nullptr,
567 AllowCompatibleDifferences);
568}
569
570bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
571 StringRef ModuleFilename, bool Complain,
572 bool AllowCompatibleDifferences) {
573 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
574 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
575 Diags: Complain ? &Reader.Diags : nullptr,
576 AllowCompatibleDifferences);
577}
578
579bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
580 StringRef ModuleFilename, bool Complain,
581 bool AllowCompatibleDifferences) {
582 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
583 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
584 Diags: Complain ? &Reader.Diags : nullptr,
585 AllowCompatibleDifferences);
586}
587
588namespace {
589
590using MacroDefinitionsMap =
591 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
592
593class DeclsSet {
594 SmallVector<NamedDecl *, 64> Decls;
595 llvm::SmallPtrSet<NamedDecl *, 8> Found;
596
597public:
598 operator ArrayRef<NamedDecl *>() const { return Decls; }
599
600 bool empty() const { return Decls.empty(); }
601
602 bool insert(NamedDecl *ND) {
603 auto [_, Inserted] = Found.insert(Ptr: ND);
604 if (Inserted)
605 Decls.push_back(Elt: ND);
606 return Inserted;
607 }
608};
609
610using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
611
612} // namespace
613
614static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
615 DiagnosticsEngine &Diags,
616 StringRef ModuleFilename,
617 bool Complain) {
618 using Level = DiagnosticsEngine::Level;
619
620 // Check current mappings for new -Werror mappings, and the stored mappings
621 // for cases that were explicitly mapped to *not* be errors that are now
622 // errors because of options like -Werror.
623 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
624
625 for (DiagnosticsEngine *MappingSource : MappingSources) {
626 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
627 diag::kind DiagID = DiagIDMappingPair.first;
628 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
629 if (CurLevel < DiagnosticsEngine::Error)
630 continue; // not significant
631 Level StoredLevel =
632 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
633 if (StoredLevel < DiagnosticsEngine::Error) {
634 if (Complain)
635 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
636 << "-Werror=" + Diags.getDiagnosticIDs()
637 ->getWarningOptionForDiag(DiagID)
638 .str()
639 << ModuleFilename;
640 return true;
641 }
642 }
643 }
644
645 return false;
646}
647
648static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
649 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
650 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
651 return true;
652 return Ext >= diag::Severity::Error;
653}
654
655static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
656 DiagnosticsEngine &Diags,
657 StringRef ModuleFilename, bool IsSystem,
658 bool SystemHeaderWarningsInModule,
659 bool Complain) {
660 // Top-level options
661 if (IsSystem) {
662 if (Diags.getSuppressSystemWarnings())
663 return false;
664 // If -Wsystem-headers was not enabled before, and it was not explicit,
665 // be conservative
666 if (StoredDiags.getSuppressSystemWarnings() &&
667 !SystemHeaderWarningsInModule) {
668 if (Complain)
669 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
670 << "-Wsystem-headers" << ModuleFilename;
671 return true;
672 }
673 }
674
675 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
676 if (Complain)
677 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
678 << "-Werror" << ModuleFilename;
679 return true;
680 }
681
682 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
683 !StoredDiags.getEnableAllWarnings()) {
684 if (Complain)
685 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
686 << "-Weverything -Werror" << ModuleFilename;
687 return true;
688 }
689
690 if (isExtHandlingFromDiagsError(Diags) &&
691 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
692 if (Complain)
693 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
694 << "-pedantic-errors" << ModuleFilename;
695 return true;
696 }
697
698 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
699 Complain);
700}
701
702/// Return the top import module if it is implicit, nullptr otherwise.
703static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
704 Preprocessor &PP) {
705 // If the original import came from a file explicitly generated by the user,
706 // don't check the diagnostic mappings.
707 // FIXME: currently this is approximated by checking whether this is not a
708 // module import of an implicitly-loaded module file.
709 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
710 // the transitive closure of its imports, since unrelated modules cannot be
711 // imported until after this module finishes validation.
712 ModuleFile *TopImport = &*ModuleMgr.rbegin();
713 while (!TopImport->ImportedBy.empty())
714 TopImport = TopImport->ImportedBy[0];
715 if (TopImport->Kind != MK_ImplicitModule)
716 return nullptr;
717
718 StringRef ModuleName = TopImport->ModuleName;
719 assert(!ModuleName.empty() && "diagnostic options read before module name");
720
721 Module *M =
722 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
723 assert(M && "missing module");
724 return M;
725}
726
727bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
728 StringRef ModuleFilename,
729 bool Complain) {
730 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
731 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
732 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
733 // This should never fail, because we would have processed these options
734 // before writing them to an ASTFile.
735 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
736 VFS&: PP.getFileManager().getVirtualFileSystem(),
737 /*Report*/ ReportDiags: false);
738
739 ModuleManager &ModuleMgr = Reader.getModuleManager();
740 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
741
742 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
743 if (!TopM)
744 return false;
745
746 Module *Importer = PP.getCurrentModule();
747
748 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
749 bool SystemHeaderWarningsInModule =
750 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
751 Element: Importer->Name);
752
753 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
754 // contains the union of their flags.
755 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
756 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
757 Complain);
758}
759
760/// Collect the macro definitions provided by the given preprocessor
761/// options.
762static void
763collectMacroDefinitions(const PreprocessorOptions &PPOpts,
764 MacroDefinitionsMap &Macros,
765 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
766 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
767 StringRef Macro = PPOpts.Macros[I].first;
768 bool IsUndef = PPOpts.Macros[I].second;
769
770 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
771 StringRef MacroName = MacroPair.first;
772 StringRef MacroBody = MacroPair.second;
773
774 // For an #undef'd macro, we only care about the name.
775 if (IsUndef) {
776 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
777 if (MacroNames && Inserted)
778 MacroNames->push_back(Elt: MacroName);
779
780 It->second = std::make_pair(x: "", y: true);
781 continue;
782 }
783
784 // For a #define'd macro, figure out the actual definition.
785 if (MacroName.size() == Macro.size())
786 MacroBody = "1";
787 else {
788 // Note: GCC drops anything following an end-of-line character.
789 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
790 MacroBody = MacroBody.substr(Start: 0, N: End);
791 }
792
793 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
794 if (MacroNames && Inserted)
795 MacroNames->push_back(Elt: MacroName);
796 It->second = std::make_pair(x&: MacroBody, y: false);
797 }
798}
799
800enum OptionValidation {
801 OptionValidateNone,
802 OptionValidateContradictions,
803 OptionValidateStrictMatches,
804};
805
806/// Check the preprocessor options deserialized from the control block
807/// against the preprocessor options in an existing preprocessor.
808///
809/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
810/// \param Validation If set to OptionValidateNone, ignore differences in
811/// preprocessor options. If set to OptionValidateContradictions,
812/// require that options passed both in the AST file and on the command
813/// line (-D or -U) match, but tolerate options missing in one or the
814/// other. If set to OptionValidateContradictions, require that there
815/// are no differences in the options between the two.
816static bool checkPreprocessorOptions(
817 const PreprocessorOptions &PPOpts,
818 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
819 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
820 std::string &SuggestedPredefines, const LangOptions &LangOpts,
821 OptionValidation Validation = OptionValidateContradictions) {
822 if (ReadMacros) {
823 // Check macro definitions.
824 MacroDefinitionsMap ASTFileMacros;
825 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
826 MacroDefinitionsMap ExistingMacros;
827 SmallVector<StringRef, 4> ExistingMacroNames;
828 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
829 MacroNames: &ExistingMacroNames);
830
831 // Use a line marker to enter the <command line> file, as the defines and
832 // undefines here will have come from the command line.
833 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
834
835 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
836 // Dig out the macro definition in the existing preprocessor options.
837 StringRef MacroName = ExistingMacroNames[I];
838 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
839
840 // Check whether we know anything about this macro name or not.
841 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
842 ASTFileMacros.find(Key: MacroName);
843 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
844 if (Validation == OptionValidateStrictMatches) {
845 // If strict matches are requested, don't tolerate any extra defines
846 // on the command line that are missing in the AST file.
847 if (Diags) {
848 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
849 << MacroName << true << ModuleFilename;
850 }
851 return true;
852 }
853 // FIXME: Check whether this identifier was referenced anywhere in the
854 // AST file. If so, we should reject the AST file. Unfortunately, this
855 // information isn't in the control block. What shall we do about it?
856
857 if (Existing.second) {
858 SuggestedPredefines += "#undef ";
859 SuggestedPredefines += MacroName.str();
860 SuggestedPredefines += '\n';
861 } else {
862 SuggestedPredefines += "#define ";
863 SuggestedPredefines += MacroName.str();
864 SuggestedPredefines += ' ';
865 SuggestedPredefines += Existing.first.str();
866 SuggestedPredefines += '\n';
867 }
868 continue;
869 }
870
871 // If the macro was defined in one but undef'd in the other, we have a
872 // conflict.
873 if (Existing.second != Known->second.second) {
874 if (Diags) {
875 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
876 << MacroName << Known->second.second << ModuleFilename;
877 }
878 return true;
879 }
880
881 // If the macro was #undef'd in both, or if the macro bodies are
882 // identical, it's fine.
883 if (Existing.second || Existing.first == Known->second.first) {
884 ASTFileMacros.erase(I: Known);
885 continue;
886 }
887
888 // The macro bodies differ; complain.
889 if (Diags) {
890 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
891 << MacroName << Known->second.first << Existing.first
892 << ModuleFilename;
893 }
894 return true;
895 }
896
897 // Leave the <command line> file and return to <built-in>.
898 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
899
900 if (Validation == OptionValidateStrictMatches) {
901 // If strict matches are requested, don't tolerate any extra defines in
902 // the AST file that are missing on the command line.
903 for (const auto &MacroName : ASTFileMacros.keys()) {
904 if (Diags) {
905 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
906 << MacroName << false << ModuleFilename;
907 }
908 return true;
909 }
910 }
911 }
912
913 // Check whether we're using predefines.
914 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
915 Validation != OptionValidateNone) {
916 if (Diags) {
917 Diags->Report(DiagID: diag::err_ast_file_undef)
918 << ExistingPPOpts.UsePredefines << ModuleFilename;
919 }
920 return true;
921 }
922
923 // Detailed record is important since it is used for the module cache hash.
924 if (LangOpts.Modules &&
925 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
926 Validation != OptionValidateNone) {
927 if (Diags) {
928 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
929 << PPOpts.DetailedRecord << ModuleFilename;
930 }
931 return true;
932 }
933
934 // Compute the #include and #include_macros lines we need.
935 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
936 StringRef File = ExistingPPOpts.MacroIncludes[I];
937 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
938 continue;
939
940 SuggestedPredefines += "#__include_macros \"";
941 SuggestedPredefines += File;
942 SuggestedPredefines += "\"\n##\n";
943 }
944
945 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
946 StringRef File = ExistingPPOpts.Includes[I];
947
948 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
949 !ExistingPPOpts.PCHThroughHeader.empty()) {
950 // In case the through header is an include, we must add all the includes
951 // to the predefines so the start point can be determined.
952 SuggestedPredefines += "#include \"";
953 SuggestedPredefines += File;
954 SuggestedPredefines += "\"\n";
955 continue;
956 }
957
958 if (File == ExistingPPOpts.ImplicitPCHInclude)
959 continue;
960
961 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
962 continue;
963
964 SuggestedPredefines += "#include \"";
965 SuggestedPredefines += File;
966 SuggestedPredefines += "\"\n";
967 }
968
969 return false;
970}
971
972bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
973 StringRef ModuleFilename,
974 bool ReadMacros, bool Complain,
975 std::string &SuggestedPredefines) {
976 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
977
978 return checkPreprocessorOptions(
979 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
980 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
981 SuggestedPredefines, LangOpts: PP.getLangOpts());
982}
983
984bool SimpleASTReaderListener::ReadPreprocessorOptions(
985 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
986 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
987 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
988 ModuleFilename, ReadMacros, Diags: nullptr,
989 FileMgr&: PP.getFileManager(), SuggestedPredefines,
990 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
991}
992
993/// Check that the specified and the existing module cache paths are equivalent.
994///
995/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
996/// \returns true when the module cache paths differ.
997static bool checkModuleCachePath(FileManager &FileMgr, StringRef ContextHash,
998 StringRef ExistingSpecificModuleCachePath,
999 StringRef ASTFilename,
1000 DiagnosticsEngine *Diags,
1001 const LangOptions &LangOpts,
1002 const PreprocessorOptions &PPOpts,
1003 const HeaderSearchOptions &HSOpts,
1004 const HeaderSearchOptions &ASTFileHSOpts) {
1005 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
1006 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
1007 ContextHash: std::string(ContextHash));
1008
1009 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
1010 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
1011 return false;
1012 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1013 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
1014 if (EqualOrErr && *EqualOrErr)
1015 return false;
1016 if (Diags) {
1017 // If the module cache arguments provided from the command line are the
1018 // same, the mismatch must come from other arguments of the configuration
1019 // and not directly the cache path.
1020 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1021 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
1022 if (EqualOrErr && *EqualOrErr)
1023 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
1024 else
1025 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
1026 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
1027 << ASTFilename;
1028 }
1029 return true;
1030}
1031
1032bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
1033 StringRef ASTFilename,
1034 StringRef ContextHash,
1035 bool Complain) {
1036 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1037 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1038 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1039 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1040 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1041 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1042}
1043
1044void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1045 PP.setCounterValue(Value);
1046}
1047
1048//===----------------------------------------------------------------------===//
1049// AST reader implementation
1050//===----------------------------------------------------------------------===//
1051
1052static uint64_t readULEB(const unsigned char *&P) {
1053 unsigned Length = 0;
1054 const char *Error = nullptr;
1055
1056 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1057 if (Error)
1058 llvm::report_fatal_error(reason: Error);
1059 P += Length;
1060 return Val;
1061}
1062
1063/// Read ULEB-encoded key length and data length.
1064static std::pair<unsigned, unsigned>
1065readULEBKeyDataLength(const unsigned char *&P) {
1066 unsigned KeyLen = readULEB(P);
1067 if ((unsigned)KeyLen != KeyLen)
1068 llvm::report_fatal_error(reason: "key too large");
1069
1070 unsigned DataLen = readULEB(P);
1071 if ((unsigned)DataLen != DataLen)
1072 llvm::report_fatal_error(reason: "data too large");
1073
1074 return std::make_pair(x&: KeyLen, y&: DataLen);
1075}
1076
1077void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1078 bool TakeOwnership) {
1079 DeserializationListener = Listener;
1080 OwnsDeserializationListener = TakeOwnership;
1081}
1082
1083unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1084 return serialization::ComputeHash(Sel);
1085}
1086
1087LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1088 LocalDeclID ID(Value);
1089#ifndef NDEBUG
1090 if (!MF.ModuleOffsetMap.empty())
1091 Reader.ReadModuleOffsetMap(MF);
1092
1093 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1094 unsigned LocalDeclID = ID.getLocalDeclIndex();
1095
1096 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1097
1098 ModuleFile *OwningModuleFile =
1099 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1100 assert(OwningModuleFile);
1101
1102 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1103
1104 if (!ModuleFileIndex)
1105 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1106
1107 assert(LocalDeclID < LocalNumDecls);
1108#endif
1109 (void)Reader;
1110 (void)MF;
1111 return ID;
1112}
1113
1114LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1115 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1116 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1117 return LocalDeclID::get(Reader, MF, Value);
1118}
1119
1120std::pair<unsigned, unsigned>
1121ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1122 return readULEBKeyDataLength(P&: d);
1123}
1124
1125ASTSelectorLookupTrait::internal_key_type
1126ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1127 using namespace llvm::support;
1128
1129 SelectorTable &SelTable = Reader.getContext().Selectors;
1130 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1131 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1132 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1133 if (N == 0)
1134 return SelTable.getNullarySelector(ID: FirstII);
1135 else if (N == 1)
1136 return SelTable.getUnarySelector(ID: FirstII);
1137
1138 SmallVector<const IdentifierInfo *, 16> Args;
1139 Args.push_back(Elt: FirstII);
1140 for (unsigned I = 1; I != N; ++I)
1141 Args.push_back(Elt: Reader.getLocalIdentifier(
1142 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1143
1144 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1145}
1146
1147ASTSelectorLookupTrait::data_type
1148ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1149 unsigned DataLen) {
1150 using namespace llvm::support;
1151
1152 data_type Result;
1153
1154 Result.ID = Reader.getGlobalSelectorID(
1155 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1156 unsigned FullInstanceBits =
1157 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1158 unsigned FullFactoryBits =
1159 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1160 Result.InstanceBits = FullInstanceBits & 0x3;
1161 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1162 Result.FactoryBits = FullFactoryBits & 0x3;
1163 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1164 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1165 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1166
1167 // Load instance methods
1168 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1169 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1170 F, LocalID: LocalDeclID::get(
1171 Reader, MF&: F,
1172 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1173 Result.Instance.push_back(Elt: Method);
1174 }
1175
1176 // Load factory methods
1177 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1178 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1179 F, LocalID: LocalDeclID::get(
1180 Reader, MF&: F,
1181 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1182 Result.Factory.push_back(Elt: Method);
1183 }
1184
1185 return Result;
1186}
1187
1188unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1189 return llvm::djbHash(Buffer: a);
1190}
1191
1192std::pair<unsigned, unsigned>
1193ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1194 return readULEBKeyDataLength(P&: d);
1195}
1196
1197ASTIdentifierLookupTraitBase::internal_key_type
1198ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1199 assert(n >= 2 && d[n-1] == '\0');
1200 return StringRef((const char*) d, n-1);
1201}
1202
1203/// Whether the given identifier is "interesting".
1204static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1205 bool IsModule) {
1206 bool IsInteresting =
1207 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1208 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1209 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1210 return II.hadMacroDefinition() || II.isPoisoned() ||
1211 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1212 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1213 II.getFETokenInfo());
1214}
1215
1216static bool readBit(unsigned &Bits) {
1217 bool Value = Bits & 0x1;
1218 Bits >>= 1;
1219 return Value;
1220}
1221
1222IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1223 using namespace llvm::support;
1224
1225 IdentifierID RawID =
1226 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1227 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1228}
1229
1230static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1231 bool IsModule) {
1232 if (!II.isFromAST()) {
1233 II.setIsFromAST();
1234 if (isInterestingIdentifier(Reader, II, IsModule))
1235 II.setChangedSinceDeserialization();
1236 }
1237}
1238
1239IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1240 const unsigned char* d,
1241 unsigned DataLen) {
1242 using namespace llvm::support;
1243
1244 IdentifierID RawID =
1245 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1246 bool IsInteresting = RawID & 0x01;
1247
1248 DataLen -= sizeof(IdentifierID);
1249
1250 // Wipe out the "is interesting" bit.
1251 RawID = RawID >> 1;
1252
1253 // Build the IdentifierInfo and link the identifier ID with it.
1254 IdentifierInfo *II = KnownII;
1255 if (!II) {
1256 II = &Reader.getIdentifierTable().getOwn(Name: k);
1257 KnownII = II;
1258 }
1259 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1260 markIdentifierFromAST(Reader, II&: *II, IsModule);
1261 Reader.markIdentifierUpToDate(II);
1262
1263 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1264 if (!IsInteresting) {
1265 // For uninteresting identifiers, there's nothing else to do. Just notify
1266 // the reader that we've finished loading this identifier.
1267 Reader.SetIdentifierInfo(ID, II);
1268 return II;
1269 }
1270
1271 unsigned ObjCOrBuiltinID =
1272 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1273 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1274 bool CPlusPlusOperatorKeyword = readBit(Bits);
1275 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1276 bool Poisoned = readBit(Bits);
1277 bool ExtensionToken = readBit(Bits);
1278 bool HasMacroDefinition = readBit(Bits);
1279
1280 assert(Bits == 0 && "Extra bits in the identifier?");
1281 DataLen -= sizeof(uint16_t) * 2;
1282
1283 // Set or check the various bits in the IdentifierInfo structure.
1284 // Token IDs are read-only.
1285 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1286 II->revertTokenIDToIdentifier();
1287 if (!F.isModule())
1288 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1289 assert(II->isExtensionToken() == ExtensionToken &&
1290 "Incorrect extension token flag");
1291 (void)ExtensionToken;
1292 if (Poisoned)
1293 II->setIsPoisoned(true);
1294 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1295 "Incorrect C++ operator keyword flag");
1296 (void)CPlusPlusOperatorKeyword;
1297
1298 // If this identifier has a macro definition, deserialize it or notify the
1299 // visitor the actual definition is in a different module.
1300 if (HasMacroDefinition) {
1301 uint32_t MacroDirectivesOffset =
1302 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1303 DataLen -= 4;
1304
1305 if (MacroDirectivesOffset)
1306 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1307 else
1308 hasMacroDefinitionInDependencies = true;
1309 }
1310
1311 Reader.SetIdentifierInfo(ID, II);
1312
1313 // Read all of the declarations visible at global scope with this
1314 // name.
1315 if (DataLen > 0) {
1316 SmallVector<GlobalDeclID, 4> DeclIDs;
1317 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1318 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1319 F, LocalID: LocalDeclID::get(
1320 Reader, MF&: F,
1321 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1322 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1323 }
1324
1325 return II;
1326}
1327
1328DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1329 : Kind(Name.getNameKind()) {
1330 switch (Kind) {
1331 case DeclarationName::Identifier:
1332 Data = (uint64_t)Name.getAsIdentifierInfo();
1333 break;
1334 case DeclarationName::ObjCZeroArgSelector:
1335 case DeclarationName::ObjCOneArgSelector:
1336 case DeclarationName::ObjCMultiArgSelector:
1337 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1338 break;
1339 case DeclarationName::CXXOperatorName:
1340 Data = Name.getCXXOverloadedOperator();
1341 break;
1342 case DeclarationName::CXXLiteralOperatorName:
1343 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1344 break;
1345 case DeclarationName::CXXDeductionGuideName:
1346 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1347 ->getDeclName().getAsIdentifierInfo();
1348 break;
1349 case DeclarationName::CXXConstructorName:
1350 case DeclarationName::CXXDestructorName:
1351 case DeclarationName::CXXConversionFunctionName:
1352 case DeclarationName::CXXUsingDirective:
1353 Data = 0;
1354 break;
1355 }
1356}
1357
1358unsigned DeclarationNameKey::getHash() const {
1359 llvm::FoldingSetNodeID ID;
1360 ID.AddInteger(I: Kind);
1361
1362 switch (Kind) {
1363 case DeclarationName::Identifier:
1364 case DeclarationName::CXXLiteralOperatorName:
1365 case DeclarationName::CXXDeductionGuideName:
1366 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1367 break;
1368 case DeclarationName::ObjCZeroArgSelector:
1369 case DeclarationName::ObjCOneArgSelector:
1370 case DeclarationName::ObjCMultiArgSelector:
1371 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1372 break;
1373 case DeclarationName::CXXOperatorName:
1374 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1375 break;
1376 case DeclarationName::CXXConstructorName:
1377 case DeclarationName::CXXDestructorName:
1378 case DeclarationName::CXXConversionFunctionName:
1379 case DeclarationName::CXXUsingDirective:
1380 break;
1381 }
1382
1383 return ID.computeStableHash();
1384}
1385
1386ModuleFile *
1387ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1388 using namespace llvm::support;
1389
1390 uint32_t ModuleFileID =
1391 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1392 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1393}
1394
1395std::pair<unsigned, unsigned>
1396ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1397 return readULEBKeyDataLength(P&: d);
1398}
1399
1400DeclarationNameKey
1401ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1402 using namespace llvm::support;
1403
1404 auto Kind = (DeclarationName::NameKind)*d++;
1405 uint64_t Data;
1406 switch (Kind) {
1407 case DeclarationName::Identifier:
1408 case DeclarationName::CXXLiteralOperatorName:
1409 case DeclarationName::CXXDeductionGuideName:
1410 Data = (uint64_t)Reader.getLocalIdentifier(
1411 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1412 break;
1413 case DeclarationName::ObjCZeroArgSelector:
1414 case DeclarationName::ObjCOneArgSelector:
1415 case DeclarationName::ObjCMultiArgSelector:
1416 Data = (uint64_t)Reader
1417 .getLocalSelector(
1418 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1419 .getAsOpaquePtr();
1420 break;
1421 case DeclarationName::CXXOperatorName:
1422 Data = *d++; // OverloadedOperatorKind
1423 break;
1424 case DeclarationName::CXXConstructorName:
1425 case DeclarationName::CXXDestructorName:
1426 case DeclarationName::CXXConversionFunctionName:
1427 case DeclarationName::CXXUsingDirective:
1428 Data = 0;
1429 break;
1430 }
1431
1432 return DeclarationNameKey(Kind, Data);
1433}
1434
1435ASTDeclContextNameLookupTrait::internal_key_type
1436ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1437 return ReadKeyBase(d);
1438}
1439
1440void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1441 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1442 using namespace llvm::support;
1443
1444 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1445 LocalDeclID ID = LocalDeclID::get(
1446 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1447 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1448 }
1449}
1450
1451void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1452 const unsigned char *d,
1453 unsigned DataLen,
1454 data_type_builder &Val) {
1455 ReadDataIntoImpl(d, DataLen, Val);
1456}
1457
1458ModuleLocalNameLookupTrait::hash_value_type
1459ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1460 llvm::FoldingSetNodeID ID;
1461 ID.AddInteger(I: Key.first.getHash());
1462 ID.AddInteger(I: Key.second);
1463 return ID.computeStableHash();
1464}
1465
1466ModuleLocalNameLookupTrait::internal_key_type
1467ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1468 DeclarationNameKey Name(Key.first);
1469
1470 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1471 if (!ModuleHash)
1472 return {Name, 0};
1473
1474 return {Name, *ModuleHash};
1475}
1476
1477ModuleLocalNameLookupTrait::internal_key_type
1478ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1479 DeclarationNameKey Name = ReadKeyBase(d);
1480 unsigned PrimaryModuleHash =
1481 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1482 return {Name, PrimaryModuleHash};
1483}
1484
1485void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1486 const unsigned char *d,
1487 unsigned DataLen,
1488 data_type_builder &Val) {
1489 ReadDataIntoImpl(d, DataLen, Val);
1490}
1491
1492ModuleFile *
1493LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1494 using namespace llvm::support;
1495
1496 uint32_t ModuleFileID =
1497 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1498 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1499}
1500
1501LazySpecializationInfoLookupTrait::internal_key_type
1502LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1503 using namespace llvm::support;
1504 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1505}
1506
1507std::pair<unsigned, unsigned>
1508LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1509 return readULEBKeyDataLength(P&: d);
1510}
1511
1512void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1513 const unsigned char *d,
1514 unsigned DataLen,
1515 data_type_builder &Val) {
1516 using namespace llvm::support;
1517
1518 for (unsigned NumDecls =
1519 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1520 NumDecls; --NumDecls) {
1521 LocalDeclID LocalID = LocalDeclID::get(
1522 Reader, MF&: F,
1523 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1524 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1525 }
1526}
1527
1528bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1529 BitstreamCursor &Cursor,
1530 uint64_t Offset,
1531 DeclContext *DC) {
1532 assert(Offset != 0);
1533
1534 SavedStreamPosition SavedPosition(Cursor);
1535 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1536 Error(Err: std::move(Err));
1537 return true;
1538 }
1539
1540 RecordData Record;
1541 StringRef Blob;
1542 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1543 if (!MaybeCode) {
1544 Error(Err: MaybeCode.takeError());
1545 return true;
1546 }
1547 unsigned Code = MaybeCode.get();
1548
1549 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1550 if (!MaybeRecCode) {
1551 Error(Err: MaybeRecCode.takeError());
1552 return true;
1553 }
1554 unsigned RecCode = MaybeRecCode.get();
1555 if (RecCode != DECL_CONTEXT_LEXICAL) {
1556 Error(Msg: "Expected lexical block");
1557 return true;
1558 }
1559
1560 assert(!isa<TranslationUnitDecl>(DC) &&
1561 "expected a TU_UPDATE_LEXICAL record for TU");
1562 // If we are handling a C++ class template instantiation, we can see multiple
1563 // lexical updates for the same record. It's important that we select only one
1564 // of them, so that field numbering works properly. Just pick the first one we
1565 // see.
1566 auto &Lex = LexicalDecls[DC];
1567 if (!Lex.first) {
1568 Lex = std::make_pair(
1569 x: &M, y: llvm::ArrayRef(
1570 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1571 Blob.size() / sizeof(DeclID)));
1572 }
1573 DC->setHasExternalLexicalStorage(true);
1574 return false;
1575}
1576
1577bool ASTReader::ReadVisibleDeclContextStorage(
1578 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1579 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1580 assert(Offset != 0);
1581
1582 SavedStreamPosition SavedPosition(Cursor);
1583 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1584 Error(Err: std::move(Err));
1585 return true;
1586 }
1587
1588 RecordData Record;
1589 StringRef Blob;
1590 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1591 if (!MaybeCode) {
1592 Error(Err: MaybeCode.takeError());
1593 return true;
1594 }
1595 unsigned Code = MaybeCode.get();
1596
1597 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1598 if (!MaybeRecCode) {
1599 Error(Err: MaybeRecCode.takeError());
1600 return true;
1601 }
1602 unsigned RecCode = MaybeRecCode.get();
1603 switch (VisibleKind) {
1604 case VisibleDeclContextStorageKind::GenerallyVisible:
1605 if (RecCode != DECL_CONTEXT_VISIBLE) {
1606 Error(Msg: "Expected visible lookup table block");
1607 return true;
1608 }
1609 break;
1610 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1611 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1612 Error(Msg: "Expected module local visible lookup table block");
1613 return true;
1614 }
1615 break;
1616 case VisibleDeclContextStorageKind::TULocalVisible:
1617 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1618 Error(Msg: "Expected TU local lookup table block");
1619 return true;
1620 }
1621 break;
1622 }
1623
1624 // We can't safely determine the primary context yet, so delay attaching the
1625 // lookup table until we're done with recursive deserialization.
1626 auto *Data = (const unsigned char*)Blob.data();
1627 switch (VisibleKind) {
1628 case VisibleDeclContextStorageKind::GenerallyVisible:
1629 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1630 break;
1631 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1632 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1633 break;
1634 case VisibleDeclContextStorageKind::TULocalVisible:
1635 if (M.Kind == MK_MainFile)
1636 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1637 break;
1638 }
1639 return false;
1640}
1641
1642void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1643 ModuleFile &M, bool IsPartial) {
1644 D = D->getCanonicalDecl();
1645 auto &SpecLookups =
1646 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1647 SpecLookups[D].Table.add(File: &M, Data,
1648 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1649}
1650
1651bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1652 uint64_t Offset, Decl *D, bool IsPartial) {
1653 assert(Offset != 0);
1654
1655 SavedStreamPosition SavedPosition(Cursor);
1656 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1657 Error(Err: std::move(Err));
1658 return true;
1659 }
1660
1661 RecordData Record;
1662 StringRef Blob;
1663 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1664 if (!MaybeCode) {
1665 Error(Err: MaybeCode.takeError());
1666 return true;
1667 }
1668 unsigned Code = MaybeCode.get();
1669
1670 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1671 if (!MaybeRecCode) {
1672 Error(Err: MaybeRecCode.takeError());
1673 return true;
1674 }
1675 unsigned RecCode = MaybeRecCode.get();
1676 if (RecCode != DECL_SPECIALIZATIONS &&
1677 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1678 Error(Msg: "Expected decl specs block");
1679 return true;
1680 }
1681
1682 auto *Data = (const unsigned char *)Blob.data();
1683 AddSpecializations(D, Data, M, IsPartial);
1684 return false;
1685}
1686
1687void ASTReader::Error(StringRef Msg) const {
1688 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1689 if (PP.getLangOpts().Modules &&
1690 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1691 Diag(DiagID: diag::note_module_cache_path)
1692 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1693 }
1694}
1695
1696void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1697 StringRef Arg3) const {
1698 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1699}
1700
1701namespace {
1702struct AlreadyReportedDiagnosticError
1703 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1704 static char ID;
1705
1706 void log(raw_ostream &OS) const override {
1707 llvm_unreachable("reporting an already-reported diagnostic error");
1708 }
1709
1710 std::error_code convertToErrorCode() const override {
1711 return llvm::inconvertibleErrorCode();
1712 }
1713};
1714
1715char AlreadyReportedDiagnosticError::ID = 0;
1716} // namespace
1717
1718void ASTReader::Error(llvm::Error &&Err) const {
1719 handleAllErrors(
1720 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1721 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1722}
1723
1724//===----------------------------------------------------------------------===//
1725// Source Manager Deserialization
1726//===----------------------------------------------------------------------===//
1727
1728/// Read the line table in the source manager block.
1729void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1730 unsigned Idx = 0;
1731 LineTableInfo &LineTable = SourceMgr.getLineTable();
1732
1733 // Parse the file names
1734 std::map<int, int> FileIDs;
1735 FileIDs[-1] = -1; // For unspecified filenames.
1736 for (unsigned I = 0; Record[Idx]; ++I) {
1737 // Extract the file name
1738 auto Filename = ReadPath(F, Record, Idx);
1739 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1740 }
1741 ++Idx;
1742
1743 // Parse the line entries
1744 std::vector<LineEntry> Entries;
1745 while (Idx < Record.size()) {
1746 FileID FID = ReadFileID(F, Record, Idx);
1747
1748 // Extract the line entries
1749 unsigned NumEntries = Record[Idx++];
1750 assert(NumEntries && "no line entries for file ID");
1751 Entries.clear();
1752 Entries.reserve(n: NumEntries);
1753 for (unsigned I = 0; I != NumEntries; ++I) {
1754 unsigned FileOffset = Record[Idx++];
1755 unsigned LineNo = Record[Idx++];
1756 int FilenameID = FileIDs[Record[Idx++]];
1757 SrcMgr::CharacteristicKind FileKind
1758 = (SrcMgr::CharacteristicKind)Record[Idx++];
1759 unsigned IncludeOffset = Record[Idx++];
1760 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1761 FileKind, IncludeOffset));
1762 }
1763 LineTable.AddEntry(FID, Entries);
1764 }
1765}
1766
1767/// Read a source manager block
1768llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1769 using namespace SrcMgr;
1770
1771 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1772
1773 // Set the source-location entry cursor to the current position in
1774 // the stream. This cursor will be used to read the contents of the
1775 // source manager block initially, and then lazily read
1776 // source-location entries as needed.
1777 SLocEntryCursor = F.Stream;
1778
1779 // The stream itself is going to skip over the source manager block.
1780 if (llvm::Error Err = F.Stream.SkipBlock())
1781 return Err;
1782
1783 // Enter the source manager block.
1784 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1785 return Err;
1786 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1787
1788 RecordData Record;
1789 while (true) {
1790 Expected<llvm::BitstreamEntry> MaybeE =
1791 SLocEntryCursor.advanceSkippingSubblocks();
1792 if (!MaybeE)
1793 return MaybeE.takeError();
1794 llvm::BitstreamEntry E = MaybeE.get();
1795
1796 switch (E.Kind) {
1797 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1798 case llvm::BitstreamEntry::Error:
1799 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1800 Fmt: "malformed block record in AST file");
1801 case llvm::BitstreamEntry::EndBlock:
1802 return llvm::Error::success();
1803 case llvm::BitstreamEntry::Record:
1804 // The interesting case.
1805 break;
1806 }
1807
1808 // Read a record.
1809 Record.clear();
1810 StringRef Blob;
1811 Expected<unsigned> MaybeRecord =
1812 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1813 if (!MaybeRecord)
1814 return MaybeRecord.takeError();
1815 switch (MaybeRecord.get()) {
1816 default: // Default behavior: ignore.
1817 break;
1818
1819 case SM_SLOC_FILE_ENTRY:
1820 case SM_SLOC_BUFFER_ENTRY:
1821 case SM_SLOC_EXPANSION_ENTRY:
1822 // Once we hit one of the source location entries, we're done.
1823 return llvm::Error::success();
1824 }
1825 }
1826}
1827
1828llvm::Expected<SourceLocation::UIntTy>
1829ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1830 BitstreamCursor &Cursor = F->SLocEntryCursor;
1831 SavedStreamPosition SavedPosition(Cursor);
1832 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1833 F->SLocEntryOffsets[Index]))
1834 return std::move(Err);
1835
1836 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1837 if (!MaybeEntry)
1838 return MaybeEntry.takeError();
1839
1840 llvm::BitstreamEntry Entry = MaybeEntry.get();
1841 if (Entry.Kind != llvm::BitstreamEntry::Record)
1842 return llvm::createStringError(
1843 EC: std::errc::illegal_byte_sequence,
1844 Fmt: "incorrectly-formatted source location entry in AST file");
1845
1846 RecordData Record;
1847 StringRef Blob;
1848 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1849 if (!MaybeSLOC)
1850 return MaybeSLOC.takeError();
1851
1852 switch (MaybeSLOC.get()) {
1853 default:
1854 return llvm::createStringError(
1855 EC: std::errc::illegal_byte_sequence,
1856 Fmt: "incorrectly-formatted source location entry in AST file");
1857 case SM_SLOC_FILE_ENTRY:
1858 case SM_SLOC_BUFFER_ENTRY:
1859 case SM_SLOC_EXPANSION_ENTRY:
1860 return F->SLocEntryBaseOffset + Record[0];
1861 }
1862}
1863
1864int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1865 auto SLocMapI =
1866 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1867 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1868 "Corrupted global sloc offset map");
1869 ModuleFile *F = SLocMapI->second;
1870
1871 bool Invalid = false;
1872
1873 auto It = llvm::upper_bound(
1874 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1875 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1876 int ID = F->SLocEntryBaseID + LocalIndex;
1877 std::size_t Index = -ID - 2;
1878 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1879 assert(!SourceMgr.SLocEntryLoaded[Index]);
1880 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1881 if (!MaybeEntryOffset) {
1882 Error(Err: MaybeEntryOffset.takeError());
1883 Invalid = true;
1884 return true;
1885 }
1886 SourceMgr.LoadedSLocEntryTable[Index] =
1887 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1888 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1889 }
1890 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1891 });
1892
1893 if (Invalid)
1894 return 0;
1895
1896 // The iterator points to the first entry with start offset greater than the
1897 // offset of interest. The previous entry must contain the offset of interest.
1898 return F->SLocEntryBaseID + *std::prev(x: It);
1899}
1900
1901std::pair<SourceLocation::UIntTy, SourceLocationEncoding::Chain>
1902ASTReader::ReadSourceLocationOffset(const RecordDataImpl &Record, unsigned Idx,
1903 SourceLocation::UIntTy InitialDelta) {
1904 SourceLocation::UIntTy Offset = Record[Idx];
1905 return {Offset, SourceLocationEncoding::Chain(Offset + InitialDelta)};
1906}
1907
1908std::pair<SourceLocation::UIntTy, SourceLocationEncoding::Chain>
1909ASTReader::ReadEntryOffset(const RecordDataImpl &Record) {
1910 // Anchor the chain at the entry's own local offset, deriving it exactly as
1911 // the writer does -- see ASTWriter::EmitEntryOffset. The field has the dummy
1912 // entry subtracted out, so add it back. The anchor stays in the writing
1913 // module's local space, which is the space the deltas are differences in;
1914 // deltaDecode therefore runs before the locations are translated into ours.
1915 return ReadSourceLocationOffset(Record, Idx: 0, InitialDelta: 2);
1916}
1917
1918bool ASTReader::ReadSLocEntry(int ID) {
1919 if (ID == 0)
1920 return false;
1921
1922 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1923 Error(Msg: "source location entry ID out-of-range for AST file");
1924 return true;
1925 }
1926
1927 // Local helper to read the (possibly-compressed) buffer data following the
1928 // entry record.
1929 auto ReadBuffer = [this](
1930 BitstreamCursor &SLocEntryCursor,
1931 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1932 RecordData Record;
1933 StringRef Blob;
1934 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1935 if (!MaybeCode) {
1936 Error(Err: MaybeCode.takeError());
1937 return nullptr;
1938 }
1939 unsigned Code = MaybeCode.get();
1940
1941 Expected<unsigned> MaybeRecCode =
1942 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1943 if (!MaybeRecCode) {
1944 Error(Err: MaybeRecCode.takeError());
1945 return nullptr;
1946 }
1947 unsigned RecCode = MaybeRecCode.get();
1948
1949 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1950 SmallVector<uint8_t, 0> Decompressed;
1951 if (llvm::Error E = llvm::compression::decompress(
1952 Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1953 Error(Msg: "could not decompress embedded file contents: " +
1954 llvm::toString(E: std::move(E)));
1955 return nullptr;
1956 }
1957 return llvm::MemoryBuffer::getMemBufferCopy(
1958 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1959 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1960 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1961 } else {
1962 Error(Msg: "AST record has invalid code");
1963 return nullptr;
1964 }
1965 };
1966
1967 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1968 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1969 BitNo: F->SLocEntryOffsetsBase +
1970 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1971 Error(Err: std::move(Err));
1972 return true;
1973 }
1974
1975 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1976 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1977
1978 ++NumSLocEntriesRead;
1979 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1980 if (!MaybeEntry) {
1981 Error(Err: MaybeEntry.takeError());
1982 return true;
1983 }
1984 llvm::BitstreamEntry Entry = MaybeEntry.get();
1985
1986 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1987 Error(Msg: "incorrectly-formatted source location entry in AST file");
1988 return true;
1989 }
1990
1991 RecordData Record;
1992 StringRef Blob;
1993 Expected<unsigned> MaybeSLOC =
1994 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1995 if (!MaybeSLOC) {
1996 Error(Err: MaybeSLOC.takeError());
1997 return true;
1998 }
1999 switch (MaybeSLOC.get()) {
2000 default:
2001 Error(Msg: "incorrectly-formatted source location entry in AST file");
2002 return true;
2003
2004 case SM_SLOC_FILE_ENTRY: {
2005 // We will detect whether a file changed and return 'Failure' for it, but
2006 // we will also try to fail gracefully by setting up the SLocEntry.
2007 unsigned InputID = Record[4];
2008 InputFile IF = getInputFile(F&: *F, ID: InputID);
2009 OptionalFileEntryRef File = IF.getFile();
2010 bool OverriddenBuffer = IF.isOverridden();
2011
2012 // Note that we only check if a File was returned. If it was out-of-date
2013 // we have complained but we will continue creating a FileID to recover
2014 // gracefully.
2015 if (!File)
2016 return true;
2017
2018 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2019 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
2020 // This is the module's main file.
2021 IncludeLoc = getImportLocation(F);
2022 }
2023 SrcMgr::CharacteristicKind
2024 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2025 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
2026 LoadedOffset: BaseOffset + Record[0]);
2027 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2028 FileInfo.NumCreatedFIDs = Record[5];
2029 if (Record[3])
2030 FileInfo.setHasLineDirectives();
2031
2032 unsigned NumFileDecls = Record[7];
2033 if (NumFileDecls && ContextObj) {
2034 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
2035 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
2036 FileDeclIDs[FID] =
2037 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
2038 }
2039
2040 const SrcMgr::ContentCache &ContentCache =
2041 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2042 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2043 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2044 !ContentCache.getBufferIfLoaded()) {
2045 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2046 if (!Buffer)
2047 return true;
2048 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2049 }
2050
2051 break;
2052 }
2053
2054 case SM_SLOC_BUFFER_ENTRY: {
2055 const char *Name = Blob.data();
2056 unsigned Offset = Record[0];
2057 SrcMgr::CharacteristicKind
2058 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2059 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2060 if (IncludeLoc.isInvalid() && F->isModule()) {
2061 IncludeLoc = getImportLocation(F);
2062 }
2063
2064 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2065 if (!Buffer)
2066 return true;
2067 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2068 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2069 if (Record[3]) {
2070 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2071 FileInfo.setHasLineDirectives();
2072 }
2073 break;
2074 }
2075
2076 case SM_SLOC_EXPANSION_ENTRY: {
2077 auto [EntryOffset, Chain] = ReadEntryOffset(Record);
2078 // The chain is stateful: decode in the same order the writer emitted, each
2079 // in its own statement. See CreateSLocExpansionAbbrev for the field order.
2080 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[1], Chain);
2081 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2], Chain);
2082 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[3], Chain);
2083 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2084 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2085 LoadedOffset: BaseOffset + EntryOffset);
2086 break;
2087 }
2088 }
2089
2090 return false;
2091}
2092
2093std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2094 if (ID == 0)
2095 return std::make_pair(x: SourceLocation(), y: "");
2096
2097 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2098 Error(Msg: "source location entry ID out-of-range for AST file");
2099 return std::make_pair(x: SourceLocation(), y: "");
2100 }
2101
2102 // Find which module file this entry lands in.
2103 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2104 if (!M->isModule())
2105 return std::make_pair(x: SourceLocation(), y: "");
2106
2107 // FIXME: Can we map this down to a particular submodule? That would be
2108 // ideal.
2109 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2110}
2111
2112/// Find the location where the module F is imported.
2113SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2114 if (F->ImportLoc.isValid())
2115 return F->ImportLoc;
2116
2117 // Otherwise we have a PCH. It's considered to be "imported" at the first
2118 // location of its includer.
2119 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2120 // Main file is the importer.
2121 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2122 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2123 }
2124 return F->ImportedBy[0]->FirstLoc;
2125}
2126
2127/// Enter a subblock of the specified BlockID with the specified cursor. Read
2128/// the abbreviations that are at the top of the block and then leave the cursor
2129/// pointing into the block.
2130llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2131 unsigned BlockID,
2132 uint64_t *StartOfBlockOffset) {
2133 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2134 return Err;
2135
2136 if (StartOfBlockOffset)
2137 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2138
2139 while (true) {
2140 uint64_t Offset = Cursor.GetCurrentBitNo();
2141 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2142 if (!MaybeCode)
2143 return MaybeCode.takeError();
2144 unsigned Code = MaybeCode.get();
2145
2146 // We expect all abbrevs to be at the start of the block.
2147 if (Code != llvm::bitc::DEFINE_ABBREV) {
2148 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2149 return Err;
2150 return llvm::Error::success();
2151 }
2152 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2153 return Err;
2154 }
2155}
2156
2157Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2158 unsigned &Idx) {
2159 Token Tok;
2160 Tok.startToken();
2161 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2162 Tok.setKind((tok::TokenKind)Record[Idx++]);
2163 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2164
2165 if (Tok.isAnnotation()) {
2166 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2167 switch (Tok.getKind()) {
2168 case tok::annot_pragma_loop_hint: {
2169 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2170 Info->PragmaName = ReadToken(M, Record, Idx);
2171 Info->Option = ReadToken(M, Record, Idx);
2172 unsigned NumTokens = Record[Idx++];
2173 SmallVector<Token, 4> Toks;
2174 Toks.reserve(N: NumTokens);
2175 for (unsigned I = 0; I < NumTokens; ++I)
2176 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2177 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2178 Tok.setAnnotationValue(static_cast<void *>(Info));
2179 break;
2180 }
2181 case tok::annot_pragma_pack: {
2182 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2183 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2184 auto SlotLabel = ReadString(Record, Idx);
2185 Info->SlotLabel =
2186 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2187 Info->Alignment = ReadToken(M, Record, Idx);
2188 Tok.setAnnotationValue(static_cast<void *>(Info));
2189 break;
2190 }
2191 // Some annotation tokens do not use the PtrData field.
2192 case tok::annot_pragma_openmp:
2193 case tok::annot_pragma_openmp_end:
2194 case tok::annot_pragma_unused:
2195 case tok::annot_pragma_openacc:
2196 case tok::annot_pragma_openacc_end:
2197 case tok::annot_repl_input_end:
2198 break;
2199 default:
2200 llvm_unreachable("missing deserialization code for annotation token");
2201 }
2202 } else {
2203 Tok.setLength(Record[Idx++]);
2204 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2205 Tok.setIdentifierInfo(II);
2206 }
2207 return Tok;
2208}
2209
2210MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2211 BitstreamCursor &Stream = F.MacroCursor;
2212
2213 // Keep track of where we are in the stream, then jump back there
2214 // after reading this macro.
2215 SavedStreamPosition SavedPosition(Stream);
2216
2217 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2218 // FIXME this drops errors on the floor.
2219 consumeError(Err: std::move(Err));
2220 return nullptr;
2221 }
2222 RecordData Record;
2223 SmallVector<IdentifierInfo*, 16> MacroParams;
2224 MacroInfo *Macro = nullptr;
2225 llvm::MutableArrayRef<Token> MacroTokens;
2226
2227 while (true) {
2228 // Advance to the next record, but if we get to the end of the block, don't
2229 // pop it (removing all the abbreviations from the cursor) since we want to
2230 // be able to reseek within the block and read entries.
2231 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2232 Expected<llvm::BitstreamEntry> MaybeEntry =
2233 Stream.advanceSkippingSubblocks(Flags);
2234 if (!MaybeEntry) {
2235 Error(Err: MaybeEntry.takeError());
2236 return Macro;
2237 }
2238 llvm::BitstreamEntry Entry = MaybeEntry.get();
2239
2240 switch (Entry.Kind) {
2241 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2242 case llvm::BitstreamEntry::Error:
2243 Error(Msg: "malformed block record in AST file");
2244 return Macro;
2245 case llvm::BitstreamEntry::EndBlock:
2246 return Macro;
2247 case llvm::BitstreamEntry::Record:
2248 // The interesting case.
2249 break;
2250 }
2251
2252 // Read a record.
2253 Record.clear();
2254 PreprocessorRecordTypes RecType;
2255 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2256 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2257 else {
2258 Error(Err: MaybeRecType.takeError());
2259 return Macro;
2260 }
2261 switch (RecType) {
2262 case PP_MODULE_MACRO:
2263 case PP_MACRO_DIRECTIVE_HISTORY:
2264 return Macro;
2265
2266 case PP_MACRO_OBJECT_LIKE:
2267 case PP_MACRO_FUNCTION_LIKE: {
2268 // If we already have a macro, that means that we've hit the end
2269 // of the definition of the macro we were looking for. We're
2270 // done.
2271 if (Macro)
2272 return Macro;
2273
2274 unsigned NextIndex = 1; // Skip identifier ID.
2275 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2276 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2277 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2278 MI->setIsUsed(Record[NextIndex++]);
2279 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2280 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2281 PPAllocator&: PP.getPreprocessorAllocator());
2282 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2283 // Decode function-like macro info.
2284 bool isC99VarArgs = Record[NextIndex++];
2285 bool isGNUVarArgs = Record[NextIndex++];
2286 bool hasCommaPasting = Record[NextIndex++];
2287 MacroParams.clear();
2288 unsigned NumArgs = Record[NextIndex++];
2289 for (unsigned i = 0; i != NumArgs; ++i)
2290 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2291
2292 // Install function-like macro info.
2293 MI->setIsFunctionLike();
2294 if (isC99VarArgs) MI->setIsC99Varargs();
2295 if (isGNUVarArgs) MI->setIsGNUVarargs();
2296 if (hasCommaPasting) MI->setHasCommaPasting();
2297 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2298 }
2299
2300 // Remember that we saw this macro last so that we add the tokens that
2301 // form its body to it.
2302 Macro = MI;
2303
2304 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2305 Record[NextIndex]) {
2306 // We have a macro definition. Register the association
2307 PreprocessedEntityID
2308 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2309 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2310 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2311 PreprocessingRecord::PPEntityID PPID =
2312 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2313 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2314 Val: PPRec.getPreprocessedEntity(PPID));
2315 if (PPDef)
2316 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2317 }
2318
2319 ++NumMacrosRead;
2320 break;
2321 }
2322
2323 case PP_TOKEN: {
2324 // If we see a TOKEN before a PP_MACRO_*, then the file is
2325 // erroneous, just pretend we didn't see this.
2326 if (!Macro) break;
2327 if (MacroTokens.empty()) {
2328 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2329 return Macro;
2330 }
2331
2332 unsigned Idx = 0;
2333 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2334 MacroTokens = MacroTokens.drop_front();
2335 break;
2336 }
2337 }
2338 }
2339}
2340
2341PreprocessedEntityID
2342ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2343 PreprocessedEntityID LocalID) const {
2344 if (!M.ModuleOffsetMap.empty())
2345 ReadModuleOffsetMap(F&: M);
2346
2347 unsigned ModuleFileIndex = LocalID >> 32;
2348 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2349 ModuleFile *MF =
2350 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2351 assert(MF && "malformed identifier ID encoding?");
2352
2353 if (!ModuleFileIndex) {
2354 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2355 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2356 }
2357
2358 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2359}
2360
2361OptionalFileEntryRef
2362HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2363 FileManager &FileMgr = Reader.getFileManager();
2364 if (!Key.Imported)
2365 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2366
2367 auto Resolved =
2368 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2369 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2370}
2371
2372unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2373 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2374 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2375 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2376 return llvm::xxh3_64bits(data: buf);
2377}
2378
2379HeaderFileInfoTrait::internal_key_type
2380HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2381 internal_key_type ikey = {.Size: ekey.getSize(),
2382 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2383 .Filename: ekey.getName(), /*Imported*/ false};
2384 return ikey;
2385}
2386
2387bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2388 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2389 return false;
2390
2391 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2392 return true;
2393
2394 // Determine whether the actual files are equivalent.
2395 OptionalFileEntryRef FEA = getFile(Key: a);
2396 OptionalFileEntryRef FEB = getFile(Key: b);
2397 return FEA && FEA == FEB;
2398}
2399
2400std::pair<unsigned, unsigned>
2401HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2402 return readULEBKeyDataLength(P&: d);
2403}
2404
2405HeaderFileInfoTrait::internal_key_type
2406HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2407 using namespace llvm::support;
2408
2409 internal_key_type ikey;
2410 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2411 ikey.ModTime =
2412 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2413 ikey.Filename = (const char *)d;
2414 ikey.Imported = true;
2415 return ikey;
2416}
2417
2418HeaderFileInfoTrait::data_type
2419HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2420 unsigned DataLen) {
2421 using namespace llvm::support;
2422
2423 const unsigned char *End = d + DataLen;
2424 HeaderFileInfo HFI;
2425 unsigned Flags = *d++;
2426
2427 OptionalFileEntryRef FE;
2428 bool Included = (Flags >> 6) & 0x01;
2429 if (Included)
2430 if ((FE = getFile(Key: key)))
2431 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2432 // deserialize this header file info again.
2433 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2434
2435 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2436 HFI.isImport |= (Flags >> 5) & 0x01;
2437 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2438 HFI.DirInfo = (Flags >> 1) & 0x07;
2439 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2440 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2441
2442 assert((End - d) % 4 == 0 &&
2443 "Wrong data length in HeaderFileInfo deserialization");
2444 while (d != End) {
2445 uint32_t LocalSMID =
2446 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2447 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2448 LocalSMID >>= 3;
2449
2450 // This header is part of a module. Associate it with the module to enable
2451 // implicit module import.
2452 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2453 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2454 ModuleMap &ModMap =
2455 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2456
2457 if (FE || (FE = getFile(Key: key))) {
2458 // FIXME: NameAsWritten
2459 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2460 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2461 }
2462 HFI.mergeModuleMembership(Role: HeaderRole);
2463 }
2464
2465 // This HeaderFileInfo was externally loaded.
2466 HFI.External = true;
2467 HFI.IsValid = true;
2468 return HFI;
2469}
2470
2471void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2472 uint32_t MacroDirectivesOffset) {
2473 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2474 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2475}
2476
2477void ASTReader::ReadDefinedMacros() {
2478 // Note that we are loading defined macros.
2479 Deserializing Macros(this);
2480
2481 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2482 BitstreamCursor &MacroCursor = I.MacroCursor;
2483
2484 // If there was no preprocessor block, skip this file.
2485 if (MacroCursor.getBitcodeBytes().empty())
2486 continue;
2487
2488 BitstreamCursor Cursor = MacroCursor;
2489 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2490 Error(Err: std::move(Err));
2491 return;
2492 }
2493
2494 RecordData Record;
2495 while (true) {
2496 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2497 if (!MaybeE) {
2498 Error(Err: MaybeE.takeError());
2499 return;
2500 }
2501 llvm::BitstreamEntry E = MaybeE.get();
2502
2503 switch (E.Kind) {
2504 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2505 case llvm::BitstreamEntry::Error:
2506 Error(Msg: "malformed block record in AST file");
2507 return;
2508 case llvm::BitstreamEntry::EndBlock:
2509 goto NextCursor;
2510
2511 case llvm::BitstreamEntry::Record: {
2512 Record.clear();
2513 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2514 if (!MaybeRecord) {
2515 Error(Err: MaybeRecord.takeError());
2516 return;
2517 }
2518 switch (MaybeRecord.get()) {
2519 default: // Default behavior: ignore.
2520 break;
2521
2522 case PP_MACRO_OBJECT_LIKE:
2523 case PP_MACRO_FUNCTION_LIKE: {
2524 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2525 if (II->isOutOfDate())
2526 updateOutOfDateIdentifier(II: *II);
2527 break;
2528 }
2529
2530 case PP_TOKEN:
2531 // Ignore tokens.
2532 break;
2533 }
2534 break;
2535 }
2536 }
2537 }
2538 NextCursor: ;
2539 }
2540}
2541
2542namespace {
2543
2544 /// Visitor class used to look up identifirs in an AST file.
2545 class IdentifierLookupVisitor {
2546 StringRef Name;
2547 unsigned NameHash;
2548 unsigned PriorGeneration;
2549 unsigned &NumIdentifierLookups;
2550 unsigned &NumIdentifierLookupHits;
2551 IdentifierInfo *Found = nullptr;
2552
2553 public:
2554 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2555 unsigned &NumIdentifierLookups,
2556 unsigned &NumIdentifierLookupHits)
2557 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2558 PriorGeneration(PriorGeneration),
2559 NumIdentifierLookups(NumIdentifierLookups),
2560 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2561
2562 bool operator()(ModuleFile &M) {
2563 // If we've already searched this module file, skip it now.
2564 if (M.Generation <= PriorGeneration)
2565 return true;
2566
2567 ASTIdentifierLookupTable *IdTable
2568 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2569 if (!IdTable)
2570 return false;
2571
2572 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2573 Found);
2574 ++NumIdentifierLookups;
2575 ASTIdentifierLookupTable::iterator Pos =
2576 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2577 if (Pos == IdTable->end())
2578 return false;
2579
2580 // Dereferencing the iterator has the effect of building the
2581 // IdentifierInfo node and populating it with the various
2582 // declarations it needs.
2583 ++NumIdentifierLookupHits;
2584 Found = *Pos;
2585 if (Trait.hasMoreInformationInDependencies()) {
2586 // Look for the identifier in extra modules as they contain more info.
2587 return false;
2588 }
2589 return true;
2590 }
2591
2592 // Retrieve the identifier info found within the module
2593 // files.
2594 IdentifierInfo *getIdentifierInfo() const { return Found; }
2595 };
2596
2597} // namespace
2598
2599void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2600 // Note that we are loading an identifier.
2601 Deserializing AnIdentifier(this);
2602
2603 unsigned PriorGeneration = 0;
2604 if (getContext().getLangOpts().Modules)
2605 PriorGeneration = IdentifierGeneration[&II];
2606
2607 // If there is a global index, look there first to determine which modules
2608 // provably do not have any results for this identifier.
2609 GlobalModuleIndex::HitSet Hits;
2610 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2611 if (!loadGlobalIndex()) {
2612 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2613 HitsPtr = &Hits;
2614 }
2615 }
2616
2617 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2618 NumIdentifierLookups,
2619 NumIdentifierLookupHits);
2620 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2621 markIdentifierUpToDate(II: &II);
2622}
2623
2624void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2625 if (!II)
2626 return;
2627
2628 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2629
2630 // Update the generation for this identifier.
2631 if (getContext().getLangOpts().Modules)
2632 IdentifierGeneration[II] = getGeneration();
2633}
2634
2635MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2636 unsigned &Idx) {
2637 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2638 uint64_t LocalIndex = Record[Idx++];
2639 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2640}
2641
2642void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2643 const PendingMacroInfo &PMInfo) {
2644 ModuleFile &M = *PMInfo.M;
2645
2646 BitstreamCursor &Cursor = M.MacroCursor;
2647 SavedStreamPosition SavedPosition(Cursor);
2648 if (llvm::Error Err =
2649 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2650 Error(Err: std::move(Err));
2651 return;
2652 }
2653
2654 struct ModuleMacroRecord {
2655 SubmoduleID SubModID;
2656 MacroInfo *MI;
2657 SmallVector<SubmoduleID, 8> Overrides;
2658 };
2659 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2660
2661 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2662 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2663 // macro histroy.
2664 RecordData Record;
2665 while (true) {
2666 Expected<llvm::BitstreamEntry> MaybeEntry =
2667 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2668 if (!MaybeEntry) {
2669 Error(Err: MaybeEntry.takeError());
2670 return;
2671 }
2672 llvm::BitstreamEntry Entry = MaybeEntry.get();
2673
2674 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2675 Error(Msg: "malformed block record in AST file");
2676 return;
2677 }
2678
2679 Record.clear();
2680 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2681 if (!MaybePP) {
2682 Error(Err: MaybePP.takeError());
2683 return;
2684 }
2685 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2686 case PP_MACRO_DIRECTIVE_HISTORY:
2687 break;
2688
2689 case PP_MODULE_MACRO: {
2690 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2691 auto &Info = ModuleMacros.back();
2692 unsigned Idx = 0;
2693 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2694 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2695 for (int I = Idx, N = Record.size(); I != N; ++I)
2696 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2697 continue;
2698 }
2699
2700 default:
2701 Error(Msg: "malformed block record in AST file");
2702 return;
2703 }
2704
2705 // We found the macro directive history; that's the last record
2706 // for this macro.
2707 break;
2708 }
2709
2710 // Module macros are listed in reverse dependency order.
2711 {
2712 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2713 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2714 for (auto &MMR : ModuleMacros) {
2715 Overrides.clear();
2716 for (unsigned ModID : MMR.Overrides) {
2717 Module *Mod = getSubmodule(GlobalID: ModID);
2718 auto *Macro = PP.getModuleMacro(Mod, II);
2719 assert(Macro && "missing definition for overridden macro");
2720 Overrides.push_back(Elt: Macro);
2721 }
2722
2723 bool Inserted = false;
2724 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2725 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2726 }
2727 }
2728
2729 // Don't read the directive history for a module; we don't have anywhere
2730 // to put it.
2731 if (M.isModule())
2732 return;
2733
2734 // Deserialize the macro directives history in reverse source-order.
2735 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2736 unsigned Idx = 0, N = Record.size();
2737 while (Idx < N) {
2738 MacroDirective *MD = nullptr;
2739 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2740 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2741 switch (K) {
2742 case MacroDirective::MD_Define: {
2743 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2744 MD = PP.AllocateDefMacroDirective(MI, Loc);
2745 break;
2746 }
2747 case MacroDirective::MD_Undefine:
2748 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2749 break;
2750 case MacroDirective::MD_Visibility:
2751 bool isPublic = Record[Idx++];
2752 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2753 break;
2754 }
2755
2756 if (!Latest)
2757 Latest = MD;
2758 if (Earliest)
2759 Earliest->setPrevious(MD);
2760 Earliest = MD;
2761 }
2762
2763 if (Latest)
2764 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2765}
2766
2767bool ASTReader::shouldDisableValidationForFile(
2768 const serialization::ModuleFile &M) const {
2769 if (DisableValidationKind == DisableValidationForModuleKind::None)
2770 return false;
2771
2772 // If a PCH is loaded and validation is disabled for PCH then disable
2773 // validation for the PCH and the modules it loads.
2774 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2775
2776 switch (K) {
2777 case MK_MainFile:
2778 case MK_Preamble:
2779 case MK_PCH:
2780 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2781 case MK_ImplicitModule:
2782 case MK_ExplicitModule:
2783 case MK_PrebuiltModule:
2784 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2785 }
2786
2787 return false;
2788}
2789
2790static std::pair<StringRef, StringRef>
2791getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2792 const StringRef InputBlob) {
2793 uint16_t AsRequestedLength = Record[7];
2794 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2795 InputBlob.substr(Start: AsRequestedLength)};
2796}
2797
2798InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2799 // If this ID is bogus, just return an empty input file.
2800 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2801 return InputFileInfo();
2802
2803 // If we've already loaded this input file, return it.
2804 if (F.InputFileInfosLoaded[ID - 1].isValid())
2805 return F.InputFileInfosLoaded[ID - 1];
2806
2807 // Go find this input file.
2808 BitstreamCursor &Cursor = F.InputFilesCursor;
2809 SavedStreamPosition SavedPosition(Cursor);
2810 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2811 F.InputFileOffsets[ID - 1])) {
2812 // FIXME this drops errors on the floor.
2813 consumeError(Err: std::move(Err));
2814 }
2815
2816 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2817 if (!MaybeCode) {
2818 // FIXME this drops errors on the floor.
2819 consumeError(Err: MaybeCode.takeError());
2820 }
2821 unsigned Code = MaybeCode.get();
2822 RecordData Record;
2823 StringRef Blob;
2824
2825 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2826 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2827 "invalid record type for input file");
2828 else {
2829 // FIXME this drops errors on the floor.
2830 consumeError(Err: Maybe.takeError());
2831 }
2832
2833 assert(Record[0] == ID && "Bogus stored ID or offset");
2834 InputFileInfo R;
2835 R.StoredSize = static_cast<off_t>(Record[1]);
2836 R.StoredTime = static_cast<time_t>(Record[2]);
2837 R.Overridden = static_cast<bool>(Record[3]);
2838 R.Transient = static_cast<bool>(Record[4]);
2839 R.TopLevel = static_cast<bool>(Record[5]);
2840 R.ModuleMap = static_cast<bool>(Record[6]);
2841 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2842 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2843 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2844 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2845 ? UnresolvedFilenameAsRequested
2846 : UnresolvedFilename;
2847
2848 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2849 if (!MaybeEntry) // FIXME this drops errors on the floor.
2850 consumeError(Err: MaybeEntry.takeError());
2851 llvm::BitstreamEntry Entry = MaybeEntry.get();
2852 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2853 "expected record type for input file hash");
2854
2855 Record.clear();
2856 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2857 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2858 "invalid record type for input file hash");
2859 else {
2860 // FIXME this drops errors on the floor.
2861 consumeError(Err: Maybe.takeError());
2862 }
2863 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2864 static_cast<uint64_t>(Record[0]);
2865
2866 // Note that we've loaded this input file info.
2867 F.InputFileInfosLoaded[ID - 1] = R;
2868 return R;
2869}
2870
2871static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2872InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2873 // If this ID is bogus, just return an empty input file.
2874 if (ID == 0 || ID > F.InputFilesLoaded.size())
2875 return InputFile();
2876
2877 // If we've already loaded this input file, return it.
2878 if (F.InputFilesLoaded[ID-1].getFile())
2879 return F.InputFilesLoaded[ID-1];
2880
2881 if (F.InputFilesLoaded[ID-1].isNotFound())
2882 return InputFile();
2883
2884 // Go find this input file.
2885 BitstreamCursor &Cursor = F.InputFilesCursor;
2886 SavedStreamPosition SavedPosition(Cursor);
2887 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2888 F.InputFileOffsets[ID - 1])) {
2889 // FIXME this drops errors on the floor.
2890 consumeError(Err: std::move(Err));
2891 }
2892
2893 InputFileInfo FI = getInputFileInfo(F, ID);
2894 off_t StoredSize = FI.StoredSize;
2895 time_t StoredTime = FI.StoredTime;
2896 bool Overridden = FI.Overridden;
2897 bool Transient = FI.Transient;
2898 auto Filename =
2899 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2900 uint64_t StoredContentHash = FI.ContentHash;
2901
2902 // For standard C++ modules, we don't need to check the inputs.
2903 bool SkipChecks = F.StandardCXXModule;
2904
2905 const HeaderSearchOptions &HSOpts =
2906 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2907
2908 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2909 // modules.
2910 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2911 SkipChecks = false;
2912 Overridden = false;
2913 }
2914
2915 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2916
2917 // For an overridden file, create a virtual file with the stored
2918 // size/timestamp.
2919 if ((Overridden || Transient || SkipChecks) && !File)
2920 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2921
2922 if (!File) {
2923 if (Complain) {
2924 std::string ErrorStr = "could not find file '";
2925 ErrorStr += *Filename;
2926 ErrorStr += "' referenced by AST file '";
2927 ErrorStr += F.FileName.str();
2928 ErrorStr += "'";
2929 Error(Msg: ErrorStr);
2930 }
2931 // Record that we didn't find the file.
2932 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2933 return InputFile();
2934 }
2935
2936 // Check if there was a request to override the contents of the file
2937 // that was part of the precompiled header. Overriding such a file
2938 // can lead to problems when lexing using the source locations from the
2939 // PCH.
2940 SourceManager &SM = getSourceManager();
2941 // FIXME: Reject if the overrides are different.
2942 if ((!Overridden && !Transient) && !SkipChecks &&
2943 SM.isFileOverridden(File: *File)) {
2944 if (Complain)
2945 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2946
2947 // After emitting the diagnostic, bypass the overriding file to recover
2948 // (this creates a separate FileEntry).
2949 File = SM.bypassFileContentsOverride(File: *File);
2950 if (!File) {
2951 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2952 return InputFile();
2953 }
2954 }
2955
2956 auto HasInputContentChanged = [&](Change OriginalChange) {
2957 assert(ValidateASTInputFilesContent &&
2958 "We should only check the content of the inputs with "
2959 "ValidateASTInputFilesContent enabled.");
2960
2961 if (StoredContentHash == 0)
2962 return OriginalChange;
2963
2964 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2965 if (!MemBuffOrError) {
2966 if (!Complain)
2967 return OriginalChange;
2968 std::string ErrorStr = "could not get buffer for file '";
2969 ErrorStr += File->getName();
2970 ErrorStr += "'";
2971 Error(Msg: ErrorStr);
2972 return OriginalChange;
2973 }
2974
2975 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2976 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2977 return Change{.Kind: Change::None};
2978
2979 return Change{.Kind: Change::Content};
2980 };
2981 auto HasInputFileChanged = [&]() {
2982 if (StoredSize != File->getSize())
2983 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2984 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2985 StoredTime != File->getModificationTime()) {
2986 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2987 .New: File->getModificationTime()};
2988
2989 // In case the modification time changes but not the content,
2990 // accept the cached file as legit.
2991 if (ValidateASTInputFilesContent)
2992 return HasInputContentChanged(MTimeChange);
2993
2994 return MTimeChange;
2995 }
2996 return Change{.Kind: Change::None};
2997 };
2998
2999 bool IsOutOfDate = false;
3000 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
3001 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
3002 // enabled, it is better to check the contents of the inputs. Since we can't
3003 // get correct modified time information for inputs from overriden inputs.
3004 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
3005 F.StandardCXXModule && FileChange.Kind == Change::None)
3006 FileChange = HasInputContentChanged(FileChange);
3007
3008 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
3009 // it is better to check the content of the input files because we cannot rely
3010 // on the file modification time, which will be the same (zero) for these
3011 // files.
3012 if (!StoredTime && ValidateASTInputFilesContent &&
3013 FileChange.Kind == Change::None)
3014 FileChange = HasInputContentChanged(FileChange);
3015
3016 // For an overridden file, there is nothing to validate.
3017 if (!Overridden && FileChange.Kind != Change::None) {
3018 if (Complain) {
3019 // Build a list of the PCH imports that got us here (in reverse).
3020 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
3021 while (!ImportStack.back()->ImportedBy.empty())
3022 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
3023
3024 // The top-level AST file is stale.
3025 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
3026 Diag(DiagID: diag::err_fe_ast_file_modified)
3027 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
3028 << TopLevelASTFileName;
3029 Diag(DiagID: diag::note_fe_ast_file_modified)
3030 << FileChange.Kind << (FileChange.Old && FileChange.New)
3031 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
3032 << llvm::itostr(X: FileChange.New.value_or(u: 0));
3033 if (getModuleManager()
3034 .getModuleCache()
3035 .getInMemoryModuleCache()
3036 .isPCMFinal(Filename: F.FileName))
3037 Diag(DiagID: diag::note_fe_ast_file_modified_finalized) << F.ModuleName;
3038
3039 // Print the import stack.
3040 if (ImportStack.size() > 1) {
3041 Diag(DiagID: diag::note_ast_file_required_by)
3042 << *Filename << ImportStack[0]->FileName;
3043 for (unsigned I = 1; I < ImportStack.size(); ++I)
3044 Diag(DiagID: diag::note_ast_file_required_by)
3045 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3046 }
3047
3048 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3049 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3050 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3051 << F.InputFilesValidationStatus;
3052 }
3053
3054 IsOutOfDate = true;
3055 }
3056 // FIXME: If the file is overridden and we've already opened it,
3057 // issue an error (or split it into a separate FileEntry).
3058
3059 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3060
3061 // Note that we've loaded this input file.
3062 F.InputFilesLoaded[ID-1] = IF;
3063 return IF;
3064}
3065
3066ASTReader::TemporarilyOwnedStringRef
3067ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3068 ModuleFile &ModF) {
3069 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3070}
3071
3072ASTReader::TemporarilyOwnedStringRef
3073ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3074 StringRef Prefix) {
3075 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3076
3077 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3078 Path == "<built-in>" || Path == "<command line>")
3079 return {Path, Buf};
3080
3081 // The writer makes the base directory itself relative as ".".
3082 if (Path == ".")
3083 return {Prefix, Buf};
3084
3085 Buf.clear();
3086 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3087 StringRef ResolvedPath{Buf.data(), Buf.size()};
3088 return {ResolvedPath, Buf};
3089}
3090
3091std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3092 StringRef P,
3093 ModuleFile &ModF) {
3094 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3095}
3096
3097std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3098 StringRef P,
3099 StringRef Prefix) {
3100 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3101 return ResolvedPath->str();
3102}
3103
3104static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3105 switch (ARR) {
3106 case ASTReader::Failure: return true;
3107 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3108 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3109 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3110 case ASTReader::ConfigurationMismatch:
3111 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3112 case ASTReader::HadErrors: return true;
3113 case ASTReader::Success: return false;
3114 }
3115
3116 llvm_unreachable("unknown ASTReadResult");
3117}
3118
3119ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3120 BitstreamCursor &Stream, StringRef Filename,
3121 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3122 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3123 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3124 // FIXME this drops errors on the floor.
3125 consumeError(Err: std::move(Err));
3126 return Failure;
3127 }
3128
3129 // Read all of the records in the options block.
3130 RecordData Record;
3131 ASTReadResult Result = Success;
3132 while (true) {
3133 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3134 if (!MaybeEntry) {
3135 // FIXME this drops errors on the floor.
3136 consumeError(Err: MaybeEntry.takeError());
3137 return Failure;
3138 }
3139 llvm::BitstreamEntry Entry = MaybeEntry.get();
3140
3141 switch (Entry.Kind) {
3142 case llvm::BitstreamEntry::Error:
3143 case llvm::BitstreamEntry::SubBlock:
3144 return Failure;
3145
3146 case llvm::BitstreamEntry::EndBlock:
3147 return Result;
3148
3149 case llvm::BitstreamEntry::Record:
3150 // The interesting case.
3151 break;
3152 }
3153
3154 // Read and process a record.
3155 Record.clear();
3156 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3157 if (!MaybeRecordType) {
3158 // FIXME this drops errors on the floor.
3159 consumeError(Err: MaybeRecordType.takeError());
3160 return Failure;
3161 }
3162 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3163 case LANGUAGE_OPTIONS: {
3164 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3165 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3166 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3167 Result = ConfigurationMismatch;
3168 break;
3169 }
3170
3171 case CODEGEN_OPTIONS: {
3172 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3173 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3174 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3175 Result = ConfigurationMismatch;
3176 break;
3177 }
3178
3179 case TARGET_OPTIONS: {
3180 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3181 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3182 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3183 Result = ConfigurationMismatch;
3184 break;
3185 }
3186
3187 case FILE_SYSTEM_OPTIONS: {
3188 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3189 if (!AllowCompatibleConfigurationMismatch &&
3190 ParseFileSystemOptions(Record, Complain, Listener))
3191 Result = ConfigurationMismatch;
3192 break;
3193 }
3194
3195 case HEADER_SEARCH_OPTIONS: {
3196 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3197 if (!AllowCompatibleConfigurationMismatch &&
3198 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3199 Result = ConfigurationMismatch;
3200 break;
3201 }
3202
3203 case PREPROCESSOR_OPTIONS:
3204 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3205 if (!AllowCompatibleConfigurationMismatch &&
3206 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3207 SuggestedPredefines))
3208 Result = ConfigurationMismatch;
3209 break;
3210 }
3211 }
3212}
3213
3214/// Returns {build-session validation applies, MF was validated this session}.
3215static std::pair<bool, bool>
3216wasValidatedInBuildSession(const ModuleFile &MF,
3217 const HeaderSearchOptions &HSOpts) {
3218 const bool EnablesBSValidation =
3219 HSOpts.ModulesValidateOncePerBuildSession && MF.Kind == MK_ImplicitModule;
3220 const bool WasValidated =
3221 EnablesBSValidation &&
3222 MF.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp;
3223 return {EnablesBSValidation, WasValidated};
3224}
3225
3226ASTReader::RelocationResult
3227ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3228 // Don't emit module relocation errors if we have -fno-validate-pch.
3229 const bool IgnoreError =
3230 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3231 DisableValidationForModuleKind::Module);
3232
3233 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3234 return {std::nullopt, IgnoreError};
3235
3236 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3237
3238 if (!DirectoryCheck &&
3239 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3240 return {std::nullopt, IgnoreError};
3241
3242 const HeaderSearchOptions &HSOpts =
3243 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3244
3245 // When only validating modules once per build session,
3246 // Skip check if the timestamp is up to date or module was built in same build
3247 // session.
3248 auto [EnablesBSValidation, WasValidated] =
3249 wasValidatedInBuildSession(MF: F, HSOpts);
3250 const bool SkipModuleLookup =
3251 !PP.getPreprocessorOpts().ModulesForceRedundantLookup &&
3252 (WasValidated ||
3253 (EnablesBSValidation &&
3254 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp));
3255
3256 if (SkipModuleLookup)
3257 return {std::nullopt, IgnoreError};
3258
3259 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3260
3261 // If we've already loaded a module map file covering this module, we may
3262 // have a better path for it (relative to the current build if doing directory
3263 // check).
3264 Module *M = PP.getHeaderSearchInfo().lookupModule(
3265 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3266 /*AllowSearch=*/true,
3267 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3268
3269 return {M, IgnoreError};
3270}
3271
3272ASTReader::ASTReadResult
3273ASTReader::ReadControlBlock(ModuleFile &F,
3274 SmallVectorImpl<ImportedModule> &Loaded,
3275 const ModuleFile *ImportedBy,
3276 unsigned ClientLoadCapabilities) {
3277 BitstreamCursor &Stream = F.Stream;
3278
3279 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3280 Error(Err: std::move(Err));
3281 return Failure;
3282 }
3283
3284 // Lambda to read the unhashed control block the first time it's called.
3285 //
3286 // For PCM files, the unhashed control block cannot be read until after the
3287 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3288 // need to look ahead before reading the IMPORTS record. For consistency,
3289 // this block is always read somehow (see BitstreamEntry::EndBlock).
3290 bool HasReadUnhashedControlBlock = false;
3291 auto readUnhashedControlBlockOnce = [&]() {
3292 if (!HasReadUnhashedControlBlock) {
3293 HasReadUnhashedControlBlock = true;
3294 if (ASTReadResult Result =
3295 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3296 return Result;
3297 }
3298 return Success;
3299 };
3300
3301 bool DisableValidation = shouldDisableValidationForFile(M: F);
3302
3303 // Read all of the records and blocks in the control block.
3304 RecordData Record;
3305 unsigned NumInputs = 0;
3306 unsigned NumUserInputs = 0;
3307 StringRef BaseDirectoryAsWritten;
3308 while (true) {
3309 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3310 if (!MaybeEntry) {
3311 Error(Err: MaybeEntry.takeError());
3312 return Failure;
3313 }
3314 llvm::BitstreamEntry Entry = MaybeEntry.get();
3315
3316 switch (Entry.Kind) {
3317 case llvm::BitstreamEntry::Error:
3318 Error(Msg: "malformed block record in AST file");
3319 return Failure;
3320 case llvm::BitstreamEntry::EndBlock: {
3321 // Validate the module before returning. This call catches an AST with
3322 // no module name and no imports.
3323 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3324 return Result;
3325
3326 // Validate input files.
3327 const HeaderSearchOptions &HSOpts =
3328 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3329
3330 // All user input files reside at the index range [0, NumUserInputs), and
3331 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3332 // loaded module files, ignore missing inputs.
3333 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3334 F.Kind != MK_PrebuiltModule) {
3335 bool Complain =
3336 !canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities);
3337
3338 // If we are reading a module, we will create a verification timestamp,
3339 // so we verify all input files. Otherwise, verify only user input
3340 // files.
3341
3342 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3343 F.InputFilesValidationStatus = ValidateSystemInputs
3344 ? InputFilesValidation::AllFiles
3345 : InputFilesValidation::UserFiles;
3346 auto [_, WasValidated] = wasValidatedInBuildSession(MF: F, HSOpts);
3347 if (WasValidated) {
3348 N = ForceValidateUserInputs ? NumUserInputs : 0;
3349 F.InputFilesValidationStatus =
3350 ForceValidateUserInputs
3351 ? InputFilesValidation::UserFiles
3352 : InputFilesValidation::SkippedInBuildSession;
3353 }
3354
3355 if (N != 0)
3356 Diag(DiagID: diag::remark_module_validation)
3357 << N << F.ModuleName << F.FileName;
3358
3359 for (unsigned I = 0; I < N; ++I) {
3360 InputFile IF = getInputFile(F, ID: I+1, Complain);
3361 if (!IF.getFile() || IF.isOutOfDate())
3362 return OutOfDate;
3363 }
3364
3365 // A header added to a directory the module enumerated was never
3366 // recorded as an input file, so the check above misses it.
3367 if (!WasValidated && F.Kind == MK_ImplicitModule &&
3368 HSOpts.ModulesValidateDirectoryDependencies &&
3369 isDirectoryDependencyOutOfDate(F, Complain))
3370 return OutOfDate;
3371 } else {
3372 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3373 }
3374
3375 if (Listener)
3376 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind, DirectlyImported: F.isDirectlyImported());
3377
3378 if (Listener && Listener->needsInputFileVisitation()) {
3379 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3380 : NumUserInputs;
3381 for (unsigned I = 0; I < N; ++I) {
3382 bool IsSystem = I >= NumUserInputs;
3383 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3384 auto FilenameAsRequested = ResolveImportedPath(
3385 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3386 Listener->visitInputFile(
3387 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3388 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3389 }
3390 }
3391
3392 return Success;
3393 }
3394
3395 case llvm::BitstreamEntry::SubBlock:
3396 switch (Entry.ID) {
3397 case INPUT_FILES_BLOCK_ID:
3398 F.InputFilesCursor = Stream;
3399 if (llvm::Error Err = Stream.SkipBlock()) {
3400 Error(Err: std::move(Err));
3401 return Failure;
3402 }
3403 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3404 Error(Msg: "malformed block record in AST file");
3405 return Failure;
3406 }
3407 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3408 continue;
3409
3410 case OPTIONS_BLOCK_ID:
3411 // If we're reading the first module for this group, check its options
3412 // are compatible with ours. For modules it imports, no further checking
3413 // is required, because we checked them when we built it.
3414 if (Listener && !ImportedBy) {
3415 // Should we allow the configuration of the module file to differ from
3416 // the configuration of the current translation unit in a compatible
3417 // way?
3418 //
3419 // FIXME: Allow this for files explicitly specified with -include-pch.
3420 bool AllowCompatibleConfigurationMismatch =
3421 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3422
3423 ASTReadResult Result =
3424 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3425 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3426 SuggestedPredefines);
3427 if (Result == Failure) {
3428 Error(Msg: "malformed block record in AST file");
3429 return Result;
3430 }
3431
3432 if (DisableValidation ||
3433 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3434 Result = Success;
3435
3436 // If we can't load the module, exit early since we likely
3437 // will rebuild the module anyway. The stream may be in the
3438 // middle of a block.
3439 if (Result != Success)
3440 return Result;
3441 } else if (llvm::Error Err = Stream.SkipBlock()) {
3442 Error(Err: std::move(Err));
3443 return Failure;
3444 }
3445 continue;
3446
3447 default:
3448 if (llvm::Error Err = Stream.SkipBlock()) {
3449 Error(Err: std::move(Err));
3450 return Failure;
3451 }
3452 continue;
3453 }
3454
3455 case llvm::BitstreamEntry::Record:
3456 // The interesting case.
3457 break;
3458 }
3459
3460 // Read and process a record.
3461 Record.clear();
3462 StringRef Blob;
3463 Expected<unsigned> MaybeRecordType =
3464 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3465 if (!MaybeRecordType) {
3466 Error(Err: MaybeRecordType.takeError());
3467 return Failure;
3468 }
3469 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3470 case METADATA: {
3471 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3472 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3473 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3474 : diag::err_ast_file_version_too_new)
3475 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3476 return VersionMismatch;
3477 }
3478
3479 bool hasErrors = Record[7];
3480 if (hasErrors && !DisableValidation) {
3481 // If requested by the caller and the module hasn't already been read
3482 // or compiled, mark modules on error as out-of-date.
3483 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3484 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3485 return OutOfDate;
3486
3487 if (!AllowASTWithCompilerErrors) {
3488 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3489 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3490 return HadErrors;
3491 }
3492 }
3493 if (hasErrors) {
3494 Diags.ErrorOccurred = true;
3495 Diags.UncompilableErrorOccurred = true;
3496 Diags.UnrecoverableErrorOccurred = true;
3497 }
3498
3499 F.RelocatablePCH = Record[4];
3500 // Relative paths in a relocatable PCH are relative to our sysroot.
3501 if (F.RelocatablePCH)
3502 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3503
3504 F.StandardCXXModule = Record[5];
3505
3506 F.HasTimestamps = Record[6];
3507
3508 const std::string &CurBranch = getClangFullRepositoryVersion();
3509 StringRef ASTBranch = Blob;
3510 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3511 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3512 Diag(DiagID: diag::err_ast_file_different_branch)
3513 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3514 << CurBranch;
3515 return VersionMismatch;
3516 }
3517 break;
3518 }
3519
3520 case IMPORT: {
3521 // Validate the AST before processing any imports (otherwise, untangling
3522 // them can be error-prone and expensive). A module will have a name and
3523 // will already have been validated, but this catches the PCH case.
3524 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3525 return Result;
3526
3527 unsigned Idx = 0;
3528 // Read information about the AST file.
3529 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3530
3531 // The import location will be the local one for now; we will adjust
3532 // all import locations of module imports after the global source
3533 // location info are setup, in ReadAST.
3534 auto [ImportLoc, ImportModuleFileIndex] =
3535 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3536 // The import location must belong to the current module file itself.
3537 assert(ImportModuleFileIndex == 0);
3538
3539 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3540
3541 bool IsImportingStdCXXModule = Record[Idx++];
3542
3543 off_t StoredSize = 0;
3544 time_t StoredModTime = 0;
3545 unsigned FileNameKind = 0;
3546 ASTFileSignature StoredSignature;
3547 ModuleFileName ImportedFile;
3548 std::string StoredFile;
3549 bool IgnoreImportedByNote = false;
3550
3551 // For prebuilt and explicit modules first consult the file map for
3552 // an override. Note that here we don't search prebuilt module
3553 // directories if we're not importing standard c++ module, only the
3554 // explicit name to file mappings. Also, we will still verify the
3555 // size/signature making sure it is essentially the same file but
3556 // perhaps in a different location.
3557 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3558 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3559 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3560
3561 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3562 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3563 return Missing;
3564 }
3565
3566 if (!IsImportingStdCXXModule) {
3567 StoredSize = (off_t)Record[Idx++];
3568 StoredModTime = (time_t)Record[Idx++];
3569 FileNameKind = (unsigned)Record[Idx++];
3570
3571 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3572 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3573 Last: SignatureBytes.end());
3574 Blob = Blob.substr(Start: ASTFileSignature::size);
3575
3576 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3577 if (ImportedFile.empty()) {
3578 ImportedFile = ModuleFileName::makeFromRaw(Name: StoredFile, RawKind: FileNameKind);
3579 } else if (!getDiags().isIgnored(
3580 DiagID: diag::warn_module_file_mapping_mismatch,
3581 Loc: CurrentImportLoc)) {
3582 auto ImportedFileRef =
3583 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3584 auto StoredFileRef =
3585 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3586 if ((ImportedFileRef && StoredFileRef) &&
3587 (*ImportedFileRef != *StoredFileRef)) {
3588 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3589 << ImportedFile << StoredFile;
3590 Diag(DiagID: diag::note_module_file_imported_by)
3591 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3592 IgnoreImportedByNote = true;
3593 }
3594 }
3595 }
3596
3597 // If our client can't cope with us being out of date, we can't cope with
3598 // our dependency being missing.
3599 unsigned Capabilities = ClientLoadCapabilities;
3600 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3601 Capabilities &= ~ARR_Missing;
3602
3603 // Load the AST file.
3604 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3605 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3606 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3607
3608 // Check the AST we just read from ImportedFile contains a different
3609 // module than we expected (ImportedName). This can occur for C++20
3610 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3611 if (IsImportingStdCXXModule) {
3612 if (const auto *Imported =
3613 getModuleManager().lookupByFileName(FileName: ImportedFile);
3614 Imported != nullptr && Imported->ModuleName != ImportedName) {
3615 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3616 Result = Missing;
3617 }
3618 }
3619
3620 // If we diagnosed a problem, produce a backtrace.
3621 bool recompilingFinalized = Result == OutOfDate &&
3622 (Capabilities & ARR_OutOfDate) &&
3623 getModuleManager()
3624 .getModuleCache()
3625 .getInMemoryModuleCache()
3626 .isPCMFinal(Filename: F.FileName);
3627 if (!IgnoreImportedByNote &&
3628 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3629 Diag(DiagID: diag::note_module_file_imported_by)
3630 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3631
3632 switch (Result) {
3633 case Failure: return Failure;
3634 // If we have to ignore the dependency, we'll have to ignore this too.
3635 case Missing:
3636 case OutOfDate: return OutOfDate;
3637 case VersionMismatch: return VersionMismatch;
3638 case ConfigurationMismatch: return ConfigurationMismatch;
3639 case HadErrors: return HadErrors;
3640 case Success: break;
3641 }
3642 break;
3643 }
3644
3645 case ORIGINAL_FILE:
3646 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3647 F.ActualOriginalSourceFileName = std::string(Blob);
3648 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3649 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3650 break;
3651
3652 case ORIGINAL_FILE_ID:
3653 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3654 break;
3655
3656 case MODULE_NAME:
3657 F.ModuleName = std::string(Blob);
3658 Diag(DiagID: diag::remark_module_import)
3659 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3660 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3661 if (Listener)
3662 Listener->ReadModuleName(ModuleName: F.ModuleName);
3663
3664 // Validate the AST as soon as we have a name so we can exit early on
3665 // failure.
3666 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3667 return Result;
3668
3669 break;
3670
3671 case MODULE_DIRECTORY: {
3672 // Save the BaseDirectory as written in the PCM for computing the module
3673 // filename for the ModuleCache.
3674 BaseDirectoryAsWritten = Blob;
3675 assert(!F.ModuleName.empty() &&
3676 "MODULE_DIRECTORY found before MODULE_NAME");
3677 F.BaseDirectory = std::string(Blob);
3678
3679 auto [MaybeM, IgnoreError] =
3680 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3681 if (!MaybeM.has_value())
3682 break;
3683
3684 Module *M = MaybeM.value();
3685 if (!M || !M->Directory)
3686 break;
3687 if (IgnoreError) {
3688 F.BaseDirectory = std::string(M->Directory->getName());
3689 break;
3690 }
3691 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3692 break;
3693
3694 // If we're implicitly loading a module, the base directory can't
3695 // change between the build and use.
3696 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3697 if (BuildDir && (*BuildDir == M->Directory)) {
3698 F.BaseDirectory = std::string(M->Directory->getName());
3699 break;
3700 }
3701 Diag(DiagID: diag::remark_module_relocated)
3702 << F.ModuleName << Blob << M->Directory->getName();
3703
3704 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3705 Diag(DiagID: diag::err_imported_module_relocated)
3706 << F.ModuleName << Blob << M->Directory->getName();
3707 return OutOfDate;
3708 }
3709
3710 case MODULE_MAP_FILE:
3711 if (ASTReadResult Result =
3712 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3713 return Result;
3714 break;
3715
3716 case MODULE_DIRECTORY_DEPENDENCIES:
3717 ReadDirectoryDependencies(Record, F);
3718 break;
3719
3720 case INPUT_FILE_OFFSETS:
3721 NumInputs = Record[0];
3722 NumUserInputs = Record[1];
3723 F.InputFileOffsets =
3724 (const llvm::support::unaligned_uint64_t *)Blob.data();
3725 F.InputFilesLoaded.resize(new_size: NumInputs);
3726 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3727 F.NumUserInputFiles = NumUserInputs;
3728 break;
3729 }
3730 }
3731}
3732
3733llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3734 unsigned ClientLoadCapabilities) {
3735 BitstreamCursor &Stream = F.Stream;
3736
3737 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3738 return Err;
3739 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3740
3741 // Read all of the records and blocks for the AST file.
3742 RecordData Record;
3743 while (true) {
3744 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3745 if (!MaybeEntry)
3746 return MaybeEntry.takeError();
3747 llvm::BitstreamEntry Entry = MaybeEntry.get();
3748
3749 switch (Entry.Kind) {
3750 case llvm::BitstreamEntry::Error:
3751 return llvm::createStringError(
3752 EC: std::errc::illegal_byte_sequence,
3753 Fmt: "error at end of module block in AST file");
3754 case llvm::BitstreamEntry::EndBlock:
3755 // Outside of C++, we do not store a lookup map for the translation unit.
3756 // Instead, mark it as needing a lookup map to be built if this module
3757 // contains any declarations lexically within it (which it always does!).
3758 // This usually has no cost, since we very rarely need the lookup map for
3759 // the translation unit outside C++.
3760 if (ASTContext *Ctx = ContextObj) {
3761 DeclContext *DC = Ctx->getTranslationUnitDecl();
3762 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3763 DC->setMustBuildLookupTable();
3764 }
3765
3766 return llvm::Error::success();
3767 case llvm::BitstreamEntry::SubBlock:
3768 switch (Entry.ID) {
3769 case DECLTYPES_BLOCK_ID:
3770 // We lazily load the decls block, but we want to set up the
3771 // DeclsCursor cursor to point into it. Clone our current bitcode
3772 // cursor to it, enter the block and read the abbrevs in that block.
3773 // With the main cursor, we just skip over it.
3774 F.DeclsCursor = Stream;
3775 if (llvm::Error Err = Stream.SkipBlock())
3776 return Err;
3777 if (llvm::Error Err = ReadBlockAbbrevs(
3778 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3779 return Err;
3780 break;
3781
3782 case PREPROCESSOR_BLOCK_ID:
3783 F.MacroCursor = Stream;
3784 if (!PP.getExternalSource())
3785 PP.setExternalSource(this);
3786
3787 if (llvm::Error Err = Stream.SkipBlock())
3788 return Err;
3789 if (llvm::Error Err =
3790 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3791 return Err;
3792 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3793 break;
3794
3795 case PREPROCESSOR_DETAIL_BLOCK_ID:
3796 F.PreprocessorDetailCursor = Stream;
3797
3798 if (llvm::Error Err = Stream.SkipBlock()) {
3799 return Err;
3800 }
3801 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3802 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3803 return Err;
3804 F.PreprocessorDetailStartOffset
3805 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3806
3807 if (!PP.getPreprocessingRecord())
3808 PP.createPreprocessingRecord();
3809 if (!PP.getPreprocessingRecord()->getExternalSource())
3810 PP.getPreprocessingRecord()->SetExternalSource(*this);
3811 break;
3812
3813 case SOURCE_MANAGER_BLOCK_ID:
3814 if (llvm::Error Err = ReadSourceManagerBlock(F))
3815 return Err;
3816 break;
3817
3818 case SUBMODULE_BLOCK_ID:
3819 F.SubmodulesCursor = Stream;
3820 if (llvm::Error Err = Stream.SkipBlock())
3821 return Err;
3822 if (llvm::Error Err =
3823 ReadBlockAbbrevs(Cursor&: F.SubmodulesCursor, BlockID: SUBMODULE_BLOCK_ID))
3824 return Err;
3825 F.SubmodulesOffsetBase = F.SubmodulesCursor.GetCurrentBitNo();
3826 break;
3827
3828 case COMMENTS_BLOCK_ID: {
3829 BitstreamCursor C = Stream;
3830
3831 if (llvm::Error Err = Stream.SkipBlock())
3832 return Err;
3833 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3834 return Err;
3835 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3836 break;
3837 }
3838
3839 default:
3840 if (llvm::Error Err = Stream.SkipBlock())
3841 return Err;
3842 break;
3843 }
3844 continue;
3845
3846 case llvm::BitstreamEntry::Record:
3847 // The interesting case.
3848 break;
3849 }
3850
3851 // Read and process a record.
3852 Record.clear();
3853 StringRef Blob;
3854 Expected<unsigned> MaybeRecordType =
3855 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3856 if (!MaybeRecordType)
3857 return MaybeRecordType.takeError();
3858 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3859
3860 // If we're not loading an AST context, we don't care about most records.
3861 if (!ContextObj) {
3862 switch (RecordType) {
3863 case IDENTIFIER_TABLE:
3864 case IDENTIFIER_OFFSET:
3865 case INTERESTING_IDENTIFIERS:
3866 case STATISTICS:
3867 case PP_ASSUME_NONNULL_LOC:
3868 case PP_CONDITIONAL_STACK:
3869 case PP_COUNTER_VALUE:
3870 case SOURCE_LOCATION_OFFSETS:
3871 case MODULE_OFFSET_MAP:
3872 case SOURCE_MANAGER_LINE_TABLE:
3873 case PPD_ENTITIES_OFFSETS:
3874 case HEADER_SEARCH_TABLE:
3875 case IMPORTED_MODULES:
3876 case MACRO_OFFSET:
3877 case SUBMODULE_METADATA:
3878 break;
3879 default:
3880 continue;
3881 }
3882 }
3883
3884 switch (RecordType) {
3885 default: // Default behavior: ignore.
3886 break;
3887
3888 case SUBMODULE_METADATA: {
3889 F.BaseSubmoduleID = getTotalNumSubmodules();
3890 F.LocalNumSubmodules = Record[0];
3891 F.LocalBaseSubmoduleID = Record[1];
3892 F.LocalTopLevelSubmoduleID = Record[2];
3893 F.SubmoduleOffsets =
3894 (const llvm::support::unaligned_uint64_t *)Blob.data();
3895 if (F.LocalNumSubmodules > 0) {
3896 // Introduce the global -> local mapping for submodules within this
3897 // module.
3898 GlobalSubmoduleMap.insert(
3899 Val: std::make_pair(x: getTotalNumSubmodules() + 1, y: &F));
3900
3901 // Introduce the local -> global mapping for submodules within this
3902 // module.
3903 F.SubmoduleRemap.insertOrReplace(
3904 Val: std::make_pair(x&: F.LocalBaseSubmoduleID,
3905 y: F.BaseSubmoduleID - F.LocalBaseSubmoduleID));
3906
3907 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
3908 }
3909
3910 auto ReadSubmodule = [&](unsigned LocalID) -> Module * {
3911 return getSubmodule(GlobalID: getGlobalSubmoduleID(M&: F, LocalID));
3912 };
3913
3914 if (PP.getHeaderSearchInfo().getModuleMap().findModule(Name: F.ModuleName)) {
3915 // If we already knew about this module, make sure to bring all
3916 // submodules up to date.
3917 for (unsigned Index = 0; Index != F.LocalNumSubmodules; ++Index) {
3918 unsigned LocalID =
3919 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
3920 ReadSubmodule(LocalID);
3921 }
3922 } else {
3923 // If we didn't know this module, we loaded it transitively. Deserialize
3924 // just the top-level module to register it with ModuleMap, but load the
3925 // rest lazily.
3926 ReadSubmodule(F.LocalTopLevelSubmoduleID);
3927 }
3928
3929 break;
3930 }
3931
3932 case TYPE_OFFSET: {
3933 if (F.LocalNumTypes != 0)
3934 return llvm::createStringError(
3935 EC: std::errc::illegal_byte_sequence,
3936 Fmt: "duplicate TYPE_OFFSET record in AST file");
3937 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3938 F.LocalNumTypes = Record[0];
3939 F.BaseTypeIndex = getTotalNumTypes();
3940
3941 if (F.LocalNumTypes > 0)
3942 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3943
3944 break;
3945 }
3946
3947 case DECL_OFFSET: {
3948 if (F.LocalNumDecls != 0)
3949 return llvm::createStringError(
3950 EC: std::errc::illegal_byte_sequence,
3951 Fmt: "duplicate DECL_OFFSET record in AST file");
3952 F.DeclOffsets = (const DeclOffset *)Blob.data();
3953 F.LocalNumDecls = Record[0];
3954 F.BaseDeclIndex = getTotalNumDecls();
3955
3956 if (F.LocalNumDecls > 0)
3957 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3958
3959 break;
3960 }
3961
3962 case TU_UPDATE_LEXICAL: {
3963 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3964 LexicalContents Contents(
3965 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3966 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3967 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3968 TU->setHasExternalLexicalStorage(true);
3969 break;
3970 }
3971
3972 case UPDATE_VISIBLE: {
3973 unsigned Idx = 0;
3974 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3975 auto *Data = (const unsigned char*)Blob.data();
3976 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3977 // If we've already loaded the decl, perform the updates when we finish
3978 // loading this block.
3979 if (Decl *D = GetExistingDecl(ID))
3980 PendingUpdateRecords.push_back(
3981 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3982 break;
3983 }
3984
3985 case UPDATE_MODULE_LOCAL_VISIBLE: {
3986 unsigned Idx = 0;
3987 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3988 auto *Data = (const unsigned char *)Blob.data();
3989 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3990 // If we've already loaded the decl, perform the updates when we finish
3991 // loading this block.
3992 if (Decl *D = GetExistingDecl(ID))
3993 PendingUpdateRecords.push_back(
3994 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3995 break;
3996 }
3997
3998 case UPDATE_TU_LOCAL_VISIBLE: {
3999 if (F.Kind != MK_MainFile)
4000 break;
4001 unsigned Idx = 0;
4002 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4003 auto *Data = (const unsigned char *)Blob.data();
4004 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4005 // If we've already loaded the decl, perform the updates when we finish
4006 // loading this block.
4007 if (Decl *D = GetExistingDecl(ID))
4008 PendingUpdateRecords.push_back(
4009 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4010 break;
4011 }
4012
4013 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
4014 unsigned Idx = 0;
4015 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4016 auto *Data = (const unsigned char *)Blob.data();
4017 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4018 // If we've already loaded the decl, perform the updates when we finish
4019 // loading this block.
4020 if (Decl *D = GetExistingDecl(ID))
4021 PendingUpdateRecords.push_back(
4022 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4023 break;
4024 }
4025
4026 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
4027 unsigned Idx = 0;
4028 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4029 auto *Data = (const unsigned char *)Blob.data();
4030 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4031 // If we've already loaded the decl, perform the updates when we finish
4032 // loading this block.
4033 if (Decl *D = GetExistingDecl(ID))
4034 PendingUpdateRecords.push_back(
4035 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4036 break;
4037 }
4038
4039 case IDENTIFIER_TABLE:
4040 F.IdentifierTableData =
4041 reinterpret_cast<const unsigned char *>(Blob.data());
4042 if (Record[0]) {
4043 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
4044 Buckets: F.IdentifierTableData + Record[0],
4045 Payload: F.IdentifierTableData + sizeof(uint32_t),
4046 Base: F.IdentifierTableData,
4047 InfoObj: ASTIdentifierLookupTrait(*this, F));
4048
4049 PP.getIdentifierTable().setExternalIdentifierLookup(this);
4050 }
4051 break;
4052
4053 case IDENTIFIER_OFFSET: {
4054 if (F.LocalNumIdentifiers != 0)
4055 return llvm::createStringError(
4056 EC: std::errc::illegal_byte_sequence,
4057 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
4058 F.IdentifierOffsets = (const uint32_t *)Blob.data();
4059 F.LocalNumIdentifiers = Record[0];
4060 F.BaseIdentifierID = getTotalNumIdentifiers();
4061
4062 if (F.LocalNumIdentifiers > 0)
4063 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
4064 + F.LocalNumIdentifiers);
4065 break;
4066 }
4067
4068 case INTERESTING_IDENTIFIERS:
4069 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
4070 break;
4071
4072 case EAGERLY_DESERIALIZED_DECLS:
4073 // FIXME: Skip reading this record if our ASTConsumer doesn't care
4074 // about "interesting" decls (for instance, if we're building a module).
4075 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4076 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4077 break;
4078
4079 case MODULAR_CODEGEN_DECLS:
4080 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
4081 // them (ie: if we're not codegenerating this module).
4082 if (F.Kind == MK_MainFile ||
4083 getContext().getLangOpts().BuildingPCHWithObjectFile)
4084 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4085 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4086 break;
4087
4088 case SPECIAL_TYPES:
4089 if (SpecialTypes.empty()) {
4090 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4091 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
4092 break;
4093 }
4094
4095 if (Record.empty())
4096 break;
4097
4098 if (SpecialTypes.size() != Record.size())
4099 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4100 Fmt: "invalid special-types record");
4101
4102 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4103 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
4104 if (!SpecialTypes[I])
4105 SpecialTypes[I] = ID;
4106 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
4107 // merge step?
4108 }
4109 break;
4110
4111 case STATISTICS:
4112 TotalNumStatements += Record[0];
4113 TotalNumMacros += Record[1];
4114 TotalLexicalDeclContexts += Record[2];
4115 TotalVisibleDeclContexts += Record[3];
4116 TotalModuleLocalVisibleDeclContexts += Record[4];
4117 TotalTULocalVisibleDeclContexts += Record[5];
4118 break;
4119
4120 case UNUSED_FILESCOPED_DECLS:
4121 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4122 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4123 break;
4124
4125 case DELEGATING_CTORS:
4126 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4127 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4128 break;
4129
4130 case WEAK_UNDECLARED_IDENTIFIERS:
4131 if (Record.size() % 3 != 0)
4132 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4133 Fmt: "invalid weak identifiers record");
4134
4135 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4136 // files. This isn't the way to do it :)
4137 WeakUndeclaredIdentifiers.clear();
4138
4139 // Translate the weak, undeclared identifiers into global IDs.
4140 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4141 WeakUndeclaredIdentifiers.push_back(
4142 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4143 WeakUndeclaredIdentifiers.push_back(
4144 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4145 WeakUndeclaredIdentifiers.push_back(
4146 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4147 }
4148 break;
4149
4150 case EXTNAME_UNDECLARED_IDENTIFIERS:
4151 if (Record.size() % 3 != 0)
4152 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4153 Fmt: "invalid extname identifiers record");
4154
4155 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4156 // non-original PCH files. This isn't the way to do it :)
4157 ExtnameUndeclaredIdentifiers.clear();
4158
4159 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4160 // global IDs.
4161 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4162 ExtnameUndeclaredIdentifiers.push_back(
4163 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4164 ExtnameUndeclaredIdentifiers.push_back(
4165 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4166 ExtnameUndeclaredIdentifiers.push_back(
4167 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4168 }
4169 break;
4170
4171 case SELECTOR_OFFSETS: {
4172 F.SelectorOffsets = (const uint32_t *)Blob.data();
4173 F.LocalNumSelectors = Record[0];
4174 unsigned LocalBaseSelectorID = Record[1];
4175 F.BaseSelectorID = getTotalNumSelectors();
4176
4177 if (F.LocalNumSelectors > 0) {
4178 // Introduce the global -> local mapping for selectors within this
4179 // module.
4180 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4181
4182 // Introduce the local -> global mapping for selectors within this
4183 // module.
4184 F.SelectorRemap.insertOrReplace(
4185 Val: std::make_pair(x&: LocalBaseSelectorID,
4186 y: F.BaseSelectorID - LocalBaseSelectorID));
4187
4188 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4189 }
4190 break;
4191 }
4192
4193 case METHOD_POOL:
4194 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4195 if (Record[0])
4196 F.SelectorLookupTable
4197 = ASTSelectorLookupTable::Create(
4198 Buckets: F.SelectorLookupTableData + Record[0],
4199 Base: F.SelectorLookupTableData,
4200 InfoObj: ASTSelectorLookupTrait(*this, F));
4201 TotalNumMethodPoolEntries += Record[1];
4202 break;
4203
4204 case REFERENCED_SELECTOR_POOL:
4205 if (!Record.empty()) {
4206 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4207 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4208 LocalID: Record[Idx++]));
4209 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4210 getRawEncoding());
4211 }
4212 }
4213 break;
4214
4215 case PP_ASSUME_NONNULL_LOC: {
4216 unsigned Idx = 0;
4217 if (!Record.empty())
4218 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4219 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4220 break;
4221 }
4222
4223 case PP_UNSAFE_BUFFER_USAGE: {
4224 if (!Record.empty()) {
4225 SmallVector<SourceLocation, 64> SrcLocs;
4226 unsigned Idx = 0;
4227 while (Idx < Record.size())
4228 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4229 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4230 }
4231 break;
4232 }
4233
4234 case PP_CONDITIONAL_STACK:
4235 if (!Record.empty()) {
4236 unsigned Idx = 0, End = Record.size() - 1;
4237 bool ReachedEOFWhileSkipping = Record[Idx++];
4238 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4239 if (ReachedEOFWhileSkipping) {
4240 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4241 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4242 bool FoundNonSkipPortion = Record[Idx++];
4243 bool FoundElse = Record[Idx++];
4244 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4245 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4246 args&: FoundElse, args&: ElseLoc);
4247 }
4248 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4249 while (Idx < End) {
4250 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4251 bool WasSkipping = Record[Idx++];
4252 bool FoundNonSkip = Record[Idx++];
4253 bool FoundElse = Record[Idx++];
4254 ConditionalStack.push_back(
4255 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4256 }
4257 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4258 }
4259 break;
4260
4261 case PP_COUNTER_VALUE:
4262 if (!Record.empty() && Listener)
4263 Listener->ReadCounter(M: F, Value: Record[0]);
4264 break;
4265
4266 case FILE_SORTED_DECLS:
4267 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4268 F.NumFileSortedDecls = Record[0];
4269 break;
4270
4271 case SOURCE_LOCATION_OFFSETS: {
4272 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4273 F.LocalNumSLocEntries = Record[0];
4274 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4275 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4276 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4277 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4278 TotalSize: SLocSpaceSize);
4279 if (!F.SLocEntryBaseID) {
4280 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4281 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4282 return llvm::createStringError(EC: std::errc::invalid_argument,
4283 Fmt: "ran out of source locations");
4284 }
4285 // Make our entry in the range map. BaseID is negative and growing, so
4286 // we invert it. Because we invert it, though, we need the other end of
4287 // the range.
4288 unsigned RangeStart =
4289 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4290 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4291 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4292
4293 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4294 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4295 GlobalSLocOffsetMap.insert(
4296 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4297 - SLocSpaceSize,y: &F));
4298
4299 TotalNumSLocEntries += F.LocalNumSLocEntries;
4300 break;
4301 }
4302
4303 case MODULE_OFFSET_MAP:
4304 F.ModuleOffsetMap = Blob;
4305 break;
4306
4307 case SOURCE_MANAGER_LINE_TABLE:
4308 ParseLineTable(F, Record);
4309 break;
4310
4311 case EXT_VECTOR_DECLS:
4312 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4313 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4314 break;
4315
4316 case VTABLE_USES:
4317 if (Record.size() % 3 != 0)
4318 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4319 Fmt: "Invalid VTABLE_USES record");
4320
4321 // Later tables overwrite earlier ones.
4322 // FIXME: Modules will have some trouble with this. This is clearly not
4323 // the right way to do this.
4324 VTableUses.clear();
4325
4326 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4327 VTableUses.push_back(
4328 Elt: {.ID: ReadDeclID(F, Record, Idx),
4329 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4330 .Used: (bool)Record[Idx++]});
4331 }
4332 break;
4333
4334 case PENDING_IMPLICIT_INSTANTIATIONS:
4335
4336 if (Record.size() % 2 != 0)
4337 return llvm::createStringError(
4338 EC: std::errc::illegal_byte_sequence,
4339 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4340
4341 // For standard C++20 module, we will only reads the instantiations
4342 // if it is the main file.
4343 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4344 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4345 PendingInstantiations.push_back(
4346 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4347 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4348 }
4349 }
4350 break;
4351
4352 case SEMA_DECL_REFS:
4353 if (Record.size() != 3)
4354 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4355 Fmt: "Invalid SEMA_DECL_REFS block");
4356 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4357 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4358 break;
4359
4360 case PPD_ENTITIES_OFFSETS: {
4361 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4362 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4363 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4364
4365 unsigned StartingID;
4366 if (!PP.getPreprocessingRecord())
4367 PP.createPreprocessingRecord();
4368 if (!PP.getPreprocessingRecord()->getExternalSource())
4369 PP.getPreprocessingRecord()->SetExternalSource(*this);
4370 StartingID
4371 = PP.getPreprocessingRecord()
4372 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4373 F.BasePreprocessedEntityID = StartingID;
4374
4375 if (F.NumPreprocessedEntities > 0) {
4376 // Introduce the global -> local mapping for preprocessed entities in
4377 // this module.
4378 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4379 }
4380
4381 break;
4382 }
4383
4384 case PPD_SKIPPED_RANGES: {
4385 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4386 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4387 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4388
4389 if (!PP.getPreprocessingRecord())
4390 PP.createPreprocessingRecord();
4391 if (!PP.getPreprocessingRecord()->getExternalSource())
4392 PP.getPreprocessingRecord()->SetExternalSource(*this);
4393 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4394 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4395
4396 if (F.NumPreprocessedSkippedRanges > 0)
4397 GlobalSkippedRangeMap.insert(
4398 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4399 break;
4400 }
4401
4402 case DECL_UPDATE_OFFSETS:
4403 if (Record.size() % 2 != 0)
4404 return llvm::createStringError(
4405 EC: std::errc::illegal_byte_sequence,
4406 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4407 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4408 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4409 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4410
4411 // If we've already loaded the decl, perform the updates when we finish
4412 // loading this block.
4413 if (Decl *D = GetExistingDecl(ID))
4414 PendingUpdateRecords.push_back(
4415 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4416 }
4417 break;
4418
4419 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4420 if (Record.size() % 5 != 0)
4421 return llvm::createStringError(
4422 EC: std::errc::illegal_byte_sequence,
4423 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4424 "file");
4425 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4426 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4427
4428 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4429 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4430 uint64_t LocalLexicalOffset = Record[I++];
4431 uint64_t LexicalOffset =
4432 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4433 uint64_t LocalVisibleOffset = Record[I++];
4434 uint64_t VisibleOffset =
4435 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4436 uint64_t LocalModuleLocalOffset = Record[I++];
4437 uint64_t ModuleLocalOffset =
4438 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4439 uint64_t TULocalLocalOffset = Record[I++];
4440 uint64_t TULocalOffset =
4441 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4442
4443 DelayedNamespaceOffsetMap[ID] = {
4444 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4445
4446 assert(!GetExistingDecl(ID) &&
4447 "We shouldn't load the namespace in the front of delayed "
4448 "namespace lexical and visible block");
4449 }
4450 break;
4451 }
4452
4453 case RELATED_DECLS_MAP:
4454 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4455 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4456 auto &RelatedDecls = RelatedDeclsMap[ID];
4457 unsigned NN = Record[I++];
4458 RelatedDecls.reserve(N: NN);
4459 for (unsigned II = 0; II < NN; II++)
4460 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4461 }
4462 break;
4463
4464 case OBJC_CATEGORIES_MAP:
4465 if (F.LocalNumObjCCategoriesInMap != 0)
4466 return llvm::createStringError(
4467 EC: std::errc::illegal_byte_sequence,
4468 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4469
4470 F.LocalNumObjCCategoriesInMap = Record[0];
4471 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4472 break;
4473
4474 case OBJC_CATEGORIES:
4475 F.ObjCCategories.swap(RHS&: Record);
4476 break;
4477
4478 case CUDA_SPECIAL_DECL_REFS:
4479 // Later tables overwrite earlier ones.
4480 // FIXME: Modules will have trouble with this.
4481 CUDASpecialDeclRefs.clear();
4482 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4483 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4484 break;
4485
4486 case HEADER_SEARCH_TABLE:
4487 F.HeaderFileInfoTableData = Blob.data();
4488 F.LocalNumHeaderFileInfos = Record[1];
4489 if (Record[0]) {
4490 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4491 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4492 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4493 InfoObj: HeaderFileInfoTrait(*this, F));
4494
4495 PP.getHeaderSearchInfo().SetExternalSource(this);
4496 if (!PP.getHeaderSearchInfo().getExternalLookup())
4497 PP.getHeaderSearchInfo().SetExternalLookup(this);
4498 }
4499 break;
4500
4501 case FP_PRAGMA_OPTIONS:
4502 // Later tables overwrite earlier ones.
4503 FPPragmaOptions.swap(RHS&: Record);
4504 break;
4505
4506 case DECLS_WITH_EFFECTS_TO_VERIFY:
4507 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4508 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4509 break;
4510
4511 case OMP_REQUIRES_DECLS:
4512 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4513 OpenMPRequiresDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4514 break;
4515
4516 case OPENCL_EXTENSIONS:
4517 for (unsigned I = 0, E = Record.size(); I != E; ) {
4518 auto Name = ReadString(Record, Idx&: I);
4519 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4520 OptInfo.Supported = Record[I++] != 0;
4521 OptInfo.Enabled = Record[I++] != 0;
4522 OptInfo.WithPragma = Record[I++] != 0;
4523 OptInfo.Avail = Record[I++];
4524 OptInfo.Core = Record[I++];
4525 OptInfo.Opt = Record[I++];
4526 }
4527 break;
4528
4529 case TENTATIVE_DEFINITIONS:
4530 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4531 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4532 break;
4533
4534 case KNOWN_NAMESPACES:
4535 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4536 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4537 break;
4538
4539 case UNDEFINED_BUT_USED:
4540 if (Record.size() % 2 != 0)
4541 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4542 Fmt: "invalid undefined-but-used record");
4543 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4544 UndefinedButUsed.push_back(
4545 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4546 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4547 }
4548 break;
4549
4550 case DELETE_EXPRS_TO_ANALYZE:
4551 for (unsigned I = 0, N = Record.size(); I != N;) {
4552 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4553 const uint64_t Count = Record[I++];
4554 DelayedDeleteExprs.push_back(Elt: Count);
4555 for (uint64_t C = 0; C < Count; ++C) {
4556 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4557 bool IsArrayForm = Record[I++] == 1;
4558 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4559 }
4560 }
4561 break;
4562
4563 case VTABLES_TO_EMIT:
4564 if (F.Kind == MK_MainFile ||
4565 getContext().getLangOpts().BuildingPCHWithObjectFile)
4566 for (unsigned I = 0, N = Record.size(); I != N;)
4567 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4568 break;
4569
4570 case IMPORTED_MODULES:
4571 if (!F.isModule()) {
4572 // If we aren't loading a module (which has its own exports), make
4573 // all of the imported modules visible.
4574 // FIXME: Deal with macros-only imports.
4575 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4576 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4577 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4578 if (GlobalID) {
4579 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4580 if (DeserializationListener)
4581 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4582 }
4583 }
4584 }
4585 break;
4586
4587 case MACRO_OFFSET: {
4588 if (F.LocalNumMacros != 0)
4589 return llvm::createStringError(
4590 EC: std::errc::illegal_byte_sequence,
4591 Fmt: "duplicate MACRO_OFFSET record in AST file");
4592 F.MacroOffsets = (const uint32_t *)Blob.data();
4593 F.LocalNumMacros = Record[0];
4594 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4595 F.BaseMacroID = getTotalNumMacros();
4596
4597 if (F.LocalNumMacros > 0)
4598 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4599 break;
4600 }
4601
4602 case LATE_PARSED_TEMPLATE:
4603 LateParsedTemplates.emplace_back(
4604 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4605 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4606 break;
4607
4608 case OPTIMIZE_PRAGMA_OPTIONS:
4609 if (Record.size() != 1)
4610 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4611 Fmt: "invalid pragma optimize record");
4612 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4613 break;
4614
4615 case MSSTRUCT_PRAGMA_OPTIONS:
4616 if (Record.size() != 1)
4617 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4618 Fmt: "invalid pragma ms_struct record");
4619 PragmaMSStructState = Record[0];
4620 break;
4621
4622 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4623 if (Record.size() != 2)
4624 return llvm::createStringError(
4625 EC: std::errc::illegal_byte_sequence,
4626 Fmt: "invalid pragma pointers to members record");
4627 PragmaMSPointersToMembersState = Record[0];
4628 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4629 break;
4630
4631 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4632 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4633 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4634 break;
4635
4636 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4637 if (Record.size() != 1)
4638 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4639 Fmt: "invalid cuda pragma options record");
4640 ForceHostDeviceDepth = Record[0];
4641 break;
4642
4643 case ALIGN_PACK_PRAGMA_OPTIONS: {
4644 if (Record.size() < 3)
4645 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4646 Fmt: "invalid pragma pack record");
4647 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4648 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4649 unsigned NumStackEntries = Record[2];
4650 unsigned Idx = 3;
4651 // Reset the stack when importing a new module.
4652 PragmaAlignPackStack.clear();
4653 for (unsigned I = 0; I < NumStackEntries; ++I) {
4654 PragmaAlignPackStackEntry Entry;
4655 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4656 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4657 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4658 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4659 Entry.SlotLabel = PragmaAlignPackStrings.back();
4660 PragmaAlignPackStack.push_back(Elt: Entry);
4661 }
4662 break;
4663 }
4664
4665 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4666 if (Record.size() < 3)
4667 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4668 Fmt: "invalid pragma float control record");
4669 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4670 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4671 unsigned NumStackEntries = Record[2];
4672 unsigned Idx = 3;
4673 // Reset the stack when importing a new module.
4674 FpPragmaStack.clear();
4675 for (unsigned I = 0; I < NumStackEntries; ++I) {
4676 FpPragmaStackEntry Entry;
4677 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4678 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4679 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4680 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4681 Entry.SlotLabel = FpPragmaStrings.back();
4682 FpPragmaStack.push_back(Elt: Entry);
4683 }
4684 break;
4685 }
4686
4687 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4688 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4689 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4690 break;
4691
4692 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4693 unsigned NumRecords = Record.front();
4694 // Last record which is used to keep number of valid records.
4695 if (Record.size() - 1 != NumRecords)
4696 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4697 Fmt: "invalid rvv intrinsic pragma record");
4698
4699 if (RISCVVecIntrinsicPragma.empty())
4700 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4701 // There might be multiple precompiled modules imported, we need to union
4702 // them all.
4703 for (unsigned i = 0; i < NumRecords; ++i)
4704 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4705 break;
4706 }
4707 }
4708 }
4709}
4710
4711void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4712 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4713
4714 // Additional remapping information.
4715 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4716 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4717 F.ModuleOffsetMap = StringRef();
4718
4719 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4720 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4721 RemapBuilder SelectorRemap(F.SelectorRemap);
4722
4723 auto &ImportedModuleVector = F.TransitiveImports;
4724 assert(ImportedModuleVector.empty());
4725
4726 while (Data < DataEnd) {
4727 // FIXME: Looking up dependency modules by filename is horrible. Let's
4728 // start fixing this with prebuilt, explicit and implicit modules and see
4729 // how it goes...
4730 using namespace llvm::support;
4731 ModuleKind Kind = static_cast<ModuleKind>(
4732 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4733 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4734 StringRef Name = StringRef((const char*)Data, Len);
4735 Data += Len;
4736 ModuleFile *OM =
4737 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4738 Kind == MK_ImplicitModule
4739 ? ModuleMgr.lookupByModuleName(ModName: Name)
4740 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4741 if (!OM)
4742 OM = ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeInMemory(Name));
4743 if (!OM) {
4744 std::string Msg = "refers to unknown module, cannot find ";
4745 Msg.append(str: std::string(Name));
4746 Error(Msg);
4747 return;
4748 }
4749
4750 ImportedModuleVector.push_back(Elt: OM);
4751
4752 uint32_t SubmoduleIDOffset =
4753 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4754 uint32_t SelectorIDOffset =
4755 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4756
4757 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4758 RemapBuilder &Remap) {
4759 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4760 if (Offset != None)
4761 Remap.insert(Val: std::make_pair(x&: Offset,
4762 y: static_cast<int>(BaseOffset - Offset)));
4763 };
4764
4765 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4766 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4767 }
4768}
4769
4770ASTReader::ASTReadResult
4771ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4772 const ModuleFile *ImportedBy,
4773 unsigned ClientLoadCapabilities) {
4774 unsigned Idx = 0;
4775 F.ModuleMapPath = ReadPath(F, Record, Idx);
4776
4777 // Try to resolve ModuleName in the current header search context and
4778 // verify that it is found in the same module map file as we saved. If the
4779 // top-level AST file is a main file, skip this check because there is no
4780 // usable header search context.
4781 assert(!F.ModuleName.empty() &&
4782 "MODULE_NAME should come before MODULE_MAP_FILE");
4783 auto [MaybeM, IgnoreError] =
4784 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4785 if (MaybeM.has_value()) {
4786 // An implicitly-loaded module file should have its module listed in some
4787 // module map file that we've already loaded.
4788 Module *M = MaybeM.value();
4789 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4790 OptionalFileEntryRef ModMap =
4791 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4792 if (!IgnoreError && !ModMap) {
4793 if (M && M->Directory)
4794 Diag(DiagID: diag::remark_module_relocated)
4795 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4796
4797 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4798 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4799 // This module was defined by an imported (explicit) module.
4800 Diag(DiagID: diag::err_module_file_conflict)
4801 << F.ModuleName << F.FileName << *ASTFileName;
4802 // TODO: Add a note with the module map paths if they differ.
4803 } else {
4804 // This module was built with a different module map.
4805 Diag(DiagID: diag::err_imported_module_not_found)
4806 << F.ModuleName << F.FileName
4807 << (ImportedBy ? ImportedBy->FileName.str() : "")
4808 << F.ModuleMapPath << !ImportedBy;
4809 // In case it was imported by a PCH, there's a chance the user is
4810 // just missing to include the search path to the directory containing
4811 // the modulemap.
4812 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4813 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4814 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4815 }
4816 }
4817 return OutOfDate;
4818 }
4819
4820 assert(M && M->Name == F.ModuleName && "found module with different name");
4821
4822 // Check the primary module map file.
4823 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4824 if (!StoredModMap || *StoredModMap != ModMap) {
4825 assert(ModMap && "found module is missing module map file");
4826 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4827 "top-level import should be verified");
4828 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4829 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4830 Diag(DiagID: diag::err_imported_module_modmap_changed)
4831 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4832 << ModMap->getName() << F.ModuleMapPath << NotImported;
4833 return OutOfDate;
4834 }
4835
4836 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4837 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4838 // FIXME: we should use input files rather than storing names.
4839 std::string Filename = ReadPath(F, Record, Idx);
4840 auto SF = FileMgr.getOptionalFileRef(Filename);
4841 if (!SF) {
4842 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4843 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4844 return OutOfDate;
4845 }
4846 AdditionalStoredMaps.insert(V: *SF);
4847 }
4848
4849 // Check any additional module map files (e.g. module.private.modulemap)
4850 // that are not in the pcm.
4851 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4852 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4853 // Remove files that match
4854 // Note: SmallPtrSet::erase is really remove
4855 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4856 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4857 Diag(DiagID: diag::err_module_different_modmap)
4858 << F.ModuleName << /*new*/0 << ModMap.getName();
4859 return OutOfDate;
4860 }
4861 }
4862 }
4863
4864 // Check any additional module map files that are in the pcm, but not
4865 // found in header search. Cases that match are already removed.
4866 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4867 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4868 Diag(DiagID: diag::err_module_different_modmap)
4869 << F.ModuleName << /*not new*/1 << ModMap.getName();
4870 return OutOfDate;
4871 }
4872 }
4873
4874 if (Listener)
4875 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4876 return Success;
4877}
4878
4879void ASTReader::ReadDirectoryDependencies(const RecordData &Record,
4880 ModuleFile &F) {
4881 unsigned Idx = 0;
4882 unsigned N = Record[Idx++];
4883 F.DirectoryDependencies.reserve(n: N);
4884 for (unsigned I = 0; I != N; ++I)
4885 F.DirectoryDependencies.push_back(x: ReadPath(F, Record, Idx));
4886}
4887
4888bool ASTReader::isDirectoryDependencyOutOfDate(ModuleFile &F, bool Complain) {
4889 llvm::vfs::FileSystem &FS = PP.getFileManager().getVirtualFileSystem();
4890 // Adding or removing an entry updates the modification time of the directory
4891 // holding it, so comparing every directory in the tree against the module
4892 // file catches a header that was added, even one whose own modification time
4893 // is older. A directory that doesn't exist has no listing to depend on.
4894 auto IsNewer = [&](StringRef Dir) {
4895 llvm::ErrorOr<llvm::vfs::Status> Status = FS.status(Path: Dir);
4896 return Status && Status->isDirectory() &&
4897 llvm::sys::toTimeT(TP: Status->getLastModificationTime()) > F.ModTime;
4898 };
4899
4900 ModuleCache &ModCache = getModuleManager().getModuleCache();
4901 const HeaderSearchOptions &HSOpts =
4902 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4903 for (StringRef Dir : F.DirectoryDependencies) {
4904 if (std::optional<bool> Invalidated =
4905 ModCache.isDirectoryInvalidated(Directory: Dir)) {
4906 // The build system reports paths as changed before the build session
4907 // started, so a module built during it is newer than them.
4908 if (!*Invalidated ||
4909 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp)
4910 continue;
4911 Diag(DiagID: diag::remark_module_path_invalidated) << F.ModuleName << Dir;
4912 if (Complain)
4913 Diag(DiagID: diag::err_module_path_invalidated) << F.ModuleName << Dir;
4914 return true;
4915 }
4916
4917 std::optional<std::string> Changed;
4918 if (IsNewer(Dir)) {
4919 Changed = Dir.str();
4920 } else {
4921 std::error_code EC;
4922 for (llvm::vfs::recursive_directory_iterator I(FS, Dir, EC), E;
4923 I != E && !EC; I.increment(EC)) {
4924 if (I->type() == llvm::sys::fs::file_type::directory_file &&
4925 IsNewer(I->path())) {
4926 Changed = I->path().str();
4927 break;
4928 }
4929 }
4930 }
4931 if (!Changed)
4932 continue;
4933 Diag(DiagID: diag::remark_module_directory_dep_changed) << F.ModuleName << *Changed;
4934 if (Complain)
4935 Diag(DiagID: diag::err_module_directory_dep_changed) << F.ModuleName << *Changed;
4936 return true;
4937 }
4938 return false;
4939}
4940
4941/// Move the given method to the back of the global list of methods.
4942static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4943 // Find the entry for this selector in the method pool.
4944 SemaObjC::GlobalMethodPool::iterator Known =
4945 S.ObjC().MethodPool.find(Val: Method->getSelector());
4946 if (Known == S.ObjC().MethodPool.end())
4947 return;
4948
4949 // Retrieve the appropriate method list.
4950 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4951 : Known->second.second;
4952 bool Found = false;
4953 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4954 if (!Found) {
4955 if (List->getMethod() == Method) {
4956 Found = true;
4957 } else {
4958 // Keep searching.
4959 continue;
4960 }
4961 }
4962
4963 if (List->getNext())
4964 List->setMethod(List->getNext()->getMethod());
4965 else
4966 List->setMethod(Method);
4967 }
4968}
4969
4970void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4971 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4972 for (Decl *D : Names) {
4973 bool wasHidden = !D->isUnconditionallyVisible();
4974 D->setVisibleDespiteOwningModule();
4975
4976 if (wasHidden && SemaObj) {
4977 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4978 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4979 }
4980 }
4981 }
4982}
4983
4984void ASTReader::makeModuleVisible(Module *Mod,
4985 Module::NameVisibilityKind NameVisibility,
4986 SourceLocation ImportLoc) {
4987 llvm::SmallPtrSet<Module *, 4> Visited;
4988 SmallVector<Module *, 4> Stack;
4989 Stack.push_back(Elt: Mod);
4990 while (!Stack.empty()) {
4991 Mod = Stack.pop_back_val();
4992
4993 if (NameVisibility <= Mod->NameVisibility) {
4994 // This module already has this level of visibility (or greater), so
4995 // there is nothing more to do.
4996 continue;
4997 }
4998
4999 if (Mod->isUnimportable()) {
5000 // Modules that aren't importable cannot be made visible.
5001 continue;
5002 }
5003
5004 // Update the module's name visibility.
5005 Mod->NameVisibility = NameVisibility;
5006
5007 // If we've already deserialized any names from this module,
5008 // mark them as visible.
5009 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
5010 if (Hidden != HiddenNamesMap.end()) {
5011 auto HiddenNames = std::move(*Hidden);
5012 HiddenNamesMap.erase(I: Hidden);
5013 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
5014 assert(!HiddenNamesMap.contains(Mod) &&
5015 "making names visible added hidden names");
5016 }
5017
5018 // Push any exported modules onto the stack to be marked as visible.
5019 SmallVector<Module *, 16> Exports;
5020 Mod->getExportedModules(Exported&: Exports);
5021 for (SmallVectorImpl<Module *>::iterator
5022 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
5023 Module *Exported = *I;
5024 if (Visited.insert(Ptr: Exported).second)
5025 Stack.push_back(Elt: Exported);
5026 }
5027 }
5028}
5029
5030/// We've merged the definition \p MergedDef into the existing definition
5031/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
5032/// visible.
5033void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
5034 NamedDecl *MergedDef) {
5035 if (!Def->isUnconditionallyVisible()) {
5036 // If MergedDef is visible or becomes visible, make the definition visible.
5037 if (MergedDef->isUnconditionallyVisible())
5038 Def->setVisibleDespiteOwningModule();
5039 else {
5040 getContext().mergeDefinitionIntoModule(
5041 ND: Def, M: MergedDef->getImportedOwningModule(),
5042 /*NotifyListeners*/ false);
5043 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
5044 }
5045 }
5046}
5047
5048bool ASTReader::loadGlobalIndex() {
5049 if (GlobalIndex)
5050 return false;
5051
5052 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
5053 !PP.getLangOpts().Modules)
5054 return true;
5055
5056 // Try to load the global index.
5057 TriedLoadingGlobalIndex = true;
5058 StringRef SpecificModuleCachePath =
5059 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
5060 std::pair<GlobalModuleIndex *, llvm::Error> Result =
5061 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
5062 if (llvm::Error Err = std::move(Result.second)) {
5063 assert(!Result.first);
5064 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5065 return true;
5066 }
5067
5068 GlobalIndex.reset(p: Result.first);
5069 ModuleMgr.setGlobalIndex(GlobalIndex.get());
5070 return false;
5071}
5072
5073bool ASTReader::isGlobalIndexUnavailable() const {
5074 return PP.getLangOpts().Modules && UseGlobalIndex &&
5075 !hasGlobalIndex() && TriedLoadingGlobalIndex;
5076}
5077
5078/// Given a cursor at the start of an AST file, scan ahead and drop the
5079/// cursor into the start of the given block ID, returning false on success and
5080/// true on failure.
5081static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
5082 while (true) {
5083 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
5084 if (!MaybeEntry) {
5085 // FIXME this drops errors on the floor.
5086 consumeError(Err: MaybeEntry.takeError());
5087 return true;
5088 }
5089 llvm::BitstreamEntry Entry = MaybeEntry.get();
5090
5091 switch (Entry.Kind) {
5092 case llvm::BitstreamEntry::Error:
5093 case llvm::BitstreamEntry::EndBlock:
5094 return true;
5095
5096 case llvm::BitstreamEntry::Record:
5097 // Ignore top-level records.
5098 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
5099 break;
5100 else {
5101 // FIXME this drops errors on the floor.
5102 consumeError(Err: Skipped.takeError());
5103 return true;
5104 }
5105
5106 case llvm::BitstreamEntry::SubBlock:
5107 if (Entry.ID == BlockID) {
5108 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
5109 // FIXME this drops the error on the floor.
5110 consumeError(Err: std::move(Err));
5111 return true;
5112 }
5113 // Found it!
5114 return false;
5115 }
5116
5117 if (llvm::Error Err = Cursor.SkipBlock()) {
5118 // FIXME this drops the error on the floor.
5119 consumeError(Err: std::move(Err));
5120 return true;
5121 }
5122 }
5123 }
5124}
5125
5126ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
5127 ModuleKind Type,
5128 SourceLocation ImportLoc,
5129 unsigned ClientLoadCapabilities,
5130 ModuleFile **NewLoadedModuleFile) {
5131 llvm::TimeTraceScope scope("ReadAST", FileName);
5132
5133 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
5134 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
5135 CurrentDeserializingModuleKind, Type);
5136
5137 // Defer any pending actions until we get to the end of reading the AST file.
5138 Deserializing AnASTFile(this);
5139
5140 // Bump the generation number.
5141 unsigned PreviousGeneration = 0;
5142 if (ContextObj)
5143 PreviousGeneration = incrementGeneration(C&: *ContextObj);
5144
5145 unsigned NumModules = ModuleMgr.size();
5146 SmallVector<ImportedModule, 4> Loaded;
5147 if (ASTReadResult ReadResult =
5148 ReadASTCore(FileName, Type, ImportLoc,
5149 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
5150 ClientLoadCapabilities)) {
5151 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
5152
5153 // If we find that any modules are unusable, the global index is going
5154 // to be out-of-date. Just remove it.
5155 GlobalIndex.reset();
5156 ModuleMgr.setGlobalIndex(nullptr);
5157 return ReadResult;
5158 }
5159
5160 if (NewLoadedModuleFile && !Loaded.empty())
5161 *NewLoadedModuleFile = Loaded.back().Mod;
5162
5163 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
5164 // remove modules from this point. Various fields are updated during reading
5165 // the AST block and removing the modules would result in dangling pointers.
5166 // They are generally only incidentally dereferenced, ie. a binary search
5167 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
5168 // be dereferenced but it wouldn't actually be used.
5169
5170 // Load the AST blocks of all of the modules that we loaded. We can still
5171 // hit errors parsing the ASTs at this point.
5172 for (ImportedModule &M : Loaded) {
5173 ModuleFile &F = *M.Mod;
5174 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
5175
5176 // Read the AST block.
5177 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
5178 Error(Err: std::move(Err));
5179 return Failure;
5180 }
5181
5182 // The AST block should always have a definition for the main module.
5183 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5184 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5185 return Failure;
5186 }
5187
5188 // Read the extension blocks.
5189 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5190 if (llvm::Error Err = ReadExtensionBlock(F)) {
5191 Error(Err: std::move(Err));
5192 return Failure;
5193 }
5194 }
5195
5196 // Once read, set the ModuleFile bit base offset and update the size in
5197 // bits of all files we've seen.
5198 F.GlobalBitOffset = TotalModulesSizeInBits;
5199 TotalModulesSizeInBits += F.SizeInBits;
5200 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5201 }
5202
5203 // Preload source locations and interesting indentifiers.
5204 for (ImportedModule &M : Loaded) {
5205 ModuleFile &F = *M.Mod;
5206
5207 // Map the original source file ID into the ID space of the current
5208 // compilation.
5209 if (F.OriginalSourceFileID.isValid())
5210 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5211
5212 for (auto Offset : F.PreloadIdentifierOffsets) {
5213 const unsigned char *Data = F.IdentifierTableData + Offset;
5214
5215 ASTIdentifierLookupTrait Trait(*this, F);
5216 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5217 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5218
5219 IdentifierInfo *II;
5220 if (!PP.getLangOpts().CPlusPlus) {
5221 // Identifiers present in both the module file and the importing
5222 // instance are marked out-of-date so that they can be deserialized
5223 // on next use via ASTReader::updateOutOfDateIdentifier().
5224 // Identifiers present in the module file but not in the importing
5225 // instance are ignored for now, preventing growth of the identifier
5226 // table. They will be deserialized on first use via ASTReader::get().
5227 auto It = PP.getIdentifierTable().find(Name: Key);
5228 if (It == PP.getIdentifierTable().end())
5229 continue;
5230 II = It->second;
5231 } else {
5232 // With C++ modules, not many identifiers are considered interesting.
5233 // All identifiers in the module file can be placed into the identifier
5234 // table of the importing instance and marked as out-of-date. This makes
5235 // ASTReader::get() a no-op, and deserialization will take place on
5236 // first/next use via ASTReader::updateOutOfDateIdentifier().
5237 II = &PP.getIdentifierTable().getOwn(Name: Key);
5238 }
5239
5240 II->setOutOfDate(true);
5241
5242 // Mark this identifier as being from an AST file so that we can track
5243 // whether we need to serialize it.
5244 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5245
5246 // Associate the ID with the identifier so that the writer can reuse it.
5247 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5248 SetIdentifierInfo(ID, II);
5249 }
5250 }
5251
5252 // Builtins and library builtins have already been initialized. Mark all
5253 // identifiers as out-of-date, so that they are deserialized on first use.
5254 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5255 for (auto &Id : PP.getIdentifierTable())
5256 Id.second->setOutOfDate(true);
5257
5258 // Mark selectors as out of date.
5259 for (const auto &Sel : SelectorGeneration)
5260 SelectorOutOfDate[Sel.first] = true;
5261
5262 // Setup the import locations and notify the module manager that we've
5263 // committed to these module files.
5264 for (ImportedModule &M : Loaded) {
5265 ModuleFile &F = *M.Mod;
5266
5267 ModuleMgr.moduleFileAccepted(MF: &F);
5268
5269 // Set the import location.
5270 F.DirectImportLoc = ImportLoc;
5271 // FIXME: We assume that locations from PCH / preamble do not need
5272 // any translation.
5273 if (!M.ImportedBy)
5274 F.ImportLoc = M.ImportLoc;
5275 else
5276 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5277 }
5278
5279 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5280 // Might be unnecessary as use declarations are only used to build the
5281 // module itself.
5282
5283 if (ContextObj)
5284 InitializeContext();
5285
5286 if (SemaObj)
5287 UpdateSema();
5288
5289 if (DeserializationListener)
5290 DeserializationListener->ReaderInitialized(Reader: this);
5291
5292 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5293 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5294 // If this AST file is a precompiled preamble, then set the
5295 // preamble file ID of the source manager to the file source file
5296 // from which the preamble was built.
5297 if (Type == MK_Preamble) {
5298 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5299 } else if (Type == MK_MainFile) {
5300 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5301 }
5302 }
5303
5304 // For any Objective-C class definitions we have already loaded, make sure
5305 // that we load any additional categories.
5306 if (ContextObj) {
5307 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5308 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5309 D: ObjCClassesLoaded[I], PreviousGeneration);
5310 }
5311 }
5312
5313 const HeaderSearchOptions &HSOpts =
5314 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5315 if (HSOpts.ModulesValidateOncePerBuildSession) {
5316 // Now we are certain that the module and all modules it depends on are
5317 // up-to-date. For implicitly-built module files, ensure the corresponding
5318 // timestamp files are up-to-date in this build session.
5319 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5320 ImportedModule &M = Loaded[I];
5321 if (M.Mod->Kind == MK_ImplicitModule &&
5322 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5323 getModuleManager().getModuleCache().updateModuleTimestamp(
5324 ModuleFilename: M.Mod->FileName);
5325 }
5326 }
5327
5328 return Success;
5329}
5330
5331static ASTFileSignature readASTFileSignature(StringRef PCH);
5332
5333/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5334static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5335 // FIXME checking magic headers is done in other places such as
5336 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5337 // always done the same. Unify it all with a helper.
5338 if (!Stream.canSkipToPos(pos: 4))
5339 return llvm::createStringError(
5340 EC: std::errc::illegal_byte_sequence,
5341 Fmt: "file too small to contain precompiled file magic");
5342 for (unsigned C : {'C', 'P', 'C', 'H'})
5343 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5344 if (Res.get() != C)
5345 return llvm::createStringError(
5346 EC: std::errc::illegal_byte_sequence,
5347 Fmt: "file doesn't start with precompiled file magic");
5348 } else
5349 return Res.takeError();
5350 return llvm::Error::success();
5351}
5352
5353static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5354 switch (Kind) {
5355 case MK_PCH:
5356 return 0; // PCH
5357 case MK_ImplicitModule:
5358 case MK_ExplicitModule:
5359 case MK_PrebuiltModule:
5360 return 1; // module
5361 case MK_MainFile:
5362 case MK_Preamble:
5363 return 2; // main source file
5364 }
5365 llvm_unreachable("unknown module kind");
5366}
5367
5368ASTReader::ASTReadResult ASTReader::ReadASTCore(
5369 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5370 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5371 off_t ExpectedSize, time_t ExpectedModTime,
5372 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5373 auto Result = ModuleMgr.addModule(
5374 FileName, Type, ImportLoc, ImportedBy, Generation: getGeneration(), ExpectedSize,
5375 ExpectedModTime, ExpectedSignature, ReadSignature: readASTFileSignature);
5376 ModuleFile *M = Result.getModule();
5377
5378 switch (Result.getKind()) {
5379 case AddModuleResult::AlreadyLoaded: {
5380 Diag(DiagID: diag::remark_module_import)
5381 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5382 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5383 return Success;
5384 }
5385
5386 case AddModuleResult::NewlyLoaded:
5387 // Load module file below.
5388 break;
5389
5390 case AddModuleResult::Missing:
5391 // The module file was missing; if the client can handle that, return
5392 // it.
5393 if (ClientLoadCapabilities & ARR_Missing)
5394 return Missing;
5395
5396 // Otherwise, return an error.
5397 Diag(DiagID: diag::err_ast_file_not_found)
5398 << moduleKindForDiagnostic(Kind: Type) << FileName;
5399 if (!Result.getBufferError().empty())
5400 Diag(DiagID: diag::note_ast_file_buffer_failed) << Result.getBufferError();
5401 return Failure;
5402
5403 case AddModuleResult::OutOfDate:
5404 // We couldn't load the module file because it is out-of-date. If the
5405 // client can handle out-of-date, return it.
5406 if (ClientLoadCapabilities & ARR_OutOfDate)
5407 return OutOfDate;
5408
5409 // Otherwise, return an error.
5410 Diag(DiagID: diag::err_ast_file_out_of_date)
5411 << moduleKindForDiagnostic(Kind: Type) << FileName;
5412 for (const auto &C : Result.getChanges()) {
5413 Diag(DiagID: diag::note_fe_ast_file_modified)
5414 << C.Kind << (C.Old && C.New) << llvm::itostr(X: C.Old.value_or(u: 0))
5415 << llvm::itostr(X: C.New.value_or(u: 0));
5416 }
5417 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
5418 << Result.getValidationStatus();
5419 if (!Result.getSignatureError().empty())
5420 Diag(DiagID: diag::note_ast_file_signature_failed) << Result.getSignatureError();
5421 return Failure;
5422
5423 case AddModuleResult::None:
5424 llvm_unreachable("Unexpected value from adding module.");
5425 }
5426
5427 assert(M && "Missing module file");
5428
5429 bool ShouldFinalizePCM = false;
5430 llvm::scope_exit FinalizeOrDropPCM([&]() {
5431 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5432 if (ShouldFinalizePCM)
5433 MC.finalizePCM(Filename: FileName);
5434 else
5435 MC.tryToDropPCM(Filename: FileName);
5436 });
5437 ModuleFile &F = *M;
5438 BitstreamCursor &Stream = F.Stream;
5439 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5440 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5441
5442 // Sniff for the signature.
5443 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5444 Diag(DiagID: diag::err_ast_file_invalid)
5445 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5446 return Failure;
5447 }
5448
5449 // This is used for compatibility with older PCH formats.
5450 bool HaveReadControlBlock = false;
5451 while (true) {
5452 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5453 if (!MaybeEntry) {
5454 Error(Err: MaybeEntry.takeError());
5455 return Failure;
5456 }
5457 llvm::BitstreamEntry Entry = MaybeEntry.get();
5458
5459 switch (Entry.Kind) {
5460 case llvm::BitstreamEntry::Error:
5461 case llvm::BitstreamEntry::Record:
5462 case llvm::BitstreamEntry::EndBlock:
5463 Error(Msg: "invalid record at top-level of AST file");
5464 return Failure;
5465
5466 case llvm::BitstreamEntry::SubBlock:
5467 break;
5468 }
5469
5470 switch (Entry.ID) {
5471 case CONTROL_BLOCK_ID:
5472 HaveReadControlBlock = true;
5473 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5474 case Success:
5475 // Check that we didn't try to load a non-module AST file as a module.
5476 //
5477 // FIXME: Should we also perform the converse check? Loading a module as
5478 // a PCH file sort of works, but it's a bit wonky.
5479 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5480 Type == MK_PrebuiltModule) &&
5481 F.ModuleName.empty()) {
5482 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5483 if (Result != OutOfDate ||
5484 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5485 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5486 return Result;
5487 }
5488 break;
5489
5490 case Failure: return Failure;
5491 case Missing: return Missing;
5492 case OutOfDate: return OutOfDate;
5493 case VersionMismatch: return VersionMismatch;
5494 case ConfigurationMismatch: return ConfigurationMismatch;
5495 case HadErrors: return HadErrors;
5496 }
5497 break;
5498
5499 case AST_BLOCK_ID:
5500 if (!HaveReadControlBlock) {
5501 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5502 Diag(DiagID: diag::err_ast_file_version_too_old)
5503 << moduleKindForDiagnostic(Kind: Type) << FileName;
5504 return VersionMismatch;
5505 }
5506
5507 // Record that we've loaded this module.
5508 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5509 ShouldFinalizePCM = true;
5510 return Success;
5511
5512 default:
5513 if (llvm::Error Err = Stream.SkipBlock()) {
5514 Error(Err: std::move(Err));
5515 return Failure;
5516 }
5517 break;
5518 }
5519 }
5520
5521 llvm_unreachable("unexpected break; expected return");
5522}
5523
5524ASTReader::ASTReadResult
5525ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5526 unsigned ClientLoadCapabilities) {
5527 const HeaderSearchOptions &HSOpts =
5528 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5529 bool AllowCompatibleConfigurationMismatch =
5530 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5531 bool DisableValidation = shouldDisableValidationForFile(M: F);
5532
5533 ASTReadResult Result = readUnhashedControlBlockImpl(
5534 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5535 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5536 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5537
5538 // If F was directly imported by another module, it's implicitly validated by
5539 // the importing module.
5540 if (DisableValidation || WasImportedBy ||
5541 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5542 return Success;
5543
5544 if (Result == Failure) {
5545 Error(Msg: "malformed block record in AST file");
5546 return Failure;
5547 }
5548
5549 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5550 // If this module has already been finalized in the ModuleCache, we're stuck
5551 // with it; we can only load a single version of each module.
5552 //
5553 // This can happen when a module is imported in two contexts: in one, as a
5554 // user module; in another, as a system module (due to an import from
5555 // another module marked with the [system] flag). It usually indicates a
5556 // bug in the module map: this module should also be marked with [system].
5557 //
5558 // If -Wno-system-headers (the default), and the first import is as a
5559 // system module, then validation will fail during the as-user import,
5560 // since -Werror flags won't have been validated. However, it's reasonable
5561 // to treat this consistently as a system module.
5562 //
5563 // If -Wsystem-headers, the PCM on disk was built with
5564 // -Wno-system-headers, and the first import is as a user module, then
5565 // validation will fail during the as-system import since the PCM on disk
5566 // doesn't guarantee that -Werror was respected. However, the -Werror
5567 // flags were checked during the initial as-user import.
5568 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5569 Filename: F.FileName)) {
5570 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5571 return Success;
5572 }
5573 }
5574
5575 return Result;
5576}
5577
5578ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5579 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5580 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5581 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5582 // Initialize a stream.
5583 BitstreamCursor Stream(StreamData);
5584
5585 // Sniff for the signature.
5586 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5587 // FIXME this drops the error on the floor.
5588 consumeError(Err: std::move(Err));
5589 return Failure;
5590 }
5591
5592 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5593 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5594 return Failure;
5595
5596 // Read all of the records in the options block.
5597 RecordData Record;
5598 ASTReadResult Result = Success;
5599 while (true) {
5600 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5601 if (!MaybeEntry) {
5602 // FIXME this drops the error on the floor.
5603 consumeError(Err: MaybeEntry.takeError());
5604 return Failure;
5605 }
5606 llvm::BitstreamEntry Entry = MaybeEntry.get();
5607
5608 switch (Entry.Kind) {
5609 case llvm::BitstreamEntry::Error:
5610 case llvm::BitstreamEntry::SubBlock:
5611 return Failure;
5612
5613 case llvm::BitstreamEntry::EndBlock:
5614 return Result;
5615
5616 case llvm::BitstreamEntry::Record:
5617 // The interesting case.
5618 break;
5619 }
5620
5621 // Read and process a record.
5622 Record.clear();
5623 StringRef Blob;
5624 Expected<unsigned> MaybeRecordType =
5625 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5626 if (!MaybeRecordType) {
5627 // FIXME this drops the error.
5628 return Failure;
5629 }
5630 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5631 case SIGNATURE:
5632 if (F) {
5633 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5634 assert(F->Signature != ASTFileSignature::createDummy() &&
5635 "Dummy AST file signature not backpatched in ASTWriter.");
5636 }
5637 break;
5638 case AST_BLOCK_HASH:
5639 if (F) {
5640 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5641 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5642 "Dummy AST block hash not backpatched in ASTWriter.");
5643 }
5644 break;
5645 case DIAGNOSTIC_OPTIONS: {
5646 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5647 if (Listener && ValidateDiagnosticOptions &&
5648 !AllowCompatibleConfigurationMismatch &&
5649 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5650 Result = OutOfDate; // Don't return early. Read the signature.
5651 break;
5652 }
5653 case HEADER_SEARCH_PATHS: {
5654 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5655 if (Listener && !AllowCompatibleConfigurationMismatch &&
5656 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5657 Result = ConfigurationMismatch;
5658 break;
5659 }
5660 case DIAG_PRAGMA_MAPPINGS:
5661 if (!F)
5662 break;
5663 if (F->PragmaDiagMappings.empty())
5664 F->PragmaDiagMappings.swap(RHS&: Record);
5665 else
5666 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5667 From: Record.begin(), To: Record.end());
5668 break;
5669 case HEADER_SEARCH_ENTRY_USAGE:
5670 if (F)
5671 F->SearchPathUsage = ReadBitVector(Record, Blob);
5672 break;
5673 case VFS_USAGE:
5674 if (F)
5675 F->VFSUsage = ReadBitVector(Record, Blob);
5676 break;
5677 }
5678 }
5679}
5680
5681/// Parse a record and blob containing module file extension metadata.
5682static bool parseModuleFileExtensionMetadata(
5683 const SmallVectorImpl<uint64_t> &Record,
5684 StringRef Blob,
5685 ModuleFileExtensionMetadata &Metadata) {
5686 if (Record.size() < 4) return true;
5687
5688 Metadata.MajorVersion = Record[0];
5689 Metadata.MinorVersion = Record[1];
5690
5691 unsigned BlockNameLen = Record[2];
5692 unsigned UserInfoLen = Record[3];
5693
5694 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5695
5696 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5697 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5698 Blob.data() + BlockNameLen + UserInfoLen);
5699 return false;
5700}
5701
5702llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5703 BitstreamCursor &Stream = F.Stream;
5704
5705 RecordData Record;
5706 while (true) {
5707 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5708 if (!MaybeEntry)
5709 return MaybeEntry.takeError();
5710 llvm::BitstreamEntry Entry = MaybeEntry.get();
5711
5712 switch (Entry.Kind) {
5713 case llvm::BitstreamEntry::SubBlock:
5714 if (llvm::Error Err = Stream.SkipBlock())
5715 return Err;
5716 continue;
5717 case llvm::BitstreamEntry::EndBlock:
5718 return llvm::Error::success();
5719 case llvm::BitstreamEntry::Error:
5720 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5721 Fmt: "malformed block record in AST file");
5722 case llvm::BitstreamEntry::Record:
5723 break;
5724 }
5725
5726 Record.clear();
5727 StringRef Blob;
5728 Expected<unsigned> MaybeRecCode =
5729 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5730 if (!MaybeRecCode)
5731 return MaybeRecCode.takeError();
5732 switch (MaybeRecCode.get()) {
5733 case EXTENSION_METADATA: {
5734 ModuleFileExtensionMetadata Metadata;
5735 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5736 return llvm::createStringError(
5737 EC: std::errc::illegal_byte_sequence,
5738 Fmt: "malformed EXTENSION_METADATA in AST file");
5739
5740 // Find a module file extension with this block name.
5741 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5742 if (Known == ModuleFileExtensions.end()) break;
5743
5744 // Form a reader.
5745 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5746 Mod&: F, Stream)) {
5747 F.ExtensionReaders.push_back(x: std::move(Reader));
5748 }
5749
5750 break;
5751 }
5752 }
5753 }
5754
5755 llvm_unreachable("ReadExtensionBlock should return from while loop");
5756}
5757
5758void ASTReader::InitializeContext() {
5759 assert(ContextObj && "no context to initialize");
5760 ASTContext &Context = *ContextObj;
5761
5762 // If there's a listener, notify them that we "read" the translation unit.
5763 if (DeserializationListener)
5764 DeserializationListener->DeclRead(
5765 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5766 D: Context.getTranslationUnitDecl());
5767
5768 // FIXME: Find a better way to deal with collisions between these
5769 // built-in types. Right now, we just ignore the problem.
5770
5771 // Load the special types.
5772 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5773 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5774 if (!Context.CFConstantStringTypeDecl)
5775 Context.setCFConstantStringType(GetType(ID: String));
5776 }
5777
5778 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5779 QualType FileType = GetType(ID: File);
5780 if (FileType.isNull()) {
5781 Error(Msg: "FILE type is NULL");
5782 return;
5783 }
5784
5785 if (!Context.FILEDecl) {
5786 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5787 Context.setFILEDecl(Typedef->getDecl());
5788 else {
5789 const TagType *Tag = FileType->getAs<TagType>();
5790 if (!Tag) {
5791 Error(Msg: "Invalid FILE type in AST file");
5792 return;
5793 }
5794 Context.setFILEDecl(Tag->getDecl());
5795 }
5796 }
5797 }
5798
5799 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5800 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5801 if (Jmp_bufType.isNull()) {
5802 Error(Msg: "jmp_buf type is NULL");
5803 return;
5804 }
5805
5806 if (!Context.jmp_bufDecl) {
5807 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5808 Context.setjmp_bufDecl(Typedef->getDecl());
5809 else {
5810 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5811 if (!Tag) {
5812 Error(Msg: "Invalid jmp_buf type in AST file");
5813 return;
5814 }
5815 Context.setjmp_bufDecl(Tag->getDecl());
5816 }
5817 }
5818 }
5819
5820 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5821 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5822 if (Sigjmp_bufType.isNull()) {
5823 Error(Msg: "sigjmp_buf type is NULL");
5824 return;
5825 }
5826
5827 if (!Context.sigjmp_bufDecl) {
5828 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5829 Context.setsigjmp_bufDecl(Typedef->getDecl());
5830 else {
5831 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5832 assert(Tag && "Invalid sigjmp_buf type in AST file");
5833 Context.setsigjmp_bufDecl(Tag->getDecl());
5834 }
5835 }
5836 }
5837
5838 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5839 if (Context.ObjCIdRedefinitionType.isNull())
5840 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5841 }
5842
5843 if (TypeID ObjCClassRedef =
5844 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5845 if (Context.ObjCClassRedefinitionType.isNull())
5846 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5847 }
5848
5849 if (TypeID ObjCSelRedef =
5850 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5851 if (Context.ObjCSelRedefinitionType.isNull())
5852 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5853 }
5854
5855 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5856 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5857 if (Ucontext_tType.isNull()) {
5858 Error(Msg: "ucontext_t type is NULL");
5859 return;
5860 }
5861
5862 if (!Context.ucontext_tDecl) {
5863 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5864 Context.setucontext_tDecl(Typedef->getDecl());
5865 else {
5866 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5867 assert(Tag && "Invalid ucontext_t type in AST file");
5868 Context.setucontext_tDecl(Tag->getDecl());
5869 }
5870 }
5871 }
5872 }
5873
5874 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5875
5876 // If there were any CUDA special declarations, deserialize them.
5877 if (!CUDASpecialDeclRefs.empty()) {
5878 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5879 Context.setcudaConfigureCallDecl(
5880 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5881 Context.setcudaGetParameterBufferDecl(
5882 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5883 Context.setcudaLaunchDeviceDecl(
5884 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5885 }
5886
5887 // Re-export any modules that were imported by a non-module AST file.
5888 // FIXME: This does not make macro-only imports visible again.
5889 for (auto &Import : PendingImportedModules) {
5890 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5891 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5892 /*ImportLoc=*/Import.ImportLoc);
5893 if (Import.ImportLoc.isValid())
5894 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5895 // This updates visibility for Preprocessor only. For Sema, which can be
5896 // nullptr here, we do the same later, in UpdateSema().
5897 }
5898 }
5899
5900 // Hand off these modules to Sema.
5901 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5902 PendingImportedModules.clear();
5903}
5904
5905void ASTReader::finalizeForWriting() {
5906 // Nothing to do for now.
5907}
5908
5909/// Reads and return the signature record from \p PCH's control block, or
5910/// else returns 0.
5911static ASTFileSignature readASTFileSignature(StringRef PCH) {
5912 BitstreamCursor Stream(PCH);
5913 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5914 // FIXME this drops the error on the floor.
5915 consumeError(Err: std::move(Err));
5916 return ASTFileSignature();
5917 }
5918
5919 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5920 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5921 return ASTFileSignature();
5922
5923 // Scan for SIGNATURE inside the diagnostic options block.
5924 ASTReader::RecordData Record;
5925 while (true) {
5926 Expected<llvm::BitstreamEntry> MaybeEntry =
5927 Stream.advanceSkippingSubblocks();
5928 if (!MaybeEntry) {
5929 // FIXME this drops the error on the floor.
5930 consumeError(Err: MaybeEntry.takeError());
5931 return ASTFileSignature();
5932 }
5933 llvm::BitstreamEntry Entry = MaybeEntry.get();
5934
5935 if (Entry.Kind != llvm::BitstreamEntry::Record)
5936 return ASTFileSignature();
5937
5938 Record.clear();
5939 StringRef Blob;
5940 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5941 if (!MaybeRecord) {
5942 // FIXME this drops the error on the floor.
5943 consumeError(Err: MaybeRecord.takeError());
5944 return ASTFileSignature();
5945 }
5946 if (SIGNATURE == MaybeRecord.get()) {
5947 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5948 assert(Signature != ASTFileSignature::createDummy() &&
5949 "Dummy AST file signature not backpatched in ASTWriter.");
5950 return Signature;
5951 }
5952 }
5953}
5954
5955/// Retrieve the name of the original source file name
5956/// directly from the AST file, without actually loading the AST
5957/// file.
5958std::string ASTReader::getOriginalSourceFile(
5959 const std::string &ASTFileName, FileManager &FileMgr,
5960 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5961 // Open the AST file.
5962 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5963 /*RequiresNullTerminator=*/false,
5964 /*MaybeLimit=*/std::nullopt,
5965 /*IsText=*/false);
5966 if (!Buffer) {
5967 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5968 << ASTFileName << Buffer.getError().message();
5969 return std::string();
5970 }
5971
5972 // Initialize the stream
5973 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5974
5975 // Sniff for the signature.
5976 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5977 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5978 return std::string();
5979 }
5980
5981 // Scan for the CONTROL_BLOCK_ID block.
5982 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5983 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5984 return std::string();
5985 }
5986
5987 // Scan for ORIGINAL_FILE inside the control block.
5988 RecordData Record;
5989 while (true) {
5990 Expected<llvm::BitstreamEntry> MaybeEntry =
5991 Stream.advanceSkippingSubblocks();
5992 if (!MaybeEntry) {
5993 // FIXME this drops errors on the floor.
5994 consumeError(Err: MaybeEntry.takeError());
5995 return std::string();
5996 }
5997 llvm::BitstreamEntry Entry = MaybeEntry.get();
5998
5999 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
6000 return std::string();
6001
6002 if (Entry.Kind != llvm::BitstreamEntry::Record) {
6003 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
6004 return std::string();
6005 }
6006
6007 Record.clear();
6008 StringRef Blob;
6009 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6010 if (!MaybeRecord) {
6011 // FIXME this drops the errors on the floor.
6012 consumeError(Err: MaybeRecord.takeError());
6013 return std::string();
6014 }
6015 if (ORIGINAL_FILE == MaybeRecord.get())
6016 return Blob.str();
6017 }
6018}
6019
6020namespace {
6021
6022 class SimplePCHValidator : public ASTReaderListener {
6023 const LangOptions &ExistingLangOpts;
6024 const CodeGenOptions &ExistingCGOpts;
6025 const TargetOptions &ExistingTargetOpts;
6026 const PreprocessorOptions &ExistingPPOpts;
6027 const HeaderSearchOptions &ExistingHSOpts;
6028 std::string ExistingSpecificModuleCachePath;
6029 FileManager &FileMgr;
6030 bool StrictOptionMatches;
6031
6032 public:
6033 SimplePCHValidator(const LangOptions &ExistingLangOpts,
6034 const CodeGenOptions &ExistingCGOpts,
6035 const TargetOptions &ExistingTargetOpts,
6036 const PreprocessorOptions &ExistingPPOpts,
6037 const HeaderSearchOptions &ExistingHSOpts,
6038 StringRef ExistingSpecificModuleCachePath,
6039 FileManager &FileMgr, bool StrictOptionMatches)
6040 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
6041 ExistingTargetOpts(ExistingTargetOpts),
6042 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
6043 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
6044 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
6045
6046 bool ReadLanguageOptions(const LangOptions &LangOpts,
6047 StringRef ModuleFilename, bool Complain,
6048 bool AllowCompatibleDifferences) override {
6049 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
6050 Diags: nullptr, AllowCompatibleDifferences);
6051 }
6052
6053 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
6054 StringRef ModuleFilename, bool Complain,
6055 bool AllowCompatibleDifferences) override {
6056 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
6057 Diags: nullptr, AllowCompatibleDifferences);
6058 }
6059
6060 bool ReadTargetOptions(const TargetOptions &TargetOpts,
6061 StringRef ModuleFilename, bool Complain,
6062 bool AllowCompatibleDifferences) override {
6063 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
6064 Diags: nullptr, AllowCompatibleDifferences);
6065 }
6066
6067 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
6068 StringRef ASTFilename, StringRef ContextHash,
6069 bool Complain) override {
6070 return checkModuleCachePath(
6071 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
6072 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
6073 }
6074
6075 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
6076 StringRef ModuleFilename, bool ReadMacros,
6077 bool Complain,
6078 std::string &SuggestedPredefines) override {
6079 return checkPreprocessorOptions(
6080 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
6081 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
6082 Validation: StrictOptionMatches ? OptionValidateStrictMatches
6083 : OptionValidateContradictions);
6084 }
6085 };
6086
6087} // namespace
6088
6089bool ASTReader::readASTFileControlBlock(
6090 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6091 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
6092 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
6093 unsigned ClientLoadCapabilities) {
6094 // Open the AST file.
6095 off_t Size;
6096 time_t ModTime;
6097 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
6098 llvm::MemoryBuffer *Buffer =
6099 ModCache.getInMemoryModuleCache().lookupPCM(Filename, Size, ModTime);
6100 if (!Buffer) {
6101 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
6102 // read again later, but we do not have the context here to determine if it
6103 // is safe to change the result of InMemoryModuleCache::getPCMState().
6104
6105 // FIXME: This allows use of the VFS; we do not allow use of the
6106 // VFS when actually loading a module.
6107 auto Entry = Filename == "-" ? FileMgr.getSTDIN()
6108 : FileMgr.getFileRef(Filename,
6109 /*OpenFile=*/false,
6110 /*CacheFailure=*/true,
6111 /*IsText=*/false);
6112 if (!Entry) {
6113 llvm::consumeError(Err: Entry.takeError());
6114 return true;
6115 }
6116 auto BufferOrErr =
6117 FileMgr.getBufferForFile(Entry: *Entry,
6118 /*IsVolatile=*/isVolatile: false,
6119 /*RequiresNullTerminator=*/false,
6120 /*MaybeLimit=*/std::nullopt,
6121 /*IsText=*/false);
6122 if (!BufferOrErr)
6123 return true;
6124 OwnedBuffer = std::move(*BufferOrErr);
6125 Buffer = OwnedBuffer.get();
6126 }
6127
6128 // Initialize the stream
6129 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
6130 BitstreamCursor Stream(Bytes);
6131
6132 // Sniff for the signature.
6133 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
6134 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
6135 return true;
6136 }
6137
6138 // Scan for the CONTROL_BLOCK_ID block.
6139 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
6140 return true;
6141
6142 bool NeedsInputFiles = Listener.needsInputFileVisitation();
6143 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
6144 bool NeedsImports = Listener.needsImportVisitation();
6145 BitstreamCursor InputFilesCursor;
6146 uint64_t InputFilesOffsetBase = 0;
6147
6148 RecordData Record;
6149 std::string ModuleDir;
6150 bool DoneWithControlBlock = false;
6151 SmallString<0> PathBuf;
6152 PathBuf.reserve(N: 256);
6153 // Additional path buffer to use when multiple paths need to be resolved.
6154 // For example, when deserializing input files that contains a path that was
6155 // resolved from a vfs overlay and an external location.
6156 SmallString<0> AdditionalPathBuf;
6157 AdditionalPathBuf.reserve(N: 256);
6158 while (!DoneWithControlBlock) {
6159 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6160 if (!MaybeEntry) {
6161 // FIXME this drops the error on the floor.
6162 consumeError(Err: MaybeEntry.takeError());
6163 return true;
6164 }
6165 llvm::BitstreamEntry Entry = MaybeEntry.get();
6166
6167 switch (Entry.Kind) {
6168 case llvm::BitstreamEntry::SubBlock: {
6169 switch (Entry.ID) {
6170 case OPTIONS_BLOCK_ID: {
6171 std::string IgnoredSuggestedPredefines;
6172 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6173 /*AllowCompatibleConfigurationMismatch*/ false,
6174 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6175 return true;
6176 break;
6177 }
6178
6179 case INPUT_FILES_BLOCK_ID:
6180 InputFilesCursor = Stream;
6181 if (llvm::Error Err = Stream.SkipBlock()) {
6182 // FIXME this drops the error on the floor.
6183 consumeError(Err: std::move(Err));
6184 return true;
6185 }
6186 if (NeedsInputFiles &&
6187 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6188 return true;
6189 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6190 break;
6191
6192 default:
6193 if (llvm::Error Err = Stream.SkipBlock()) {
6194 // FIXME this drops the error on the floor.
6195 consumeError(Err: std::move(Err));
6196 return true;
6197 }
6198 break;
6199 }
6200
6201 continue;
6202 }
6203
6204 case llvm::BitstreamEntry::EndBlock:
6205 DoneWithControlBlock = true;
6206 break;
6207
6208 case llvm::BitstreamEntry::Error:
6209 return true;
6210
6211 case llvm::BitstreamEntry::Record:
6212 break;
6213 }
6214
6215 if (DoneWithControlBlock) break;
6216
6217 Record.clear();
6218 StringRef Blob;
6219 Expected<unsigned> MaybeRecCode =
6220 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6221 if (!MaybeRecCode) {
6222 // FIXME this drops the error.
6223 return Failure;
6224 }
6225 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6226 case METADATA:
6227 if (Record[0] != VERSION_MAJOR)
6228 return true;
6229 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6230 return true;
6231 break;
6232 case MODULE_NAME:
6233 Listener.ReadModuleName(ModuleName: Blob);
6234 break;
6235 case MODULE_DIRECTORY:
6236 ModuleDir = std::string(Blob);
6237 break;
6238 case MODULE_MAP_FILE: {
6239 unsigned Idx = 0;
6240 std::string PathStr = ReadString(Record, Idx);
6241 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6242 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6243 break;
6244 }
6245 case INPUT_FILE_OFFSETS: {
6246 if (!NeedsInputFiles)
6247 break;
6248
6249 unsigned NumInputFiles = Record[0];
6250 unsigned NumUserFiles = Record[1];
6251 const llvm::support::unaligned_uint64_t *InputFileOffs =
6252 (const llvm::support::unaligned_uint64_t *)Blob.data();
6253 for (unsigned I = 0; I != NumInputFiles; ++I) {
6254 // Go find this input file.
6255 bool isSystemFile = I >= NumUserFiles;
6256
6257 if (isSystemFile && !NeedsSystemInputFiles)
6258 break; // the rest are system input files
6259
6260 BitstreamCursor &Cursor = InputFilesCursor;
6261 SavedStreamPosition SavedPosition(Cursor);
6262 if (llvm::Error Err =
6263 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6264 // FIXME this drops errors on the floor.
6265 consumeError(Err: std::move(Err));
6266 }
6267
6268 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6269 if (!MaybeCode) {
6270 // FIXME this drops errors on the floor.
6271 consumeError(Err: MaybeCode.takeError());
6272 }
6273 unsigned Code = MaybeCode.get();
6274
6275 RecordData Record;
6276 StringRef Blob;
6277 bool shouldContinue = false;
6278 Expected<unsigned> MaybeRecordType =
6279 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6280 if (!MaybeRecordType) {
6281 // FIXME this drops errors on the floor.
6282 consumeError(Err: MaybeRecordType.takeError());
6283 }
6284 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6285 case INPUT_FILE_HASH:
6286 break;
6287 case INPUT_FILE:
6288 time_t StoredTime = static_cast<time_t>(Record[2]);
6289 bool Overridden = static_cast<bool>(Record[3]);
6290 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6291 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6292 auto FilenameAsRequestedBuf = ResolveImportedPath(
6293 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6294 StringRef Filename;
6295 if (UnresolvedFilename.empty())
6296 Filename = *FilenameAsRequestedBuf;
6297 else {
6298 auto FilenameBuf = ResolveImportedPath(
6299 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6300 Filename = *FilenameBuf;
6301 }
6302 shouldContinue = Listener.visitInputFileAsRequested(
6303 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6304 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6305 break;
6306 }
6307 if (!shouldContinue)
6308 break;
6309 }
6310 break;
6311 }
6312
6313 case IMPORT: {
6314 if (!NeedsImports)
6315 break;
6316
6317 unsigned Idx = 0;
6318 // Read information about the AST file.
6319
6320 // Skip Kind
6321 Idx++;
6322
6323 // Skip ImportLoc
6324 Idx++;
6325
6326 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6327
6328 bool IsStandardCXXModule = Record[Idx++];
6329
6330 // In C++20 Modules, we don't record the path to imported
6331 // modules in the BMI files.
6332 if (IsStandardCXXModule) {
6333 Listener.visitImport(ModuleName, /*Filename=*/"");
6334 continue;
6335 }
6336
6337 // Skip Size, ModTime and ImplicitModuleSuffix.
6338 Idx += 1 + 1 + 1;
6339 // Skip signature.
6340 Blob = Blob.substr(Start: ASTFileSignature::size);
6341
6342 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6343 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6344 Listener.visitImport(ModuleName, Filename: *Filename);
6345 break;
6346 }
6347
6348 default:
6349 // No other validation to perform.
6350 break;
6351 }
6352 }
6353
6354 // Look for module file extension blocks, if requested.
6355 if (FindModuleFileExtensions) {
6356 BitstreamCursor SavedStream = Stream;
6357 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6358 bool DoneWithExtensionBlock = false;
6359 while (!DoneWithExtensionBlock) {
6360 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6361 if (!MaybeEntry) {
6362 // FIXME this drops the error.
6363 return true;
6364 }
6365 llvm::BitstreamEntry Entry = MaybeEntry.get();
6366
6367 switch (Entry.Kind) {
6368 case llvm::BitstreamEntry::SubBlock:
6369 if (llvm::Error Err = Stream.SkipBlock()) {
6370 // FIXME this drops the error on the floor.
6371 consumeError(Err: std::move(Err));
6372 return true;
6373 }
6374 continue;
6375
6376 case llvm::BitstreamEntry::EndBlock:
6377 DoneWithExtensionBlock = true;
6378 continue;
6379
6380 case llvm::BitstreamEntry::Error:
6381 return true;
6382
6383 case llvm::BitstreamEntry::Record:
6384 break;
6385 }
6386
6387 Record.clear();
6388 StringRef Blob;
6389 Expected<unsigned> MaybeRecCode =
6390 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6391 if (!MaybeRecCode) {
6392 // FIXME this drops the error.
6393 return true;
6394 }
6395 switch (MaybeRecCode.get()) {
6396 case EXTENSION_METADATA: {
6397 ModuleFileExtensionMetadata Metadata;
6398 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6399 return true;
6400
6401 Listener.readModuleFileExtension(Metadata);
6402 break;
6403 }
6404 }
6405 }
6406 }
6407 Stream = std::move(SavedStream);
6408 }
6409
6410 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6411 if (readUnhashedControlBlockImpl(
6412 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6413 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6414 ValidateDiagnosticOptions) != Success)
6415 return true;
6416
6417 return false;
6418}
6419
6420bool ASTReader::isAcceptableASTFile(
6421 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6422 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6423 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6424 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6425 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6426 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6427 SpecificModuleCachePath, FileMgr,
6428 RequireStrictOptionMatches);
6429 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6430 /*FindModuleFileExtensions=*/false, Listener&: validator,
6431 /*ValidateDiagnosticOptions=*/true);
6432}
6433
6434Module *ASTReader::getSubmodule(uint32_t GlobalID) {
6435 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6436 assert(GlobalID == 0 && "Unhandled global submodule ID");
6437 return nullptr;
6438 }
6439
6440 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6441 if (GlobalIndex >= SubmodulesLoaded.size()) {
6442 Error(Msg: "submodule ID out of range in AST file");
6443 return nullptr;
6444 }
6445
6446 if (SubmodulesLoaded[GlobalIndex])
6447 return SubmodulesLoaded[GlobalIndex];
6448
6449 GlobalSubmoduleMapType::iterator It = GlobalSubmoduleMap.find(K: GlobalID);
6450 assert(It != GlobalSubmoduleMap.end());
6451 ModuleFile &F = *It->second;
6452 unsigned Index = GlobalID - F.BaseSubmoduleID - NUM_PREDEF_SUBMODULE_IDS;
6453 [[maybe_unused]] unsigned LocalID =
6454 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
6455
6456 BitstreamCursor &Cursor = F.SubmodulesCursor;
6457 SavedStreamPosition SavedPosition(Cursor);
6458 unsigned Offset = F.SubmoduleOffsets[Index];
6459 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.SubmodulesOffsetBase + Offset)) {
6460 Error(Err: std::move(Err));
6461 return nullptr;
6462 }
6463
6464 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6465 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6466 // If we don't know the top-level module, there's no point in doing qualified
6467 // lookup of its submodules; it won't find anything anywhere within this tree.
6468 // Let's skip that and avoid some string lookups.
6469 auto CreateModule = !KnowsTopLevelModule
6470 ? &ModuleMap::createModule
6471 : &ModuleMap::findOrCreateModuleFirst;
6472
6473 Module *CurrentModule = nullptr;
6474 RecordData Record;
6475 while (true) {
6476 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
6477 if (!MaybeEntry) {
6478 Error(Err: MaybeEntry.takeError());
6479 return nullptr;
6480 }
6481 llvm::BitstreamEntry Entry = MaybeEntry.get();
6482
6483 switch (Entry.Kind) {
6484 case llvm::BitstreamEntry::SubBlock:
6485 case llvm::BitstreamEntry::Error:
6486 case llvm::BitstreamEntry::EndBlock: {
6487 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6488 Fmt: "malformed block record in AST file"));
6489 return nullptr;
6490 }
6491 case llvm::BitstreamEntry::Record:
6492 // The interesting case.
6493 break;
6494 }
6495
6496 // Read a record.
6497 StringRef Blob;
6498 Record.clear();
6499 Expected<unsigned> MaybeKind = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6500 if (!MaybeKind) {
6501 Error(Err: MaybeKind.takeError());
6502 return nullptr;
6503 }
6504 auto Kind = static_cast<SubmoduleRecordTypes>(MaybeKind.get());
6505
6506 switch (Kind) {
6507 case SUBMODULE_END:
6508 if (!CurrentModule) {
6509 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6510 Fmt: "malformed module definition"));
6511 return nullptr;
6512 }
6513 return CurrentModule;
6514
6515 case SUBMODULE_DEFINITION: {
6516 if (Record.size() < 13) {
6517 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6518 Fmt: "malformed module definition"));
6519 return nullptr;
6520 }
6521
6522 StringRef Name = Blob;
6523 unsigned Idx = 0;
6524 [[maybe_unused]] unsigned ReadLocalID = Record[Idx++];
6525 assert(LocalID == ReadLocalID);
6526 assert(GlobalID == getGlobalSubmoduleID(F, ReadLocalID));
6527 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6528 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6529 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6530 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6531 bool IsFramework = Record[Idx++];
6532 bool IsExplicit = Record[Idx++];
6533 bool IsSystem = Record[Idx++];
6534 bool IsExternC = Record[Idx++];
6535 bool InferSubmodules = Record[Idx++];
6536 bool InferExplicitSubmodules = Record[Idx++];
6537 bool InferExportWildcard = Record[Idx++];
6538 bool ConfigMacrosExhaustive = Record[Idx++];
6539 bool ModuleMapIsPrivate = Record[Idx++];
6540 bool NamedModuleHasInit = Record[Idx++];
6541
6542 Module *ParentModule = nullptr;
6543 if (Parent) {
6544 ParentModule = getSubmodule(GlobalID: Parent);
6545 if (!ParentModule)
6546 return nullptr;
6547 }
6548
6549 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6550 args&: IsFramework, args&: IsExplicit);
6551
6552 if (!ParentModule) {
6553 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6554 CurrentModule->getASTFileKey()) {
6555 // Don't emit module relocation error if we have -fno-validate-pch
6556 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6557 DisableValidationForModuleKind::Module)) {
6558 assert(*CurFileKey != F.FileKey &&
6559 "ModuleManager did not de-duplicate");
6560
6561 Diag(DiagID: diag::err_module_file_conflict)
6562 << CurrentModule->getTopLevelModuleName()
6563 << *CurrentModule->getASTFileName() << F.FileName;
6564
6565 auto CurModMapFile =
6566 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6567 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6568 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6569 Diag(DiagID: diag::note_module_file_conflict)
6570 << CurModMapFile->getName() << ModMapFile->getName();
6571
6572 return nullptr;
6573 }
6574 }
6575
6576 F.DidReadTopLevelSubmodule = true;
6577 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6578 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6579 }
6580
6581 CurrentModule->Kind = Kind;
6582 // Note that we may be rewriting an existing location and it is important
6583 // to keep doing that. In particular, we would like to prefer a
6584 // `DefinitionLoc` loaded from the module file instead of the location
6585 // created in the current source manager, because it allows the new
6586 // location to be marked as "unaffecting" when writing and avoid creating
6587 // duplicate locations for the same module map file.
6588 CurrentModule->DefinitionLoc = DefinitionLoc;
6589 CurrentModule->Signature = F.Signature;
6590 CurrentModule->IsFromModuleFile = true;
6591 if (InferredAllowedBy.isValid())
6592 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6593 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6594 CurrentModule->IsExternC = IsExternC;
6595 CurrentModule->InferSubmodules = InferSubmodules;
6596 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6597 CurrentModule->InferExportWildcard = InferExportWildcard;
6598 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6599 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6600 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6601
6602 if (!ParentModule && !F.BaseDirectory.empty()) {
6603 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6604 CurrentModule->Directory = *Dir;
6605 } else if (ParentModule && ParentModule->Directory) {
6606 // Submodules inherit the directory from their parent.
6607 CurrentModule->Directory = ParentModule->Directory;
6608 }
6609
6610 if (DeserializationListener)
6611 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6612
6613 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6614
6615 // Clear out data that will be replaced by what is in the module file.
6616 CurrentModule->LinkLibraries.clear();
6617 CurrentModule->ConfigMacros.clear();
6618 CurrentModule->UnresolvedConflicts.clear();
6619 CurrentModule->Conflicts.clear();
6620
6621 // The module is available unless it's missing a requirement; relevant
6622 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6623 // Missing headers that were present when the module was built do not
6624 // make it unavailable -- if we got this far, this must be an explicitly
6625 // imported module file.
6626 CurrentModule->Requirements.clear();
6627 CurrentModule->MissingHeaders.clear();
6628 CurrentModule->IsUnimportable =
6629 ParentModule && ParentModule->IsUnimportable;
6630 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6631 break;
6632 }
6633
6634 case SUBMODULE_UMBRELLA_HEADER: {
6635 SmallString<128> RelativePathName;
6636 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6637 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6638 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6639 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6640 PathRelativeToRootModuleDirectory: RelativePathName);
6641 }
6642 // Note that it's too late at this point to return out of date if the
6643 // name from the PCM doesn't match up with the one in the module map,
6644 // but also quite unlikely since we will have already checked the
6645 // modification time and size of the module map file itself.
6646 }
6647 break;
6648 }
6649
6650 case SUBMODULE_HEADER:
6651 case SUBMODULE_EXCLUDED_HEADER:
6652 case SUBMODULE_PRIVATE_HEADER:
6653 // We lazily associate headers with their modules via the HeaderInfo table.
6654 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6655 // of complete filenames or remove it entirely.
6656 break;
6657
6658 case SUBMODULE_TEXTUAL_HEADER:
6659 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6660 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6661 // them here.
6662 break;
6663
6664 case SUBMODULE_TOPHEADER: {
6665 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6666 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6667 break;
6668 }
6669
6670 case SUBMODULE_UMBRELLA_DIR: {
6671 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6672 if (auto Umbrella =
6673 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6674 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6675 // FIXME: NameAsWritten
6676 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6677 }
6678 }
6679 break;
6680 }
6681
6682 case SUBMODULE_IMPORTS:
6683 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6684 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6685 CurrentModule->Imports.push_back(Elt: ModuleRef(this, GlobalID));
6686 }
6687 break;
6688
6689 case SUBMODULE_AFFECTING_MODULES:
6690 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6691 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6692 CurrentModule->AffectingClangModules.push_back(
6693 Elt: ModuleRef(this, GlobalID));
6694 }
6695 break;
6696
6697 case SUBMODULE_EXPORTS:
6698 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6699 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6700 bool IsWildcard = Record[Idx + 1];
6701 ModuleRef ExportedMod =
6702 GlobalID ? ModuleRef(this, GlobalID) : ModuleRef();
6703 if (ExportedMod || IsWildcard)
6704 CurrentModule->Exports.push_back(Elt: {ExportedMod, IsWildcard});
6705 }
6706
6707 // Once we've loaded the set of exports, there's no reason to keep
6708 // the parsed, unresolved exports around.
6709 CurrentModule->UnresolvedExports.clear();
6710 break;
6711
6712 case SUBMODULE_REQUIRES:
6713 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6714 Target: PP.getTargetInfo());
6715 break;
6716
6717 case SUBMODULE_LINK_LIBRARY:
6718 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6719 CurrentModule->LinkLibraries.push_back(
6720 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6721 break;
6722
6723 case SUBMODULE_CONFIG_MACRO:
6724 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6725 break;
6726
6727 case SUBMODULE_CONFLICT: {
6728 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6729 Module::Conflict Conflict;
6730 Conflict.Other = ModuleRef(this, GlobalID);
6731 Conflict.Message = Blob.str();
6732 CurrentModule->Conflicts.push_back(x: Conflict);
6733 break;
6734 }
6735
6736 case SUBMODULE_INITIALIZERS: {
6737 if (!ContextObj)
6738 break;
6739 // Standard C++ module has its own way to initialize variables.
6740 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6741 SmallVector<GlobalDeclID, 16> Inits;
6742 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6743 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6744 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6745 }
6746 break;
6747 }
6748
6749 case SUBMODULE_EXPORT_AS:
6750 CurrentModule->ExportAsModule = Blob.str();
6751 ModMap.addLinkAsDependency(Mod: CurrentModule);
6752 break;
6753
6754 case SUBMODULE_CHILD: {
6755 // Record a not-yet-loaded direct child for on-demand deserialization.
6756 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6757 CurrentModule->addSubmodule(Name: Blob, ExternalSource: this, SubmoduleID: GlobalID);
6758 break;
6759 }
6760 }
6761 }
6762}
6763
6764/// Parse the record that corresponds to a LangOptions data
6765/// structure.
6766///
6767/// This routine parses the language options from the AST file and then gives
6768/// them to the AST listener if one is set.
6769///
6770/// \returns true if the listener deems the file unacceptable, false otherwise.
6771bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6772 StringRef ModuleFilename, bool Complain,
6773 ASTReaderListener &Listener,
6774 bool AllowCompatibleDifferences) {
6775 LangOptions LangOpts;
6776 unsigned Idx = 0;
6777 LangOpts.LangStd = static_cast<LangStandard::Kind>(Record[Idx++]);
6778#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6779 LangOpts.Name = Record[Idx++];
6780#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6781 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6782#include "clang/Basic/LangOptions.def"
6783#define SANITIZER(NAME, ID) \
6784 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6785#include "clang/Basic/Sanitizers.def"
6786
6787 for (unsigned N = Record[Idx++]; N; --N)
6788 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6789
6790 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6791 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6792 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6793
6794 LangOpts.CurrentModule = ReadString(Record, Idx);
6795
6796 // Comment options.
6797 for (unsigned N = Record[Idx++]; N; --N) {
6798 LangOpts.CommentOpts.BlockCommandNames.push_back(
6799 x: ReadString(Record, Idx));
6800 }
6801 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6802
6803 // OpenMP offloading options.
6804 for (unsigned N = Record[Idx++]; N; --N) {
6805 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6806 }
6807
6808 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6809
6810 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6811 AllowCompatibleDifferences);
6812}
6813
6814bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6815 StringRef ModuleFilename, bool Complain,
6816 ASTReaderListener &Listener,
6817 bool AllowCompatibleDifferences) {
6818 unsigned Idx = 0;
6819 CodeGenOptions CGOpts;
6820 using CK = CodeGenOptions::CompatibilityKind;
6821#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6822 if constexpr (CK::Compatibility != CK::Benign) \
6823 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6824#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6825 if constexpr (CK::Compatibility != CK::Benign) \
6826 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6827#define DEBUGOPT(Name, Bits, Default, Compatibility)
6828#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6829#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6830#include "clang/Basic/CodeGenOptions.def"
6831
6832 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6833 AllowCompatibleDifferences);
6834}
6835
6836bool ASTReader::ParseTargetOptions(const RecordData &Record,
6837 StringRef ModuleFilename, bool Complain,
6838 ASTReaderListener &Listener,
6839 bool AllowCompatibleDifferences) {
6840 unsigned Idx = 0;
6841 TargetOptions TargetOpts;
6842 TargetOpts.Triple = ReadString(Record, Idx);
6843 TargetOpts.CPU = ReadString(Record, Idx);
6844 TargetOpts.TuneCPU = ReadString(Record, Idx);
6845 TargetOpts.ABI = ReadString(Record, Idx);
6846 for (unsigned N = Record[Idx++]; N; --N) {
6847 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6848 }
6849 for (unsigned N = Record[Idx++]; N; --N) {
6850 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6851 }
6852
6853 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6854 AllowCompatibleDifferences);
6855}
6856
6857bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6858 StringRef ModuleFilename, bool Complain,
6859 ASTReaderListener &Listener) {
6860 DiagnosticOptions DiagOpts;
6861 unsigned Idx = 0;
6862#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6863#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6864 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6865#include "clang/Basic/DiagnosticOptions.def"
6866
6867 for (unsigned N = Record[Idx++]; N; --N)
6868 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6869 for (unsigned N = Record[Idx++]; N; --N)
6870 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6871
6872 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6873}
6874
6875bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6876 ASTReaderListener &Listener) {
6877 FileSystemOptions FSOpts;
6878 unsigned Idx = 0;
6879 FSOpts.WorkingDir = ReadString(Record, Idx);
6880 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6881}
6882
6883bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6884 StringRef ModuleFilename,
6885 bool Complain,
6886 ASTReaderListener &Listener) {
6887 HeaderSearchOptions HSOpts;
6888 unsigned Idx = 0;
6889 HSOpts.Sysroot = ReadString(Record, Idx);
6890
6891 HSOpts.ResourceDir = ReadString(Record, Idx);
6892 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6893 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6894 HSOpts.DisableModuleHash = Record[Idx++];
6895 HSOpts.ImplicitModuleMaps = Record[Idx++];
6896 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6897 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6898 HSOpts.UseBuiltinIncludes = Record[Idx++];
6899 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6900 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6901 HSOpts.UseLibcxx = Record[Idx++];
6902 std::string ContextHash = ReadString(Record, Idx);
6903
6904 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6905 Complain);
6906}
6907
6908bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6909 ASTReaderListener &Listener) {
6910 HeaderSearchOptions HSOpts;
6911 unsigned Idx = 0;
6912
6913 // Include entries.
6914 for (unsigned N = Record[Idx++]; N; --N) {
6915 std::string Path = ReadString(Record, Idx);
6916 frontend::IncludeDirGroup Group
6917 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6918 bool IsFramework = Record[Idx++];
6919 bool IgnoreSysRoot = Record[Idx++];
6920 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6921 args&: IgnoreSysRoot);
6922 }
6923
6924 // System header prefixes.
6925 for (unsigned N = Record[Idx++]; N; --N) {
6926 std::string Prefix = ReadString(Record, Idx);
6927 bool IsSystemHeader = Record[Idx++];
6928 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6929 }
6930
6931 // VFS overlay files.
6932 for (unsigned N = Record[Idx++]; N; --N) {
6933 std::string VFSOverlayFile = ReadString(Record, Idx);
6934 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6935 }
6936
6937 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6938}
6939
6940bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6941 StringRef ModuleFilename,
6942 bool Complain,
6943 ASTReaderListener &Listener,
6944 std::string &SuggestedPredefines) {
6945 PreprocessorOptions PPOpts;
6946 unsigned Idx = 0;
6947
6948 // Macro definitions/undefs
6949 bool ReadMacros = Record[Idx++];
6950 if (ReadMacros) {
6951 for (unsigned N = Record[Idx++]; N; --N) {
6952 std::string Macro = ReadString(Record, Idx);
6953 bool IsUndef = Record[Idx++];
6954 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6955 }
6956 }
6957
6958 // Includes
6959 for (unsigned N = Record[Idx++]; N; --N) {
6960 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6961 }
6962
6963 // Macro Includes
6964 for (unsigned N = Record[Idx++]; N; --N) {
6965 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6966 }
6967
6968 PPOpts.UsePredefines = Record[Idx++];
6969 PPOpts.DetailedRecord = Record[Idx++];
6970 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6971 PPOpts.ObjCXXARCStandardLibrary =
6972 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6973 SuggestedPredefines.clear();
6974 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6975 Complain, SuggestedPredefines);
6976}
6977
6978std::pair<ModuleFile *, unsigned>
6979ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6980 GlobalPreprocessedEntityMapType::iterator
6981 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6982 assert(I != GlobalPreprocessedEntityMap.end() &&
6983 "Corrupted global preprocessed entity map");
6984 ModuleFile *M = I->second;
6985 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6986 return std::make_pair(x&: M, y&: LocalIndex);
6987}
6988
6989llvm::iterator_range<PreprocessingRecord::iterator>
6990ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6991 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6992 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6993 count: Mod.NumPreprocessedEntities);
6994
6995 return llvm::make_range(x: PreprocessingRecord::iterator(),
6996 y: PreprocessingRecord::iterator());
6997}
6998
6999bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
7000 unsigned int ClientLoadCapabilities) {
7001 return ClientLoadCapabilities & ARR_OutOfDate &&
7002 !getModuleManager()
7003 .getModuleCache()
7004 .getInMemoryModuleCache()
7005 .isPCMFinal(Filename: ModuleFileName);
7006}
7007
7008llvm::iterator_range<ASTReader::ModuleDeclIterator>
7009ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
7010 return llvm::make_range(
7011 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
7012 y: ModuleDeclIterator(this, &Mod,
7013 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
7014}
7015
7016SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
7017 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
7018 assert(I != GlobalSkippedRangeMap.end() &&
7019 "Corrupted global skipped range map");
7020 ModuleFile *M = I->second;
7021 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
7022 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
7023 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
7024 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
7025 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
7026 assert(Range.isValid());
7027 return Range;
7028}
7029
7030unsigned
7031ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
7032 unsigned ModuleFileIndex = ID >> 32;
7033 assert(ModuleFileIndex && "not translating loaded MacroID?");
7034 assert(getModuleManager().size() > ModuleFileIndex - 1);
7035 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
7036
7037 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
7038 return MF.BasePreprocessedEntityID + ID;
7039}
7040
7041PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
7042 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7043 ModuleFile &M = *PPInfo.first;
7044 unsigned LocalIndex = PPInfo.second;
7045 PreprocessedEntityID PPID =
7046 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
7047 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7048
7049 if (!PP.getPreprocessingRecord()) {
7050 Error(Msg: "no preprocessing record");
7051 return nullptr;
7052 }
7053
7054 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
7055 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
7056 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
7057 Error(Err: std::move(Err));
7058 return nullptr;
7059 }
7060
7061 Expected<llvm::BitstreamEntry> MaybeEntry =
7062 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
7063 if (!MaybeEntry) {
7064 Error(Err: MaybeEntry.takeError());
7065 return nullptr;
7066 }
7067 llvm::BitstreamEntry Entry = MaybeEntry.get();
7068
7069 if (Entry.Kind != llvm::BitstreamEntry::Record)
7070 return nullptr;
7071
7072 // Read the record.
7073 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
7074 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
7075 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
7076 StringRef Blob;
7077 RecordData Record;
7078 Expected<unsigned> MaybeRecType =
7079 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
7080 if (!MaybeRecType) {
7081 Error(Err: MaybeRecType.takeError());
7082 return nullptr;
7083 }
7084 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
7085 case PPD_MACRO_EXPANSION: {
7086 bool isBuiltin = Record[0];
7087 IdentifierInfo *Name = nullptr;
7088 MacroDefinitionRecord *Def = nullptr;
7089 if (isBuiltin)
7090 Name = getLocalIdentifier(M, LocalID: Record[1]);
7091 else {
7092 PreprocessedEntityID GlobalID =
7093 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
7094 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
7095 Def =
7096 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
7097 }
7098
7099 MacroExpansion *ME;
7100 if (isBuiltin)
7101 ME = new (PPRec) MacroExpansion(Name, Range);
7102 else
7103 ME = new (PPRec) MacroExpansion(Def, Range);
7104
7105 return ME;
7106 }
7107
7108 case PPD_MACRO_DEFINITION: {
7109 // Decode the identifier info and then check again; if the macro is
7110 // still defined and associated with the identifier,
7111 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
7112 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
7113
7114 if (DeserializationListener)
7115 DeserializationListener->MacroDefinitionRead(PPID, MD);
7116
7117 return MD;
7118 }
7119
7120 case PPD_INCLUSION_DIRECTIVE: {
7121 const char *FullFileNameStart = Blob.data() + Record[0];
7122 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
7123 OptionalFileEntryRef File;
7124 if (!FullFileName.empty())
7125 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
7126
7127 // FIXME: Stable encoding
7128 InclusionDirective::InclusionKind Kind
7129 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
7130 InclusionDirective *ID
7131 = new (PPRec) InclusionDirective(PPRec, Kind,
7132 StringRef(Blob.data(), Record[0]),
7133 Record[1], Record[3],
7134 File,
7135 Range);
7136 return ID;
7137 }
7138 }
7139
7140 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
7141}
7142
7143/// Find the next module that contains entities and return the ID
7144/// of the first entry.
7145///
7146/// \param SLocMapI points at a chunk of a module that contains no
7147/// preprocessed entities or the entities it contains are not the ones we are
7148/// looking for.
7149unsigned ASTReader::findNextPreprocessedEntity(
7150 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
7151 ++SLocMapI;
7152 for (GlobalSLocOffsetMapType::const_iterator
7153 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
7154 ModuleFile &M = *SLocMapI->second;
7155 if (M.NumPreprocessedEntities)
7156 return M.BasePreprocessedEntityID;
7157 }
7158
7159 return getTotalNumPreprocessedEntities();
7160}
7161
7162namespace {
7163
7164struct PPEntityComp {
7165 const ASTReader &Reader;
7166 ModuleFile &M;
7167
7168 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7169
7170 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7171 SourceLocation LHS = getLoc(PPE: L);
7172 SourceLocation RHS = getLoc(PPE: R);
7173 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7174 }
7175
7176 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7177 SourceLocation LHS = getLoc(PPE: L);
7178 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7179 }
7180
7181 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7182 SourceLocation RHS = getLoc(PPE: R);
7183 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7184 }
7185
7186 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7187 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7188 }
7189};
7190
7191} // namespace
7192
7193unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7194 bool EndsAfter) const {
7195 if (SourceMgr.isLocalSourceLocation(Loc))
7196 return getTotalNumPreprocessedEntities();
7197
7198 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7199 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7200 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7201 "Corrupted global sloc offset map");
7202
7203 if (SLocMapI->second->NumPreprocessedEntities == 0)
7204 return findNextPreprocessedEntity(SLocMapI);
7205
7206 ModuleFile &M = *SLocMapI->second;
7207
7208 using pp_iterator = const PPEntityOffset *;
7209
7210 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7211 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7212
7213 size_t Count = M.NumPreprocessedEntities;
7214 size_t Half;
7215 pp_iterator First = pp_begin;
7216 pp_iterator PPI;
7217
7218 if (EndsAfter) {
7219 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7220 comp: PPEntityComp(*this, M));
7221 } else {
7222 // Do a binary search manually instead of using std::lower_bound because
7223 // The end locations of entities may be unordered (when a macro expansion
7224 // is inside another macro argument), but for this case it is not important
7225 // whether we get the first macro expansion or its containing macro.
7226 while (Count > 0) {
7227 Half = Count / 2;
7228 PPI = First;
7229 std::advance(i&: PPI, n: Half);
7230 if (SourceMgr.isBeforeInTranslationUnit(
7231 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7232 First = PPI;
7233 ++First;
7234 Count = Count - Half - 1;
7235 } else
7236 Count = Half;
7237 }
7238 }
7239
7240 if (PPI == pp_end)
7241 return findNextPreprocessedEntity(SLocMapI);
7242
7243 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7244}
7245
7246/// Returns a pair of [Begin, End) indices of preallocated
7247/// preprocessed entities that \arg Range encompasses.
7248std::pair<unsigned, unsigned>
7249 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7250 if (Range.isInvalid())
7251 return std::make_pair(x: 0,y: 0);
7252 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7253
7254 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7255 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7256 return std::make_pair(x&: BeginID, y&: EndID);
7257}
7258
7259/// Optionally returns true or false if the preallocated preprocessed
7260/// entity with index \arg Index came from file \arg FID.
7261std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7262 FileID FID) {
7263 if (FID.isInvalid())
7264 return false;
7265
7266 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7267 ModuleFile &M = *PPInfo.first;
7268 unsigned LocalIndex = PPInfo.second;
7269 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7270
7271 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7272 if (Loc.isInvalid())
7273 return false;
7274
7275 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7276 return true;
7277 else
7278 return false;
7279}
7280
7281namespace {
7282
7283 /// Visitor used to search for information about a header file.
7284 class HeaderFileInfoVisitor {
7285 FileEntryRef FE;
7286 std::optional<HeaderFileInfo> HFI;
7287
7288 public:
7289 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7290
7291 bool operator()(ModuleFile &M) {
7292 HeaderFileInfoLookupTable *Table
7293 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7294 if (!Table)
7295 return false;
7296
7297 // Look in the on-disk hash table for an entry for this file name.
7298 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7299 if (Pos == Table->end())
7300 return false;
7301
7302 HFI = *Pos;
7303 return true;
7304 }
7305
7306 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7307 };
7308
7309} // namespace
7310
7311HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7312 HeaderFileInfoVisitor Visitor(FE);
7313 ModuleMgr.visit(Visitor);
7314 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7315 return *HFI;
7316
7317 return HeaderFileInfo();
7318}
7319
7320void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7321 using DiagState = DiagnosticsEngine::DiagState;
7322 SmallVector<DiagState *, 32> DiagStates;
7323
7324 for (ModuleFile &F : ModuleMgr) {
7325 unsigned Idx = 0;
7326 auto &Record = F.PragmaDiagMappings;
7327 if (Record.empty())
7328 continue;
7329
7330 DiagStates.clear();
7331
7332 auto ReadDiagState = [&](const DiagState &BasedOn,
7333 bool IncludeNonPragmaStates) {
7334 unsigned BackrefID = Record[Idx++];
7335 if (BackrefID != 0)
7336 return DiagStates[BackrefID - 1];
7337
7338 // A new DiagState was created here.
7339 Diag.DiagStates.push_back(x: BasedOn);
7340 DiagState *NewState = &Diag.DiagStates.back();
7341 DiagStates.push_back(Elt: NewState);
7342 unsigned Size = Record[Idx++];
7343 assert(Idx + Size * 2 <= Record.size() &&
7344 "Invalid data, not enough diag/map pairs");
7345 while (Size--) {
7346 unsigned DiagID = Record[Idx++];
7347 DiagnosticMapping NewMapping =
7348 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7349 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7350 continue;
7351
7352 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7353
7354 // If this mapping was specified as a warning but the severity was
7355 // upgraded due to diagnostic settings, simulate the current diagnostic
7356 // settings (and use a warning).
7357 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7358 NewMapping.setSeverity(diag::Severity::Warning);
7359 NewMapping.setUpgradedFromWarning(false);
7360 }
7361
7362 Mapping = NewMapping;
7363 }
7364 return NewState;
7365 };
7366
7367 // Read the first state.
7368 DiagState *FirstState;
7369 if (F.Kind == MK_ImplicitModule) {
7370 // Implicitly-built modules are reused with different diagnostic
7371 // settings. Use the initial diagnostic state from Diag to simulate this
7372 // compilation's diagnostic settings.
7373 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7374 DiagStates.push_back(Elt: FirstState);
7375
7376 // Skip the initial diagnostic state from the serialized module.
7377 assert(Record[1] == 0 &&
7378 "Invalid data, unexpected backref in initial state");
7379 Idx = 3 + Record[2] * 2;
7380 assert(Idx < Record.size() &&
7381 "Invalid data, not enough state change pairs in initial state");
7382 } else if (F.isModule()) {
7383 // For an explicit module, preserve the flags from the module build
7384 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7385 // -Wblah flags.
7386 unsigned Flags = Record[Idx++];
7387 DiagState Initial(*Diag.getDiagnosticIDs());
7388 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7389 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7390 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7391 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7392 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7393 Initial.ExtBehavior = (diag::Severity)Flags;
7394 FirstState = ReadDiagState(Initial, true);
7395
7396 assert(F.OriginalSourceFileID.isValid());
7397
7398 // Set up the root buffer of the module to start with the initial
7399 // diagnostic state of the module itself, to cover files that contain no
7400 // explicit transitions (for which we did not serialize anything).
7401 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7402 .StateTransitions.push_back(Elt: {FirstState, 0});
7403 } else {
7404 // For prefix ASTs, start with whatever the user configured on the
7405 // command line.
7406 Idx++; // Skip flags.
7407 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7408 }
7409
7410 // Read the state transitions.
7411 unsigned NumLocations = Record[Idx++];
7412 while (NumLocations--) {
7413 assert(Idx < Record.size() &&
7414 "Invalid data, missing pragma diagnostic states");
7415 FileID FID = ReadFileID(F, Record, Idx);
7416 assert(FID.isValid() && "invalid FileID for transition");
7417 unsigned Transitions = Record[Idx++];
7418
7419 // Note that we don't need to set up Parent/ParentOffset here, because
7420 // we won't be changing the diagnostic state within imported FileIDs
7421 // (other than perhaps appending to the main source file, which has no
7422 // parent).
7423 auto &F = Diag.DiagStatesByLoc.Files[FID];
7424 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7425 for (unsigned I = 0; I != Transitions; ++I) {
7426 unsigned Offset = Record[Idx++];
7427 auto *State = ReadDiagState(*FirstState, false);
7428 F.StateTransitions.push_back(Elt: {State, Offset});
7429 }
7430 }
7431
7432 // Read the final state.
7433 assert(Idx < Record.size() &&
7434 "Invalid data, missing final pragma diagnostic state");
7435 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7436 auto *CurState = ReadDiagState(*FirstState, false);
7437
7438 if (!F.isModule()) {
7439 Diag.DiagStatesByLoc.CurDiagState = CurState;
7440 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7441
7442 // Preserve the property that the imaginary root file describes the
7443 // current state.
7444 FileID NullFile;
7445 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7446 if (T.empty())
7447 T.push_back(Elt: {CurState, 0});
7448 else
7449 T[0].State = CurState;
7450 }
7451
7452 // Restore the push stack so that unmatched pushes from a preamble are
7453 // visible when the main file is parsed, allowing the corresponding
7454 // `#pragma diagnostic pop` to succeed.
7455 assert(Idx < Record.size() &&
7456 "Invalid data, missing diagnostic push stack");
7457 unsigned NumPushes = Record[Idx++];
7458 for (unsigned I = 0; I != NumPushes; ++I) {
7459 auto *State = ReadDiagState(*FirstState, false);
7460 if (!F.isModule())
7461 Diag.DiagStateOnPushStack.push_back(x: State);
7462 }
7463
7464 // Don't try to read these mappings again.
7465 Record.clear();
7466 }
7467}
7468
7469/// Get the correct cursor and offset for loading a type.
7470ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7471 auto [M, Index] = translateTypeIDToIndex(ID);
7472 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7473 M->DeclsBlockStartOffset);
7474}
7475
7476static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7477 switch (code) {
7478#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7479 case TYPE_##CODE_ID: return Type::CLASS_ID;
7480#include "clang/Serialization/TypeBitCodes.def"
7481 default:
7482 return std::nullopt;
7483 }
7484}
7485
7486/// Read and return the type with the given index..
7487///
7488/// The index is the type ID, shifted and minus the number of predefs. This
7489/// routine actually reads the record corresponding to the type at the given
7490/// location. It is a helper routine for GetType, which deals with reading type
7491/// IDs.
7492QualType ASTReader::readTypeRecord(TypeID ID) {
7493 assert(ContextObj && "reading type with no AST context");
7494 ASTContext &Context = *ContextObj;
7495 RecordLocation Loc = TypeCursorForIndex(ID);
7496 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7497
7498 // Keep track of where we are in the stream, then jump back there
7499 // after reading this type.
7500 SavedStreamPosition SavedPosition(DeclsCursor);
7501
7502 ReadingKindTracker ReadingKind(Read_Type, *this);
7503
7504 // Note that we are loading a type record.
7505 Deserializing AType(this);
7506
7507 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7508 Error(Err: std::move(Err));
7509 return QualType();
7510 }
7511 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7512 if (!RawCode) {
7513 Error(Err: RawCode.takeError());
7514 return QualType();
7515 }
7516
7517 ASTRecordReader Record(*this, *Loc.F);
7518 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7519 if (!Code) {
7520 Error(Err: Code.takeError());
7521 return QualType();
7522 }
7523 if (Code.get() == TYPE_EXT_QUAL) {
7524 QualType baseType = Record.readQualType();
7525 Qualifiers quals = Record.readQualifiers();
7526 return Context.getQualifiedType(T: baseType, Qs: quals);
7527 }
7528
7529 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7530 if (!maybeClass) {
7531 Error(Msg: "Unexpected code for type");
7532 return QualType();
7533 }
7534
7535 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7536 return TypeReader.read(kind: *maybeClass);
7537}
7538
7539namespace clang {
7540
7541class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7542 ASTRecordReader &Reader;
7543
7544 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7545 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7546
7547 TypeSourceInfo *GetTypeSourceInfo() {
7548 return Reader.readTypeSourceInfo();
7549 }
7550
7551 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7552 return Reader.readNestedNameSpecifierLoc();
7553 }
7554
7555 Attr *ReadAttr() {
7556 return Reader.readAttr();
7557 }
7558
7559public:
7560 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7561
7562 // We want compile-time assurance that we've enumerated all of
7563 // these, so unfortunately we have to declare them first, then
7564 // define them out-of-line.
7565#define ABSTRACT_TYPELOC(CLASS, PARENT)
7566#define TYPELOC(CLASS, PARENT) \
7567 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7568#include "clang/AST/TypeLocNodes.def"
7569
7570 void VisitFunctionTypeLoc(FunctionTypeLoc);
7571 void VisitArrayTypeLoc(ArrayTypeLoc);
7572 void VisitTagTypeLoc(TagTypeLoc TL);
7573};
7574
7575} // namespace clang
7576
7577void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7578 // nothing to do
7579}
7580
7581void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7582 TL.setBuiltinLoc(readSourceLocation());
7583 if (TL.needsExtraLocalData()) {
7584 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7585 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7586 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7587 TL.setModeAttr(Reader.readInt());
7588 }
7589}
7590
7591void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7592 TL.setNameLoc(readSourceLocation());
7593}
7594
7595void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7596 TL.setStarLoc(readSourceLocation());
7597}
7598
7599void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7600 // nothing to do
7601}
7602
7603void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7604 // nothing to do
7605}
7606
7607void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7608 // nothing to do
7609}
7610
7611void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7612 TL.setExpansionLoc(readSourceLocation());
7613}
7614
7615void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7616 TL.setCaretLoc(readSourceLocation());
7617}
7618
7619void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7620 TL.setAmpLoc(readSourceLocation());
7621}
7622
7623void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7624 TL.setAmpAmpLoc(readSourceLocation());
7625}
7626
7627void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7628 TL.setStarLoc(readSourceLocation());
7629 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7630}
7631
7632void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7633 TL.setLBracketLoc(readSourceLocation());
7634 TL.setRBracketLoc(readSourceLocation());
7635 if (Reader.readBool())
7636 TL.setSizeExpr(Reader.readExpr());
7637 else
7638 TL.setSizeExpr(nullptr);
7639}
7640
7641void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7642 VisitArrayTypeLoc(TL);
7643}
7644
7645void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7646 VisitArrayTypeLoc(TL);
7647}
7648
7649void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7650 VisitArrayTypeLoc(TL);
7651}
7652
7653void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7654 DependentSizedArrayTypeLoc TL) {
7655 VisitArrayTypeLoc(TL);
7656}
7657
7658void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7659 DependentAddressSpaceTypeLoc TL) {
7660
7661 TL.setAttrNameLoc(readSourceLocation());
7662 TL.setAttrOperandParensRange(readSourceRange());
7663 TL.setAttrExprOperand(Reader.readExpr());
7664}
7665
7666void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7667 DependentSizedExtVectorTypeLoc TL) {
7668 TL.setNameLoc(readSourceLocation());
7669}
7670
7671void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7672 TL.setNameLoc(readSourceLocation());
7673}
7674
7675void TypeLocReader::VisitDependentVectorTypeLoc(
7676 DependentVectorTypeLoc TL) {
7677 TL.setNameLoc(readSourceLocation());
7678}
7679
7680void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7681 TL.setNameLoc(readSourceLocation());
7682}
7683
7684void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7685 TL.setAttrNameLoc(readSourceLocation());
7686 TL.setAttrOperandParensRange(readSourceRange());
7687 TL.setAttrRowOperand(Reader.readExpr());
7688 TL.setAttrColumnOperand(Reader.readExpr());
7689}
7690
7691void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7692 DependentSizedMatrixTypeLoc TL) {
7693 TL.setAttrNameLoc(readSourceLocation());
7694 TL.setAttrOperandParensRange(readSourceRange());
7695 TL.setAttrRowOperand(Reader.readExpr());
7696 TL.setAttrColumnOperand(Reader.readExpr());
7697}
7698
7699void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7700 TL.setLocalRangeBegin(readSourceLocation());
7701 TL.setLParenLoc(readSourceLocation());
7702 TL.setRParenLoc(readSourceLocation());
7703 TL.setExceptionSpecRange(readSourceRange());
7704 TL.setLocalRangeEnd(readSourceLocation());
7705 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7706 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7707 }
7708}
7709
7710void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7711 VisitFunctionTypeLoc(TL);
7712}
7713
7714void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7715 VisitFunctionTypeLoc(TL);
7716}
7717
7718void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7719 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7720 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7721 SourceLocation NameLoc = readSourceLocation();
7722 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7723}
7724
7725void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7726 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7727 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7728 SourceLocation NameLoc = readSourceLocation();
7729 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7730}
7731
7732void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7733 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7734 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7735 SourceLocation NameLoc = readSourceLocation();
7736 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7737}
7738
7739void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7740 TL.setTypeofLoc(readSourceLocation());
7741 TL.setLParenLoc(readSourceLocation());
7742 TL.setRParenLoc(readSourceLocation());
7743}
7744
7745void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7746 TL.setTypeofLoc(readSourceLocation());
7747 TL.setLParenLoc(readSourceLocation());
7748 TL.setRParenLoc(readSourceLocation());
7749 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7750}
7751
7752void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7753 TL.setDecltypeLoc(readSourceLocation());
7754 TL.setRParenLoc(readSourceLocation());
7755}
7756
7757void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7758 TL.setEllipsisLoc(readSourceLocation());
7759}
7760
7761void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7762 TL.setKWLoc(readSourceLocation());
7763 TL.setLParenLoc(readSourceLocation());
7764 TL.setRParenLoc(readSourceLocation());
7765 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7766}
7767
7768ConceptReference *ASTRecordReader::readConceptReference() {
7769 auto NNS = readNestedNameSpecifierLoc();
7770 auto TemplateKWLoc = readSourceLocation();
7771 auto ConceptNameLoc = readDeclarationNameInfo();
7772 auto FoundDecl = readDeclAs<NamedDecl>();
7773 auto NamedConcept = readTemplateName();
7774 auto *CR = ConceptReference::Create(
7775 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7776 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7777 return CR;
7778}
7779
7780void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7781 TL.setNameLoc(readSourceLocation());
7782 if (Reader.readBool())
7783 TL.setConceptReference(Reader.readConceptReference());
7784 if (Reader.readBool())
7785 TL.setRParenLoc(readSourceLocation());
7786}
7787
7788void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7789 DeducedTemplateSpecializationTypeLoc TL) {
7790 TL.setElaboratedKeywordLoc(readSourceLocation());
7791 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7792 TL.setTemplateNameLoc(readSourceLocation());
7793}
7794
7795void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7796 TL.setElaboratedKeywordLoc(readSourceLocation());
7797 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7798 TL.setNameLoc(readSourceLocation());
7799}
7800
7801void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7802 VisitTagTypeLoc(TL);
7803}
7804
7805void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7806 VisitTagTypeLoc(TL);
7807}
7808
7809void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7810
7811void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7812 TL.setAttr(ReadAttr());
7813}
7814
7815void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7816 // Nothing to do
7817}
7818
7819void TypeLocReader::VisitLateParsedAttrTypeLoc(LateParsedAttrTypeLoc TL) {
7820 llvm_unreachable(
7821 "should be replaced with a concrete type before serialization");
7822}
7823
7824void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7825 // Nothing to do.
7826}
7827
7828void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7829 TL.setAttrLoc(readSourceLocation());
7830}
7831
7832void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7833 HLSLAttributedResourceTypeLoc TL) {
7834 // Nothing to do.
7835}
7836
7837void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7838 // Nothing to do.
7839}
7840
7841void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7842 TL.setNameLoc(readSourceLocation());
7843}
7844
7845void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7846 SubstTemplateTypeParmTypeLoc TL) {
7847 TL.setNameLoc(readSourceLocation());
7848}
7849
7850void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7851 SubstTemplateTypeParmPackTypeLoc TL) {
7852 TL.setNameLoc(readSourceLocation());
7853}
7854
7855void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7856 SubstBuiltinTemplatePackTypeLoc TL) {
7857 TL.setNameLoc(readSourceLocation());
7858}
7859
7860void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7861 TemplateSpecializationTypeLoc TL) {
7862 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7863 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7864 SourceLocation TemplateKeywordLoc = readSourceLocation();
7865 SourceLocation NameLoc = readSourceLocation();
7866 SourceLocation LAngleLoc = readSourceLocation();
7867 SourceLocation RAngleLoc = readSourceLocation();
7868 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7869 LAngleLoc, RAngleLoc);
7870 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7871 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7872 Args[I] = Reader.readTemplateArgumentLocInfo(
7873 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7874}
7875
7876void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7877 TL.setLParenLoc(readSourceLocation());
7878 TL.setRParenLoc(readSourceLocation());
7879}
7880
7881void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7882 TL.setElaboratedKeywordLoc(readSourceLocation());
7883 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7884 TL.setNameLoc(readSourceLocation());
7885}
7886
7887void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7888 TL.setEllipsisLoc(readSourceLocation());
7889}
7890
7891void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7892 TL.setNameLoc(readSourceLocation());
7893 TL.setNameEndLoc(readSourceLocation());
7894}
7895
7896void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7897 if (TL.getNumProtocols()) {
7898 TL.setProtocolLAngleLoc(readSourceLocation());
7899 TL.setProtocolRAngleLoc(readSourceLocation());
7900 }
7901 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7902 TL.setProtocolLoc(i, Loc: readSourceLocation());
7903}
7904
7905void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7906 TL.setHasBaseTypeAsWritten(Reader.readBool());
7907 TL.setTypeArgsLAngleLoc(readSourceLocation());
7908 TL.setTypeArgsRAngleLoc(readSourceLocation());
7909 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7910 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7911 TL.setProtocolLAngleLoc(readSourceLocation());
7912 TL.setProtocolRAngleLoc(readSourceLocation());
7913 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7914 TL.setProtocolLoc(i, Loc: readSourceLocation());
7915}
7916
7917void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7918 TL.setStarLoc(readSourceLocation());
7919}
7920
7921void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7922 TL.setKWLoc(readSourceLocation());
7923 TL.setLParenLoc(readSourceLocation());
7924 TL.setRParenLoc(readSourceLocation());
7925}
7926
7927void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7928 TL.setKWLoc(readSourceLocation());
7929}
7930
7931void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7932 TL.setNameLoc(readSourceLocation());
7933}
7934
7935void TypeLocReader::VisitDependentBitIntTypeLoc(
7936 clang::DependentBitIntTypeLoc TL) {
7937 TL.setNameLoc(readSourceLocation());
7938}
7939
7940void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7941 // Nothing to do.
7942}
7943
7944void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7945 TypeLocReader TLR(*this);
7946 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7947 TLR.Visit(TyLoc: TL);
7948}
7949
7950TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7951 QualType InfoTy = readType();
7952 if (InfoTy.isNull())
7953 return nullptr;
7954
7955 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7956 readTypeLoc(TL: TInfo->getTypeLoc());
7957 return TInfo;
7958}
7959
7960static unsigned getIndexForTypeID(serialization::TypeID ID) {
7961 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7962}
7963
7964static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7965 return ID >> 32;
7966}
7967
7968static bool isPredefinedType(serialization::TypeID ID) {
7969 // We don't need to erase the higher bits since if these bits are not 0,
7970 // it must be larger than NUM_PREDEF_TYPE_IDS.
7971 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7972}
7973
7974std::pair<ModuleFile *, unsigned>
7975ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7976 assert(!isPredefinedType(ID) &&
7977 "Predefined type shouldn't be in TypesLoaded");
7978 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7979 assert(ModuleFileIndex && "Untranslated Local Decl?");
7980
7981 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7982 assert(OwningModuleFile &&
7983 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7984
7985 return {OwningModuleFile,
7986 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7987}
7988
7989QualType ASTReader::GetType(TypeID ID) {
7990 assert(ContextObj && "reading type with no AST context");
7991 ASTContext &Context = *ContextObj;
7992
7993 unsigned FastQuals = ID & Qualifiers::FastMask;
7994
7995 if (isPredefinedType(ID)) {
7996 QualType T;
7997 unsigned Index = getIndexForTypeID(ID);
7998 switch ((PredefinedTypeIDs)Index) {
7999 case PREDEF_TYPE_LAST_ID:
8000 // We should never use this one.
8001 llvm_unreachable("Invalid predefined type");
8002 break;
8003 case PREDEF_TYPE_NULL_ID:
8004 return QualType();
8005 case PREDEF_TYPE_VOID_ID:
8006 T = Context.VoidTy;
8007 break;
8008 case PREDEF_TYPE_BOOL_ID:
8009 T = Context.BoolTy;
8010 break;
8011 case PREDEF_TYPE_CHAR_U_ID:
8012 case PREDEF_TYPE_CHAR_S_ID:
8013 // FIXME: Check that the signedness of CharTy is correct!
8014 T = Context.CharTy;
8015 break;
8016 case PREDEF_TYPE_UCHAR_ID:
8017 T = Context.UnsignedCharTy;
8018 break;
8019 case PREDEF_TYPE_USHORT_ID:
8020 T = Context.UnsignedShortTy;
8021 break;
8022 case PREDEF_TYPE_UINT_ID:
8023 T = Context.UnsignedIntTy;
8024 break;
8025 case PREDEF_TYPE_ULONG_ID:
8026 T = Context.UnsignedLongTy;
8027 break;
8028 case PREDEF_TYPE_ULONGLONG_ID:
8029 T = Context.UnsignedLongLongTy;
8030 break;
8031 case PREDEF_TYPE_UINT128_ID:
8032 T = Context.UnsignedInt128Ty;
8033 break;
8034 case PREDEF_TYPE_SCHAR_ID:
8035 T = Context.SignedCharTy;
8036 break;
8037 case PREDEF_TYPE_WCHAR_ID:
8038 T = Context.WCharTy;
8039 break;
8040 case PREDEF_TYPE_SHORT_ID:
8041 T = Context.ShortTy;
8042 break;
8043 case PREDEF_TYPE_INT_ID:
8044 T = Context.IntTy;
8045 break;
8046 case PREDEF_TYPE_LONG_ID:
8047 T = Context.LongTy;
8048 break;
8049 case PREDEF_TYPE_LONGLONG_ID:
8050 T = Context.LongLongTy;
8051 break;
8052 case PREDEF_TYPE_INT128_ID:
8053 T = Context.Int128Ty;
8054 break;
8055 case PREDEF_TYPE_BFLOAT16_ID:
8056 T = Context.BFloat16Ty;
8057 break;
8058 case PREDEF_TYPE_HALF_ID:
8059 T = Context.HalfTy;
8060 break;
8061 case PREDEF_TYPE_FLOAT_ID:
8062 T = Context.FloatTy;
8063 break;
8064 case PREDEF_TYPE_DOUBLE_ID:
8065 T = Context.DoubleTy;
8066 break;
8067 case PREDEF_TYPE_LONGDOUBLE_ID:
8068 T = Context.LongDoubleTy;
8069 break;
8070 case PREDEF_TYPE_SHORT_ACCUM_ID:
8071 T = Context.ShortAccumTy;
8072 break;
8073 case PREDEF_TYPE_ACCUM_ID:
8074 T = Context.AccumTy;
8075 break;
8076 case PREDEF_TYPE_LONG_ACCUM_ID:
8077 T = Context.LongAccumTy;
8078 break;
8079 case PREDEF_TYPE_USHORT_ACCUM_ID:
8080 T = Context.UnsignedShortAccumTy;
8081 break;
8082 case PREDEF_TYPE_UACCUM_ID:
8083 T = Context.UnsignedAccumTy;
8084 break;
8085 case PREDEF_TYPE_ULONG_ACCUM_ID:
8086 T = Context.UnsignedLongAccumTy;
8087 break;
8088 case PREDEF_TYPE_SHORT_FRACT_ID:
8089 T = Context.ShortFractTy;
8090 break;
8091 case PREDEF_TYPE_FRACT_ID:
8092 T = Context.FractTy;
8093 break;
8094 case PREDEF_TYPE_LONG_FRACT_ID:
8095 T = Context.LongFractTy;
8096 break;
8097 case PREDEF_TYPE_USHORT_FRACT_ID:
8098 T = Context.UnsignedShortFractTy;
8099 break;
8100 case PREDEF_TYPE_UFRACT_ID:
8101 T = Context.UnsignedFractTy;
8102 break;
8103 case PREDEF_TYPE_ULONG_FRACT_ID:
8104 T = Context.UnsignedLongFractTy;
8105 break;
8106 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
8107 T = Context.SatShortAccumTy;
8108 break;
8109 case PREDEF_TYPE_SAT_ACCUM_ID:
8110 T = Context.SatAccumTy;
8111 break;
8112 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
8113 T = Context.SatLongAccumTy;
8114 break;
8115 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
8116 T = Context.SatUnsignedShortAccumTy;
8117 break;
8118 case PREDEF_TYPE_SAT_UACCUM_ID:
8119 T = Context.SatUnsignedAccumTy;
8120 break;
8121 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
8122 T = Context.SatUnsignedLongAccumTy;
8123 break;
8124 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
8125 T = Context.SatShortFractTy;
8126 break;
8127 case PREDEF_TYPE_SAT_FRACT_ID:
8128 T = Context.SatFractTy;
8129 break;
8130 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
8131 T = Context.SatLongFractTy;
8132 break;
8133 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
8134 T = Context.SatUnsignedShortFractTy;
8135 break;
8136 case PREDEF_TYPE_SAT_UFRACT_ID:
8137 T = Context.SatUnsignedFractTy;
8138 break;
8139 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
8140 T = Context.SatUnsignedLongFractTy;
8141 break;
8142 case PREDEF_TYPE_FLOAT16_ID:
8143 T = Context.Float16Ty;
8144 break;
8145 case PREDEF_TYPE_FLOAT128_ID:
8146 T = Context.Float128Ty;
8147 break;
8148 case PREDEF_TYPE_IBM128_ID:
8149 T = Context.Ibm128Ty;
8150 break;
8151 case PREDEF_TYPE_OVERLOAD_ID:
8152 T = Context.OverloadTy;
8153 break;
8154 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
8155 T = Context.UnresolvedTemplateTy;
8156 break;
8157 case PREDEF_TYPE_BOUND_MEMBER:
8158 T = Context.BoundMemberTy;
8159 break;
8160 case PREDEF_TYPE_PSEUDO_OBJECT:
8161 T = Context.PseudoObjectTy;
8162 break;
8163 case PREDEF_TYPE_DEPENDENT_ID:
8164 T = Context.DependentTy;
8165 break;
8166 case PREDEF_TYPE_UNKNOWN_ANY:
8167 T = Context.UnknownAnyTy;
8168 break;
8169 case PREDEF_TYPE_NULLPTR_ID:
8170 T = Context.NullPtrTy;
8171 break;
8172 case PREDEF_TYPE_META_INFO_ID:
8173 T = Context.MetaInfoTy;
8174 break;
8175 case PREDEF_TYPE_CHAR8_ID:
8176 T = Context.Char8Ty;
8177 break;
8178 case PREDEF_TYPE_CHAR16_ID:
8179 T = Context.Char16Ty;
8180 break;
8181 case PREDEF_TYPE_CHAR32_ID:
8182 T = Context.Char32Ty;
8183 break;
8184 case PREDEF_TYPE_OBJC_ID:
8185 T = Context.ObjCBuiltinIdTy;
8186 break;
8187 case PREDEF_TYPE_OBJC_CLASS:
8188 T = Context.ObjCBuiltinClassTy;
8189 break;
8190 case PREDEF_TYPE_OBJC_SEL:
8191 T = Context.ObjCBuiltinSelTy;
8192 break;
8193#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8194 case PREDEF_TYPE_##Id##_ID: \
8195 T = Context.SingletonId; \
8196 break;
8197#include "clang/Basic/OpenCLImageTypes.def"
8198#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8199 case PREDEF_TYPE_##Id##_ID: \
8200 T = Context.Id##Ty; \
8201 break;
8202#include "clang/Basic/OpenCLExtensionTypes.def"
8203 case PREDEF_TYPE_SAMPLER_ID:
8204 T = Context.OCLSamplerTy;
8205 break;
8206 case PREDEF_TYPE_EVENT_ID:
8207 T = Context.OCLEventTy;
8208 break;
8209 case PREDEF_TYPE_CLK_EVENT_ID:
8210 T = Context.OCLClkEventTy;
8211 break;
8212 case PREDEF_TYPE_QUEUE_ID:
8213 T = Context.OCLQueueTy;
8214 break;
8215 case PREDEF_TYPE_RESERVE_ID_ID:
8216 T = Context.OCLReserveIDTy;
8217 break;
8218 case PREDEF_TYPE_AUTO_DEDUCT:
8219 T = Context.getAutoDeductType();
8220 break;
8221 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8222 T = Context.getAutoRRefDeductType();
8223 break;
8224 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8225 T = Context.ARCUnbridgedCastTy;
8226 break;
8227 case PREDEF_TYPE_BUILTIN_FN:
8228 T = Context.BuiltinFnTy;
8229 break;
8230 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8231 T = Context.IncompleteMatrixIdxTy;
8232 break;
8233 case PREDEF_TYPE_ARRAY_SECTION:
8234 T = Context.ArraySectionTy;
8235 break;
8236 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8237 T = Context.OMPArrayShapingTy;
8238 break;
8239 case PREDEF_TYPE_OMP_ITERATOR:
8240 T = Context.OMPIteratorTy;
8241 break;
8242#define SVE_TYPE(Name, Id, SingletonId) \
8243 case PREDEF_TYPE_##Id##_ID: \
8244 T = Context.SingletonId; \
8245 break;
8246#include "clang/Basic/AArch64ACLETypes.def"
8247#define PPC_VECTOR_TYPE(Name, Id, Size) \
8248 case PREDEF_TYPE_##Id##_ID: \
8249 T = Context.Id##Ty; \
8250 break;
8251#include "clang/Basic/PPCTypes.def"
8252#define RVV_TYPE(Name, Id, SingletonId) \
8253 case PREDEF_TYPE_##Id##_ID: \
8254 T = Context.SingletonId; \
8255 break;
8256#include "clang/Basic/RISCVVTypes.def"
8257#define WASM_TYPE(Name, Id, SingletonId) \
8258 case PREDEF_TYPE_##Id##_ID: \
8259 T = Context.SingletonId; \
8260 break;
8261#include "clang/Basic/WebAssemblyReferenceTypes.def"
8262#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8263 case PREDEF_TYPE_##Id##_ID: \
8264 T = Context.SingletonId; \
8265 break;
8266#include "clang/Basic/AMDGPUTypes.def"
8267#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8268 case PREDEF_TYPE_##Id##_ID: \
8269 T = Context.SingletonId; \
8270 break;
8271#include "clang/Basic/HLSLIntangibleTypes.def"
8272#define HLSL_PACKED_TYPE(Name, Id, SingletonId) \
8273 case PREDEF_TYPE_##Id##_ID: \
8274 T = Context.SingletonId; \
8275 break;
8276#include "clang/Basic/HLSLPackedTypes.def"
8277#define SPIRV_TYPE(Name, Id, SingletonId) \
8278 case PREDEF_TYPE_##Id##_ID: \
8279 T = Context.SingletonId; \
8280 break;
8281#include "clang/Basic/SPIRVTypes.def"
8282 }
8283
8284 assert(!T.isNull() && "Unknown predefined type");
8285 return T.withFastQualifiers(TQs: FastQuals);
8286 }
8287
8288 unsigned Index = translateTypeIDToIndex(ID).second;
8289
8290 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8291 if (TypesLoaded[Index].isNull()) {
8292 TypesLoaded[Index] = readTypeRecord(ID);
8293 if (TypesLoaded[Index].isNull())
8294 return QualType();
8295
8296 TypesLoaded[Index]->setFromAST();
8297 if (DeserializationListener)
8298 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8299 T: TypesLoaded[Index]);
8300 }
8301
8302 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8303}
8304
8305QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8306 return GetType(ID: getGlobalTypeID(F, LocalID));
8307}
8308
8309serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8310 LocalTypeID LocalID) const {
8311 if (isPredefinedType(ID: LocalID))
8312 return LocalID;
8313
8314 if (!F.ModuleOffsetMap.empty())
8315 ReadModuleOffsetMap(F);
8316
8317 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8318 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8319
8320 if (ModuleFileIndex == 0)
8321 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8322
8323 ModuleFile &MF =
8324 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8325 ModuleFileIndex = MF.Index + 1;
8326 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8327}
8328
8329TemplateArgumentLocInfo
8330ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8331 switch (Kind) {
8332 case TemplateArgument::Expression:
8333 return readExpr();
8334 case TemplateArgument::Type:
8335 return readTypeSourceInfo();
8336 case TemplateArgument::Template:
8337 case TemplateArgument::TemplateExpansion: {
8338 SourceLocation TemplateKWLoc = readSourceLocation();
8339 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8340 SourceLocation TemplateNameLoc = readSourceLocation();
8341 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8342 ? readSourceLocation()
8343 : SourceLocation();
8344 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8345 TemplateNameLoc, EllipsisLoc);
8346 }
8347 case TemplateArgument::Null:
8348 case TemplateArgument::Integral:
8349 case TemplateArgument::Declaration:
8350 case TemplateArgument::NullPtr:
8351 case TemplateArgument::StructuralValue:
8352 case TemplateArgument::Pack:
8353 // FIXME: Is this right?
8354 return TemplateArgumentLocInfo();
8355 }
8356 llvm_unreachable("unexpected template argument loc");
8357}
8358
8359TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8360 TemplateArgument Arg = readTemplateArgument();
8361
8362 if (Arg.getKind() == TemplateArgument::Expression) {
8363 if (readBool()) // bool InfoHasSameExpr.
8364 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8365 }
8366 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8367}
8368
8369void ASTRecordReader::readTemplateArgumentListInfo(
8370 TemplateArgumentListInfo &Result) {
8371 Result.setLAngleLoc(readSourceLocation());
8372 Result.setRAngleLoc(readSourceLocation());
8373 unsigned NumArgsAsWritten = readInt();
8374 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8375 Result.addArgument(Loc: readTemplateArgumentLoc());
8376}
8377
8378const ASTTemplateArgumentListInfo *
8379ASTRecordReader::readASTTemplateArgumentListInfo() {
8380 TemplateArgumentListInfo Result;
8381 readTemplateArgumentListInfo(Result);
8382 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8383}
8384
8385Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8386
8387void ASTReader::CompleteRedeclChain(const Decl *D) {
8388 if (NumCurrentElementsDeserializing) {
8389 // We arrange to not care about the complete redeclaration chain while we're
8390 // deserializing. Just remember that the AST has marked this one as complete
8391 // but that it's not actually complete yet, so we know we still need to
8392 // complete it later.
8393 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8394 return;
8395 }
8396
8397 if (!D->getDeclContext()) {
8398 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8399 return;
8400 }
8401
8402 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8403
8404 // If this is a named declaration, complete it by looking it up
8405 // within its context.
8406 //
8407 // FIXME: Merging a function definition should merge
8408 // all mergeable entities within it.
8409 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8410 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8411 if (!getContext().getLangOpts().CPlusPlus &&
8412 isa<TranslationUnitDecl>(Val: DC)) {
8413 // Outside of C++, we don't have a lookup table for the TU, so update
8414 // the identifier instead. (For C++ modules, we don't store decls
8415 // in the serialized identifier table, so we do the lookup in the TU.)
8416 auto *II = Name.getAsIdentifierInfo();
8417 assert(II && "non-identifier name in C?");
8418 if (II->isOutOfDate())
8419 updateOutOfDateIdentifier(II: *II);
8420 } else
8421 DC->lookup(Name);
8422 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8423 // Find all declarations of this kind from the relevant context.
8424 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8425 auto *DC = cast<DeclContext>(Val: DCDecl);
8426 SmallVector<Decl*, 8> Decls;
8427 FindExternalLexicalDecls(
8428 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8429 }
8430 }
8431 }
8432
8433 RedeclarableTemplateDecl *Template = nullptr;
8434 ArrayRef<TemplateArgument> Args;
8435 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8436 Template = CTSD->getSpecializedTemplate();
8437 Args = CTSD->getTemplateArgs().asArray();
8438 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8439 Template = VTSD->getSpecializedTemplate();
8440 Args = VTSD->getTemplateArgs().asArray();
8441 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8442 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8443 Template = Tmplt;
8444 Args = FD->getTemplateSpecializationArgs()->asArray();
8445 }
8446 }
8447
8448 if (Template)
8449 Template->loadLazySpecializationsImpl(Args);
8450}
8451
8452CXXCtorInitializer **
8453ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8454 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8455 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8456 SavedStreamPosition SavedPosition(Cursor);
8457 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8458 Error(Err: std::move(Err));
8459 return nullptr;
8460 }
8461 ReadingKindTracker ReadingKind(Read_Decl, *this);
8462 Deserializing D(this);
8463
8464 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8465 if (!MaybeCode) {
8466 Error(Err: MaybeCode.takeError());
8467 return nullptr;
8468 }
8469 unsigned Code = MaybeCode.get();
8470
8471 ASTRecordReader Record(*this, *Loc.F);
8472 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8473 if (!MaybeRecCode) {
8474 Error(Err: MaybeRecCode.takeError());
8475 return nullptr;
8476 }
8477 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8478 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8479 return nullptr;
8480 }
8481
8482 return Record.readCXXCtorInitializers();
8483}
8484
8485CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8486 assert(ContextObj && "reading base specifiers with no AST context");
8487 ASTContext &Context = *ContextObj;
8488
8489 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8490 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8491 SavedStreamPosition SavedPosition(Cursor);
8492 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8493 Error(Err: std::move(Err));
8494 return nullptr;
8495 }
8496 ReadingKindTracker ReadingKind(Read_Decl, *this);
8497 Deserializing D(this);
8498
8499 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8500 if (!MaybeCode) {
8501 Error(Err: MaybeCode.takeError());
8502 return nullptr;
8503 }
8504 unsigned Code = MaybeCode.get();
8505
8506 ASTRecordReader Record(*this, *Loc.F);
8507 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8508 if (!MaybeRecCode) {
8509 Error(Err: MaybeCode.takeError());
8510 return nullptr;
8511 }
8512 unsigned RecCode = MaybeRecCode.get();
8513
8514 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8515 Error(Msg: "malformed AST file: missing C++ base specifiers");
8516 return nullptr;
8517 }
8518
8519 unsigned NumBases = Record.readInt();
8520 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8521 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8522 for (unsigned I = 0; I != NumBases; ++I)
8523 Bases[I] = Record.readCXXBaseSpecifier();
8524 return Bases;
8525}
8526
8527GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8528 LocalDeclID LocalID) const {
8529 if (LocalID < NUM_PREDEF_DECL_IDS)
8530 return GlobalDeclID(LocalID.getRawValue());
8531
8532 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8533 DeclID ID = LocalID.getLocalDeclIndex();
8534
8535 if (!F.ModuleOffsetMap.empty())
8536 ReadModuleOffsetMap(F);
8537
8538 ModuleFile *OwningModuleFile =
8539 OwningModuleFileIndex == 0
8540 ? &F
8541 : F.TransitiveImports[OwningModuleFileIndex - 1];
8542
8543 if (OwningModuleFileIndex == 0)
8544 ID -= NUM_PREDEF_DECL_IDS;
8545
8546 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8547 return GlobalDeclID(NewModuleFileIndex, ID);
8548}
8549
8550bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8551 // Predefined decls aren't from any module.
8552 if (ID < NUM_PREDEF_DECL_IDS)
8553 return false;
8554
8555 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8556 return M.Index == ModuleFileIndex - 1;
8557}
8558
8559ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8560 // Predefined decls aren't from any module.
8561 if (ID < NUM_PREDEF_DECL_IDS)
8562 return nullptr;
8563
8564 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8565 assert(ModuleFileIndex && "Untranslated Local Decl?");
8566
8567 return &getModuleManager()[ModuleFileIndex - 1];
8568}
8569
8570ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8571 if (!D->isFromASTFile())
8572 return nullptr;
8573
8574 return getOwningModuleFile(ID: D->getGlobalID());
8575}
8576
8577SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8578 if (ID < NUM_PREDEF_DECL_IDS)
8579 return SourceLocation();
8580
8581 if (Decl *D = GetExistingDecl(ID))
8582 return D->getLocation();
8583
8584 SourceLocation Loc;
8585 DeclCursorForID(ID, Location&: Loc);
8586 return Loc;
8587}
8588
8589Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8590 assert(ContextObj && "reading predefined decl without AST context");
8591 ASTContext &Context = *ContextObj;
8592 Decl *NewLoaded = nullptr;
8593 switch (ID) {
8594 case PREDEF_DECL_NULL_ID:
8595 return nullptr;
8596
8597 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8598 return Context.getTranslationUnitDecl();
8599
8600 case PREDEF_DECL_OBJC_ID_ID:
8601 if (Context.ObjCIdDecl)
8602 return Context.ObjCIdDecl;
8603 NewLoaded = Context.getObjCIdDecl();
8604 break;
8605
8606 case PREDEF_DECL_OBJC_SEL_ID:
8607 if (Context.ObjCSelDecl)
8608 return Context.ObjCSelDecl;
8609 NewLoaded = Context.getObjCSelDecl();
8610 break;
8611
8612 case PREDEF_DECL_OBJC_CLASS_ID:
8613 if (Context.ObjCClassDecl)
8614 return Context.ObjCClassDecl;
8615 NewLoaded = Context.getObjCClassDecl();
8616 break;
8617
8618 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8619 if (Context.ObjCProtocolClassDecl)
8620 return Context.ObjCProtocolClassDecl;
8621 NewLoaded = Context.getObjCProtocolDecl();
8622 break;
8623
8624 case PREDEF_DECL_INT_128_ID:
8625 if (Context.Int128Decl)
8626 return Context.Int128Decl;
8627 NewLoaded = Context.getInt128Decl();
8628 break;
8629
8630 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8631 if (Context.UInt128Decl)
8632 return Context.UInt128Decl;
8633 NewLoaded = Context.getUInt128Decl();
8634 break;
8635
8636 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8637 if (Context.ObjCInstanceTypeDecl)
8638 return Context.ObjCInstanceTypeDecl;
8639 NewLoaded = Context.getObjCInstanceTypeDecl();
8640 break;
8641
8642 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8643 if (Context.BuiltinVaListDecl)
8644 return Context.BuiltinVaListDecl;
8645 NewLoaded = Context.getBuiltinVaListDecl();
8646 break;
8647
8648 case PREDEF_DECL_VA_LIST_TAG:
8649 if (Context.VaListTagDecl)
8650 return Context.VaListTagDecl;
8651 NewLoaded = Context.getVaListTagDecl();
8652 break;
8653
8654 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8655 if (Context.BuiltinMSVaListDecl)
8656 return Context.BuiltinMSVaListDecl;
8657 NewLoaded = Context.getBuiltinMSVaListDecl();
8658 break;
8659
8660 case PREDEF_DECL_BUILTIN_ZOS_VA_LIST_ID:
8661 if (Context.BuiltinZOSVaListDecl)
8662 return Context.BuiltinZOSVaListDecl;
8663 NewLoaded = Context.getBuiltinZOSVaListDecl();
8664 break;
8665
8666 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8667 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8668 return Context.getMSGuidTagDecl();
8669
8670 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8671 if (Context.ExternCContext)
8672 return Context.ExternCContext;
8673 NewLoaded = Context.getExternCContextDecl();
8674 break;
8675
8676 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8677 if (Context.CFConstantStringTypeDecl)
8678 return Context.CFConstantStringTypeDecl;
8679 NewLoaded = Context.getCFConstantStringDecl();
8680 break;
8681
8682 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8683 if (Context.CFConstantStringTagDecl)
8684 return Context.CFConstantStringTagDecl;
8685 NewLoaded = Context.getCFConstantStringTagDecl();
8686 break;
8687
8688 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8689 return Context.getMSTypeInfoTagDecl();
8690
8691#define BuiltinTemplate(BTName) \
8692 case PREDEF_DECL##BTName##_ID: \
8693 if (Context.Decl##BTName) \
8694 return Context.Decl##BTName; \
8695 NewLoaded = Context.get##BTName##Decl(); \
8696 break;
8697#include "clang/Basic/BuiltinTemplates.inc"
8698
8699 case NUM_PREDEF_DECL_IDS:
8700 llvm_unreachable("Invalid decl ID");
8701 break;
8702 }
8703
8704 assert(NewLoaded && "Failed to load predefined decl?");
8705
8706 if (DeserializationListener)
8707 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8708
8709 return NewLoaded;
8710}
8711
8712unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8713 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8714 if (!OwningModuleFile) {
8715 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8716 return GlobalID.getRawValue();
8717 }
8718
8719 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8720}
8721
8722Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8723 assert(ContextObj && "reading decl with no AST context");
8724
8725 if (ID < NUM_PREDEF_DECL_IDS) {
8726 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8727 if (D) {
8728 // Track that we have merged the declaration with ID \p ID into the
8729 // pre-existing predefined declaration \p D.
8730 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8731 if (Merged.empty())
8732 Merged.push_back(Elt: ID);
8733 }
8734 return D;
8735 }
8736
8737 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8738
8739 if (Index >= DeclsLoaded.size()) {
8740 assert(0 && "declaration ID out-of-range for AST file");
8741 Error(Msg: "declaration ID out-of-range for AST file");
8742 return nullptr;
8743 }
8744
8745 return DeclsLoaded[Index];
8746}
8747
8748Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8749 if (ID < NUM_PREDEF_DECL_IDS)
8750 return GetExistingDecl(ID);
8751
8752 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8753
8754 if (Index >= DeclsLoaded.size()) {
8755 assert(0 && "declaration ID out-of-range for AST file");
8756 Error(Msg: "declaration ID out-of-range for AST file");
8757 return nullptr;
8758 }
8759
8760 if (!DeclsLoaded[Index]) {
8761 ReadDeclRecord(ID);
8762 if (DeserializationListener)
8763 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8764 }
8765
8766 return DeclsLoaded[Index];
8767}
8768
8769LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8770 GlobalDeclID GlobalID) {
8771 if (GlobalID < NUM_PREDEF_DECL_IDS)
8772 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8773
8774 if (!M.ModuleOffsetMap.empty())
8775 ReadModuleOffsetMap(F&: M);
8776
8777 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8778 DeclID ID = GlobalID.getLocalDeclIndex();
8779
8780 if (Owner == &M) {
8781 ID += NUM_PREDEF_DECL_IDS;
8782 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8783 }
8784
8785 uint64_t OrignalModuleFileIndex = 0;
8786 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8787 if (M.TransitiveImports[I] == Owner) {
8788 OrignalModuleFileIndex = I + 1;
8789 break;
8790 }
8791
8792 if (!OrignalModuleFileIndex)
8793 return LocalDeclID();
8794
8795 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8796}
8797
8798GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8799 unsigned &Idx) {
8800 if (Idx >= Record.size()) {
8801 Error(Msg: "Corrupted AST file");
8802 return GlobalDeclID(0);
8803 }
8804
8805 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8806}
8807
8808/// Resolve the offset of a statement into a statement.
8809///
8810/// This operation will read a new statement from the external
8811/// source each time it is called, and is meant to be used via a
8812/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8813Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8814 // Switch case IDs are per Decl.
8815 ClearSwitchCaseIDs();
8816
8817 // Offset here is a global offset across the entire chain.
8818 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8819 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8820 Error(Err: std::move(Err));
8821 return nullptr;
8822 }
8823 assert(NumCurrentElementsDeserializing == 0 &&
8824 "should not be called while already deserializing");
8825 Deserializing D(this);
8826 return ReadStmtFromStream(F&: *Loc.F);
8827}
8828
8829bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8830 const Decl *D) {
8831 assert(D);
8832
8833 auto It = SpecLookups.find(Val: D);
8834 if (It == SpecLookups.end())
8835 return false;
8836
8837 // Get Decl may violate the iterator from SpecializationsLookups so we store
8838 // the DeclIDs in ahead.
8839 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8840 It->second.Table.findAll();
8841
8842 // Since we've loaded all the specializations, we can erase it from
8843 // the lookup table.
8844 SpecLookups.erase(I: It);
8845
8846 bool NewSpecsFound = false;
8847 Deserializing LookupResults(this);
8848 for (auto &Info : Infos) {
8849 if (GetExistingDecl(ID: Info))
8850 continue;
8851 NewSpecsFound = true;
8852 GetDecl(ID: Info);
8853 }
8854
8855 return NewSpecsFound;
8856}
8857
8858bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8859 assert(D);
8860
8861 CompleteRedeclChain(D);
8862 bool NewSpecsFound =
8863 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8864 if (OnlyPartial)
8865 return NewSpecsFound;
8866
8867 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8868 return NewSpecsFound;
8869}
8870
8871bool ASTReader::LoadExternalSpecializationsImpl(
8872 SpecLookupTableTy &SpecLookups, const Decl *D,
8873 ArrayRef<TemplateArgument> TemplateArgs) {
8874 assert(D);
8875
8876 auto It = SpecLookups.find(Val: D);
8877 if (It == SpecLookups.end())
8878 return false;
8879
8880 Deserializing LookupResults(this);
8881 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8882
8883 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8884 It->second.Table.find(EKey: HashValue);
8885
8886 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8887 std::string Name;
8888 llvm::raw_string_ostream OS(Name);
8889 auto *ND = cast<NamedDecl>(Val: D);
8890 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8891 /*Qualified=*/true);
8892 return Name;
8893 });
8894
8895 bool NewSpecsFound = false;
8896 for (auto &Info : Infos) {
8897 if (GetExistingDecl(ID: Info))
8898 continue;
8899 NewSpecsFound = true;
8900 GetDecl(ID: Info);
8901 }
8902
8903 return NewSpecsFound;
8904}
8905
8906bool ASTReader::LoadExternalSpecializations(
8907 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8908 assert(D);
8909
8910 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8911 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8912 NewDeclsFound |=
8913 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8914
8915 return NewDeclsFound;
8916}
8917
8918void ASTReader::FindExternalLexicalDecls(
8919 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8920 SmallVectorImpl<Decl *> &Decls) {
8921 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8922
8923 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8924 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8925 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8926 auto K = (Decl::Kind)+LexicalDecls[I];
8927 if (!IsKindWeWant(K))
8928 continue;
8929
8930 auto ID = (DeclID) + LexicalDecls[I + 1];
8931
8932 // Don't add predefined declarations to the lexical context more
8933 // than once.
8934 if (ID < NUM_PREDEF_DECL_IDS) {
8935 if (PredefsVisited[ID])
8936 continue;
8937
8938 PredefsVisited[ID] = true;
8939 }
8940
8941 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8942 assert(D->getKind() == K && "wrong kind for lexical decl");
8943 if (!DC->isDeclInLexicalTraversal(D))
8944 Decls.push_back(Elt: D);
8945 }
8946 }
8947 };
8948
8949 if (isa<TranslationUnitDecl>(Val: DC)) {
8950 for (const auto &Lexical : TULexicalDecls)
8951 Visit(Lexical.first, Lexical.second);
8952 } else {
8953 auto I = LexicalDecls.find(Val: DC);
8954 if (I != LexicalDecls.end())
8955 Visit(I->second.first, I->second.second);
8956 }
8957
8958 ++NumLexicalDeclContextsRead;
8959}
8960
8961namespace {
8962
8963class UnalignedDeclIDComp {
8964 ASTReader &Reader;
8965 ModuleFile &Mod;
8966
8967public:
8968 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8969 : Reader(Reader), Mod(M) {}
8970
8971 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8972 SourceLocation LHS = getLocation(ID: L);
8973 SourceLocation RHS = getLocation(ID: R);
8974 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8975 }
8976
8977 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8978 SourceLocation RHS = getLocation(ID: R);
8979 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8980 }
8981
8982 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8983 SourceLocation LHS = getLocation(ID: L);
8984 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8985 }
8986
8987 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8988 return Reader.getSourceManager().getFileLoc(
8989 Loc: Reader.getSourceLocationForDeclID(
8990 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8991 }
8992};
8993
8994} // namespace
8995
8996void ASTReader::FindFileRegionDecls(FileID File,
8997 unsigned Offset, unsigned Length,
8998 SmallVectorImpl<Decl *> &Decls) {
8999 SourceManager &SM = getSourceManager();
9000
9001 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
9002 if (I == FileDeclIDs.end())
9003 return;
9004
9005 FileDeclsInfo &DInfo = I->second;
9006 if (DInfo.Decls.empty())
9007 return;
9008
9009 SourceLocation
9010 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
9011 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
9012
9013 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
9014 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
9015 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
9016 if (BeginIt != DInfo.Decls.begin())
9017 --BeginIt;
9018
9019 // If we are pointing at a top-level decl inside an objc container, we need
9020 // to backtrack until we find it otherwise we will fail to report that the
9021 // region overlaps with an objc container.
9022 while (BeginIt != DInfo.Decls.begin() &&
9023 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
9024 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
9025 ->isTopLevelDeclInObjCContainer())
9026 --BeginIt;
9027
9028 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
9029 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
9030 if (EndIt != DInfo.Decls.end())
9031 ++EndIt;
9032
9033 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
9034 ++DIt)
9035 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
9036 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
9037}
9038
9039bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
9040 DeclarationName Name,
9041 const DeclContext *OriginalDC) {
9042 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
9043 "DeclContext has no visible decls in storage");
9044 if (!Name)
9045 return false;
9046
9047 // Load the list of declarations.
9048 DeclsSet DS;
9049
9050 auto Find = [&, this](auto &&Table, auto &&Key) {
9051 for (GlobalDeclID ID : Table.find(Key)) {
9052 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9053 if (ND->getDeclName() != Name)
9054 continue;
9055 // Special case for namespaces: There can be a lot of redeclarations of
9056 // some namespaces, and we import a "key declaration" per imported module.
9057 // Since all declarations of a namespace are essentially interchangeable,
9058 // we can optimize namespace look-up by only storing the key declaration
9059 // of the current TU, rather than storing N key declarations where N is
9060 // the # of imported modules that declare that namespace.
9061 // TODO: Try to generalize this optimization to other redeclarable decls.
9062 if (isa<NamespaceDecl>(Val: ND))
9063 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9064 DS.insert(ND);
9065 }
9066 };
9067
9068 Deserializing LookupResults(this);
9069
9070 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
9071 // available.
9072 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
9073 ++NumVisibleDeclContextsRead;
9074 Find(It->second.Table, Name);
9075 }
9076
9077 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
9078 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
9079 ++NumModuleLocalVisibleDeclContexts;
9080 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
9081 }
9082 };
9083 if (auto *NamedModule =
9084 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
9085 : nullptr)
9086 FindModuleLocalLookup(NamedModule);
9087 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
9088 // We're going to find a decl but the decl context of the lookup is
9089 // unspecified. In this case, the OriginalDC may be the decl context in other
9090 // module.
9091 if (ContextObj && ContextObj->getCurrentNamedModule())
9092 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
9093
9094 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
9095 ++NumTULocalVisibleDeclContexts;
9096 Find(It->second.Table, Name);
9097 }
9098
9099 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9100 return !DS.empty();
9101}
9102
9103void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
9104 if (!DC->hasExternalVisibleStorage())
9105 return;
9106
9107 DeclsMap Decls;
9108
9109 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
9110 auto It = LookupTables.find(DC);
9111 if (It == LookupTables.end())
9112 return;
9113
9114 NumRead++;
9115
9116 for (GlobalDeclID ID : It->second.Table.findAll()) {
9117 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9118 // Special case for namespaces: There can be a lot of redeclarations of
9119 // some namespaces, and we import a "key declaration" per imported module.
9120 // Since all declarations of a namespace are essentially interchangeable,
9121 // we can optimize namespace look-up by only storing the key declaration
9122 // of the current TU, rather than storing N key declarations where N is
9123 // the # of imported modules that declare that namespace.
9124 // TODO: Try to generalize this optimization to other redeclarable decls.
9125 if (isa<NamespaceDecl>(Val: ND))
9126 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9127 Decls[ND->getDeclName()].insert(ND);
9128 }
9129
9130 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
9131 };
9132
9133 findAll(Lookups, NumVisibleDeclContextsRead);
9134 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
9135 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
9136
9137 for (auto &[Name, DS] : Decls)
9138 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9139
9140 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
9141}
9142
9143const serialization::reader::DeclContextLookupTable *
9144ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
9145 auto I = Lookups.find(Val: Primary);
9146 return I == Lookups.end() ? nullptr : &I->second;
9147}
9148
9149const serialization::reader::ModuleLocalLookupTable *
9150ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
9151 auto I = ModuleLocalLookups.find(Val: Primary);
9152 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
9153}
9154
9155const serialization::reader::DeclContextLookupTable *
9156ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
9157 auto I = TULocalLookups.find(Val: Primary);
9158 return I == TULocalLookups.end() ? nullptr : &I->second;
9159}
9160
9161serialization::reader::LazySpecializationInfoLookupTable *
9162ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
9163 assert(D->isCanonicalDecl());
9164 auto &LookupTable =
9165 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
9166 auto I = LookupTable.find(Val: D);
9167 return I == LookupTable.end() ? nullptr : &I->second;
9168}
9169
9170bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
9171 assert(D->isCanonicalDecl());
9172 return PartialSpecializationsLookups.contains(Val: D) ||
9173 SpecializationsLookups.contains(Val: D);
9174}
9175
9176/// Under non-PCH compilation the consumer receives the objc methods
9177/// before receiving the implementation, and codegen depends on this.
9178/// We simulate this by deserializing and passing to consumer the methods of the
9179/// implementation before passing the deserialized implementation decl.
9180static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
9181 ASTConsumer *Consumer) {
9182 assert(ImplD && Consumer);
9183
9184 for (auto *I : ImplD->methods())
9185 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
9186
9187 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
9188}
9189
9190void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
9191 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
9192 PassObjCImplDeclToConsumer(ImplD, Consumer);
9193 else
9194 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9195}
9196
9197void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9198 Consumer->HandleVTable(RD);
9199}
9200
9201void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9202 this->Consumer = Consumer;
9203
9204 if (Consumer)
9205 PassInterestingDeclsToConsumer();
9206
9207 if (DeserializationListener)
9208 DeserializationListener->ReaderInitialized(Reader: this);
9209}
9210
9211void ASTReader::PrintStats() {
9212 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9213
9214 unsigned NumTypesLoaded =
9215 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9216 unsigned NumDeclsLoaded =
9217 DeclsLoaded.size() -
9218 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9219 unsigned NumIdentifiersLoaded =
9220 IdentifiersLoaded.size() -
9221 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9222 unsigned NumMacrosLoaded =
9223 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9224 unsigned NumSelectorsLoaded =
9225 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9226
9227 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9228 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9229 NumSLocEntriesRead, TotalNumSLocEntries,
9230 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9231 if (!TypesLoaded.empty())
9232 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9233 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9234 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9235 if (!DeclsLoaded.empty())
9236 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9237 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9238 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9239 if (!IdentifiersLoaded.empty())
9240 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9241 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9242 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9243 if (!MacrosLoaded.empty())
9244 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9245 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9246 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9247 if (!SelectorsLoaded.empty())
9248 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9249 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9250 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9251 if (TotalNumStatements)
9252 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9253 NumStatementsRead, TotalNumStatements,
9254 ((float)NumStatementsRead/TotalNumStatements * 100));
9255 if (TotalNumMacros)
9256 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9257 NumMacrosRead, TotalNumMacros,
9258 ((float)NumMacrosRead/TotalNumMacros * 100));
9259 if (TotalLexicalDeclContexts)
9260 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9261 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9262 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9263 * 100));
9264 if (TotalVisibleDeclContexts)
9265 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9266 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9267 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9268 * 100));
9269 if (TotalModuleLocalVisibleDeclContexts)
9270 std::fprintf(
9271 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9272 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9273 ((float)NumModuleLocalVisibleDeclContexts /
9274 TotalModuleLocalVisibleDeclContexts * 100));
9275 if (TotalTULocalVisibleDeclContexts)
9276 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9277 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9278 ((float)NumTULocalVisibleDeclContexts /
9279 TotalTULocalVisibleDeclContexts * 100));
9280 if (TotalNumMethodPoolEntries)
9281 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9282 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9283 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9284 * 100));
9285 if (NumMethodPoolLookups)
9286 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9287 NumMethodPoolHits, NumMethodPoolLookups,
9288 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9289 if (NumMethodPoolTableLookups)
9290 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9291 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9292 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9293 * 100.0));
9294 if (NumIdentifierLookupHits)
9295 std::fprintf(stderr,
9296 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9297 NumIdentifierLookupHits, NumIdentifierLookups,
9298 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9299
9300 if (GlobalIndex) {
9301 std::fprintf(stderr, format: "\n");
9302 GlobalIndex->printStats();
9303 }
9304
9305 std::fprintf(stderr, format: "\n");
9306 dump();
9307 std::fprintf(stderr, format: "\n");
9308}
9309
9310template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9311LLVM_DUMP_METHOD static void
9312dumpModuleIDMap(StringRef Name,
9313 const ContinuousRangeMap<Key, ModuleFile *,
9314 InitialCapacity> &Map) {
9315 if (Map.begin() == Map.end())
9316 return;
9317
9318 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9319
9320 llvm::errs() << Name << ":\n";
9321 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9322 I != IEnd; ++I)
9323 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9324 << "\n";
9325}
9326
9327LLVM_DUMP_METHOD void ASTReader::dump() {
9328 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9329 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9330 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9331 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9332 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9333 dumpModuleIDMap(Name: "Global preprocessed entity map",
9334 Map: GlobalPreprocessedEntityMap);
9335
9336 llvm::errs() << "\n*** PCH/Modules Loaded:";
9337 for (ModuleFile &M : ModuleMgr)
9338 M.dump();
9339}
9340
9341/// Return the amount of memory used by memory buffers, breaking down
9342/// by heap-backed versus mmap'ed memory.
9343void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9344 for (ModuleFile &I : ModuleMgr) {
9345 if (llvm::MemoryBuffer *buf = I.Buffer) {
9346 size_t bytes = buf->getBufferSize();
9347 switch (buf->getBufferKind()) {
9348 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9349 sizes.malloc_bytes += bytes;
9350 break;
9351 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9352 sizes.mmap_bytes += bytes;
9353 break;
9354 }
9355 }
9356 }
9357}
9358
9359void ASTReader::InitializeSema(Sema &S) {
9360 SemaObj = &S;
9361 S.addExternalSource(E: this);
9362
9363 // Makes sure any declarations that were deserialized "too early"
9364 // still get added to the identifier's declaration chains.
9365 for (GlobalDeclID ID : PreloadedDeclIDs) {
9366 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9367 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9368 }
9369 PreloadedDeclIDs.clear();
9370
9371 // FIXME: What happens if these are changed by a module import?
9372 if (!FPPragmaOptions.empty()) {
9373 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9374 FPOptionsOverride NewOverrides =
9375 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9376 SemaObj->CurFPFeatures =
9377 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9378 }
9379
9380 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9381 Decl *D = GetDecl(ID);
9382 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9383 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9384 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9385 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9386 else
9387 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9388 }
9389 DeclsWithEffectsToVerify.clear();
9390
9391 SemaObj->OpenCLFeatures = OpenCLExtensions;
9392
9393 UpdateSema();
9394}
9395
9396void ASTReader::UpdateSema() {
9397 assert(SemaObj && "no Sema to update");
9398
9399 // UpdateSema() runs after each AST file is loaded, not only the first, so a
9400 // 'requires' directive from a module is registered too.
9401 for (GlobalDeclID ID : OpenMPRequiresDecls)
9402 SemaObj->OpenMP().addRequiresDecl(D: cast<OMPRequiresDecl>(Val: GetDecl(ID)));
9403 OpenMPRequiresDecls.clear();
9404
9405 // Load the offsets of the declarations that Sema references.
9406 // They will be lazily deserialized when needed.
9407 if (!SemaDeclRefs.empty()) {
9408 assert(SemaDeclRefs.size() % 3 == 0);
9409 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9410 if (!SemaObj->StdNamespace)
9411 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9412 if (!SemaObj->StdBadAlloc)
9413 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9414 if (!SemaObj->StdAlignValT)
9415 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9416 }
9417 SemaDeclRefs.clear();
9418 }
9419
9420 // Update the state of pragmas. Use the same API as if we had encountered the
9421 // pragma in the source.
9422 if(OptimizeOffPragmaLocation.isValid())
9423 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9424 if (PragmaMSStructState != -1)
9425 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9426 if (PointersToMembersPragmaLocation.isValid()) {
9427 SemaObj->ActOnPragmaMSPointersToMembers(
9428 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9429 PragmaMSPointersToMembersState,
9430 PragmaLoc: PointersToMembersPragmaLocation);
9431 }
9432 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9433 if (!RISCVVecIntrinsicPragma.empty()) {
9434 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9435 "Wrong number of RISCVVecIntrinsicPragma");
9436 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9437 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9438 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9439 }
9440
9441 if (PragmaAlignPackCurrentValue) {
9442 // The bottom of the stack might have a default value. It must be adjusted
9443 // to the current value to ensure that the packing state is preserved after
9444 // popping entries that were included/imported from a PCH/module.
9445 bool DropFirst = false;
9446 if (!PragmaAlignPackStack.empty() &&
9447 PragmaAlignPackStack.front().Location.isInvalid()) {
9448 assert(PragmaAlignPackStack.front().Value ==
9449 SemaObj->AlignPackStack.DefaultValue &&
9450 "Expected a default alignment value");
9451 SemaObj->AlignPackStack.Stack.emplace_back(
9452 Args&: PragmaAlignPackStack.front().SlotLabel,
9453 Args&: SemaObj->AlignPackStack.CurrentValue,
9454 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9455 Args&: PragmaAlignPackStack.front().PushLocation);
9456 DropFirst = true;
9457 }
9458 for (const auto &Entry :
9459 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9460 SemaObj->AlignPackStack.Stack.emplace_back(
9461 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9462 }
9463 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9464 assert(*PragmaAlignPackCurrentValue ==
9465 SemaObj->AlignPackStack.DefaultValue &&
9466 "Expected a default align and pack value");
9467 // Keep the current values.
9468 } else {
9469 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9470 SemaObj->AlignPackStack.CurrentPragmaLocation =
9471 PragmaAlignPackCurrentLocation;
9472 }
9473 }
9474 if (FpPragmaCurrentValue) {
9475 // The bottom of the stack might have a default value. It must be adjusted
9476 // to the current value to ensure that fp-pragma state is preserved after
9477 // popping entries that were included/imported from a PCH/module.
9478 bool DropFirst = false;
9479 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9480 assert(FpPragmaStack.front().Value ==
9481 SemaObj->FpPragmaStack.DefaultValue &&
9482 "Expected a default pragma float_control value");
9483 SemaObj->FpPragmaStack.Stack.emplace_back(
9484 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9485 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9486 Args&: FpPragmaStack.front().PushLocation);
9487 DropFirst = true;
9488 }
9489 for (const auto &Entry :
9490 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9491 SemaObj->FpPragmaStack.Stack.emplace_back(
9492 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9493 if (FpPragmaCurrentLocation.isInvalid()) {
9494 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9495 "Expected a default pragma float_control value");
9496 // Keep the current values.
9497 } else {
9498 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9499 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9500 }
9501 }
9502
9503 // For non-modular AST files, restore visiblity of modules.
9504 for (auto &Import : PendingImportedModulesSema) {
9505 if (Import.ImportLoc.isInvalid())
9506 continue;
9507 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9508 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9509 }
9510 }
9511 PendingImportedModulesSema.clear();
9512}
9513
9514IdentifierInfo *ASTReader::get(StringRef Name) {
9515 // Note that we are loading an identifier.
9516 Deserializing AnIdentifier(this);
9517
9518 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9519 NumIdentifierLookups,
9520 NumIdentifierLookupHits);
9521
9522 // We don't need to do identifier table lookups in C++ modules (we preload
9523 // all interesting declarations, and don't need to use the scope for name
9524 // lookups). Perform the lookup in PCH files, though, since we don't build
9525 // a complete initial identifier table if we're carrying on from a PCH.
9526 if (PP.getLangOpts().CPlusPlus) {
9527 for (auto *F : ModuleMgr.pch_modules())
9528 if (Visitor(*F))
9529 break;
9530 } else {
9531 // If there is a global index, look there first to determine which modules
9532 // provably do not have any results for this identifier.
9533 GlobalModuleIndex::HitSet Hits;
9534 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9535 if (!loadGlobalIndex()) {
9536 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9537 HitsPtr = &Hits;
9538 }
9539 }
9540
9541 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9542 }
9543
9544 IdentifierInfo *II = Visitor.getIdentifierInfo();
9545 markIdentifierUpToDate(II);
9546 return II;
9547}
9548
9549namespace clang {
9550
9551 /// An identifier-lookup iterator that enumerates all of the
9552 /// identifiers stored within a set of AST files.
9553 class ASTIdentifierIterator : public IdentifierIterator {
9554 /// The AST reader whose identifiers are being enumerated.
9555 const ASTReader &Reader;
9556
9557 /// The current index into the chain of AST files stored in
9558 /// the AST reader.
9559 unsigned Index;
9560
9561 /// The current position within the identifier lookup table
9562 /// of the current AST file.
9563 ASTIdentifierLookupTable::key_iterator Current;
9564
9565 /// The end position within the identifier lookup table of
9566 /// the current AST file.
9567 ASTIdentifierLookupTable::key_iterator End;
9568
9569 /// Whether to skip any modules in the ASTReader.
9570 bool SkipModules;
9571
9572 public:
9573 explicit ASTIdentifierIterator(const ASTReader &Reader,
9574 bool SkipModules = false);
9575
9576 StringRef Next() override;
9577 };
9578
9579} // namespace clang
9580
9581ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9582 bool SkipModules)
9583 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9584}
9585
9586StringRef ASTIdentifierIterator::Next() {
9587 while (Current == End) {
9588 // If we have exhausted all of our AST files, we're done.
9589 if (Index == 0)
9590 return StringRef();
9591
9592 --Index;
9593 ModuleFile &F = Reader.ModuleMgr[Index];
9594 if (SkipModules && F.isModule())
9595 continue;
9596
9597 ASTIdentifierLookupTable *IdTable =
9598 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9599 Current = IdTable->key_begin();
9600 End = IdTable->key_end();
9601 }
9602
9603 // We have any identifiers remaining in the current AST file; return
9604 // the next one.
9605 StringRef Result = *Current;
9606 ++Current;
9607 return Result;
9608}
9609
9610namespace {
9611
9612/// A utility for appending two IdentifierIterators.
9613class ChainedIdentifierIterator : public IdentifierIterator {
9614 std::unique_ptr<IdentifierIterator> Current;
9615 std::unique_ptr<IdentifierIterator> Queued;
9616
9617public:
9618 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9619 std::unique_ptr<IdentifierIterator> Second)
9620 : Current(std::move(First)), Queued(std::move(Second)) {}
9621
9622 StringRef Next() override {
9623 if (!Current)
9624 return StringRef();
9625
9626 StringRef result = Current->Next();
9627 if (!result.empty())
9628 return result;
9629
9630 // Try the queued iterator, which may itself be empty.
9631 Current.reset();
9632 std::swap(x&: Current, y&: Queued);
9633 return Next();
9634 }
9635};
9636
9637} // namespace
9638
9639IdentifierIterator *ASTReader::getIdentifiers() {
9640 if (!loadGlobalIndex()) {
9641 std::unique_ptr<IdentifierIterator> ReaderIter(
9642 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9643 std::unique_ptr<IdentifierIterator> ModulesIter(
9644 GlobalIndex->createIdentifierIterator());
9645 return new ChainedIdentifierIterator(std::move(ReaderIter),
9646 std::move(ModulesIter));
9647 }
9648
9649 return new ASTIdentifierIterator(*this);
9650}
9651
9652namespace clang {
9653namespace serialization {
9654
9655 class ReadMethodPoolVisitor {
9656 ASTReader &Reader;
9657 Selector Sel;
9658 unsigned PriorGeneration;
9659 unsigned InstanceBits = 0;
9660 unsigned FactoryBits = 0;
9661 bool InstanceHasMoreThanOneDecl = false;
9662 bool FactoryHasMoreThanOneDecl = false;
9663 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9664 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9665
9666 public:
9667 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9668 unsigned PriorGeneration)
9669 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9670
9671 bool operator()(ModuleFile &M) {
9672 if (!M.SelectorLookupTable)
9673 return false;
9674
9675 // If we've already searched this module file, skip it now.
9676 if (M.Generation <= PriorGeneration)
9677 return true;
9678
9679 ++Reader.NumMethodPoolTableLookups;
9680 ASTSelectorLookupTable *PoolTable
9681 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9682 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9683 if (Pos == PoolTable->end())
9684 return false;
9685
9686 ++Reader.NumMethodPoolTableHits;
9687 ++Reader.NumSelectorsRead;
9688 // FIXME: Not quite happy with the statistics here. We probably should
9689 // disable this tracking when called via LoadSelector.
9690 // Also, should entries without methods count as misses?
9691 ++Reader.NumMethodPoolEntriesRead;
9692 ASTSelectorLookupTrait::data_type Data = *Pos;
9693 if (Reader.DeserializationListener)
9694 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9695
9696 // Append methods in the reverse order, so that later we can process them
9697 // in the order they appear in the source code by iterating through
9698 // the vector in the reverse order.
9699 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9700 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9701 InstanceBits = Data.InstanceBits;
9702 FactoryBits = Data.FactoryBits;
9703 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9704 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9705 return false;
9706 }
9707
9708 /// Retrieve the instance methods found by this visitor.
9709 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9710 return InstanceMethods;
9711 }
9712
9713 /// Retrieve the instance methods found by this visitor.
9714 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9715 return FactoryMethods;
9716 }
9717
9718 unsigned getInstanceBits() const { return InstanceBits; }
9719 unsigned getFactoryBits() const { return FactoryBits; }
9720
9721 bool instanceHasMoreThanOneDecl() const {
9722 return InstanceHasMoreThanOneDecl;
9723 }
9724
9725 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9726 };
9727
9728} // namespace serialization
9729} // namespace clang
9730
9731/// Add the given set of methods to the method list.
9732static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9733 ObjCMethodList &List) {
9734 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9735 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9736}
9737
9738void ASTReader::ReadMethodPool(Selector Sel) {
9739 // Get the selector generation and update it to the current generation.
9740 unsigned &Generation = SelectorGeneration[Sel];
9741 unsigned PriorGeneration = Generation;
9742 Generation = getGeneration();
9743 SelectorOutOfDate[Sel] = false;
9744
9745 // Search for methods defined with this selector.
9746 ++NumMethodPoolLookups;
9747 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9748 ModuleMgr.visit(Visitor);
9749
9750 if (Visitor.getInstanceMethods().empty() &&
9751 Visitor.getFactoryMethods().empty())
9752 return;
9753
9754 ++NumMethodPoolHits;
9755
9756 if (!getSema())
9757 return;
9758
9759 Sema &S = *getSema();
9760 auto &Methods = S.ObjC().MethodPool[Sel];
9761
9762 Methods.first.setBits(Visitor.getInstanceBits());
9763 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9764 Methods.second.setBits(Visitor.getFactoryBits());
9765 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9766
9767 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9768 // when building a module we keep every method individually and may need to
9769 // update hasMoreThanOneDecl as we add the methods.
9770 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9771 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9772}
9773
9774void ASTReader::updateOutOfDateSelector(Selector Sel) {
9775 if (SelectorOutOfDate[Sel])
9776 ReadMethodPool(Sel);
9777}
9778
9779void ASTReader::ReadKnownNamespaces(
9780 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9781 Namespaces.clear();
9782
9783 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9784 if (NamespaceDecl *Namespace
9785 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9786 Namespaces.push_back(Elt: Namespace);
9787 }
9788}
9789
9790void ASTReader::ReadUndefinedButUsed(
9791 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9792 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9793 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9794 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9795 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9796 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9797 }
9798 UndefinedButUsed.clear();
9799}
9800
9801void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9802 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9803 Exprs) {
9804 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9805 FieldDecl *FD =
9806 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9807 uint64_t Count = DelayedDeleteExprs[Idx++];
9808 for (uint64_t C = 0; C < Count; ++C) {
9809 SourceLocation DeleteLoc =
9810 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9811 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9812 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9813 }
9814 }
9815}
9816
9817void ASTReader::ReadTentativeDefinitions(
9818 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9819 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9820 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9821 if (Var)
9822 TentativeDefs.push_back(Elt: Var);
9823 }
9824 TentativeDefinitions.clear();
9825}
9826
9827void ASTReader::ReadUnusedFileScopedDecls(
9828 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9829 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9830 DeclaratorDecl *D
9831 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9832 if (D)
9833 Decls.push_back(Elt: D);
9834 }
9835 UnusedFileScopedDecls.clear();
9836}
9837
9838void ASTReader::ReadDelegatingConstructors(
9839 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9840 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9841 CXXConstructorDecl *D
9842 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9843 if (D)
9844 Decls.push_back(Elt: D);
9845 }
9846 DelegatingCtorDecls.clear();
9847}
9848
9849void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9850 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9851 TypedefNameDecl *D
9852 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9853 if (D)
9854 Decls.push_back(Elt: D);
9855 }
9856 ExtVectorDecls.clear();
9857}
9858
9859void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9860 llvm::SmallPtrSetImpl<const TypedefNameDecl *> &Decls) {
9861 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9862 ++I) {
9863 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9864 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9865 if (D)
9866 Decls.insert(Ptr: D);
9867 }
9868 UnusedLocalTypedefNameCandidates.clear();
9869}
9870
9871void ASTReader::ReadDeclsToCheckForDeferredDiags(
9872 llvm::SmallSetVector<Decl *, 4> &Decls) {
9873 for (auto I : DeclsToCheckForDeferredDiags) {
9874 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9875 if (D)
9876 Decls.insert(X: D);
9877 }
9878 DeclsToCheckForDeferredDiags.clear();
9879}
9880
9881void ASTReader::ReadReferencedSelectors(
9882 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9883 if (ReferencedSelectorsData.empty())
9884 return;
9885
9886 // If there are @selector references added them to its pool. This is for
9887 // implementation of -Wselector.
9888 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9889 unsigned I = 0;
9890 while (I < DataSize) {
9891 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9892 SourceLocation SelLoc
9893 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9894 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9895 }
9896 ReferencedSelectorsData.clear();
9897}
9898
9899void ASTReader::ReadWeakUndeclaredIdentifiers(
9900 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9901 if (WeakUndeclaredIdentifiers.empty())
9902 return;
9903
9904 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9905 IdentifierInfo *WeakId
9906 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9907 IdentifierInfo *AliasId
9908 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9909 SourceLocation Loc =
9910 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9911 WeakInfo WI(AliasId, Loc);
9912 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9913 }
9914 WeakUndeclaredIdentifiers.clear();
9915}
9916
9917void ASTReader::ReadExtnameUndeclaredIdentifiers(
9918 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9919 if (ExtnameUndeclaredIdentifiers.empty())
9920 return;
9921
9922 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9923 IdentifierInfo *NameId =
9924 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9925 IdentifierInfo *ExtnameId =
9926 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I + 1]);
9927 SourceLocation Loc =
9928 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I + 2]);
9929 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9930 Ctx&: getContext(), Label: ExtnameId->getName(),
9931 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9932 AttributeCommonInfo::Form::Pragma()));
9933 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9934 }
9935 ExtnameUndeclaredIdentifiers.clear();
9936}
9937
9938void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9939 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9940 ExternalVTableUse VT;
9941 VTableUse &TableInfo = VTableUses[Idx++];
9942 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9943 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9944 VT.DefinitionRequired = TableInfo.Used;
9945 VTables.push_back(Elt: VT);
9946 }
9947
9948 VTableUses.clear();
9949}
9950
9951void ASTReader::ReadPendingInstantiations(
9952 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9953 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9954 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9955 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9956 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9957
9958 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9959 }
9960 PendingInstantiations.clear();
9961}
9962
9963void ASTReader::ReadLateParsedTemplates(
9964 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9965 &LPTMap) {
9966 for (auto &LPT : LateParsedTemplates) {
9967 ModuleFile *FMod = LPT.first;
9968 RecordDataImpl &LateParsed = LPT.second;
9969 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9970 /* In loop */) {
9971 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9972
9973 auto LT = std::make_unique<LateParsedTemplate>();
9974 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9975 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9976
9977 ModuleFile *F = getOwningModuleFile(D: LT->D);
9978 assert(F && "No module");
9979
9980 unsigned TokN = LateParsed[Idx++];
9981 LT->Toks.reserve(N: TokN);
9982 for (unsigned T = 0; T < TokN; ++T)
9983 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9984
9985 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9986 }
9987 }
9988
9989 LateParsedTemplates.clear();
9990}
9991
9992void ASTReader::LoadSelector(Selector Sel) {
9993 // It would be complicated to avoid reading the methods anyway. So don't.
9994 ReadMethodPool(Sel);
9995}
9996
9997void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9998 assert(ID && "Non-zero identifier ID required");
9999 unsigned Index = translateIdentifierIDToIndex(ID).second;
10000 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
10001 IdentifiersLoaded[Index] = II;
10002 if (DeserializationListener)
10003 DeserializationListener->IdentifierRead(ID, II);
10004}
10005
10006/// Set the globally-visible declarations associated with the given
10007/// identifier.
10008///
10009/// If the AST reader is currently in a state where the given declaration IDs
10010/// cannot safely be resolved, they are queued until it is safe to resolve
10011/// them.
10012///
10013/// \param II an IdentifierInfo that refers to one or more globally-visible
10014/// declarations.
10015///
10016/// \param DeclIDs the set of declaration IDs with the name @p II that are
10017/// visible at global scope.
10018///
10019/// \param Decls if non-null, this vector will be populated with the set of
10020/// deserialized declarations. These declarations will not be pushed into
10021/// scope.
10022void ASTReader::SetGloballyVisibleDecls(
10023 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
10024 SmallVectorImpl<Decl *> *Decls) {
10025 if (NumCurrentElementsDeserializing && !Decls) {
10026 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
10027 return;
10028 }
10029
10030 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
10031 if (!SemaObj) {
10032 // Queue this declaration so that it will be added to the
10033 // translation unit scope and identifier's declaration chain
10034 // once a Sema object is known.
10035 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
10036 continue;
10037 }
10038
10039 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
10040
10041 // If we're simply supposed to record the declarations, do so now.
10042 if (Decls) {
10043 Decls->push_back(Elt: D);
10044 continue;
10045 }
10046
10047 // Introduce this declaration into the translation-unit scope
10048 // and add it to the declaration chain for this identifier, so
10049 // that (unqualified) name lookup will find it.
10050 pushExternalDeclIntoScope(D, Name: II);
10051 }
10052}
10053
10054std::pair<ModuleFile *, unsigned>
10055ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
10056 if (ID == 0)
10057 return {nullptr, 0};
10058
10059 unsigned ModuleFileIndex = ID >> 32;
10060 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
10061
10062 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
10063 assert(getModuleManager().size() > ModuleFileIndex - 1);
10064
10065 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10066 assert(LocalID < MF.LocalNumIdentifiers);
10067 return {&MF, MF.BaseIdentifierID + LocalID};
10068}
10069
10070IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
10071 if (ID == 0)
10072 return nullptr;
10073
10074 if (IdentifiersLoaded.empty()) {
10075 Error(Msg: "no identifier table in AST file");
10076 return nullptr;
10077 }
10078
10079 auto [M, Index] = translateIdentifierIDToIndex(ID);
10080 if (!IdentifiersLoaded[Index]) {
10081 assert(M != nullptr && "Untranslated Identifier ID?");
10082 assert(Index >= M->BaseIdentifierID);
10083 unsigned LocalIndex = Index - M->BaseIdentifierID;
10084 const unsigned char *Data =
10085 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
10086
10087 ASTIdentifierLookupTrait Trait(*this, *M);
10088 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
10089 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
10090 auto &II = PP.getIdentifierTable().get(Name: Key);
10091 IdentifiersLoaded[Index] = &II;
10092 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
10093 markIdentifierFromAST(Reader&: *this, II, IsModule);
10094 if (DeserializationListener)
10095 DeserializationListener->IdentifierRead(ID, II: &II);
10096 }
10097
10098 return IdentifiersLoaded[Index];
10099}
10100
10101IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
10102 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
10103}
10104
10105IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
10106 if (LocalID < NUM_PREDEF_IDENT_IDS)
10107 return LocalID;
10108
10109 if (!M.ModuleOffsetMap.empty())
10110 ReadModuleOffsetMap(F&: M);
10111
10112 unsigned ModuleFileIndex = LocalID >> 32;
10113 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
10114 ModuleFile *MF =
10115 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10116 assert(MF && "malformed identifier ID encoding?");
10117
10118 if (!ModuleFileIndex)
10119 LocalID -= NUM_PREDEF_IDENT_IDS;
10120
10121 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
10122}
10123
10124std::pair<ModuleFile *, unsigned>
10125ASTReader::translateMacroIDToIndex(MacroID ID) const {
10126 if (ID == 0)
10127 return {nullptr, 0};
10128
10129 unsigned ModuleFileIndex = ID >> 32;
10130 assert(ModuleFileIndex && "not translating loaded MacroID?");
10131 assert(getModuleManager().size() > ModuleFileIndex - 1);
10132 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10133
10134 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
10135 assert(LocalID < MF.LocalNumMacros);
10136 return {&MF, MF.BaseMacroID + LocalID};
10137}
10138
10139MacroInfo *ASTReader::getMacro(MacroID ID) {
10140 if (ID == 0)
10141 return nullptr;
10142
10143 if (MacrosLoaded.empty()) {
10144 Error(Msg: "no macro table in AST file");
10145 return nullptr;
10146 }
10147
10148 auto [M, Index] = translateMacroIDToIndex(ID);
10149 if (!MacrosLoaded[Index]) {
10150 assert(M != nullptr && "Untranslated Macro ID?");
10151 assert(Index >= M->BaseMacroID);
10152 unsigned LocalIndex = Index - M->BaseMacroID;
10153 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
10154 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
10155
10156 if (DeserializationListener)
10157 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
10158 }
10159
10160 return MacrosLoaded[Index];
10161}
10162
10163MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
10164 if (LocalID < NUM_PREDEF_MACRO_IDS)
10165 return LocalID;
10166
10167 if (!M.ModuleOffsetMap.empty())
10168 ReadModuleOffsetMap(F&: M);
10169
10170 unsigned ModuleFileIndex = LocalID >> 32;
10171 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
10172 ModuleFile *MF =
10173 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10174 assert(MF && "malformed identifier ID encoding?");
10175
10176 if (!ModuleFileIndex) {
10177 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10178 LocalID -= NUM_PREDEF_MACRO_IDS;
10179 }
10180
10181 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10182}
10183
10184serialization::SubmoduleID
10185ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10186 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10187 return LocalID;
10188
10189 if (!M.ModuleOffsetMap.empty())
10190 ReadModuleOffsetMap(F&: M);
10191
10192 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10193 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10194 assert(I != M.SubmoduleRemap.end()
10195 && "Invalid index into submodule index remap");
10196
10197 return LocalID + I->second;
10198}
10199
10200Module *ASTReader::getModule(unsigned ID) {
10201 return getSubmodule(GlobalID: ID);
10202}
10203
10204ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10205 if (ID & 1) {
10206 // It's a module, look it up by submodule ID.
10207 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10208 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10209 } else {
10210 // It's a prefix (preamble, PCH, ...). Look it up by index.
10211 int IndexFromEnd = static_cast<int>(ID >> 1);
10212 assert(IndexFromEnd && "got reference to unknown module file");
10213 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10214 }
10215}
10216
10217unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10218 if (!M)
10219 return 1;
10220
10221 // For a file representing a module, use the submodule ID of the top-level
10222 // module as the file ID. For any other kind of file, the number of such
10223 // files loaded beforehand will be the same on reload.
10224 // FIXME: Is this true even if we have an explicit module file and a PCH?
10225 if (M->isModule())
10226 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10227
10228 auto PCHModules = getModuleManager().pch_modules();
10229 auto I = llvm::find(Range&: PCHModules, Val: M);
10230 assert(I != PCHModules.end() && "emitting reference to unknown file");
10231 return std::distance(first: I, last: PCHModules.end()) << 1;
10232}
10233
10234std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10235 if (Module *M = getSubmodule(GlobalID: ID))
10236 return ASTSourceDescriptor(*M);
10237
10238 // If there is only a single PCH, return it instead.
10239 // Chained PCH are not supported.
10240 const auto &PCHChain = ModuleMgr.pch_modules();
10241 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10242 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10243 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10244 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10245 return ASTSourceDescriptor(ModuleName,
10246 llvm::sys::path::parent_path(path: MF.FileName),
10247 FileName, MF.Signature);
10248 }
10249 return std::nullopt;
10250}
10251
10252ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10253 auto I = DefinitionSource.find(Val: FD);
10254 if (I == DefinitionSource.end())
10255 return EK_ReplyHazy;
10256 return I->second ? EK_Never : EK_Always;
10257}
10258
10259bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10260 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10261}
10262
10263Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10264 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10265}
10266
10267Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10268 if (ID == 0)
10269 return Selector();
10270
10271 if (ID > SelectorsLoaded.size()) {
10272 Error(Msg: "selector ID out of range in AST file");
10273 return Selector();
10274 }
10275
10276 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10277 // Load this selector from the selector table.
10278 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10279 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10280 ModuleFile &M = *I->second;
10281 ASTSelectorLookupTrait Trait(*this, M);
10282 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10283 SelectorsLoaded[ID - 1] =
10284 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10285 if (DeserializationListener)
10286 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10287 }
10288
10289 return SelectorsLoaded[ID - 1];
10290}
10291
10292Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10293 return DecodeSelector(ID);
10294}
10295
10296uint32_t ASTReader::GetNumExternalSelectors() {
10297 // ID 0 (the null selector) is considered an external selector.
10298 return getTotalNumSelectors() + 1;
10299}
10300
10301serialization::SelectorID
10302ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10303 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10304 return LocalID;
10305
10306 if (!M.ModuleOffsetMap.empty())
10307 ReadModuleOffsetMap(F&: M);
10308
10309 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10310 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10311 assert(I != M.SelectorRemap.end()
10312 && "Invalid index into selector index remap");
10313
10314 return LocalID + I->second;
10315}
10316
10317DeclarationNameLoc
10318ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10319 switch (Name.getNameKind()) {
10320 case DeclarationName::CXXConstructorName:
10321 case DeclarationName::CXXDestructorName:
10322 case DeclarationName::CXXConversionFunctionName:
10323 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10324
10325 case DeclarationName::CXXOperatorName:
10326 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10327
10328 case DeclarationName::CXXLiteralOperatorName:
10329 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10330 Loc: readSourceLocation());
10331
10332 case DeclarationName::Identifier:
10333 case DeclarationName::ObjCZeroArgSelector:
10334 case DeclarationName::ObjCOneArgSelector:
10335 case DeclarationName::ObjCMultiArgSelector:
10336 case DeclarationName::CXXUsingDirective:
10337 case DeclarationName::CXXDeductionGuideName:
10338 break;
10339 }
10340 return DeclarationNameLoc();
10341}
10342
10343DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10344 DeclarationNameInfo NameInfo;
10345 NameInfo.setName(readDeclarationName());
10346 NameInfo.setLoc(readSourceLocation());
10347 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10348 return NameInfo;
10349}
10350
10351TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10352 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10353}
10354
10355SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10356 auto Kind = readInt();
10357 auto ResultType = readQualType();
10358 auto Value = readAPInt();
10359 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10360 assert(Op.isValid());
10361 return Op;
10362}
10363
10364void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10365 Info.QualifierLoc = readNestedNameSpecifierLoc();
10366 unsigned NumTPLists = readInt();
10367 Info.NumTemplParamLists = NumTPLists;
10368 if (NumTPLists) {
10369 Info.TemplParamLists =
10370 new (getContext()) TemplateParameterList *[NumTPLists];
10371 for (unsigned i = 0; i != NumTPLists; ++i)
10372 Info.TemplParamLists[i] = readTemplateParameterList();
10373 }
10374}
10375
10376TemplateParameterList *
10377ASTRecordReader::readTemplateParameterList() {
10378 SourceLocation TemplateLoc = readSourceLocation();
10379 SourceLocation LAngleLoc = readSourceLocation();
10380 SourceLocation RAngleLoc = readSourceLocation();
10381
10382 unsigned NumParams = readInt();
10383 SmallVector<NamedDecl *, 16> Params;
10384 Params.reserve(N: NumParams);
10385 while (NumParams--)
10386 Params.push_back(Elt: readDeclAs<NamedDecl>());
10387
10388 bool HasRequiresClause = readBool();
10389 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10390
10391 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10392 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10393 return TemplateParams;
10394}
10395
10396void ASTRecordReader::readTemplateArgumentList(
10397 SmallVectorImpl<TemplateArgument> &TemplArgs,
10398 bool Canonicalize) {
10399 unsigned NumTemplateArgs = readInt();
10400 TemplArgs.reserve(N: NumTemplateArgs);
10401 while (NumTemplateArgs--)
10402 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10403}
10404
10405/// Read a UnresolvedSet structure.
10406void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10407 unsigned NumDecls = readInt();
10408 Set.reserve(C&: getContext(), N: NumDecls);
10409 while (NumDecls--) {
10410 GlobalDeclID ID = readDeclID();
10411 AccessSpecifier AS = (AccessSpecifier) readInt();
10412 Set.addLazyDecl(C&: getContext(), ID, AS);
10413 }
10414}
10415
10416CXXBaseSpecifier
10417ASTRecordReader::readCXXBaseSpecifier() {
10418 bool isVirtual = readBool();
10419 bool isBaseOfClass = readBool();
10420 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10421 bool inheritConstructors = readBool();
10422 TypeSourceInfo *TInfo = readTypeSourceInfo();
10423 SourceRange Range = readSourceRange();
10424 SourceLocation EllipsisLoc = readSourceLocation();
10425 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10426 EllipsisLoc);
10427 Result.setInheritConstructors(inheritConstructors);
10428 return Result;
10429}
10430
10431CXXCtorInitializer **
10432ASTRecordReader::readCXXCtorInitializers() {
10433 ASTContext &Context = getContext();
10434 unsigned NumInitializers = readInt();
10435 assert(NumInitializers && "wrote ctor initializers but have no inits");
10436 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10437 for (unsigned i = 0; i != NumInitializers; ++i) {
10438 TypeSourceInfo *TInfo = nullptr;
10439 bool IsBaseVirtual = false;
10440 FieldDecl *Member = nullptr;
10441 IndirectFieldDecl *IndirectMember = nullptr;
10442
10443 CtorInitializerType Type = (CtorInitializerType) readInt();
10444 switch (Type) {
10445 case CTOR_INITIALIZER_BASE:
10446 TInfo = readTypeSourceInfo();
10447 IsBaseVirtual = readBool();
10448 break;
10449
10450 case CTOR_INITIALIZER_DELEGATING:
10451 TInfo = readTypeSourceInfo();
10452 break;
10453
10454 case CTOR_INITIALIZER_MEMBER:
10455 Member = readDeclAs<FieldDecl>();
10456 break;
10457
10458 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10459 IndirectMember = readDeclAs<IndirectFieldDecl>();
10460 break;
10461 }
10462
10463 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10464 Expr *Init = readExpr();
10465 SourceLocation LParenLoc = readSourceLocation();
10466 SourceLocation RParenLoc = readSourceLocation();
10467
10468 CXXCtorInitializer *BOMInit;
10469 if (Type == CTOR_INITIALIZER_BASE)
10470 BOMInit = new (Context)
10471 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10472 RParenLoc, MemberOrEllipsisLoc);
10473 else if (Type == CTOR_INITIALIZER_DELEGATING)
10474 BOMInit = new (Context)
10475 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10476 else if (Member)
10477 BOMInit = new (Context)
10478 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10479 Init, RParenLoc);
10480 else
10481 BOMInit = new (Context)
10482 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10483 LParenLoc, Init, RParenLoc);
10484
10485 if (/*IsWritten*/readBool()) {
10486 unsigned SourceOrder = readInt();
10487 BOMInit->setSourceOrder(SourceOrder);
10488 }
10489
10490 CtorInitializers[i] = BOMInit;
10491 }
10492
10493 return CtorInitializers;
10494}
10495
10496NestedNameSpecifierLoc
10497ASTRecordReader::readNestedNameSpecifierLoc() {
10498 ASTContext &Context = getContext();
10499 unsigned N = readInt();
10500 NestedNameSpecifierLocBuilder Builder;
10501 for (unsigned I = 0; I != N; ++I) {
10502 auto Kind = readNestedNameSpecifierKind();
10503 switch (Kind) {
10504 case NestedNameSpecifier::Kind::Namespace: {
10505 auto *NS = readDeclAs<NamespaceBaseDecl>();
10506 SourceRange Range = readSourceRange();
10507 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10508 break;
10509 }
10510
10511 case NestedNameSpecifier::Kind::Type: {
10512 TypeSourceInfo *T = readTypeSourceInfo();
10513 if (!T)
10514 return NestedNameSpecifierLoc();
10515 SourceLocation ColonColonLoc = readSourceLocation();
10516 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10517 break;
10518 }
10519
10520 case NestedNameSpecifier::Kind::Global: {
10521 SourceLocation ColonColonLoc = readSourceLocation();
10522 Builder.MakeGlobal(Context, ColonColonLoc);
10523 break;
10524 }
10525
10526 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10527 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10528 SourceRange Range = readSourceRange();
10529 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10530 break;
10531 }
10532
10533 case NestedNameSpecifier::Kind::Null:
10534 llvm_unreachable("unexpected null nested name specifier");
10535 }
10536 }
10537
10538 return Builder.getWithLocInContext(Context);
10539}
10540
10541SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10542 unsigned &Idx) {
10543 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10544 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10545 return SourceRange(beg, end);
10546}
10547
10548llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10549 const StringRef Blob) {
10550 unsigned Count = Record[0];
10551 const char *Byte = Blob.data();
10552 llvm::BitVector Ret = llvm::BitVector(Count, false);
10553 for (unsigned I = 0; I < Count; ++Byte)
10554 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10555 if (*Byte & (1 << Bit))
10556 Ret[I] = true;
10557 return Ret;
10558}
10559
10560/// Read a floating-point value
10561llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10562 return llvm::APFloat(Sem, readAPInt());
10563}
10564
10565// Read a string
10566std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10567 unsigned Len = Record[Idx++];
10568 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10569 Idx += Len;
10570 return Result;
10571}
10572
10573StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10574 StringRef &Blob) {
10575 unsigned Len = Record[Idx++];
10576 StringRef Result = Blob.substr(Start: 0, N: Len);
10577 Blob = Blob.substr(Start: Len);
10578 return Result;
10579}
10580
10581std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10582 unsigned &Idx) {
10583 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10584}
10585
10586std::string ASTReader::ReadPath(StringRef BaseDirectory,
10587 const RecordData &Record, unsigned &Idx) {
10588 std::string Filename = ReadString(Record, Idx);
10589 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10590}
10591
10592std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10593 const RecordData &Record, unsigned &Idx,
10594 StringRef &Blob) {
10595 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10596 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10597}
10598
10599VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10600 unsigned &Idx) {
10601 unsigned Major = Record[Idx++];
10602 unsigned Minor = Record[Idx++];
10603 unsigned Subminor = Record[Idx++];
10604 if (Minor == 0)
10605 return VersionTuple(Major);
10606 if (Subminor == 0)
10607 return VersionTuple(Major, Minor - 1);
10608 return VersionTuple(Major, Minor - 1, Subminor - 1);
10609}
10610
10611CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10612 const RecordData &Record,
10613 unsigned &Idx) {
10614 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10615 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10616}
10617
10618DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10619 return Diag(Loc: CurrentImportLoc, DiagID);
10620}
10621
10622DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10623 return Diags.Report(Loc, DiagID);
10624}
10625
10626void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10627 llvm::function_ref<void()> Fn) {
10628 // When Sema is available, avoid duplicate errors.
10629 if (SemaObj) {
10630 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10631 return;
10632 }
10633
10634 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10635}
10636
10637/// Retrieve the identifier table associated with the
10638/// preprocessor.
10639IdentifierTable &ASTReader::getIdentifierTable() {
10640 return PP.getIdentifierTable();
10641}
10642
10643/// Record that the given ID maps to the given switch-case
10644/// statement.
10645void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10646 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10647 "Already have a SwitchCase with this ID");
10648 (*CurrSwitchCaseStmts)[ID] = SC;
10649}
10650
10651/// Retrieve the switch-case statement with the given ID.
10652SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10653 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10654 return (*CurrSwitchCaseStmts)[ID];
10655}
10656
10657void ASTReader::ClearSwitchCaseIDs() {
10658 CurrSwitchCaseStmts->clear();
10659}
10660
10661void ASTReader::ReadComments() {
10662 ASTContext &Context = getContext();
10663 std::vector<RawComment *> Comments;
10664 for (SmallVectorImpl<std::pair<BitstreamCursor,
10665 serialization::ModuleFile *>>::iterator
10666 I = CommentsCursors.begin(),
10667 E = CommentsCursors.end();
10668 I != E; ++I) {
10669 Comments.clear();
10670 BitstreamCursor &Cursor = I->first;
10671 serialization::ModuleFile &F = *I->second;
10672 SavedStreamPosition SavedPosition(Cursor);
10673
10674 RecordData Record;
10675 while (true) {
10676 Expected<llvm::BitstreamEntry> MaybeEntry =
10677 Cursor.advanceSkippingSubblocks(
10678 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10679 if (!MaybeEntry) {
10680 Error(Err: MaybeEntry.takeError());
10681 return;
10682 }
10683 llvm::BitstreamEntry Entry = MaybeEntry.get();
10684
10685 switch (Entry.Kind) {
10686 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10687 case llvm::BitstreamEntry::Error:
10688 Error(Msg: "malformed block record in AST file");
10689 return;
10690 case llvm::BitstreamEntry::EndBlock:
10691 goto NextCursor;
10692 case llvm::BitstreamEntry::Record:
10693 // The interesting case.
10694 break;
10695 }
10696
10697 // Read a record.
10698 Record.clear();
10699 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10700 if (!MaybeComment) {
10701 Error(Err: MaybeComment.takeError());
10702 return;
10703 }
10704 switch ((CommentRecordTypes)MaybeComment.get()) {
10705 case COMMENTS_RAW_COMMENT: {
10706 unsigned Idx = 0;
10707 SourceRange SR = ReadSourceRange(F, Record, Idx);
10708 RawComment::CommentKind Kind =
10709 (RawComment::CommentKind) Record[Idx++];
10710 bool IsTrailingComment = Record[Idx++];
10711 bool IsAlmostTrailingComment = Record[Idx++];
10712 Comments.push_back(x: new (Context) RawComment(
10713 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10714 break;
10715 }
10716 }
10717 }
10718 NextCursor:
10719 for (RawComment *C : Comments) {
10720 SourceLocation CommentLoc = C->getBeginLoc();
10721 if (CommentLoc.isValid()) {
10722 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10723 if (Loc.first.isValid())
10724 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10725 }
10726 }
10727 }
10728}
10729
10730void ASTReader::visitInputFileInfos(
10731 serialization::ModuleFile &MF, bool IncludeSystem,
10732 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10733 bool IsSystem)>
10734 Visitor) {
10735 unsigned NumUserInputs = MF.NumUserInputFiles;
10736 unsigned NumInputs = MF.InputFilesLoaded.size();
10737 assert(NumUserInputs <= NumInputs);
10738 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10739 for (unsigned I = 0; I < N; ++I) {
10740 bool IsSystem = I >= NumUserInputs;
10741 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10742 Visitor(IFI, IsSystem);
10743 }
10744}
10745
10746void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10747 bool IncludeSystem, bool Complain,
10748 llvm::function_ref<void(const serialization::InputFile &IF,
10749 bool isSystem)> Visitor) {
10750 unsigned NumUserInputs = MF.NumUserInputFiles;
10751 unsigned NumInputs = MF.InputFilesLoaded.size();
10752 assert(NumUserInputs <= NumInputs);
10753 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10754 for (unsigned I = 0; I < N; ++I) {
10755 bool IsSystem = I >= NumUserInputs;
10756 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10757 Visitor(IF, IsSystem);
10758 }
10759}
10760
10761void ASTReader::visitTopLevelModuleMaps(
10762 serialization::ModuleFile &MF,
10763 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10764 unsigned NumInputs = MF.InputFilesLoaded.size();
10765 for (unsigned I = 0; I < NumInputs; ++I) {
10766 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10767 if (IFI.TopLevel && IFI.ModuleMap)
10768 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10769 Visitor(*FE);
10770 }
10771}
10772
10773void ASTReader::finishPendingActions() {
10774 while (!PendingIdentifierInfos.empty() ||
10775 !PendingDeducedFunctionTypes.empty() ||
10776 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10777 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10778 !PendingUpdateRecords.empty() ||
10779 !PendingObjCExtensionIvarRedeclarations.empty()) {
10780 // If any identifiers with corresponding top-level declarations have
10781 // been loaded, load those declarations now.
10782 using TopLevelDeclsMap =
10783 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10784 TopLevelDeclsMap TopLevelDecls;
10785
10786 while (!PendingIdentifierInfos.empty()) {
10787 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10788 SmallVector<GlobalDeclID, 4> DeclIDs =
10789 std::move(PendingIdentifierInfos.back().second);
10790 PendingIdentifierInfos.pop_back();
10791
10792 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10793 }
10794
10795 // Load each function type that we deferred loading because it was a
10796 // deduced type that might refer to a local type declared within itself.
10797 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10798 auto *FD = PendingDeducedFunctionTypes[I].first;
10799 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10800
10801 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10802 // If we gave a function a deduced return type, remember that we need to
10803 // propagate that along the redeclaration chain.
10804 if (DT->isDeduced()) {
10805 PendingDeducedTypeUpdates.insert(
10806 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10807 continue;
10808 }
10809
10810 // The function has undeduced DeduceType return type. We hope we can
10811 // find the deduced type by iterating the redecls in other modules
10812 // later.
10813 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10814 continue;
10815 }
10816 }
10817 PendingDeducedFunctionTypes.clear();
10818
10819 // Load each variable type that we deferred loading because it was a
10820 // deduced type that might refer to a local type declared within itself.
10821 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10822 auto *VD = PendingDeducedVarTypes[I].first;
10823 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10824 }
10825 PendingDeducedVarTypes.clear();
10826
10827 // Load pending declaration chains.
10828 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10829 loadPendingDeclChain(D: PendingDeclChains[I].first,
10830 LocalOffset: PendingDeclChains[I].second);
10831 PendingDeclChains.clear();
10832
10833 // Make the most recent of the top-level declarations visible.
10834 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10835 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10836 IdentifierInfo *II = TLD->first;
10837 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10838 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10839 }
10840 }
10841
10842 // Load any pending macro definitions.
10843 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10844 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10845 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10846 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10847 // Initialize the macro history from chained-PCHs ahead of module imports.
10848 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10849 ++IDIdx) {
10850 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10851 if (!Info.M->isModule())
10852 resolvePendingMacro(II, PMInfo: Info);
10853 }
10854 // Handle module imports.
10855 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10856 ++IDIdx) {
10857 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10858 if (Info.M->isModule())
10859 resolvePendingMacro(II, PMInfo: Info);
10860 }
10861 }
10862 PendingMacroIDs.clear();
10863
10864 // Wire up the DeclContexts for Decls that we delayed setting until
10865 // recursive loading is completed.
10866 while (!PendingDeclContextInfos.empty()) {
10867 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10868 PendingDeclContextInfos.pop_front();
10869 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10870 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10871 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10872 }
10873
10874 // Perform any pending declaration updates.
10875 while (!PendingUpdateRecords.empty()) {
10876 auto Update = PendingUpdateRecords.pop_back_val();
10877 ReadingKindTracker ReadingKind(Read_Decl, *this);
10878 loadDeclUpdateRecords(Record&: Update);
10879 }
10880
10881 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10882 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10883 auto DuplicateIvars =
10884 PendingObjCExtensionIvarRedeclarations.back().second;
10885 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10886 StructuralEquivalenceContext Ctx(
10887 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10888 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10889 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10890 /*Complain =*/false,
10891 /*ErrorOnTagTypeMismatch =*/true);
10892 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10893 // Merge redeclared ivars with their predecessors.
10894 for (auto IvarPair : DuplicateIvars) {
10895 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10896 // Change semantic DeclContext but keep the lexical one.
10897 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10898 LexicalDC: Ivar->getLexicalDeclContext(),
10899 Ctx&: getContext());
10900 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10901 }
10902 // Invalidate duplicate extension and the cached ivar list.
10903 ExtensionsPair.first->setInvalidDecl();
10904 ExtensionsPair.second->getClassInterface()
10905 ->getDefinition()
10906 ->setIvarList(nullptr);
10907 } else {
10908 for (auto IvarPair : DuplicateIvars) {
10909 Diag(Loc: IvarPair.first->getLocation(),
10910 DiagID: diag::err_duplicate_ivar_declaration)
10911 << IvarPair.first->getIdentifier();
10912 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10913 }
10914 }
10915 PendingObjCExtensionIvarRedeclarations.pop_back();
10916 }
10917 }
10918
10919 // At this point, all update records for loaded decls are in place, so any
10920 // fake class definitions should have become real.
10921 assert(PendingFakeDefinitionData.empty() &&
10922 "faked up a class definition but never saw the real one");
10923
10924 // If we deserialized any C++ or Objective-C class definitions, any
10925 // Objective-C protocol definitions, or any redeclarable templates, make sure
10926 // that all redeclarations point to the definitions. Note that this can only
10927 // happen now, after the redeclaration chains have been fully wired.
10928 for (Decl *D : PendingDefinitions) {
10929 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10930 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10931 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10932 R = R->getPreviousDecl()) {
10933 assert((R == D) ==
10934 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10935 "declaration thinks it's the definition but it isn't");
10936 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10937 }
10938 }
10939
10940 continue;
10941 }
10942
10943 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10944 // Make sure that the ObjCInterfaceType points at the definition.
10945 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10946 ->Decl = ID;
10947
10948 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10949 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10950
10951 continue;
10952 }
10953
10954 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10955 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10956 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10957
10958 continue;
10959 }
10960
10961 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10962 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10963 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10964 }
10965 PendingDefinitions.clear();
10966
10967 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10968 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10969 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10970 return VD->isThisDeclarationADefinition() ||
10971 VD->isThisDeclarationADemotedDefinition();
10972
10973 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10974 return TD->isThisDeclarationADefinition() ||
10975 TD->isThisDeclarationADemotedDefinition();
10976
10977 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10978 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10979
10980 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10981 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10982
10983 // Conservatively return false here.
10984 return false;
10985 };
10986
10987 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10988 return hasDefinitionImpl(D, hasDefinitionImpl);
10989 };
10990
10991 // It is not good to prevent multiple declarations since the forward
10992 // declaration is common. Let's try to avoid duplicated definitions
10993 // only.
10994 if (!hasDefinition(D) || !hasDefinition(Previous))
10995 continue;
10996
10997 Module *PM = Previous->getOwningModule();
10998 Module *DM = D->getOwningModule();
10999 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
11000 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
11001 << (DM ? DM->getTopLevelModuleName() : "global module");
11002 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
11003 }
11004 PendingWarningForDuplicatedDefsInModuleUnits.clear();
11005
11006 // Load the bodies of any functions or methods we've encountered. We do
11007 // this now (delayed) so that we can be sure that the declaration chains
11008 // have been fully wired up (hasBody relies on this).
11009 // FIXME: We shouldn't require complete redeclaration chains here.
11010 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
11011 PBEnd = PendingBodies.end();
11012 PB != PBEnd; ++PB) {
11013 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
11014 // FIXME: Check for =delete/=default?
11015 const FunctionDecl *Defn = nullptr;
11016 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
11017 FD->setLazyBody(PB->second);
11018 } else {
11019 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
11020 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
11021
11022 if (!FD->isLateTemplateParsed() &&
11023 !NonConstDefn->isLateTemplateParsed() &&
11024 // We only perform ODR checks for decls not in the explicit
11025 // global module fragment.
11026 !shouldSkipCheckingODR(D: FD) &&
11027 !shouldSkipCheckingODR(D: NonConstDefn) &&
11028 FD->getODRHash() != NonConstDefn->getODRHash()) {
11029 if (!isa<CXXMethodDecl>(Val: FD)) {
11030 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
11031 } else if (FD->getLexicalParent()->isFileContext() &&
11032 NonConstDefn->getLexicalParent()->isFileContext()) {
11033 // Only diagnose out-of-line method definitions. If they are
11034 // in class definitions, then an error will be generated when
11035 // processing the class bodies.
11036 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
11037 }
11038 }
11039 }
11040 continue;
11041 }
11042
11043 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
11044 if (!getContext().getLangOpts().Modules || !MD->hasBody())
11045 MD->setLazyBody(PB->second);
11046 }
11047 PendingBodies.clear();
11048
11049 // Inform any classes that had members added that they now have more members.
11050 for (auto [RD, MD] : PendingAddedClassMembers) {
11051 RD->addedMember(D: MD);
11052 }
11053 PendingAddedClassMembers.clear();
11054
11055 // Do some cleanup.
11056 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
11057 getContext().deduplicateMergedDefinitionsFor(ND);
11058 PendingMergedDefinitionsToDeduplicate.clear();
11059
11060 // For each decl chain that we wanted to complete while deserializing, mark
11061 // it as "still needs to be completed".
11062 for (Decl *D : PendingIncompleteDeclChains)
11063 markIncompleteDeclChain(D);
11064 PendingIncompleteDeclChains.clear();
11065
11066 assert(PendingIdentifierInfos.empty() &&
11067 "Should be empty at the end of finishPendingActions");
11068 assert(PendingDeducedFunctionTypes.empty() &&
11069 "Should be empty at the end of finishPendingActions");
11070 assert(PendingDeducedVarTypes.empty() &&
11071 "Should be empty at the end of finishPendingActions");
11072 assert(PendingDeclChains.empty() &&
11073 "Should be empty at the end of finishPendingActions");
11074 assert(PendingMacroIDs.empty() &&
11075 "Should be empty at the end of finishPendingActions");
11076 assert(PendingDeclContextInfos.empty() &&
11077 "Should be empty at the end of finishPendingActions");
11078 assert(PendingUpdateRecords.empty() &&
11079 "Should be empty at the end of finishPendingActions");
11080 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
11081 "Should be empty at the end of finishPendingActions");
11082 assert(PendingFakeDefinitionData.empty() &&
11083 "Should be empty at the end of finishPendingActions");
11084 assert(PendingDefinitions.empty() &&
11085 "Should be empty at the end of finishPendingActions");
11086 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
11087 "Should be empty at the end of finishPendingActions");
11088 assert(PendingBodies.empty() &&
11089 "Should be empty at the end of finishPendingActions");
11090 assert(PendingAddedClassMembers.empty() &&
11091 "Should be empty at the end of finishPendingActions");
11092 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
11093 "Should be empty at the end of finishPendingActions");
11094 assert(PendingIncompleteDeclChains.empty() &&
11095 "Should be empty at the end of finishPendingActions");
11096}
11097
11098void ASTReader::diagnoseOdrViolations() {
11099 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
11100 PendingRecordOdrMergeFailures.empty() &&
11101 PendingFunctionOdrMergeFailures.empty() &&
11102 PendingEnumOdrMergeFailures.empty() &&
11103 PendingObjCInterfaceOdrMergeFailures.empty() &&
11104 PendingObjCProtocolOdrMergeFailures.empty())
11105 return;
11106
11107 // Trigger the import of the full definition of each class that had any
11108 // odr-merging problems, so we can produce better diagnostics for them.
11109 // These updates may in turn find and diagnose some ODR failures, so take
11110 // ownership of the set first.
11111 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
11112 PendingOdrMergeFailures.clear();
11113 for (auto &Merge : OdrMergeFailures) {
11114 Merge.first->buildLookup();
11115 Merge.first->decls_begin();
11116 Merge.first->bases_begin();
11117 Merge.first->vbases_begin();
11118 for (auto &RecordPair : Merge.second) {
11119 auto *RD = RecordPair.first;
11120 RD->decls_begin();
11121 RD->bases_begin();
11122 RD->vbases_begin();
11123 }
11124 }
11125
11126 // Trigger the import of the full definition of each record in C/ObjC.
11127 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
11128 PendingRecordOdrMergeFailures.clear();
11129 for (auto &Merge : RecordOdrMergeFailures) {
11130 Merge.first->decls_begin();
11131 for (auto &D : Merge.second)
11132 D->decls_begin();
11133 }
11134
11135 // Trigger the import of the full interface definition.
11136 auto ObjCInterfaceOdrMergeFailures =
11137 std::move(PendingObjCInterfaceOdrMergeFailures);
11138 PendingObjCInterfaceOdrMergeFailures.clear();
11139 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11140 Merge.first->decls_begin();
11141 for (auto &InterfacePair : Merge.second)
11142 InterfacePair.first->decls_begin();
11143 }
11144
11145 // Trigger the import of functions.
11146 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
11147 PendingFunctionOdrMergeFailures.clear();
11148 for (auto &Merge : FunctionOdrMergeFailures) {
11149 Merge.first->buildLookup();
11150 Merge.first->decls_begin();
11151 Merge.first->getBody();
11152 for (auto &FD : Merge.second) {
11153 FD->buildLookup();
11154 FD->decls_begin();
11155 FD->getBody();
11156 }
11157 }
11158
11159 // Trigger the import of enums.
11160 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
11161 PendingEnumOdrMergeFailures.clear();
11162 for (auto &Merge : EnumOdrMergeFailures) {
11163 Merge.first->decls_begin();
11164 for (auto &Enum : Merge.second) {
11165 Enum->decls_begin();
11166 }
11167 }
11168
11169 // Trigger the import of the full protocol definition.
11170 auto ObjCProtocolOdrMergeFailures =
11171 std::move(PendingObjCProtocolOdrMergeFailures);
11172 PendingObjCProtocolOdrMergeFailures.clear();
11173 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11174 Merge.first->decls_begin();
11175 for (auto &ProtocolPair : Merge.second)
11176 ProtocolPair.first->decls_begin();
11177 }
11178
11179 // For each declaration from a merged context, check that the canonical
11180 // definition of that context also contains a declaration of the same
11181 // entity.
11182 //
11183 // Caution: this loop does things that might invalidate iterators into
11184 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11185 while (!PendingOdrMergeChecks.empty()) {
11186 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11187
11188 // FIXME: Skip over implicit declarations for now. This matters for things
11189 // like implicitly-declared special member functions. This isn't entirely
11190 // correct; we can end up with multiple unmerged declarations of the same
11191 // implicit entity.
11192 if (D->isImplicit())
11193 continue;
11194
11195 DeclContext *CanonDef = D->getDeclContext();
11196
11197 bool Found = false;
11198 const Decl *DCanon = D->getCanonicalDecl();
11199
11200 for (auto *RI : D->redecls()) {
11201 if (RI->getLexicalDeclContext() == CanonDef) {
11202 Found = true;
11203 break;
11204 }
11205 }
11206 if (Found)
11207 continue;
11208
11209 // Quick check failed, time to do the slow thing. Note, we can't just
11210 // look up the name of D in CanonDef here, because the member that is
11211 // in CanonDef might not be found by name lookup (it might have been
11212 // replaced by a more recent declaration in the lookup table), and we
11213 // can't necessarily find it in the redeclaration chain because it might
11214 // be merely mergeable, not redeclarable.
11215 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11216 for (auto *CanonMember : CanonDef->decls()) {
11217 if (CanonMember->getCanonicalDecl() == DCanon) {
11218 // This can happen if the declaration is merely mergeable and not
11219 // actually redeclarable (we looked for redeclarations earlier).
11220 //
11221 // FIXME: We should be able to detect this more efficiently, without
11222 // pulling in all of the members of CanonDef.
11223 Found = true;
11224 break;
11225 }
11226 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11227 if (ND->getDeclName() == D->getDeclName())
11228 Candidates.push_back(Elt: ND);
11229 }
11230
11231 if (!Found) {
11232 // The AST doesn't like TagDecls becoming invalid after they've been
11233 // completed. We only really need to mark FieldDecls as invalid here.
11234 if (!isa<TagDecl>(Val: D))
11235 D->setInvalidDecl();
11236
11237 // Ensure we don't accidentally recursively enter deserialization while
11238 // we're producing our diagnostic.
11239 Deserializing RecursionGuard(this);
11240
11241 std::string CanonDefModule =
11242 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11243 D: cast<Decl>(Val: CanonDef));
11244 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11245 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11246 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11247
11248 if (Candidates.empty())
11249 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11250 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11251 else {
11252 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11253 Diag(Loc: Candidates[I]->getLocation(),
11254 DiagID: diag::note_module_odr_violation_possible_decl)
11255 << Candidates[I];
11256 }
11257
11258 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11259 }
11260 }
11261
11262 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11263 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11264 ObjCInterfaceOdrMergeFailures.empty() &&
11265 ObjCProtocolOdrMergeFailures.empty())
11266 return;
11267
11268 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11269 getPreprocessor().getLangOpts());
11270
11271 // Issue any pending ODR-failure diagnostics.
11272 for (auto &Merge : OdrMergeFailures) {
11273 // If we've already pointed out a specific problem with this class, don't
11274 // bother issuing a general "something's different" diagnostic.
11275 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11276 continue;
11277
11278 bool Diagnosed = false;
11279 CXXRecordDecl *FirstRecord = Merge.first;
11280 for (auto &RecordPair : Merge.second) {
11281 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11282 SecondDD: RecordPair.second)) {
11283 Diagnosed = true;
11284 break;
11285 }
11286 }
11287
11288 if (!Diagnosed) {
11289 // All definitions are updates to the same declaration. This happens if a
11290 // module instantiates the declaration of a class template specialization
11291 // and two or more other modules instantiate its definition.
11292 //
11293 // FIXME: Indicate which modules had instantiations of this definition.
11294 // FIXME: How can this even happen?
11295 Diag(Loc: Merge.first->getLocation(),
11296 DiagID: diag::err_module_odr_violation_different_instantiations)
11297 << Merge.first;
11298 }
11299 }
11300
11301 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11302 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11303 for (auto &Merge : RecordOdrMergeFailures) {
11304 // If we've already pointed out a specific problem with this class, don't
11305 // bother issuing a general "something's different" diagnostic.
11306 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11307 continue;
11308
11309 RecordDecl *FirstRecord = Merge.first;
11310 bool Diagnosed = false;
11311 for (auto *SecondRecord : Merge.second) {
11312 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11313 Diagnosed = true;
11314 break;
11315 }
11316 }
11317 (void)Diagnosed;
11318 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11319 }
11320
11321 // Issue ODR failures diagnostics for functions.
11322 for (auto &Merge : FunctionOdrMergeFailures) {
11323 FunctionDecl *FirstFunction = Merge.first;
11324 bool Diagnosed = false;
11325 for (auto &SecondFunction : Merge.second) {
11326 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11327 Diagnosed = true;
11328 break;
11329 }
11330 }
11331 (void)Diagnosed;
11332 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11333 }
11334
11335 // Issue ODR failures diagnostics for enums.
11336 for (auto &Merge : EnumOdrMergeFailures) {
11337 // If we've already pointed out a specific problem with this enum, don't
11338 // bother issuing a general "something's different" diagnostic.
11339 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11340 continue;
11341
11342 EnumDecl *FirstEnum = Merge.first;
11343 bool Diagnosed = false;
11344 for (auto &SecondEnum : Merge.second) {
11345 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11346 Diagnosed = true;
11347 break;
11348 }
11349 }
11350 (void)Diagnosed;
11351 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11352 }
11353
11354 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11355 // If we've already pointed out a specific problem with this interface,
11356 // don't bother issuing a general "something's different" diagnostic.
11357 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11358 continue;
11359
11360 bool Diagnosed = false;
11361 ObjCInterfaceDecl *FirstID = Merge.first;
11362 for (auto &InterfacePair : Merge.second) {
11363 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11364 SecondDD: InterfacePair.second)) {
11365 Diagnosed = true;
11366 break;
11367 }
11368 }
11369 (void)Diagnosed;
11370 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11371 }
11372
11373 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11374 // If we've already pointed out a specific problem with this protocol,
11375 // don't bother issuing a general "something's different" diagnostic.
11376 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11377 continue;
11378
11379 ObjCProtocolDecl *FirstProtocol = Merge.first;
11380 bool Diagnosed = false;
11381 for (auto &ProtocolPair : Merge.second) {
11382 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11383 SecondDD: ProtocolPair.second)) {
11384 Diagnosed = true;
11385 break;
11386 }
11387 }
11388 (void)Diagnosed;
11389 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11390 }
11391}
11392
11393void ASTReader::StartedDeserializing() {
11394 if (llvm::Timer *T = ReadTimer.get();
11395 ++NumCurrentElementsDeserializing == 1 && T)
11396 ReadTimeRegion.emplace(args&: T);
11397}
11398
11399void ASTReader::FinishedDeserializing() {
11400 assert(NumCurrentElementsDeserializing &&
11401 "FinishedDeserializing not paired with StartedDeserializing");
11402 if (NumCurrentElementsDeserializing == 1) {
11403 // We decrease NumCurrentElementsDeserializing only after pending actions
11404 // are finished, to avoid recursively re-calling finishPendingActions().
11405 finishPendingActions();
11406 }
11407 --NumCurrentElementsDeserializing;
11408
11409 if (NumCurrentElementsDeserializing == 0) {
11410 {
11411 // Guard variable to avoid recursively entering the process of passing
11412 // decls to consumer.
11413 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11414 /*NewValue=*/false);
11415
11416 // Propagate exception specification and deduced type updates along
11417 // redeclaration chains.
11418 //
11419 // We do this now rather than in finishPendingActions because we want to
11420 // be able to walk the complete redeclaration chains of the updated decls.
11421 while (!PendingExceptionSpecUpdates.empty() ||
11422 !PendingDeducedTypeUpdates.empty() ||
11423 !PendingUndeducedFunctionDecls.empty()) {
11424 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11425 PendingExceptionSpecUpdates.clear();
11426 for (auto Update : ESUpdates) {
11427 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11428 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11429 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11430 if (auto *Listener = getContext().getASTMutationListener())
11431 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11432 for (auto *Redecl : Update.second->redecls())
11433 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11434 }
11435
11436 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11437 PendingDeducedTypeUpdates.clear();
11438 for (auto Update : DTUpdates) {
11439 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11440 // FIXME: If the return type is already deduced, check that it
11441 // matches.
11442 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11443 ResultType: Update.second);
11444 }
11445
11446 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11447 PendingUndeducedFunctionDecls.clear();
11448 // We hope we can find the deduced type for the functions by iterating
11449 // redeclarations in other modules.
11450 for (FunctionDecl *UndeducedFD : UDTUpdates)
11451 (void)UndeducedFD->getMostRecentDecl();
11452 }
11453
11454 ReadTimeRegion.reset();
11455
11456 diagnoseOdrViolations();
11457 }
11458
11459 // We are not in recursive loading, so it's safe to pass the "interesting"
11460 // decls to the consumer.
11461 if (Consumer)
11462 PassInterestingDeclsToConsumer();
11463 }
11464}
11465
11466void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11467 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11468 // Remove any fake results before adding any real ones.
11469 auto It = PendingFakeLookupResults.find(Key: II);
11470 if (It != PendingFakeLookupResults.end()) {
11471 for (auto *ND : It->second)
11472 SemaObj->IdResolver.RemoveDecl(D: ND);
11473 // FIXME: this works around module+PCH performance issue.
11474 // Rather than erase the result from the map, which is O(n), just clear
11475 // the vector of NamedDecls.
11476 It->second.clear();
11477 }
11478 }
11479
11480 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11481 SemaObj->TUScope->AddDecl(D);
11482 } else if (SemaObj->TUScope) {
11483 // Adding the decl to IdResolver may have failed because it was already in
11484 // (even though it was not added in scope). If it is already in, make sure
11485 // it gets in the scope as well.
11486 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11487 SemaObj->TUScope->AddDecl(D);
11488 }
11489}
11490
11491ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11492 ASTContext *Context,
11493 const PCHContainerReader &PCHContainerRdr,
11494 const CodeGenOptions &CodeGenOpts,
11495 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11496 StringRef isysroot,
11497 DisableValidationForModuleKind DisableValidationKind,
11498 bool AllowASTWithCompilerErrors,
11499 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11500 bool ForceValidateUserInputs,
11501 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11502 std::unique_ptr<llvm::Timer> ReadTimer)
11503 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11504 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11505 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11506 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11507 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11508 StackHandler(Diags), PP(PP), ContextObj(Context),
11509 CodeGenOpts(CodeGenOpts),
11510 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11511 PP.getHeaderSearchInfo()),
11512 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11513 DisableValidationKind(DisableValidationKind),
11514 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11515 AllowConfigurationMismatch(AllowConfigurationMismatch),
11516 ValidateSystemInputs(ValidateSystemInputs),
11517 ForceValidateUserInputs(ForceValidateUserInputs),
11518 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11519 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11520 SourceMgr.setExternalSLocEntrySource(this);
11521
11522 PathBuf.reserve(N: 256);
11523
11524 for (const auto &Ext : Extensions) {
11525 auto BlockName = Ext->getExtensionMetadata().BlockName;
11526 auto Known = ModuleFileExtensions.find(Key: BlockName);
11527 if (Known != ModuleFileExtensions.end()) {
11528 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11529 << BlockName;
11530 continue;
11531 }
11532
11533 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11534 }
11535}
11536
11537ASTReader::~ASTReader() {
11538 if (OwnsDeserializationListener)
11539 delete DeserializationListener;
11540}
11541
11542IdentifierResolver &ASTReader::getIdResolver() {
11543 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11544}
11545
11546Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11547 unsigned AbbrevID) {
11548 Idx = 0;
11549 Record.clear();
11550 return Cursor.readRecord(AbbrevID, Vals&: Record);
11551}
11552//===----------------------------------------------------------------------===//
11553//// OMPClauseReader implementation
11554////===----------------------------------------------------------------------===//
11555
11556// This has to be in namespace clang because it's friended by all
11557// of the OMP clauses.
11558namespace clang {
11559
11560class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11561 ASTRecordReader &Record;
11562 ASTContext &Context;
11563
11564public:
11565 OMPClauseReader(ASTRecordReader &Record)
11566 : Record(Record), Context(Record.getContext()) {}
11567#define GEN_CLANG_CLAUSE_CLASS
11568#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11569#include "llvm/Frontend/OpenMP/OMP.inc"
11570 OMPClause *readClause();
11571 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11572 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11573};
11574
11575} // end namespace clang
11576
11577OMPClause *ASTRecordReader::readOMPClause() {
11578 return OMPClauseReader(*this).readClause();
11579}
11580
11581OMPClause *OMPClauseReader::readClause() {
11582 OMPClause *C = nullptr;
11583 switch (llvm::omp::Clause(Record.readInt())) {
11584 case llvm::omp::OMPC_if:
11585 C = new (Context) OMPIfClause();
11586 break;
11587 case llvm::omp::OMPC_final:
11588 C = new (Context) OMPFinalClause();
11589 break;
11590 case llvm::omp::OMPC_num_threads:
11591 C = OMPNumThreadsClause::CreateEmpty(C: Context, N: Record.readInt());
11592 break;
11593 case llvm::omp::OMPC_safelen:
11594 C = new (Context) OMPSafelenClause();
11595 break;
11596 case llvm::omp::OMPC_simdlen:
11597 C = new (Context) OMPSimdlenClause();
11598 break;
11599 case llvm::omp::OMPC_sizes: {
11600 unsigned NumSizes = Record.readInt();
11601 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11602 break;
11603 }
11604 case llvm::omp::OMPC_counts: {
11605 unsigned NumCounts = Record.readInt();
11606 C = OMPCountsClause::CreateEmpty(C: Context, NumCounts);
11607 break;
11608 }
11609 case llvm::omp::OMPC_permutation: {
11610 unsigned NumLoops = Record.readInt();
11611 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11612 break;
11613 }
11614 case llvm::omp::OMPC_full:
11615 C = new (Context) OMPFullClause();
11616 break;
11617 case llvm::omp::OMPC_partial:
11618 C = new (Context) OMPPartialClause();
11619 break;
11620 case llvm::omp::OMPC_depth:
11621 C = new (Context) OMPDepthClause();
11622 break;
11623 case llvm::omp::OMPC_looprange:
11624 C = new (Context) OMPLoopRangeClause();
11625 break;
11626 case llvm::omp::OMPC_allocator:
11627 C = new (Context) OMPAllocatorClause();
11628 break;
11629 case llvm::omp::OMPC_collapse:
11630 C = new (Context) OMPCollapseClause();
11631 break;
11632 case llvm::omp::OMPC_default:
11633 C = new (Context) OMPDefaultClause();
11634 break;
11635 case llvm::omp::OMPC_proc_bind:
11636 C = new (Context) OMPProcBindClause();
11637 break;
11638 case llvm::omp::OMPC_schedule:
11639 C = new (Context) OMPScheduleClause();
11640 break;
11641 case llvm::omp::OMPC_ordered:
11642 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11643 break;
11644 case llvm::omp::OMPC_nowait:
11645 C = new (Context) OMPNowaitClause();
11646 break;
11647 case llvm::omp::OMPC_untied:
11648 C = new (Context) OMPUntiedClause();
11649 break;
11650 case llvm::omp::OMPC_mergeable:
11651 C = new (Context) OMPMergeableClause();
11652 break;
11653 case llvm::omp::OMPC_threadset:
11654 C = new (Context) OMPThreadsetClause();
11655 break;
11656 case llvm::omp::OMPC_transparent:
11657 C = new (Context) OMPTransparentClause();
11658 break;
11659 case llvm::omp::OMPC_read:
11660 C = new (Context) OMPReadClause();
11661 break;
11662 case llvm::omp::OMPC_write:
11663 C = new (Context) OMPWriteClause();
11664 break;
11665 case llvm::omp::OMPC_update:
11666 C = new (Context) OMPUpdateClause();
11667 break;
11668 case llvm::omp::OMPC_update_depend_objects:
11669 C = OMPUpdateDependObjectsClause::CreateEmpty(C: Context);
11670 break;
11671 case llvm::omp::OMPC_capture:
11672 C = new (Context) OMPCaptureClause();
11673 break;
11674 case llvm::omp::OMPC_compare:
11675 C = new (Context) OMPCompareClause();
11676 break;
11677 case llvm::omp::OMPC_fail:
11678 C = new (Context) OMPFailClause();
11679 break;
11680 case llvm::omp::OMPC_seq_cst:
11681 C = new (Context) OMPSeqCstClause();
11682 break;
11683 case llvm::omp::OMPC_acq_rel:
11684 C = new (Context) OMPAcqRelClause();
11685 break;
11686 case llvm::omp::OMPC_absent: {
11687 unsigned NumKinds = Record.readInt();
11688 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11689 break;
11690 }
11691 case llvm::omp::OMPC_holds:
11692 C = new (Context) OMPHoldsClause();
11693 break;
11694 case llvm::omp::OMPC_contains: {
11695 unsigned NumKinds = Record.readInt();
11696 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11697 break;
11698 }
11699 case llvm::omp::OMPC_no_openmp:
11700 C = new (Context) OMPNoOpenMPClause();
11701 break;
11702 case llvm::omp::OMPC_no_openmp_routines:
11703 C = new (Context) OMPNoOpenMPRoutinesClause();
11704 break;
11705 case llvm::omp::OMPC_no_openmp_constructs:
11706 C = new (Context) OMPNoOpenMPConstructsClause();
11707 break;
11708 case llvm::omp::OMPC_no_parallelism:
11709 C = new (Context) OMPNoParallelismClause();
11710 break;
11711 case llvm::omp::OMPC_acquire:
11712 C = new (Context) OMPAcquireClause();
11713 break;
11714 case llvm::omp::OMPC_release:
11715 C = new (Context) OMPReleaseClause();
11716 break;
11717 case llvm::omp::OMPC_relaxed:
11718 C = new (Context) OMPRelaxedClause();
11719 break;
11720 case llvm::omp::OMPC_weak:
11721 C = new (Context) OMPWeakClause();
11722 break;
11723 case llvm::omp::OMPC_threads:
11724 C = new (Context) OMPThreadsClause();
11725 break;
11726 case llvm::omp::OMPC_simd:
11727 C = new (Context) OMPSIMDClause();
11728 break;
11729 case llvm::omp::OMPC_nogroup:
11730 C = new (Context) OMPNogroupClause();
11731 break;
11732 case llvm::omp::OMPC_unified_address:
11733 C = new (Context) OMPUnifiedAddressClause();
11734 break;
11735 case llvm::omp::OMPC_unified_shared_memory:
11736 C = new (Context) OMPUnifiedSharedMemoryClause();
11737 break;
11738 case llvm::omp::OMPC_reverse_offload:
11739 C = new (Context) OMPReverseOffloadClause();
11740 break;
11741 case llvm::omp::OMPC_dynamic_allocators:
11742 C = new (Context) OMPDynamicAllocatorsClause();
11743 break;
11744 case llvm::omp::OMPC_atomic_default_mem_order:
11745 C = new (Context) OMPAtomicDefaultMemOrderClause();
11746 break;
11747 case llvm::omp::OMPC_self_maps:
11748 C = new (Context) OMPSelfMapsClause();
11749 break;
11750 case llvm::omp::OMPC_at:
11751 C = new (Context) OMPAtClause();
11752 break;
11753 case llvm::omp::OMPC_severity:
11754 C = new (Context) OMPSeverityClause();
11755 break;
11756 case llvm::omp::OMPC_message:
11757 C = new (Context) OMPMessageClause();
11758 break;
11759 case llvm::omp::OMPC_private:
11760 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11761 break;
11762 case llvm::omp::OMPC_firstprivate:
11763 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11764 break;
11765 case llvm::omp::OMPC_lastprivate:
11766 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11767 break;
11768 case llvm::omp::OMPC_shared:
11769 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11770 break;
11771 case llvm::omp::OMPC_reduction: {
11772 unsigned N = Record.readInt();
11773 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11774 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11775 break;
11776 }
11777 case llvm::omp::OMPC_task_reduction:
11778 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11779 break;
11780 case llvm::omp::OMPC_in_reduction:
11781 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11782 break;
11783 case llvm::omp::OMPC_linear:
11784 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11785 break;
11786 case llvm::omp::OMPC_aligned:
11787 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11788 break;
11789 case llvm::omp::OMPC_copyin:
11790 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11791 break;
11792 case llvm::omp::OMPC_copyprivate:
11793 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11794 break;
11795 case llvm::omp::OMPC_flush:
11796 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11797 break;
11798 case llvm::omp::OMPC_depobj:
11799 C = new (Context) OMPDepobjClause();
11800 break;
11801 case llvm::omp::OMPC_depend: {
11802 unsigned NumVars = Record.readInt();
11803 unsigned NumLoops = Record.readInt();
11804 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11805 break;
11806 }
11807 case llvm::omp::OMPC_device:
11808 C = new (Context) OMPDeviceClause();
11809 break;
11810 case llvm::omp::OMPC_map: {
11811 OMPMappableExprListSizeTy Sizes;
11812 Sizes.NumVars = Record.readInt();
11813 Sizes.NumUniqueDeclarations = Record.readInt();
11814 Sizes.NumComponentLists = Record.readInt();
11815 Sizes.NumComponents = Record.readInt();
11816 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11817 break;
11818 }
11819 case llvm::omp::OMPC_num_teams:
11820 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11821 break;
11822 case llvm::omp::OMPC_thread_limit:
11823 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11824 break;
11825 case llvm::omp::OMPC_priority:
11826 C = new (Context) OMPPriorityClause();
11827 break;
11828 case llvm::omp::OMPC_grainsize:
11829 C = new (Context) OMPGrainsizeClause();
11830 break;
11831 case llvm::omp::OMPC_num_tasks:
11832 C = new (Context) OMPNumTasksClause();
11833 break;
11834 case llvm::omp::OMPC_hint:
11835 C = new (Context) OMPHintClause();
11836 break;
11837 case llvm::omp::OMPC_dist_schedule:
11838 C = new (Context) OMPDistScheduleClause();
11839 break;
11840 case llvm::omp::OMPC_defaultmap:
11841 C = new (Context) OMPDefaultmapClause();
11842 break;
11843 case llvm::omp::OMPC_to: {
11844 OMPMappableExprListSizeTy Sizes;
11845 Sizes.NumVars = Record.readInt();
11846 Sizes.NumUniqueDeclarations = Record.readInt();
11847 Sizes.NumComponentLists = Record.readInt();
11848 Sizes.NumComponents = Record.readInt();
11849 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11850 break;
11851 }
11852 case llvm::omp::OMPC_from: {
11853 OMPMappableExprListSizeTy Sizes;
11854 Sizes.NumVars = Record.readInt();
11855 Sizes.NumUniqueDeclarations = Record.readInt();
11856 Sizes.NumComponentLists = Record.readInt();
11857 Sizes.NumComponents = Record.readInt();
11858 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11859 break;
11860 }
11861 case llvm::omp::OMPC_use_device_ptr: {
11862 OMPMappableExprListSizeTy Sizes;
11863 Sizes.NumVars = Record.readInt();
11864 Sizes.NumUniqueDeclarations = Record.readInt();
11865 Sizes.NumComponentLists = Record.readInt();
11866 Sizes.NumComponents = Record.readInt();
11867 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11868 break;
11869 }
11870 case llvm::omp::OMPC_use_device_addr: {
11871 OMPMappableExprListSizeTy Sizes;
11872 Sizes.NumVars = Record.readInt();
11873 Sizes.NumUniqueDeclarations = Record.readInt();
11874 Sizes.NumComponentLists = Record.readInt();
11875 Sizes.NumComponents = Record.readInt();
11876 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11877 break;
11878 }
11879 case llvm::omp::OMPC_is_device_ptr: {
11880 OMPMappableExprListSizeTy Sizes;
11881 Sizes.NumVars = Record.readInt();
11882 Sizes.NumUniqueDeclarations = Record.readInt();
11883 Sizes.NumComponentLists = Record.readInt();
11884 Sizes.NumComponents = Record.readInt();
11885 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11886 break;
11887 }
11888 case llvm::omp::OMPC_has_device_addr: {
11889 OMPMappableExprListSizeTy Sizes;
11890 Sizes.NumVars = Record.readInt();
11891 Sizes.NumUniqueDeclarations = Record.readInt();
11892 Sizes.NumComponentLists = Record.readInt();
11893 Sizes.NumComponents = Record.readInt();
11894 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11895 break;
11896 }
11897 case llvm::omp::OMPC_allocate:
11898 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11899 break;
11900 case llvm::omp::OMPC_nontemporal:
11901 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11902 break;
11903 case llvm::omp::OMPC_inclusive:
11904 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11905 break;
11906 case llvm::omp::OMPC_exclusive:
11907 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11908 break;
11909 case llvm::omp::OMPC_order:
11910 C = new (Context) OMPOrderClause();
11911 break;
11912 case llvm::omp::OMPC_init: {
11913 unsigned VarListSize = Record.readInt();
11914 unsigned NumAttrs = Record.readInt();
11915 C = OMPInitClause::CreateEmpty(C: Context, /*NumPrefs=*/VarListSize - 1,
11916 NumAttrs);
11917 break;
11918 }
11919 case llvm::omp::OMPC_use:
11920 C = new (Context) OMPUseClause();
11921 break;
11922 case llvm::omp::OMPC_destroy:
11923 C = new (Context) OMPDestroyClause();
11924 break;
11925 case llvm::omp::OMPC_novariants:
11926 C = new (Context) OMPNovariantsClause();
11927 break;
11928 case llvm::omp::OMPC_nocontext:
11929 C = new (Context) OMPNocontextClause();
11930 break;
11931 case llvm::omp::OMPC_detach:
11932 C = new (Context) OMPDetachClause();
11933 break;
11934 case llvm::omp::OMPC_uses_allocators:
11935 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11936 break;
11937 case llvm::omp::OMPC_affinity:
11938 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11939 break;
11940 case llvm::omp::OMPC_filter:
11941 C = new (Context) OMPFilterClause();
11942 break;
11943 case llvm::omp::OMPC_bind:
11944 C = new (Context) OMPBindClause();
11945 break;
11946 case llvm::omp::OMPC_align:
11947 C = new (Context) OMPAlignClause();
11948 break;
11949 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11950 C = new (Context) OMPXDynCGroupMemClause();
11951 break;
11952 case llvm::omp::OMPC_dyn_groupprivate:
11953 C = new (Context) OMPDynGroupprivateClause();
11954 break;
11955 case llvm::omp::OMPC_doacross: {
11956 unsigned NumVars = Record.readInt();
11957 unsigned NumLoops = Record.readInt();
11958 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11959 break;
11960 }
11961 case llvm::omp::OMPC_ompx_attribute:
11962 C = new (Context) OMPXAttributeClause();
11963 break;
11964 case llvm::omp::OMPC_ompx_bare:
11965 C = new (Context) OMPXBareClause();
11966 break;
11967#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11968 case llvm::omp::Enum: \
11969 break;
11970#include "llvm/Frontend/OpenMP/OMPKinds.def"
11971 default:
11972 break;
11973 }
11974 assert(C && "Unknown OMPClause type");
11975
11976 Visit(S: C);
11977 C->setLocStart(Record.readSourceLocation());
11978 C->setLocEnd(Record.readSourceLocation());
11979
11980 return C;
11981}
11982
11983void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11984 C->setPreInitStmt(S: Record.readSubStmt(),
11985 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11986}
11987
11988void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11989 VisitOMPClauseWithPreInit(C);
11990 C->setPostUpdateExpr(Record.readSubExpr());
11991}
11992
11993void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11994 VisitOMPClauseWithPreInit(C);
11995 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11996 C->setNameModifierLoc(Record.readSourceLocation());
11997 C->setColonLoc(Record.readSourceLocation());
11998 C->setCondition(Record.readSubExpr());
11999 C->setLParenLoc(Record.readSourceLocation());
12000}
12001
12002void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12003 VisitOMPClauseWithPreInit(C);
12004 C->setCondition(Record.readSubExpr());
12005 C->setLParenLoc(Record.readSourceLocation());
12006}
12007
12008void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12009 C->setPrescriptivenessModifier(
12010 Record.readEnum<OpenMPNumThreadsClauseModifier>());
12011 C->setPrescriptivenessModifierLoc(Record.readSourceLocation());
12012 C->setDimsModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
12013 C->setDimsModifierLoc(Record.readSourceLocation());
12014 C->setDimsModifierExpr(Record.readSubExpr());
12015 VisitOMPClauseWithPreInit(C);
12016 C->setLParenLoc(Record.readSourceLocation());
12017 unsigned NumVars = C->varlist_size();
12018 SmallVector<Expr *, 16> Vars;
12019 Vars.reserve(N: NumVars);
12020 for (unsigned I = 0; I != NumVars; ++I)
12021 Vars.push_back(Elt: Record.readSubExpr());
12022 C->setVarRefs(Vars);
12023}
12024
12025void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12026 C->setSafelen(Record.readSubExpr());
12027 C->setLParenLoc(Record.readSourceLocation());
12028}
12029
12030void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12031 C->setSimdlen(Record.readSubExpr());
12032 C->setLParenLoc(Record.readSourceLocation());
12033}
12034
12035void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12036 for (Expr *&E : C->getSizesRefs())
12037 E = Record.readSubExpr();
12038 C->setLParenLoc(Record.readSourceLocation());
12039}
12040
12041void OMPClauseReader::VisitOMPCountsClause(OMPCountsClause *C) {
12042 bool HasFill = Record.readBool();
12043 if (HasFill)
12044 C->setOmpFillIndex(Record.readInt());
12045 C->setOmpFillLoc(Record.readSourceLocation());
12046 for (Expr *&E : C->getCountsRefs())
12047 E = Record.readSubExpr();
12048 C->setLParenLoc(Record.readSourceLocation());
12049}
12050
12051void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
12052 for (Expr *&E : C->getArgsRefs())
12053 E = Record.readSubExpr();
12054 C->setLParenLoc(Record.readSourceLocation());
12055}
12056
12057void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12058
12059void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12060 C->setFactor(Record.readSubExpr());
12061 C->setLParenLoc(Record.readSourceLocation());
12062}
12063
12064void OMPClauseReader::VisitOMPDepthClause(OMPDepthClause *C) {
12065 C->setDepth(Record.readSubExpr());
12066 C->setLParenLoc(Record.readSourceLocation());
12067}
12068
12069void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
12070 C->setFirst(Record.readSubExpr());
12071 C->setCount(Record.readSubExpr());
12072 C->setLParenLoc(Record.readSourceLocation());
12073 C->setFirstLoc(Record.readSourceLocation());
12074 C->setCountLoc(Record.readSourceLocation());
12075}
12076
12077void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12078 C->setAllocator(Record.readExpr());
12079 C->setLParenLoc(Record.readSourceLocation());
12080}
12081
12082void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12083 C->setNumForLoops(Record.readSubExpr());
12084 C->setLParenLoc(Record.readSourceLocation());
12085}
12086
12087void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12088 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12089 C->setLParenLoc(Record.readSourceLocation());
12090 C->setDefaultKindKwLoc(Record.readSourceLocation());
12091 C->setDefaultVariableCategory(
12092 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
12093 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
12094}
12095
12096// Read the parameter of threadset clause. This will have been saved when
12097// OMPClauseWriter is called.
12098void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
12099 C->setLParenLoc(Record.readSourceLocation());
12100 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
12101 C->setThreadsetKindLoc(ThreadsetKindLoc);
12102 OpenMPThreadsetKind TKind =
12103 static_cast<OpenMPThreadsetKind>(Record.readInt());
12104 C->setThreadsetKind(TKind);
12105}
12106
12107void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
12108 C->setLParenLoc(Record.readSourceLocation());
12109 C->setImpexTypeKind(Record.readSubExpr());
12110}
12111
12112void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12113 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12114 C->setLParenLoc(Record.readSourceLocation());
12115 C->setProcBindKindKwLoc(Record.readSourceLocation());
12116}
12117
12118void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12119 VisitOMPClauseWithPreInit(C);
12120 C->setScheduleKind(
12121 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12122 C->setFirstScheduleModifier(
12123 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12124 C->setSecondScheduleModifier(
12125 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12126 C->setChunkSize(Record.readSubExpr());
12127 C->setLParenLoc(Record.readSourceLocation());
12128 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12129 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12130 C->setScheduleKindLoc(Record.readSourceLocation());
12131 C->setCommaLoc(Record.readSourceLocation());
12132}
12133
12134void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12135 C->setNumForLoops(Record.readSubExpr());
12136 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12137 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
12138 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12139 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
12140 C->setLParenLoc(Record.readSourceLocation());
12141}
12142
12143void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12144 C->setEventHandler(Record.readSubExpr());
12145 C->setLParenLoc(Record.readSourceLocation());
12146}
12147
12148void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
12149 C->setCondition(Record.readSubExpr());
12150 C->setLParenLoc(Record.readSourceLocation());
12151}
12152
12153void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12154
12155void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12156
12157void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12158
12159void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12160
12161void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12162
12163void OMPClauseReader::VisitOMPUpdateDependObjectsClause(
12164 OMPUpdateDependObjectsClause *C) {
12165 C->setLParenLoc(Record.readSourceLocation());
12166 C->setArgumentLoc(Record.readSourceLocation());
12167 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12168}
12169
12170void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12171
12172void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12173
12174// Read the parameter of fail clause. This will have been saved when
12175// OMPClauseWriter is called.
12176void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
12177 C->setLParenLoc(Record.readSourceLocation());
12178 SourceLocation FailParameterLoc = Record.readSourceLocation();
12179 C->setFailParameterLoc(FailParameterLoc);
12180 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
12181 C->setFailParameter(CKind);
12182}
12183
12184void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
12185 unsigned Count = C->getDirectiveKinds().size();
12186 C->setLParenLoc(Record.readSourceLocation());
12187 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12188 DKVec.reserve(N: Count);
12189 for (unsigned I = 0; I < Count; I++) {
12190 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12191 }
12192 C->setDirectiveKinds(DKVec);
12193}
12194
12195void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
12196 C->setExpr(Record.readExpr());
12197 C->setLParenLoc(Record.readSourceLocation());
12198}
12199
12200void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
12201 unsigned Count = C->getDirectiveKinds().size();
12202 C->setLParenLoc(Record.readSourceLocation());
12203 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12204 DKVec.reserve(N: Count);
12205 for (unsigned I = 0; I < Count; I++) {
12206 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12207 }
12208 C->setDirectiveKinds(DKVec);
12209}
12210
12211void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12212
12213void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12214 OMPNoOpenMPRoutinesClause *) {}
12215
12216void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12217 OMPNoOpenMPConstructsClause *) {}
12218
12219void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12220
12221void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12222
12223void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12224
12225void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12226
12227void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12228
12229void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12230
12231void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12232
12233void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12234
12235void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12236
12237void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12238
12239void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12240 unsigned NumVars = C->varlist_size();
12241 SmallVector<Expr *, 16> Vars;
12242 Vars.reserve(N: NumVars);
12243 for (unsigned I = 0; I != NumVars; ++I)
12244 Vars.push_back(Elt: Record.readSubExpr());
12245 C->setVarRefs(Vars);
12246 C->setIsTarget(Record.readBool());
12247 C->setIsTargetSync(Record.readBool());
12248 C->setHasPreferAttrs(Record.readBool());
12249
12250 unsigned NumPrefs = C->varlist_size() - 1;
12251 SmallVector<unsigned, 4> Counts;
12252 SmallVector<Expr *, 8> Attrs;
12253 Counts.reserve(N: NumPrefs);
12254 for (unsigned I = 0; I < NumPrefs; ++I) {
12255 unsigned NA = Record.readInt();
12256 Counts.push_back(Elt: NA);
12257 for (unsigned J = 0; J < NA; ++J)
12258 Attrs.push_back(Elt: Record.readSubExpr());
12259 }
12260 C->setAttrs(Counts, Attrs);
12261
12262 C->setLParenLoc(Record.readSourceLocation());
12263 C->setVarLoc(Record.readSourceLocation());
12264}
12265
12266void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12267 C->setInteropVar(Record.readSubExpr());
12268 C->setLParenLoc(Record.readSourceLocation());
12269 C->setVarLoc(Record.readSourceLocation());
12270}
12271
12272void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12273 C->setInteropVar(Record.readSubExpr());
12274 C->setLParenLoc(Record.readSourceLocation());
12275 C->setVarLoc(Record.readSourceLocation());
12276}
12277
12278void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12279 VisitOMPClauseWithPreInit(C);
12280 C->setCondition(Record.readSubExpr());
12281 C->setLParenLoc(Record.readSourceLocation());
12282}
12283
12284void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12285 VisitOMPClauseWithPreInit(C);
12286 C->setCondition(Record.readSubExpr());
12287 C->setLParenLoc(Record.readSourceLocation());
12288}
12289
12290void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12291
12292void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12293 OMPUnifiedSharedMemoryClause *) {}
12294
12295void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12296
12297void
12298OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12299}
12300
12301void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12302 OMPAtomicDefaultMemOrderClause *C) {
12303 C->setAtomicDefaultMemOrderKind(
12304 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12305 C->setLParenLoc(Record.readSourceLocation());
12306 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12307}
12308
12309void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12310
12311void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12312 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12313 C->setLParenLoc(Record.readSourceLocation());
12314 C->setAtKindKwLoc(Record.readSourceLocation());
12315}
12316
12317void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12318 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12319 C->setLParenLoc(Record.readSourceLocation());
12320 C->setSeverityKindKwLoc(Record.readSourceLocation());
12321}
12322
12323void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12324 VisitOMPClauseWithPreInit(C);
12325 C->setMessageString(Record.readSubExpr());
12326 C->setLParenLoc(Record.readSourceLocation());
12327}
12328
12329void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12330 C->setLParenLoc(Record.readSourceLocation());
12331 unsigned NumVars = C->varlist_size();
12332 SmallVector<Expr *, 16> Vars;
12333 Vars.reserve(N: NumVars);
12334 for (unsigned i = 0; i != NumVars; ++i)
12335 Vars.push_back(Elt: Record.readSubExpr());
12336 C->setVarRefs(Vars);
12337 Vars.clear();
12338 for (unsigned i = 0; i != NumVars; ++i)
12339 Vars.push_back(Elt: Record.readSubExpr());
12340 C->setPrivateCopies(Vars);
12341}
12342
12343void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12344 VisitOMPClauseWithPreInit(C);
12345 C->setLParenLoc(Record.readSourceLocation());
12346 unsigned NumVars = C->varlist_size();
12347 SmallVector<Expr *, 16> Vars;
12348 Vars.reserve(N: NumVars);
12349 for (unsigned i = 0; i != NumVars; ++i)
12350 Vars.push_back(Elt: Record.readSubExpr());
12351 C->setVarRefs(Vars);
12352 Vars.clear();
12353 for (unsigned i = 0; i != NumVars; ++i)
12354 Vars.push_back(Elt: Record.readSubExpr());
12355 C->setPrivateCopies(Vars);
12356 Vars.clear();
12357 for (unsigned i = 0; i != NumVars; ++i)
12358 Vars.push_back(Elt: Record.readSubExpr());
12359 C->setInits(Vars);
12360}
12361
12362void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12363 VisitOMPClauseWithPostUpdate(C);
12364 C->setLParenLoc(Record.readSourceLocation());
12365 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12366 C->setKindLoc(Record.readSourceLocation());
12367 C->setColonLoc(Record.readSourceLocation());
12368 unsigned NumVars = C->varlist_size();
12369 SmallVector<Expr *, 16> Vars;
12370 Vars.reserve(N: NumVars);
12371 for (unsigned i = 0; i != NumVars; ++i)
12372 Vars.push_back(Elt: Record.readSubExpr());
12373 C->setVarRefs(Vars);
12374 Vars.clear();
12375 for (unsigned i = 0; i != NumVars; ++i)
12376 Vars.push_back(Elt: Record.readSubExpr());
12377 C->setPrivateCopies(Vars);
12378 Vars.clear();
12379 for (unsigned i = 0; i != NumVars; ++i)
12380 Vars.push_back(Elt: Record.readSubExpr());
12381 C->setSourceExprs(Vars);
12382 Vars.clear();
12383 for (unsigned i = 0; i != NumVars; ++i)
12384 Vars.push_back(Elt: Record.readSubExpr());
12385 C->setDestinationExprs(Vars);
12386 Vars.clear();
12387 for (unsigned i = 0; i != NumVars; ++i)
12388 Vars.push_back(Elt: Record.readSubExpr());
12389 C->setAssignmentOps(Vars);
12390}
12391
12392void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12393 C->setLParenLoc(Record.readSourceLocation());
12394 unsigned NumVars = C->varlist_size();
12395 SmallVector<Expr *, 16> Vars;
12396 Vars.reserve(N: NumVars);
12397 for (unsigned i = 0; i != NumVars; ++i)
12398 Vars.push_back(Elt: Record.readSubExpr());
12399 C->setVarRefs(Vars);
12400}
12401
12402void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12403 VisitOMPClauseWithPostUpdate(C);
12404 C->setLParenLoc(Record.readSourceLocation());
12405 C->setModifierLoc(Record.readSourceLocation());
12406 C->setColonLoc(Record.readSourceLocation());
12407 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12408 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12409 C->setQualifierLoc(NNSL);
12410 C->setNameInfo(DNI);
12411
12412 unsigned NumVars = C->varlist_size();
12413 SmallVector<Expr *, 16> Vars;
12414 Vars.reserve(N: NumVars);
12415 for (unsigned i = 0; i != NumVars; ++i)
12416 Vars.push_back(Elt: Record.readSubExpr());
12417 C->setVarRefs(Vars);
12418 Vars.clear();
12419 for (unsigned i = 0; i != NumVars; ++i)
12420 Vars.push_back(Elt: Record.readSubExpr());
12421 C->setPrivates(Vars);
12422 Vars.clear();
12423 for (unsigned i = 0; i != NumVars; ++i)
12424 Vars.push_back(Elt: Record.readSubExpr());
12425 C->setLHSExprs(Vars);
12426 Vars.clear();
12427 for (unsigned i = 0; i != NumVars; ++i)
12428 Vars.push_back(Elt: Record.readSubExpr());
12429 C->setRHSExprs(Vars);
12430 Vars.clear();
12431 for (unsigned i = 0; i != NumVars; ++i)
12432 Vars.push_back(Elt: Record.readSubExpr());
12433 C->setReductionOps(Vars);
12434 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12435 Vars.clear();
12436 for (unsigned i = 0; i != NumVars; ++i)
12437 Vars.push_back(Elt: Record.readSubExpr());
12438 C->setInscanCopyOps(Vars);
12439 Vars.clear();
12440 for (unsigned i = 0; i != NumVars; ++i)
12441 Vars.push_back(Elt: Record.readSubExpr());
12442 C->setInscanCopyArrayTemps(Vars);
12443 Vars.clear();
12444 for (unsigned i = 0; i != NumVars; ++i)
12445 Vars.push_back(Elt: Record.readSubExpr());
12446 C->setInscanCopyArrayElems(Vars);
12447 }
12448 unsigned NumFlags = Record.readInt();
12449 SmallVector<bool, 16> Flags;
12450 Flags.reserve(N: NumFlags);
12451 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12452 Flags.push_back(Elt: Record.readInt());
12453 C->setPrivateVariableReductionFlags(Flags);
12454}
12455
12456void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12457 VisitOMPClauseWithPostUpdate(C);
12458 C->setLParenLoc(Record.readSourceLocation());
12459 C->setColonLoc(Record.readSourceLocation());
12460 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12461 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12462 C->setQualifierLoc(NNSL);
12463 C->setNameInfo(DNI);
12464
12465 unsigned NumVars = C->varlist_size();
12466 SmallVector<Expr *, 16> Vars;
12467 Vars.reserve(N: NumVars);
12468 for (unsigned I = 0; I != NumVars; ++I)
12469 Vars.push_back(Elt: Record.readSubExpr());
12470 C->setVarRefs(Vars);
12471 Vars.clear();
12472 for (unsigned I = 0; I != NumVars; ++I)
12473 Vars.push_back(Elt: Record.readSubExpr());
12474 C->setPrivates(Vars);
12475 Vars.clear();
12476 for (unsigned I = 0; I != NumVars; ++I)
12477 Vars.push_back(Elt: Record.readSubExpr());
12478 C->setLHSExprs(Vars);
12479 Vars.clear();
12480 for (unsigned I = 0; I != NumVars; ++I)
12481 Vars.push_back(Elt: Record.readSubExpr());
12482 C->setRHSExprs(Vars);
12483 Vars.clear();
12484 for (unsigned I = 0; I != NumVars; ++I)
12485 Vars.push_back(Elt: Record.readSubExpr());
12486 C->setReductionOps(Vars);
12487}
12488
12489void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12490 VisitOMPClauseWithPostUpdate(C);
12491 C->setLParenLoc(Record.readSourceLocation());
12492 C->setColonLoc(Record.readSourceLocation());
12493 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12494 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12495 C->setQualifierLoc(NNSL);
12496 C->setNameInfo(DNI);
12497
12498 unsigned NumVars = C->varlist_size();
12499 SmallVector<Expr *, 16> Vars;
12500 Vars.reserve(N: NumVars);
12501 for (unsigned I = 0; I != NumVars; ++I)
12502 Vars.push_back(Elt: Record.readSubExpr());
12503 C->setVarRefs(Vars);
12504 Vars.clear();
12505 for (unsigned I = 0; I != NumVars; ++I)
12506 Vars.push_back(Elt: Record.readSubExpr());
12507 C->setPrivates(Vars);
12508 Vars.clear();
12509 for (unsigned I = 0; I != NumVars; ++I)
12510 Vars.push_back(Elt: Record.readSubExpr());
12511 C->setLHSExprs(Vars);
12512 Vars.clear();
12513 for (unsigned I = 0; I != NumVars; ++I)
12514 Vars.push_back(Elt: Record.readSubExpr());
12515 C->setRHSExprs(Vars);
12516 Vars.clear();
12517 for (unsigned I = 0; I != NumVars; ++I)
12518 Vars.push_back(Elt: Record.readSubExpr());
12519 C->setReductionOps(Vars);
12520 Vars.clear();
12521 for (unsigned I = 0; I != NumVars; ++I)
12522 Vars.push_back(Elt: Record.readSubExpr());
12523 C->setTaskgroupDescriptors(Vars);
12524}
12525
12526void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12527 VisitOMPClauseWithPostUpdate(C);
12528 C->setLParenLoc(Record.readSourceLocation());
12529 C->setColonLoc(Record.readSourceLocation());
12530 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12531 C->setModifierLoc(Record.readSourceLocation());
12532 unsigned NumVars = C->varlist_size();
12533 SmallVector<Expr *, 16> Vars;
12534 Vars.reserve(N: NumVars);
12535 for (unsigned i = 0; i != NumVars; ++i)
12536 Vars.push_back(Elt: Record.readSubExpr());
12537 C->setVarRefs(Vars);
12538 Vars.clear();
12539 for (unsigned i = 0; i != NumVars; ++i)
12540 Vars.push_back(Elt: Record.readSubExpr());
12541 C->setPrivates(Vars);
12542 Vars.clear();
12543 for (unsigned i = 0; i != NumVars; ++i)
12544 Vars.push_back(Elt: Record.readSubExpr());
12545 C->setInits(Vars);
12546 Vars.clear();
12547 for (unsigned i = 0; i != NumVars; ++i)
12548 Vars.push_back(Elt: Record.readSubExpr());
12549 C->setUpdates(Vars);
12550 Vars.clear();
12551 for (unsigned i = 0; i != NumVars; ++i)
12552 Vars.push_back(Elt: Record.readSubExpr());
12553 C->setFinals(Vars);
12554 C->setStep(Record.readSubExpr());
12555 C->setCalcStep(Record.readSubExpr());
12556 Vars.clear();
12557 for (unsigned I = 0; I != NumVars + 1; ++I)
12558 Vars.push_back(Elt: Record.readSubExpr());
12559 C->setUsedExprs(Vars);
12560}
12561
12562void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12563 C->setLParenLoc(Record.readSourceLocation());
12564 C->setColonLoc(Record.readSourceLocation());
12565 unsigned NumVars = C->varlist_size();
12566 SmallVector<Expr *, 16> Vars;
12567 Vars.reserve(N: NumVars);
12568 for (unsigned i = 0; i != NumVars; ++i)
12569 Vars.push_back(Elt: Record.readSubExpr());
12570 C->setVarRefs(Vars);
12571 C->setAlignment(Record.readSubExpr());
12572}
12573
12574void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12575 C->setLParenLoc(Record.readSourceLocation());
12576 unsigned NumVars = C->varlist_size();
12577 SmallVector<Expr *, 16> Exprs;
12578 Exprs.reserve(N: NumVars);
12579 for (unsigned i = 0; i != NumVars; ++i)
12580 Exprs.push_back(Elt: Record.readSubExpr());
12581 C->setVarRefs(Exprs);
12582 Exprs.clear();
12583 for (unsigned i = 0; i != NumVars; ++i)
12584 Exprs.push_back(Elt: Record.readSubExpr());
12585 C->setSourceExprs(Exprs);
12586 Exprs.clear();
12587 for (unsigned i = 0; i != NumVars; ++i)
12588 Exprs.push_back(Elt: Record.readSubExpr());
12589 C->setDestinationExprs(Exprs);
12590 Exprs.clear();
12591 for (unsigned i = 0; i != NumVars; ++i)
12592 Exprs.push_back(Elt: Record.readSubExpr());
12593 C->setAssignmentOps(Exprs);
12594}
12595
12596void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12597 C->setLParenLoc(Record.readSourceLocation());
12598 unsigned NumVars = C->varlist_size();
12599 SmallVector<Expr *, 16> Exprs;
12600 Exprs.reserve(N: NumVars);
12601 for (unsigned i = 0; i != NumVars; ++i)
12602 Exprs.push_back(Elt: Record.readSubExpr());
12603 C->setVarRefs(Exprs);
12604 Exprs.clear();
12605 for (unsigned i = 0; i != NumVars; ++i)
12606 Exprs.push_back(Elt: Record.readSubExpr());
12607 C->setSourceExprs(Exprs);
12608 Exprs.clear();
12609 for (unsigned i = 0; i != NumVars; ++i)
12610 Exprs.push_back(Elt: Record.readSubExpr());
12611 C->setDestinationExprs(Exprs);
12612 Exprs.clear();
12613 for (unsigned i = 0; i != NumVars; ++i)
12614 Exprs.push_back(Elt: Record.readSubExpr());
12615 C->setAssignmentOps(Exprs);
12616}
12617
12618void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12619 C->setLParenLoc(Record.readSourceLocation());
12620 unsigned NumVars = C->varlist_size();
12621 SmallVector<Expr *, 16> Vars;
12622 Vars.reserve(N: NumVars);
12623 for (unsigned i = 0; i != NumVars; ++i)
12624 Vars.push_back(Elt: Record.readSubExpr());
12625 C->setVarRefs(Vars);
12626}
12627
12628void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12629 C->setDepobj(Record.readSubExpr());
12630 C->setLParenLoc(Record.readSourceLocation());
12631}
12632
12633void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12634 C->setLParenLoc(Record.readSourceLocation());
12635 C->setModifier(Record.readSubExpr());
12636 C->setDependencyKind(
12637 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12638 C->setDependencyLoc(Record.readSourceLocation());
12639 C->setColonLoc(Record.readSourceLocation());
12640 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12641 unsigned NumVars = C->varlist_size();
12642 SmallVector<Expr *, 16> Vars;
12643 Vars.reserve(N: NumVars);
12644 for (unsigned I = 0; I != NumVars; ++I)
12645 Vars.push_back(Elt: Record.readSubExpr());
12646 C->setVarRefs(Vars);
12647 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12648 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12649}
12650
12651void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12652 VisitOMPClauseWithPreInit(C);
12653 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12654 C->setDevice(Record.readSubExpr());
12655 C->setModifierLoc(Record.readSourceLocation());
12656 C->setLParenLoc(Record.readSourceLocation());
12657}
12658
12659void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12660 C->setLParenLoc(Record.readSourceLocation());
12661 bool HasIteratorModifier = false;
12662 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12663 C->setMapTypeModifier(
12664 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12665 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12666 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12667 HasIteratorModifier = true;
12668 }
12669 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12670 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12671 C->setMapType(
12672 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12673 C->setMapLoc(Record.readSourceLocation());
12674 C->setColonLoc(Record.readSourceLocation());
12675 auto NumVars = C->varlist_size();
12676 auto UniqueDecls = C->getUniqueDeclarationsNum();
12677 auto TotalLists = C->getTotalComponentListNum();
12678 auto TotalComponents = C->getTotalComponentsNum();
12679
12680 SmallVector<Expr *, 16> Vars;
12681 Vars.reserve(N: NumVars);
12682 for (unsigned i = 0; i != NumVars; ++i)
12683 Vars.push_back(Elt: Record.readExpr());
12684 C->setVarRefs(Vars);
12685
12686 SmallVector<Expr *, 16> UDMappers;
12687 UDMappers.reserve(N: NumVars);
12688 for (unsigned I = 0; I < NumVars; ++I)
12689 UDMappers.push_back(Elt: Record.readExpr());
12690 C->setUDMapperRefs(UDMappers);
12691
12692 if (HasIteratorModifier)
12693 C->setIteratorModifier(Record.readExpr());
12694
12695 SmallVector<ValueDecl *, 16> Decls;
12696 Decls.reserve(N: UniqueDecls);
12697 for (unsigned i = 0; i < UniqueDecls; ++i)
12698 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12699 C->setUniqueDecls(Decls);
12700
12701 SmallVector<unsigned, 16> ListsPerDecl;
12702 ListsPerDecl.reserve(N: UniqueDecls);
12703 for (unsigned i = 0; i < UniqueDecls; ++i)
12704 ListsPerDecl.push_back(Elt: Record.readInt());
12705 C->setDeclNumLists(ListsPerDecl);
12706
12707 SmallVector<unsigned, 32> ListSizes;
12708 ListSizes.reserve(N: TotalLists);
12709 for (unsigned i = 0; i < TotalLists; ++i)
12710 ListSizes.push_back(Elt: Record.readInt());
12711 C->setComponentListSizes(ListSizes);
12712
12713 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12714 Components.reserve(N: TotalComponents);
12715 for (unsigned i = 0; i < TotalComponents; ++i) {
12716 Expr *AssociatedExprPr = Record.readExpr();
12717 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12718 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12719 /*IsNonContiguous=*/Args: false);
12720 }
12721 C->setComponents(Components, CLSs: ListSizes);
12722}
12723
12724void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12725 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12726 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12727 C->setLParenLoc(Record.readSourceLocation());
12728 C->setColonLoc(Record.readSourceLocation());
12729 C->setAllocator(Record.readSubExpr());
12730 C->setAlignment(Record.readSubExpr());
12731 unsigned NumVars = C->varlist_size();
12732 SmallVector<Expr *, 16> Vars;
12733 Vars.reserve(N: NumVars);
12734 for (unsigned i = 0; i != NumVars; ++i)
12735 Vars.push_back(Elt: Record.readSubExpr());
12736 C->setVarRefs(Vars);
12737}
12738
12739void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12740 C->setModifier(Record.readEnum<OpenMPNumTeamsClauseModifier>());
12741 C->setModifierLoc(Record.readSourceLocation());
12742 C->setModifierExpr(Record.readSubExpr());
12743 VisitOMPClauseWithPreInit(C);
12744 C->setLParenLoc(Record.readSourceLocation());
12745 unsigned NumVars = C->varlist_size();
12746 SmallVector<Expr *, 16> Vars;
12747 Vars.reserve(N: NumVars);
12748 for (unsigned I = 0; I != NumVars; ++I)
12749 Vars.push_back(Elt: Record.readSubExpr());
12750 C->setVarRefs(Vars);
12751}
12752
12753void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12754 C->setModifier(Record.readEnum<OpenMPThreadLimitClauseModifier>());
12755 C->setModifierLoc(Record.readSourceLocation());
12756 C->setModifierExpr(Record.readSubExpr());
12757 VisitOMPClauseWithPreInit(C);
12758 C->setLParenLoc(Record.readSourceLocation());
12759 unsigned NumVars = C->varlist_size();
12760 SmallVector<Expr *, 16> Vars;
12761 Vars.reserve(N: NumVars);
12762 for (unsigned I = 0; I != NumVars; ++I)
12763 Vars.push_back(Elt: Record.readSubExpr());
12764 C->setVarRefs(Vars);
12765}
12766
12767void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12768 VisitOMPClauseWithPreInit(C);
12769 C->setPriority(Record.readSubExpr());
12770 C->setLParenLoc(Record.readSourceLocation());
12771}
12772
12773void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12774 VisitOMPClauseWithPreInit(C);
12775 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12776 C->setGrainsize(Record.readSubExpr());
12777 C->setModifierLoc(Record.readSourceLocation());
12778 C->setLParenLoc(Record.readSourceLocation());
12779}
12780
12781void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12782 VisitOMPClauseWithPreInit(C);
12783 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12784 C->setNumTasks(Record.readSubExpr());
12785 C->setModifierLoc(Record.readSourceLocation());
12786 C->setLParenLoc(Record.readSourceLocation());
12787}
12788
12789void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12790 C->setHint(Record.readSubExpr());
12791 C->setLParenLoc(Record.readSourceLocation());
12792}
12793
12794void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12795 VisitOMPClauseWithPreInit(C);
12796 C->setDistScheduleKind(
12797 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12798 C->setChunkSize(Record.readSubExpr());
12799 C->setLParenLoc(Record.readSourceLocation());
12800 C->setDistScheduleKindLoc(Record.readSourceLocation());
12801 C->setCommaLoc(Record.readSourceLocation());
12802}
12803
12804void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12805 C->setDefaultmapKind(
12806 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12807 C->setDefaultmapModifier(
12808 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12809 C->setLParenLoc(Record.readSourceLocation());
12810 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12811 C->setDefaultmapKindLoc(Record.readSourceLocation());
12812}
12813
12814void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12815 C->setLParenLoc(Record.readSourceLocation());
12816 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12817 C->setMotionModifier(
12818 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12819 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12820 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12821 C->setIteratorModifier(Record.readExpr());
12822 }
12823 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12824 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12825 C->setColonLoc(Record.readSourceLocation());
12826 auto NumVars = C->varlist_size();
12827 auto UniqueDecls = C->getUniqueDeclarationsNum();
12828 auto TotalLists = C->getTotalComponentListNum();
12829 auto TotalComponents = C->getTotalComponentsNum();
12830
12831 SmallVector<Expr *, 16> Vars;
12832 Vars.reserve(N: NumVars);
12833 for (unsigned i = 0; i != NumVars; ++i)
12834 Vars.push_back(Elt: Record.readSubExpr());
12835 C->setVarRefs(Vars);
12836
12837 SmallVector<Expr *, 16> UDMappers;
12838 UDMappers.reserve(N: NumVars);
12839 for (unsigned I = 0; I < NumVars; ++I)
12840 UDMappers.push_back(Elt: Record.readSubExpr());
12841 C->setUDMapperRefs(UDMappers);
12842
12843 SmallVector<ValueDecl *, 16> Decls;
12844 Decls.reserve(N: UniqueDecls);
12845 for (unsigned i = 0; i < UniqueDecls; ++i)
12846 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12847 C->setUniqueDecls(Decls);
12848
12849 SmallVector<unsigned, 16> ListsPerDecl;
12850 ListsPerDecl.reserve(N: UniqueDecls);
12851 for (unsigned i = 0; i < UniqueDecls; ++i)
12852 ListsPerDecl.push_back(Elt: Record.readInt());
12853 C->setDeclNumLists(ListsPerDecl);
12854
12855 SmallVector<unsigned, 32> ListSizes;
12856 ListSizes.reserve(N: TotalLists);
12857 for (unsigned i = 0; i < TotalLists; ++i)
12858 ListSizes.push_back(Elt: Record.readInt());
12859 C->setComponentListSizes(ListSizes);
12860
12861 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12862 Components.reserve(N: TotalComponents);
12863 for (unsigned i = 0; i < TotalComponents; ++i) {
12864 Expr *AssociatedExprPr = Record.readSubExpr();
12865 bool IsNonContiguous = Record.readBool();
12866 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12867 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12868 }
12869 C->setComponents(Components, CLSs: ListSizes);
12870}
12871
12872void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12873 C->setLParenLoc(Record.readSourceLocation());
12874 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12875 C->setMotionModifier(
12876 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12877 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12878 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12879 C->setIteratorModifier(Record.readExpr());
12880 }
12881 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12882 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12883 C->setColonLoc(Record.readSourceLocation());
12884 auto NumVars = C->varlist_size();
12885 auto UniqueDecls = C->getUniqueDeclarationsNum();
12886 auto TotalLists = C->getTotalComponentListNum();
12887 auto TotalComponents = C->getTotalComponentsNum();
12888
12889 SmallVector<Expr *, 16> Vars;
12890 Vars.reserve(N: NumVars);
12891 for (unsigned i = 0; i != NumVars; ++i)
12892 Vars.push_back(Elt: Record.readSubExpr());
12893 C->setVarRefs(Vars);
12894
12895 SmallVector<Expr *, 16> UDMappers;
12896 UDMappers.reserve(N: NumVars);
12897 for (unsigned I = 0; I < NumVars; ++I)
12898 UDMappers.push_back(Elt: Record.readSubExpr());
12899 C->setUDMapperRefs(UDMappers);
12900
12901 SmallVector<ValueDecl *, 16> Decls;
12902 Decls.reserve(N: UniqueDecls);
12903 for (unsigned i = 0; i < UniqueDecls; ++i)
12904 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12905 C->setUniqueDecls(Decls);
12906
12907 SmallVector<unsigned, 16> ListsPerDecl;
12908 ListsPerDecl.reserve(N: UniqueDecls);
12909 for (unsigned i = 0; i < UniqueDecls; ++i)
12910 ListsPerDecl.push_back(Elt: Record.readInt());
12911 C->setDeclNumLists(ListsPerDecl);
12912
12913 SmallVector<unsigned, 32> ListSizes;
12914 ListSizes.reserve(N: TotalLists);
12915 for (unsigned i = 0; i < TotalLists; ++i)
12916 ListSizes.push_back(Elt: Record.readInt());
12917 C->setComponentListSizes(ListSizes);
12918
12919 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12920 Components.reserve(N: TotalComponents);
12921 for (unsigned i = 0; i < TotalComponents; ++i) {
12922 Expr *AssociatedExprPr = Record.readSubExpr();
12923 bool IsNonContiguous = Record.readBool();
12924 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12925 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12926 }
12927 C->setComponents(Components, CLSs: ListSizes);
12928}
12929
12930void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12931 C->setLParenLoc(Record.readSourceLocation());
12932 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12933 C->setFallbackModifierLoc(Record.readSourceLocation());
12934 auto NumVars = C->varlist_size();
12935 auto UniqueDecls = C->getUniqueDeclarationsNum();
12936 auto TotalLists = C->getTotalComponentListNum();
12937 auto TotalComponents = C->getTotalComponentsNum();
12938
12939 SmallVector<Expr *, 16> Vars;
12940 Vars.reserve(N: NumVars);
12941 for (unsigned i = 0; i != NumVars; ++i)
12942 Vars.push_back(Elt: Record.readSubExpr());
12943 C->setVarRefs(Vars);
12944 Vars.clear();
12945 for (unsigned i = 0; i != NumVars; ++i)
12946 Vars.push_back(Elt: Record.readSubExpr());
12947 C->setPrivateCopies(Vars);
12948 Vars.clear();
12949 for (unsigned i = 0; i != NumVars; ++i)
12950 Vars.push_back(Elt: Record.readSubExpr());
12951 C->setInits(Vars);
12952
12953 SmallVector<ValueDecl *, 16> Decls;
12954 Decls.reserve(N: UniqueDecls);
12955 for (unsigned i = 0; i < UniqueDecls; ++i)
12956 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12957 C->setUniqueDecls(Decls);
12958
12959 SmallVector<unsigned, 16> ListsPerDecl;
12960 ListsPerDecl.reserve(N: UniqueDecls);
12961 for (unsigned i = 0; i < UniqueDecls; ++i)
12962 ListsPerDecl.push_back(Elt: Record.readInt());
12963 C->setDeclNumLists(ListsPerDecl);
12964
12965 SmallVector<unsigned, 32> ListSizes;
12966 ListSizes.reserve(N: TotalLists);
12967 for (unsigned i = 0; i < TotalLists; ++i)
12968 ListSizes.push_back(Elt: Record.readInt());
12969 C->setComponentListSizes(ListSizes);
12970
12971 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12972 Components.reserve(N: TotalComponents);
12973 for (unsigned i = 0; i < TotalComponents; ++i) {
12974 auto *AssociatedExprPr = Record.readSubExpr();
12975 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12976 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12977 /*IsNonContiguous=*/Args: false);
12978 }
12979 C->setComponents(Components, CLSs: ListSizes);
12980}
12981
12982void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12983 C->setLParenLoc(Record.readSourceLocation());
12984 auto NumVars = C->varlist_size();
12985 auto UniqueDecls = C->getUniqueDeclarationsNum();
12986 auto TotalLists = C->getTotalComponentListNum();
12987 auto TotalComponents = C->getTotalComponentsNum();
12988
12989 SmallVector<Expr *, 16> Vars;
12990 Vars.reserve(N: NumVars);
12991 for (unsigned i = 0; i != NumVars; ++i)
12992 Vars.push_back(Elt: Record.readSubExpr());
12993 C->setVarRefs(Vars);
12994
12995 SmallVector<ValueDecl *, 16> Decls;
12996 Decls.reserve(N: UniqueDecls);
12997 for (unsigned i = 0; i < UniqueDecls; ++i)
12998 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12999 C->setUniqueDecls(Decls);
13000
13001 SmallVector<unsigned, 16> ListsPerDecl;
13002 ListsPerDecl.reserve(N: UniqueDecls);
13003 for (unsigned i = 0; i < UniqueDecls; ++i)
13004 ListsPerDecl.push_back(Elt: Record.readInt());
13005 C->setDeclNumLists(ListsPerDecl);
13006
13007 SmallVector<unsigned, 32> ListSizes;
13008 ListSizes.reserve(N: TotalLists);
13009 for (unsigned i = 0; i < TotalLists; ++i)
13010 ListSizes.push_back(Elt: Record.readInt());
13011 C->setComponentListSizes(ListSizes);
13012
13013 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13014 Components.reserve(N: TotalComponents);
13015 for (unsigned i = 0; i < TotalComponents; ++i) {
13016 Expr *AssociatedExpr = Record.readSubExpr();
13017 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13018 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13019 /*IsNonContiguous*/ Args: false);
13020 }
13021 C->setComponents(Components, CLSs: ListSizes);
13022}
13023
13024void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13025 C->setLParenLoc(Record.readSourceLocation());
13026 auto NumVars = C->varlist_size();
13027 auto UniqueDecls = C->getUniqueDeclarationsNum();
13028 auto TotalLists = C->getTotalComponentListNum();
13029 auto TotalComponents = C->getTotalComponentsNum();
13030
13031 SmallVector<Expr *, 16> Vars;
13032 Vars.reserve(N: NumVars);
13033 for (unsigned i = 0; i != NumVars; ++i)
13034 Vars.push_back(Elt: Record.readSubExpr());
13035 C->setVarRefs(Vars);
13036 Vars.clear();
13037
13038 SmallVector<ValueDecl *, 16> Decls;
13039 Decls.reserve(N: UniqueDecls);
13040 for (unsigned i = 0; i < UniqueDecls; ++i)
13041 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13042 C->setUniqueDecls(Decls);
13043
13044 SmallVector<unsigned, 16> ListsPerDecl;
13045 ListsPerDecl.reserve(N: UniqueDecls);
13046 for (unsigned i = 0; i < UniqueDecls; ++i)
13047 ListsPerDecl.push_back(Elt: Record.readInt());
13048 C->setDeclNumLists(ListsPerDecl);
13049
13050 SmallVector<unsigned, 32> ListSizes;
13051 ListSizes.reserve(N: TotalLists);
13052 for (unsigned i = 0; i < TotalLists; ++i)
13053 ListSizes.push_back(Elt: Record.readInt());
13054 C->setComponentListSizes(ListSizes);
13055
13056 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13057 Components.reserve(N: TotalComponents);
13058 for (unsigned i = 0; i < TotalComponents; ++i) {
13059 Expr *AssociatedExpr = Record.readSubExpr();
13060 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13061 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13062 /*IsNonContiguous=*/Args: false);
13063 }
13064 C->setComponents(Components, CLSs: ListSizes);
13065}
13066
13067void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
13068 C->setLParenLoc(Record.readSourceLocation());
13069 auto NumVars = C->varlist_size();
13070 auto UniqueDecls = C->getUniqueDeclarationsNum();
13071 auto TotalLists = C->getTotalComponentListNum();
13072 auto TotalComponents = C->getTotalComponentsNum();
13073
13074 SmallVector<Expr *, 16> Vars;
13075 Vars.reserve(N: NumVars);
13076 for (unsigned I = 0; I != NumVars; ++I)
13077 Vars.push_back(Elt: Record.readSubExpr());
13078 C->setVarRefs(Vars);
13079 Vars.clear();
13080
13081 SmallVector<ValueDecl *, 16> Decls;
13082 Decls.reserve(N: UniqueDecls);
13083 for (unsigned I = 0; I < UniqueDecls; ++I)
13084 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13085 C->setUniqueDecls(Decls);
13086
13087 SmallVector<unsigned, 16> ListsPerDecl;
13088 ListsPerDecl.reserve(N: UniqueDecls);
13089 for (unsigned I = 0; I < UniqueDecls; ++I)
13090 ListsPerDecl.push_back(Elt: Record.readInt());
13091 C->setDeclNumLists(ListsPerDecl);
13092
13093 SmallVector<unsigned, 32> ListSizes;
13094 ListSizes.reserve(N: TotalLists);
13095 for (unsigned i = 0; i < TotalLists; ++i)
13096 ListSizes.push_back(Elt: Record.readInt());
13097 C->setComponentListSizes(ListSizes);
13098
13099 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13100 Components.reserve(N: TotalComponents);
13101 for (unsigned I = 0; I < TotalComponents; ++I) {
13102 Expr *AssociatedExpr = Record.readSubExpr();
13103 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13104 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13105 /*IsNonContiguous=*/Args: false);
13106 }
13107 C->setComponents(Components, CLSs: ListSizes);
13108}
13109
13110void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
13111 C->setLParenLoc(Record.readSourceLocation());
13112 unsigned NumVars = C->varlist_size();
13113 SmallVector<Expr *, 16> Vars;
13114 Vars.reserve(N: NumVars);
13115 for (unsigned i = 0; i != NumVars; ++i)
13116 Vars.push_back(Elt: Record.readSubExpr());
13117 C->setVarRefs(Vars);
13118 Vars.clear();
13119 Vars.reserve(N: NumVars);
13120 for (unsigned i = 0; i != NumVars; ++i)
13121 Vars.push_back(Elt: Record.readSubExpr());
13122 C->setPrivateRefs(Vars);
13123}
13124
13125void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
13126 C->setLParenLoc(Record.readSourceLocation());
13127 unsigned NumVars = C->varlist_size();
13128 SmallVector<Expr *, 16> Vars;
13129 Vars.reserve(N: NumVars);
13130 for (unsigned i = 0; i != NumVars; ++i)
13131 Vars.push_back(Elt: Record.readSubExpr());
13132 C->setVarRefs(Vars);
13133}
13134
13135void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
13136 C->setLParenLoc(Record.readSourceLocation());
13137 unsigned NumVars = C->varlist_size();
13138 SmallVector<Expr *, 16> Vars;
13139 Vars.reserve(N: NumVars);
13140 for (unsigned i = 0; i != NumVars; ++i)
13141 Vars.push_back(Elt: Record.readSubExpr());
13142 C->setVarRefs(Vars);
13143}
13144
13145void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13146 C->setLParenLoc(Record.readSourceLocation());
13147 unsigned NumOfAllocators = C->getNumberOfAllocators();
13148 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13149 Data.reserve(N: NumOfAllocators);
13150 for (unsigned I = 0; I != NumOfAllocators; ++I) {
13151 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13152 D.Allocator = Record.readSubExpr();
13153 D.AllocatorTraits = Record.readSubExpr();
13154 D.LParenLoc = Record.readSourceLocation();
13155 D.RParenLoc = Record.readSourceLocation();
13156 }
13157 C->setAllocatorsData(Data);
13158}
13159
13160void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13161 C->setLParenLoc(Record.readSourceLocation());
13162 C->setModifier(Record.readSubExpr());
13163 C->setColonLoc(Record.readSourceLocation());
13164 unsigned NumOfLocators = C->varlist_size();
13165 SmallVector<Expr *, 4> Locators;
13166 Locators.reserve(N: NumOfLocators);
13167 for (unsigned I = 0; I != NumOfLocators; ++I)
13168 Locators.push_back(Elt: Record.readSubExpr());
13169 C->setVarRefs(Locators);
13170}
13171
13172void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13173 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13174 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
13175 C->setLParenLoc(Record.readSourceLocation());
13176 C->setKindKwLoc(Record.readSourceLocation());
13177 C->setModifierKwLoc(Record.readSourceLocation());
13178}
13179
13180void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
13181 VisitOMPClauseWithPreInit(C);
13182 C->setThreadID(Record.readSubExpr());
13183 C->setLParenLoc(Record.readSourceLocation());
13184}
13185
13186void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13187 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13188 C->setLParenLoc(Record.readSourceLocation());
13189 C->setBindKindLoc(Record.readSourceLocation());
13190}
13191
13192void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13193 C->setAlignment(Record.readExpr());
13194 C->setLParenLoc(Record.readSourceLocation());
13195}
13196
13197void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
13198 VisitOMPClauseWithPreInit(C);
13199 C->setSize(Record.readSubExpr());
13200 C->setLParenLoc(Record.readSourceLocation());
13201}
13202
13203void OMPClauseReader::VisitOMPDynGroupprivateClause(
13204 OMPDynGroupprivateClause *C) {
13205 VisitOMPClauseWithPreInit(C);
13206 C->setDynGroupprivateModifier(
13207 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
13208 C->setDynGroupprivateFallbackModifier(
13209 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
13210 C->setSize(Record.readSubExpr());
13211 C->setLParenLoc(Record.readSourceLocation());
13212 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
13213 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
13214}
13215
13216void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
13217 C->setLParenLoc(Record.readSourceLocation());
13218 C->setDependenceType(
13219 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
13220 C->setDependenceLoc(Record.readSourceLocation());
13221 C->setColonLoc(Record.readSourceLocation());
13222 unsigned NumVars = C->varlist_size();
13223 SmallVector<Expr *, 16> Vars;
13224 Vars.reserve(N: NumVars);
13225 for (unsigned I = 0; I != NumVars; ++I)
13226 Vars.push_back(Elt: Record.readSubExpr());
13227 C->setVarRefs(Vars);
13228 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13229 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13230}
13231
13232void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13233 AttrVec Attrs;
13234 Record.readAttributes(Attrs);
13235 C->setAttrs(Attrs);
13236 C->setLocStart(Record.readSourceLocation());
13237 C->setLParenLoc(Record.readSourceLocation());
13238 C->setLocEnd(Record.readSourceLocation());
13239}
13240
13241void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13242
13243OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13244 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13245 TI.Sets.resize(N: readUInt32());
13246 for (auto &Set : TI.Sets) {
13247 Set.Kind = readEnum<llvm::omp::TraitSet>();
13248 Set.Selectors.resize(N: readUInt32());
13249 for (auto &Selector : Set.Selectors) {
13250 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13251 Selector.ScoreOrCondition = nullptr;
13252 if (readBool())
13253 Selector.ScoreOrCondition = readExprRef();
13254 Selector.Properties.resize(N: readUInt32());
13255 for (auto &Property : Selector.Properties)
13256 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13257 }
13258 }
13259 return &TI;
13260}
13261
13262void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13263 if (!Data)
13264 return;
13265 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13266 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13267 skipInts(N: 3);
13268 }
13269 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13270 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13271 Clauses[I] = readOMPClause();
13272 Data->setClauses(Clauses);
13273 if (Data->hasAssociatedStmt())
13274 Data->setAssociatedStmt(readStmt());
13275 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13276 Data->getChildren()[I] = readStmt();
13277}
13278
13279SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13280 unsigned NumVars = readInt();
13281 llvm::SmallVector<Expr *> VarList;
13282 for (unsigned I = 0; I < NumVars; ++I)
13283 VarList.push_back(Elt: readExpr());
13284 return VarList;
13285}
13286
13287SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13288 unsigned NumExprs = readInt();
13289 llvm::SmallVector<Expr *> ExprList;
13290 for (unsigned I = 0; I < NumExprs; ++I)
13291 ExprList.push_back(Elt: readSubExpr());
13292 return ExprList;
13293}
13294
13295OpenACCClause *ASTRecordReader::readOpenACCClause() {
13296 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13297 SourceLocation BeginLoc = readSourceLocation();
13298 SourceLocation EndLoc = readSourceLocation();
13299
13300 switch (ClauseKind) {
13301 case OpenACCClauseKind::Default: {
13302 SourceLocation LParenLoc = readSourceLocation();
13303 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13304 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13305 EndLoc);
13306 }
13307 case OpenACCClauseKind::If: {
13308 SourceLocation LParenLoc = readSourceLocation();
13309 Expr *CondExpr = readSubExpr();
13310 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13311 EndLoc);
13312 }
13313 case OpenACCClauseKind::Self: {
13314 SourceLocation LParenLoc = readSourceLocation();
13315 bool isConditionExprClause = readBool();
13316 if (isConditionExprClause) {
13317 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13318 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13319 ConditionExpr: CondExpr, EndLoc);
13320 }
13321 unsigned NumVars = readInt();
13322 llvm::SmallVector<Expr *> VarList;
13323 for (unsigned I = 0; I < NumVars; ++I)
13324 VarList.push_back(Elt: readSubExpr());
13325 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13326 EndLoc);
13327 }
13328 case OpenACCClauseKind::NumGangs: {
13329 SourceLocation LParenLoc = readSourceLocation();
13330 unsigned NumClauses = readInt();
13331 llvm::SmallVector<Expr *> IntExprs;
13332 for (unsigned I = 0; I < NumClauses; ++I)
13333 IntExprs.push_back(Elt: readSubExpr());
13334 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13335 IntExprs, EndLoc);
13336 }
13337 case OpenACCClauseKind::NumWorkers: {
13338 SourceLocation LParenLoc = readSourceLocation();
13339 Expr *IntExpr = readSubExpr();
13340 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13341 IntExpr, EndLoc);
13342 }
13343 case OpenACCClauseKind::DeviceNum: {
13344 SourceLocation LParenLoc = readSourceLocation();
13345 Expr *IntExpr = readSubExpr();
13346 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13347 IntExpr, EndLoc);
13348 }
13349 case OpenACCClauseKind::DefaultAsync: {
13350 SourceLocation LParenLoc = readSourceLocation();
13351 Expr *IntExpr = readSubExpr();
13352 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13353 IntExpr, EndLoc);
13354 }
13355 case OpenACCClauseKind::VectorLength: {
13356 SourceLocation LParenLoc = readSourceLocation();
13357 Expr *IntExpr = readSubExpr();
13358 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13359 IntExpr, EndLoc);
13360 }
13361 case OpenACCClauseKind::Private: {
13362 SourceLocation LParenLoc = readSourceLocation();
13363 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13364
13365 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13366 for (unsigned I = 0; I < VarList.size(); ++I) {
13367 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13368 VarDecl *Alloca = readDeclAs<VarDecl>();
13369 RecipeList.push_back(Elt: {Alloca});
13370 }
13371
13372 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13373 VarList, InitRecipes: RecipeList, EndLoc);
13374 }
13375 case OpenACCClauseKind::Host: {
13376 SourceLocation LParenLoc = readSourceLocation();
13377 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13378 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13379 EndLoc);
13380 }
13381 case OpenACCClauseKind::Device: {
13382 SourceLocation LParenLoc = readSourceLocation();
13383 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13384 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13385 VarList, EndLoc);
13386 }
13387 case OpenACCClauseKind::FirstPrivate: {
13388 SourceLocation LParenLoc = readSourceLocation();
13389 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13390 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13391 for (unsigned I = 0; I < VarList.size(); ++I) {
13392 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13393 VarDecl *Recipe = readDeclAs<VarDecl>();
13394 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13395 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13396 }
13397
13398 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13399 VarList, InitRecipes: RecipeList, EndLoc);
13400 }
13401 case OpenACCClauseKind::Attach: {
13402 SourceLocation LParenLoc = readSourceLocation();
13403 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13404 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13405 VarList, EndLoc);
13406 }
13407 case OpenACCClauseKind::Detach: {
13408 SourceLocation LParenLoc = readSourceLocation();
13409 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13410 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13411 VarList, EndLoc);
13412 }
13413 case OpenACCClauseKind::Delete: {
13414 SourceLocation LParenLoc = readSourceLocation();
13415 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13416 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13417 VarList, EndLoc);
13418 }
13419 case OpenACCClauseKind::UseDevice: {
13420 SourceLocation LParenLoc = readSourceLocation();
13421 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13422 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13423 VarList, EndLoc);
13424 }
13425 case OpenACCClauseKind::DevicePtr: {
13426 SourceLocation LParenLoc = readSourceLocation();
13427 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13428 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13429 VarList, EndLoc);
13430 }
13431 case OpenACCClauseKind::NoCreate: {
13432 SourceLocation LParenLoc = readSourceLocation();
13433 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13434 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13435 VarList, EndLoc);
13436 }
13437 case OpenACCClauseKind::Present: {
13438 SourceLocation LParenLoc = readSourceLocation();
13439 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13440 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13441 VarList, EndLoc);
13442 }
13443 case OpenACCClauseKind::PCopy:
13444 case OpenACCClauseKind::PresentOrCopy:
13445 case OpenACCClauseKind::Copy: {
13446 SourceLocation LParenLoc = readSourceLocation();
13447 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13448 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13449 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13450 LParenLoc, Mods: ModList, VarList, EndLoc);
13451 }
13452 case OpenACCClauseKind::CopyIn:
13453 case OpenACCClauseKind::PCopyIn:
13454 case OpenACCClauseKind::PresentOrCopyIn: {
13455 SourceLocation LParenLoc = readSourceLocation();
13456 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13457 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13458 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13459 LParenLoc, Mods: ModList, VarList, EndLoc);
13460 }
13461 case OpenACCClauseKind::CopyOut:
13462 case OpenACCClauseKind::PCopyOut:
13463 case OpenACCClauseKind::PresentOrCopyOut: {
13464 SourceLocation LParenLoc = readSourceLocation();
13465 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13466 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13467 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13468 LParenLoc, Mods: ModList, VarList, EndLoc);
13469 }
13470 case OpenACCClauseKind::Create:
13471 case OpenACCClauseKind::PCreate:
13472 case OpenACCClauseKind::PresentOrCreate: {
13473 SourceLocation LParenLoc = readSourceLocation();
13474 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13475 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13476 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13477 LParenLoc, Mods: ModList, VarList, EndLoc);
13478 }
13479 case OpenACCClauseKind::Async: {
13480 SourceLocation LParenLoc = readSourceLocation();
13481 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13482 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13483 IntExpr: AsyncExpr, EndLoc);
13484 }
13485 case OpenACCClauseKind::Wait: {
13486 SourceLocation LParenLoc = readSourceLocation();
13487 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13488 SourceLocation QueuesLoc = readSourceLocation();
13489 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13490 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13491 DevNumExpr, QueuesLoc, QueueIdExprs,
13492 EndLoc);
13493 }
13494 case OpenACCClauseKind::DeviceType:
13495 case OpenACCClauseKind::DType: {
13496 SourceLocation LParenLoc = readSourceLocation();
13497 llvm::SmallVector<DeviceTypeArgument> Archs;
13498 unsigned NumArchs = readInt();
13499
13500 for (unsigned I = 0; I < NumArchs; ++I) {
13501 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13502 SourceLocation Loc = readSourceLocation();
13503 Archs.emplace_back(Args&: Loc, Args&: Ident);
13504 }
13505
13506 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13507 LParenLoc, Archs, EndLoc);
13508 }
13509 case OpenACCClauseKind::Reduction: {
13510 SourceLocation LParenLoc = readSourceLocation();
13511 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13512 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13513 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13514
13515 for (unsigned I = 0; I < VarList.size(); ++I) {
13516 VarDecl *Recipe = readDeclAs<VarDecl>();
13517
13518 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13519 3 * sizeof(int *));
13520
13521 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13522 unsigned NumCombiners = readInt();
13523 for (unsigned I = 0; I < NumCombiners; ++I) {
13524 VarDecl *LHS = readDeclAs<VarDecl>();
13525 VarDecl *RHS = readDeclAs<VarDecl>();
13526 Expr *Op = readExpr();
13527
13528 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13529 }
13530
13531 RecipeList.push_back(Elt: {Recipe, Combiners});
13532 }
13533
13534 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13535 VarList, Recipes: RecipeList, EndLoc);
13536 }
13537 case OpenACCClauseKind::Seq:
13538 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13539 case OpenACCClauseKind::NoHost:
13540 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13541 case OpenACCClauseKind::Finalize:
13542 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13543 case OpenACCClauseKind::IfPresent:
13544 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13545 case OpenACCClauseKind::Independent:
13546 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13547 case OpenACCClauseKind::Auto:
13548 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13549 case OpenACCClauseKind::Collapse: {
13550 SourceLocation LParenLoc = readSourceLocation();
13551 bool HasForce = readBool();
13552 Expr *LoopCount = readSubExpr();
13553 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13554 HasForce, LoopCount, EndLoc);
13555 }
13556 case OpenACCClauseKind::Tile: {
13557 SourceLocation LParenLoc = readSourceLocation();
13558 unsigned NumClauses = readInt();
13559 llvm::SmallVector<Expr *> SizeExprs;
13560 for (unsigned I = 0; I < NumClauses; ++I)
13561 SizeExprs.push_back(Elt: readSubExpr());
13562 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13563 SizeExprs, EndLoc);
13564 }
13565 case OpenACCClauseKind::Gang: {
13566 SourceLocation LParenLoc = readSourceLocation();
13567 unsigned NumExprs = readInt();
13568 llvm::SmallVector<OpenACCGangKind> GangKinds;
13569 llvm::SmallVector<Expr *> Exprs;
13570 for (unsigned I = 0; I < NumExprs; ++I) {
13571 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13572 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13573 Exprs.push_back(Elt: readExpr());
13574 }
13575 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13576 GangKinds, IntExprs: Exprs, EndLoc);
13577 }
13578 case OpenACCClauseKind::Worker: {
13579 SourceLocation LParenLoc = readSourceLocation();
13580 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13581 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13582 IntExpr: WorkerExpr, EndLoc);
13583 }
13584 case OpenACCClauseKind::Vector: {
13585 SourceLocation LParenLoc = readSourceLocation();
13586 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13587 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13588 IntExpr: VectorExpr, EndLoc);
13589 }
13590 case OpenACCClauseKind::Link: {
13591 SourceLocation LParenLoc = readSourceLocation();
13592 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13593 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13594 EndLoc);
13595 }
13596 case OpenACCClauseKind::DeviceResident: {
13597 SourceLocation LParenLoc = readSourceLocation();
13598 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13599 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13600 LParenLoc, VarList, EndLoc);
13601 }
13602
13603 case OpenACCClauseKind::Bind: {
13604 SourceLocation LParenLoc = readSourceLocation();
13605 bool IsString = readBool();
13606 if (IsString)
13607 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13608 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13609 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13610 ID: readIdentifier(), EndLoc);
13611 }
13612 case OpenACCClauseKind::Shortloop:
13613 case OpenACCClauseKind::Invalid:
13614 llvm_unreachable("Clause serialization not yet implemented");
13615 }
13616 llvm_unreachable("Invalid Clause Kind");
13617}
13618
13619void ASTRecordReader::readOpenACCClauseList(
13620 MutableArrayRef<const OpenACCClause *> Clauses) {
13621 for (unsigned I = 0; I < Clauses.size(); ++I)
13622 Clauses[I] = readOpenACCClause();
13623}
13624
13625void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13626 unsigned NumVars = readInt();
13627 A->Clauses.resize(N: NumVars);
13628 readOpenACCClauseList(Clauses: A->Clauses);
13629}
13630
13631static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13632 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13633 // module name?
13634 llvm::FoldingSetNodeID ID;
13635 ID.AddString(String: PrimaryModuleName);
13636 return ID.computeStableHash();
13637}
13638
13639UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13640 if (!M)
13641 return std::nullopt;
13642
13643 if (M->isHeaderLikeModule())
13644 return std::nullopt;
13645
13646 if (M->isGlobalModule())
13647 return std::nullopt;
13648
13649 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13650 return getStableHashForModuleName(PrimaryModuleName);
13651}
13652