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(NewBitNo: 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(NewBitNo: 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(NewBitNo: 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(NewBitNo: 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 NewBitNo: 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(NewBitNo: 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 break;
2198 default:
2199 llvm_unreachable("missing deserialization code for annotation token");
2200 }
2201 } else {
2202 Tok.setLength(Record[Idx++]);
2203 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2204 Tok.setIdentifierInfo(II);
2205 }
2206 return Tok;
2207}
2208
2209MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2210 BitstreamCursor &Stream = F.MacroCursor;
2211
2212 // Keep track of where we are in the stream, then jump back there
2213 // after reading this macro.
2214 SavedStreamPosition SavedPosition(Stream);
2215
2216 if (llvm::Error Err = Stream.JumpToBit(NewBitNo: Offset)) {
2217 // FIXME this drops errors on the floor.
2218 consumeError(Err: std::move(Err));
2219 return nullptr;
2220 }
2221 RecordData Record;
2222 SmallVector<IdentifierInfo*, 16> MacroParams;
2223 MacroInfo *Macro = nullptr;
2224 llvm::MutableArrayRef<Token> MacroTokens;
2225
2226 while (true) {
2227 // Advance to the next record, but if we get to the end of the block, don't
2228 // pop it (removing all the abbreviations from the cursor) since we want to
2229 // be able to reseek within the block and read entries.
2230 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2231 Expected<llvm::BitstreamEntry> MaybeEntry =
2232 Stream.advanceSkippingSubblocks(Flags);
2233 if (!MaybeEntry) {
2234 Error(Err: MaybeEntry.takeError());
2235 return Macro;
2236 }
2237 llvm::BitstreamEntry Entry = MaybeEntry.get();
2238
2239 switch (Entry.Kind) {
2240 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2241 case llvm::BitstreamEntry::Error:
2242 Error(Msg: "malformed block record in AST file");
2243 return Macro;
2244 case llvm::BitstreamEntry::EndBlock:
2245 return Macro;
2246 case llvm::BitstreamEntry::Record:
2247 // The interesting case.
2248 break;
2249 }
2250
2251 // Read a record.
2252 Record.clear();
2253 PreprocessorRecordTypes RecType;
2254 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2255 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2256 else {
2257 Error(Err: MaybeRecType.takeError());
2258 return Macro;
2259 }
2260 switch (RecType) {
2261 case PP_MODULE_MACRO:
2262 case PP_MACRO_DIRECTIVE_HISTORY:
2263 return Macro;
2264
2265 case PP_MACRO_OBJECT_LIKE:
2266 case PP_MACRO_FUNCTION_LIKE: {
2267 // If we already have a macro, that means that we've hit the end
2268 // of the definition of the macro we were looking for. We're
2269 // done.
2270 if (Macro)
2271 return Macro;
2272
2273 unsigned NextIndex = 1; // Skip identifier ID.
2274 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2275 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2276 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2277 MI->setIsUsed(Record[NextIndex++]);
2278 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2279 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2280 PPAllocator&: PP.getPreprocessorAllocator());
2281 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2282 // Decode function-like macro info.
2283 bool isC99VarArgs = Record[NextIndex++];
2284 bool isGNUVarArgs = Record[NextIndex++];
2285 bool hasCommaPasting = Record[NextIndex++];
2286 MacroParams.clear();
2287 unsigned NumArgs = Record[NextIndex++];
2288 for (unsigned i = 0; i != NumArgs; ++i)
2289 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2290
2291 // Install function-like macro info.
2292 MI->setIsFunctionLike();
2293 if (isC99VarArgs) MI->setIsC99Varargs();
2294 if (isGNUVarArgs) MI->setIsGNUVarargs();
2295 if (hasCommaPasting) MI->setHasCommaPasting();
2296 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2297 }
2298
2299 // Remember that we saw this macro last so that we add the tokens that
2300 // form its body to it.
2301 Macro = MI;
2302
2303 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2304 Record[NextIndex]) {
2305 // We have a macro definition. Register the association
2306 PreprocessedEntityID
2307 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2308 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2309 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2310 PreprocessingRecord::PPEntityID PPID =
2311 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2312 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2313 Val: PPRec.getPreprocessedEntity(PPID));
2314 if (PPDef)
2315 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2316 }
2317
2318 ++NumMacrosRead;
2319 break;
2320 }
2321
2322 case PP_TOKEN: {
2323 // If we see a TOKEN before a PP_MACRO_*, then the file is
2324 // erroneous, just pretend we didn't see this.
2325 if (!Macro) break;
2326 if (MacroTokens.empty()) {
2327 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2328 return Macro;
2329 }
2330
2331 unsigned Idx = 0;
2332 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2333 MacroTokens = MacroTokens.drop_front();
2334 break;
2335 }
2336 }
2337 }
2338}
2339
2340PreprocessedEntityID
2341ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2342 PreprocessedEntityID LocalID) const {
2343 if (!M.ModuleOffsetMap.empty())
2344 ReadModuleOffsetMap(F&: M);
2345
2346 unsigned ModuleFileIndex = LocalID >> 32;
2347 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2348 ModuleFile *MF =
2349 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2350 assert(MF && "malformed identifier ID encoding?");
2351
2352 if (!ModuleFileIndex) {
2353 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2354 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2355 }
2356
2357 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2358}
2359
2360OptionalFileEntryRef
2361HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2362 FileManager &FileMgr = Reader.getFileManager();
2363 if (!Key.Imported)
2364 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2365
2366 auto Resolved =
2367 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2368 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2369}
2370
2371unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2372 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2373 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2374 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2375 return llvm::xxh3_64bits(data: buf);
2376}
2377
2378HeaderFileInfoTrait::internal_key_type
2379HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2380 internal_key_type ikey = {.Size: ekey.getSize(),
2381 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2382 .Filename: ekey.getName(), /*Imported*/ false};
2383 return ikey;
2384}
2385
2386bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2387 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2388 return false;
2389
2390 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2391 return true;
2392
2393 // Determine whether the actual files are equivalent.
2394 OptionalFileEntryRef FEA = getFile(Key: a);
2395 OptionalFileEntryRef FEB = getFile(Key: b);
2396 return FEA && FEA == FEB;
2397}
2398
2399std::pair<unsigned, unsigned>
2400HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2401 return readULEBKeyDataLength(P&: d);
2402}
2403
2404HeaderFileInfoTrait::internal_key_type
2405HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2406 using namespace llvm::support;
2407
2408 internal_key_type ikey;
2409 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2410 ikey.ModTime =
2411 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2412 ikey.Filename = (const char *)d;
2413 ikey.Imported = true;
2414 return ikey;
2415}
2416
2417HeaderFileInfoTrait::data_type
2418HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2419 unsigned DataLen) {
2420 using namespace llvm::support;
2421
2422 const unsigned char *End = d + DataLen;
2423 HeaderFileInfo HFI;
2424 unsigned Flags = *d++;
2425
2426 OptionalFileEntryRef FE;
2427 bool Included = (Flags >> 6) & 0x01;
2428 if (Included)
2429 if ((FE = getFile(Key: key)))
2430 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2431 // deserialize this header file info again.
2432 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2433
2434 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2435 HFI.isImport |= (Flags >> 5) & 0x01;
2436 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2437 HFI.DirInfo = (Flags >> 1) & 0x07;
2438 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2439 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2440
2441 assert((End - d) % 4 == 0 &&
2442 "Wrong data length in HeaderFileInfo deserialization");
2443 while (d != End) {
2444 uint32_t LocalSMID =
2445 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2446 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2447 LocalSMID >>= 3;
2448
2449 // This header is part of a module. Associate it with the module to enable
2450 // implicit module import.
2451 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2452 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2453 ModuleMap &ModMap =
2454 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2455
2456 if (FE || (FE = getFile(Key: key))) {
2457 // FIXME: NameAsWritten
2458 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2459 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2460 }
2461 HFI.mergeModuleMembership(Role: HeaderRole);
2462 }
2463
2464 // This HeaderFileInfo was externally loaded.
2465 HFI.External = true;
2466 HFI.IsValid = true;
2467 return HFI;
2468}
2469
2470void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2471 uint32_t MacroDirectivesOffset) {
2472 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2473 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2474}
2475
2476void ASTReader::ReadDefinedMacros() {
2477 // Note that we are loading defined macros.
2478 Deserializing Macros(this);
2479
2480 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2481 BitstreamCursor &MacroCursor = I.MacroCursor;
2482
2483 // If there was no preprocessor block, skip this file.
2484 if (MacroCursor.getBitcodeBytes().empty())
2485 continue;
2486
2487 BitstreamCursor Cursor = MacroCursor;
2488 if (llvm::Error Err = Cursor.JumpToBit(NewBitNo: I.MacroStartOffset)) {
2489 Error(Err: std::move(Err));
2490 return;
2491 }
2492
2493 RecordData Record;
2494 while (true) {
2495 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2496 if (!MaybeE) {
2497 Error(Err: MaybeE.takeError());
2498 return;
2499 }
2500 llvm::BitstreamEntry E = MaybeE.get();
2501
2502 switch (E.Kind) {
2503 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2504 case llvm::BitstreamEntry::Error:
2505 Error(Msg: "malformed block record in AST file");
2506 return;
2507 case llvm::BitstreamEntry::EndBlock:
2508 goto NextCursor;
2509
2510 case llvm::BitstreamEntry::Record: {
2511 Record.clear();
2512 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2513 if (!MaybeRecord) {
2514 Error(Err: MaybeRecord.takeError());
2515 return;
2516 }
2517 switch (MaybeRecord.get()) {
2518 default: // Default behavior: ignore.
2519 break;
2520
2521 case PP_MACRO_OBJECT_LIKE:
2522 case PP_MACRO_FUNCTION_LIKE: {
2523 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2524 if (II->isOutOfDate())
2525 updateOutOfDateIdentifier(II: *II);
2526 break;
2527 }
2528
2529 case PP_TOKEN:
2530 // Ignore tokens.
2531 break;
2532 }
2533 break;
2534 }
2535 }
2536 }
2537 NextCursor: ;
2538 }
2539}
2540
2541namespace {
2542
2543 /// Visitor class used to look up identifirs in an AST file.
2544 class IdentifierLookupVisitor {
2545 StringRef Name;
2546 unsigned NameHash;
2547 unsigned PriorGeneration;
2548 unsigned &NumIdentifierLookups;
2549 unsigned &NumIdentifierLookupHits;
2550 IdentifierInfo *Found = nullptr;
2551
2552 public:
2553 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2554 unsigned &NumIdentifierLookups,
2555 unsigned &NumIdentifierLookupHits)
2556 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2557 PriorGeneration(PriorGeneration),
2558 NumIdentifierLookups(NumIdentifierLookups),
2559 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2560
2561 bool operator()(ModuleFile &M) {
2562 // If we've already searched this module file, skip it now.
2563 if (M.Generation <= PriorGeneration)
2564 return true;
2565
2566 ASTIdentifierLookupTable *IdTable
2567 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2568 if (!IdTable)
2569 return false;
2570
2571 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2572 Found);
2573 ++NumIdentifierLookups;
2574 ASTIdentifierLookupTable::iterator Pos =
2575 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2576 if (Pos == IdTable->end())
2577 return false;
2578
2579 // Dereferencing the iterator has the effect of building the
2580 // IdentifierInfo node and populating it with the various
2581 // declarations it needs.
2582 ++NumIdentifierLookupHits;
2583 Found = *Pos;
2584 if (Trait.hasMoreInformationInDependencies()) {
2585 // Look for the identifier in extra modules as they contain more info.
2586 return false;
2587 }
2588 return true;
2589 }
2590
2591 // Retrieve the identifier info found within the module
2592 // files.
2593 IdentifierInfo *getIdentifierInfo() const { return Found; }
2594 };
2595
2596} // namespace
2597
2598void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2599 // Note that we are loading an identifier.
2600 Deserializing AnIdentifier(this);
2601
2602 unsigned PriorGeneration = 0;
2603 if (getContext().getLangOpts().Modules)
2604 PriorGeneration = IdentifierGeneration[&II];
2605
2606 // If there is a global index, look there first to determine which modules
2607 // provably do not have any results for this identifier.
2608 GlobalModuleIndex::HitSet Hits;
2609 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2610 if (!loadGlobalIndex()) {
2611 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2612 HitsPtr = &Hits;
2613 }
2614 }
2615
2616 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2617 NumIdentifierLookups,
2618 NumIdentifierLookupHits);
2619 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2620 markIdentifierUpToDate(II: &II);
2621}
2622
2623void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2624 if (!II)
2625 return;
2626
2627 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2628
2629 // Update the generation for this identifier.
2630 if (getContext().getLangOpts().Modules)
2631 IdentifierGeneration[II] = getGeneration();
2632}
2633
2634MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2635 unsigned &Idx) {
2636 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2637 uint64_t LocalIndex = Record[Idx++];
2638 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2639}
2640
2641void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2642 const PendingMacroInfo &PMInfo) {
2643 ModuleFile &M = *PMInfo.M;
2644
2645 BitstreamCursor &Cursor = M.MacroCursor;
2646 SavedStreamPosition SavedPosition(Cursor);
2647 if (llvm::Error Err =
2648 Cursor.JumpToBit(NewBitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2649 Error(Err: std::move(Err));
2650 return;
2651 }
2652
2653 struct ModuleMacroRecord {
2654 SubmoduleID SubModID;
2655 MacroInfo *MI;
2656 SmallVector<SubmoduleID, 8> Overrides;
2657 };
2658 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2659
2660 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2661 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2662 // macro histroy.
2663 RecordData Record;
2664 while (true) {
2665 Expected<llvm::BitstreamEntry> MaybeEntry =
2666 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2667 if (!MaybeEntry) {
2668 Error(Err: MaybeEntry.takeError());
2669 return;
2670 }
2671 llvm::BitstreamEntry Entry = MaybeEntry.get();
2672
2673 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2674 Error(Msg: "malformed block record in AST file");
2675 return;
2676 }
2677
2678 Record.clear();
2679 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2680 if (!MaybePP) {
2681 Error(Err: MaybePP.takeError());
2682 return;
2683 }
2684 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2685 case PP_MACRO_DIRECTIVE_HISTORY:
2686 break;
2687
2688 case PP_MODULE_MACRO: {
2689 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2690 auto &Info = ModuleMacros.back();
2691 unsigned Idx = 0;
2692 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2693 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2694 for (int I = Idx, N = Record.size(); I != N; ++I)
2695 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2696 continue;
2697 }
2698
2699 default:
2700 Error(Msg: "malformed block record in AST file");
2701 return;
2702 }
2703
2704 // We found the macro directive history; that's the last record
2705 // for this macro.
2706 break;
2707 }
2708
2709 // Module macros are listed in reverse dependency order.
2710 {
2711 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2712 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2713 for (auto &MMR : ModuleMacros) {
2714 Overrides.clear();
2715 for (unsigned ModID : MMR.Overrides) {
2716 Module *Mod = getSubmodule(GlobalID: ModID);
2717 auto *Macro = PP.getModuleMacro(Mod, II);
2718 assert(Macro && "missing definition for overridden macro");
2719 Overrides.push_back(Elt: Macro);
2720 }
2721
2722 bool Inserted = false;
2723 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2724 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2725 }
2726 }
2727
2728 // Don't read the directive history for a module; we don't have anywhere
2729 // to put it.
2730 if (M.isModule())
2731 return;
2732
2733 // Deserialize the macro directives history in reverse source-order.
2734 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2735 unsigned Idx = 0, N = Record.size();
2736 while (Idx < N) {
2737 MacroDirective *MD = nullptr;
2738 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2739 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2740 switch (K) {
2741 case MacroDirective::MD_Define: {
2742 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2743 MD = PP.AllocateDefMacroDirective(MI, Loc);
2744 break;
2745 }
2746 case MacroDirective::MD_Undefine:
2747 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2748 break;
2749 case MacroDirective::MD_Visibility:
2750 bool isPublic = Record[Idx++];
2751 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2752 break;
2753 }
2754
2755 if (!Latest)
2756 Latest = MD;
2757 if (Earliest)
2758 Earliest->setPrevious(MD);
2759 Earliest = MD;
2760 }
2761
2762 if (Latest)
2763 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2764}
2765
2766bool ASTReader::shouldDisableValidationForFile(
2767 const serialization::ModuleFile &M) const {
2768 if (DisableValidationKind == DisableValidationForModuleKind::None)
2769 return false;
2770
2771 // If a PCH is loaded and validation is disabled for PCH then disable
2772 // validation for the PCH and the modules it loads.
2773 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2774
2775 switch (K) {
2776 case MK_MainFile:
2777 case MK_Preamble:
2778 case MK_PCH:
2779 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2780 case MK_ImplicitModule:
2781 case MK_ExplicitModule:
2782 case MK_PrebuiltModule:
2783 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2784 }
2785
2786 return false;
2787}
2788
2789static std::pair<StringRef, StringRef>
2790getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2791 const StringRef InputBlob) {
2792 uint16_t AsRequestedLength = Record[7];
2793 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2794 InputBlob.substr(Start: AsRequestedLength)};
2795}
2796
2797InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2798 // If this ID is bogus, just return an empty input file.
2799 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2800 return InputFileInfo();
2801
2802 // If we've already loaded this input file, return it.
2803 if (F.InputFileInfosLoaded[ID - 1].isValid())
2804 return F.InputFileInfosLoaded[ID - 1];
2805
2806 // Go find this input file.
2807 BitstreamCursor &Cursor = F.InputFilesCursor;
2808 SavedStreamPosition SavedPosition(Cursor);
2809 if (llvm::Error Err = Cursor.JumpToBit(NewBitNo: F.InputFilesOffsetBase +
2810 F.InputFileOffsets[ID - 1])) {
2811 // FIXME this drops errors on the floor.
2812 consumeError(Err: std::move(Err));
2813 }
2814
2815 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2816 if (!MaybeCode) {
2817 // FIXME this drops errors on the floor.
2818 consumeError(Err: MaybeCode.takeError());
2819 }
2820 unsigned Code = MaybeCode.get();
2821 RecordData Record;
2822 StringRef Blob;
2823
2824 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2825 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2826 "invalid record type for input file");
2827 else {
2828 // FIXME this drops errors on the floor.
2829 consumeError(Err: Maybe.takeError());
2830 }
2831
2832 assert(Record[0] == ID && "Bogus stored ID or offset");
2833 InputFileInfo R;
2834 R.StoredSize = static_cast<off_t>(Record[1]);
2835 R.StoredTime = static_cast<time_t>(Record[2]);
2836 R.Overridden = static_cast<bool>(Record[3]);
2837 R.Transient = static_cast<bool>(Record[4]);
2838 R.TopLevel = static_cast<bool>(Record[5]);
2839 R.ModuleMap = static_cast<bool>(Record[6]);
2840 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2841 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2842 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2843 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2844 ? UnresolvedFilenameAsRequested
2845 : UnresolvedFilename;
2846
2847 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2848 if (!MaybeEntry) // FIXME this drops errors on the floor.
2849 consumeError(Err: MaybeEntry.takeError());
2850 llvm::BitstreamEntry Entry = MaybeEntry.get();
2851 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2852 "expected record type for input file hash");
2853
2854 Record.clear();
2855 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2856 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2857 "invalid record type for input file hash");
2858 else {
2859 // FIXME this drops errors on the floor.
2860 consumeError(Err: Maybe.takeError());
2861 }
2862 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2863 static_cast<uint64_t>(Record[0]);
2864
2865 // Note that we've loaded this input file info.
2866 F.InputFileInfosLoaded[ID - 1] = R;
2867 return R;
2868}
2869
2870static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2871InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2872 // If this ID is bogus, just return an empty input file.
2873 if (ID == 0 || ID > F.InputFilesLoaded.size())
2874 return InputFile();
2875
2876 // If we've already loaded this input file, return it.
2877 if (F.InputFilesLoaded[ID-1].getFile())
2878 return F.InputFilesLoaded[ID-1];
2879
2880 if (F.InputFilesLoaded[ID-1].isNotFound())
2881 return InputFile();
2882
2883 // Go find this input file.
2884 BitstreamCursor &Cursor = F.InputFilesCursor;
2885 SavedStreamPosition SavedPosition(Cursor);
2886 if (llvm::Error Err = Cursor.JumpToBit(NewBitNo: F.InputFilesOffsetBase +
2887 F.InputFileOffsets[ID - 1])) {
2888 // FIXME this drops errors on the floor.
2889 consumeError(Err: std::move(Err));
2890 }
2891
2892 InputFileInfo FI = getInputFileInfo(F, ID);
2893 off_t StoredSize = FI.StoredSize;
2894 time_t StoredTime = FI.StoredTime;
2895 bool Overridden = FI.Overridden;
2896 bool Transient = FI.Transient;
2897 auto Filename =
2898 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2899 uint64_t StoredContentHash = FI.ContentHash;
2900
2901 // For standard C++ modules, we don't need to check the inputs.
2902 bool SkipChecks = F.StandardCXXModule;
2903
2904 const HeaderSearchOptions &HSOpts =
2905 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2906
2907 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2908 // modules.
2909 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2910 SkipChecks = false;
2911 Overridden = false;
2912 }
2913
2914 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2915
2916 // For an overridden file, create a virtual file with the stored
2917 // size/timestamp.
2918 if ((Overridden || Transient || SkipChecks) && !File)
2919 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2920
2921 if (!File) {
2922 if (Complain) {
2923 std::string ErrorStr = "could not find file '";
2924 ErrorStr += *Filename;
2925 ErrorStr += "' referenced by AST file '";
2926 ErrorStr += F.FileName.str();
2927 ErrorStr += "'";
2928 Error(Msg: ErrorStr);
2929 }
2930 // Record that we didn't find the file.
2931 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2932 return InputFile();
2933 }
2934
2935 // Check if there was a request to override the contents of the file
2936 // that was part of the precompiled header. Overriding such a file
2937 // can lead to problems when lexing using the source locations from the
2938 // PCH.
2939 SourceManager &SM = getSourceManager();
2940 // FIXME: Reject if the overrides are different.
2941 if ((!Overridden && !Transient) && !SkipChecks &&
2942 SM.isFileOverridden(File: *File)) {
2943 if (Complain)
2944 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2945
2946 // After emitting the diagnostic, bypass the overriding file to recover
2947 // (this creates a separate FileEntry).
2948 File = SM.bypassFileContentsOverride(File: *File);
2949 if (!File) {
2950 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2951 return InputFile();
2952 }
2953 }
2954
2955 auto HasInputContentChanged = [&](Change OriginalChange) {
2956 assert(ValidateASTInputFilesContent &&
2957 "We should only check the content of the inputs with "
2958 "ValidateASTInputFilesContent enabled.");
2959
2960 if (StoredContentHash == 0)
2961 return OriginalChange;
2962
2963 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2964 if (!MemBuffOrError) {
2965 if (!Complain)
2966 return OriginalChange;
2967 std::string ErrorStr = "could not get buffer for file '";
2968 ErrorStr += File->getName();
2969 ErrorStr += "'";
2970 Error(Msg: ErrorStr);
2971 return OriginalChange;
2972 }
2973
2974 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2975 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2976 return Change{.Kind: Change::None};
2977
2978 return Change{.Kind: Change::Content};
2979 };
2980 auto HasInputFileChanged = [&]() {
2981 if (StoredSize != File->getSize())
2982 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2983 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2984 StoredTime != File->getModificationTime()) {
2985 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2986 .New: File->getModificationTime()};
2987
2988 // In case the modification time changes but not the content,
2989 // accept the cached file as legit.
2990 if (ValidateASTInputFilesContent)
2991 return HasInputContentChanged(MTimeChange);
2992
2993 return MTimeChange;
2994 }
2995 return Change{.Kind: Change::None};
2996 };
2997
2998 bool IsOutOfDate = false;
2999 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
3000 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
3001 // enabled, it is better to check the contents of the inputs. Since we can't
3002 // get correct modified time information for inputs from overriden inputs.
3003 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
3004 F.StandardCXXModule && FileChange.Kind == Change::None)
3005 FileChange = HasInputContentChanged(FileChange);
3006
3007 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
3008 // it is better to check the content of the input files because we cannot rely
3009 // on the file modification time, which will be the same (zero) for these
3010 // files.
3011 if (!StoredTime && ValidateASTInputFilesContent &&
3012 FileChange.Kind == Change::None)
3013 FileChange = HasInputContentChanged(FileChange);
3014
3015 // For an overridden file, there is nothing to validate.
3016 if (!Overridden && FileChange.Kind != Change::None) {
3017 if (Complain) {
3018 // Build a list of the PCH imports that got us here (in reverse).
3019 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
3020 while (!ImportStack.back()->ImportedBy.empty())
3021 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
3022
3023 // The top-level AST file is stale.
3024 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
3025 Diag(DiagID: diag::err_fe_ast_file_modified)
3026 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
3027 << TopLevelASTFileName;
3028 Diag(DiagID: diag::note_fe_ast_file_modified)
3029 << FileChange.Kind << (FileChange.Old && FileChange.New)
3030 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
3031 << llvm::itostr(X: FileChange.New.value_or(u: 0));
3032 if (getModuleManager()
3033 .getModuleCache()
3034 .getInMemoryModuleCache()
3035 .isPCMFinal(Filename: F.FileName))
3036 Diag(DiagID: diag::note_fe_ast_file_modified_finalized) << F.ModuleName;
3037
3038 // Print the import stack.
3039 if (ImportStack.size() > 1) {
3040 Diag(DiagID: diag::note_ast_file_required_by)
3041 << *Filename << ImportStack[0]->FileName;
3042 for (unsigned I = 1; I < ImportStack.size(); ++I)
3043 Diag(DiagID: diag::note_ast_file_required_by)
3044 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3045 }
3046
3047 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3048 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3049 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3050 << F.InputFilesValidationStatus;
3051 }
3052
3053 IsOutOfDate = true;
3054 }
3055 // FIXME: If the file is overridden and we've already opened it,
3056 // issue an error (or split it into a separate FileEntry).
3057
3058 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3059
3060 // Note that we've loaded this input file.
3061 F.InputFilesLoaded[ID-1] = IF;
3062 return IF;
3063}
3064
3065ASTReader::TemporarilyOwnedStringRef
3066ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3067 ModuleFile &ModF) {
3068 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3069}
3070
3071ASTReader::TemporarilyOwnedStringRef
3072ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3073 StringRef Prefix) {
3074 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3075
3076 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3077 Path == "<built-in>" || Path == "<command line>")
3078 return {Path, Buf};
3079
3080 // The writer makes the base directory itself relative as ".".
3081 if (Path == ".")
3082 return {Prefix, Buf};
3083
3084 Buf.clear();
3085 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3086 StringRef ResolvedPath{Buf.data(), Buf.size()};
3087 return {ResolvedPath, Buf};
3088}
3089
3090std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3091 StringRef P,
3092 ModuleFile &ModF) {
3093 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3094}
3095
3096std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3097 StringRef P,
3098 StringRef Prefix) {
3099 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3100 return ResolvedPath->str();
3101}
3102
3103static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3104 switch (ARR) {
3105 case ASTReader::Failure: return true;
3106 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3107 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3108 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3109 case ASTReader::ConfigurationMismatch:
3110 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3111 case ASTReader::HadErrors: return true;
3112 case ASTReader::Success: return false;
3113 }
3114
3115 llvm_unreachable("unknown ASTReadResult");
3116}
3117
3118ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3119 BitstreamCursor &Stream, StringRef Filename,
3120 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3121 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3122 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3123 // FIXME this drops errors on the floor.
3124 consumeError(Err: std::move(Err));
3125 return Failure;
3126 }
3127
3128 // Read all of the records in the options block.
3129 RecordData Record;
3130 ASTReadResult Result = Success;
3131 while (true) {
3132 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3133 if (!MaybeEntry) {
3134 // FIXME this drops errors on the floor.
3135 consumeError(Err: MaybeEntry.takeError());
3136 return Failure;
3137 }
3138 llvm::BitstreamEntry Entry = MaybeEntry.get();
3139
3140 switch (Entry.Kind) {
3141 case llvm::BitstreamEntry::Error:
3142 case llvm::BitstreamEntry::SubBlock:
3143 return Failure;
3144
3145 case llvm::BitstreamEntry::EndBlock:
3146 return Result;
3147
3148 case llvm::BitstreamEntry::Record:
3149 // The interesting case.
3150 break;
3151 }
3152
3153 // Read and process a record.
3154 Record.clear();
3155 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3156 if (!MaybeRecordType) {
3157 // FIXME this drops errors on the floor.
3158 consumeError(Err: MaybeRecordType.takeError());
3159 return Failure;
3160 }
3161 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3162 case LANGUAGE_OPTIONS: {
3163 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3164 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3165 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3166 Result = ConfigurationMismatch;
3167 break;
3168 }
3169
3170 case CODEGEN_OPTIONS: {
3171 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3172 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3173 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3174 Result = ConfigurationMismatch;
3175 break;
3176 }
3177
3178 case TARGET_OPTIONS: {
3179 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3180 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3181 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3182 Result = ConfigurationMismatch;
3183 break;
3184 }
3185
3186 case FILE_SYSTEM_OPTIONS: {
3187 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3188 if (!AllowCompatibleConfigurationMismatch &&
3189 ParseFileSystemOptions(Record, Complain, Listener))
3190 Result = ConfigurationMismatch;
3191 break;
3192 }
3193
3194 case HEADER_SEARCH_OPTIONS: {
3195 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3196 if (!AllowCompatibleConfigurationMismatch &&
3197 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3198 Result = ConfigurationMismatch;
3199 break;
3200 }
3201
3202 case PREPROCESSOR_OPTIONS:
3203 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3204 if (!AllowCompatibleConfigurationMismatch &&
3205 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3206 SuggestedPredefines))
3207 Result = ConfigurationMismatch;
3208 break;
3209 }
3210 }
3211}
3212
3213/// Returns {build-session validation applies, MF was validated this session}.
3214static std::pair<bool, bool>
3215wasValidatedInBuildSession(const ModuleFile &MF,
3216 const HeaderSearchOptions &HSOpts) {
3217 const bool EnablesBSValidation =
3218 HSOpts.ModulesValidateOncePerBuildSession && MF.Kind == MK_ImplicitModule;
3219 const bool WasValidated =
3220 EnablesBSValidation &&
3221 MF.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp;
3222 return {EnablesBSValidation, WasValidated};
3223}
3224
3225ASTReader::RelocationResult
3226ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3227 // Don't emit module relocation errors if we have -fno-validate-pch.
3228 const bool IgnoreError =
3229 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3230 DisableValidationForModuleKind::Module);
3231
3232 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3233 return {std::nullopt, IgnoreError};
3234
3235 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3236
3237 if (!DirectoryCheck &&
3238 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3239 return {std::nullopt, IgnoreError};
3240
3241 const HeaderSearchOptions &HSOpts =
3242 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3243
3244 // When only validating modules once per build session,
3245 // Skip check if the timestamp is up to date or module was built in same build
3246 // session.
3247 auto [EnablesBSValidation, WasValidated] =
3248 wasValidatedInBuildSession(MF: F, HSOpts);
3249 const bool SkipModuleLookup =
3250 !PP.getPreprocessorOpts().ModulesForceRedundantLookup &&
3251 (WasValidated ||
3252 (EnablesBSValidation &&
3253 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp));
3254
3255 if (SkipModuleLookup)
3256 return {std::nullopt, IgnoreError};
3257
3258 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3259
3260 // If we've already loaded a module map file covering this module, we may
3261 // have a better path for it (relative to the current build if doing directory
3262 // check).
3263 Module *M = PP.getHeaderSearchInfo().lookupModule(
3264 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3265 /*AllowSearch=*/true,
3266 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3267
3268 return {M, IgnoreError};
3269}
3270
3271ASTReader::ASTReadResult
3272ASTReader::ReadControlBlock(ModuleFile &F,
3273 SmallVectorImpl<ImportedModule> &Loaded,
3274 const ModuleFile *ImportedBy,
3275 unsigned ClientLoadCapabilities) {
3276 BitstreamCursor &Stream = F.Stream;
3277
3278 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3279 Error(Err: std::move(Err));
3280 return Failure;
3281 }
3282
3283 // Lambda to read the unhashed control block the first time it's called.
3284 //
3285 // For PCM files, the unhashed control block cannot be read until after the
3286 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3287 // need to look ahead before reading the IMPORTS record. For consistency,
3288 // this block is always read somehow (see BitstreamEntry::EndBlock).
3289 bool HasReadUnhashedControlBlock = false;
3290 auto readUnhashedControlBlockOnce = [&]() {
3291 if (!HasReadUnhashedControlBlock) {
3292 HasReadUnhashedControlBlock = true;
3293 if (ASTReadResult Result =
3294 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3295 return Result;
3296 }
3297 return Success;
3298 };
3299
3300 bool DisableValidation = shouldDisableValidationForFile(M: F);
3301
3302 // Read all of the records and blocks in the control block.
3303 RecordData Record;
3304 unsigned NumInputs = 0;
3305 unsigned NumUserInputs = 0;
3306 StringRef BaseDirectoryAsWritten;
3307 while (true) {
3308 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3309 if (!MaybeEntry) {
3310 Error(Err: MaybeEntry.takeError());
3311 return Failure;
3312 }
3313 llvm::BitstreamEntry Entry = MaybeEntry.get();
3314
3315 switch (Entry.Kind) {
3316 case llvm::BitstreamEntry::Error:
3317 Error(Msg: "malformed block record in AST file");
3318 return Failure;
3319 case llvm::BitstreamEntry::EndBlock: {
3320 // Validate the module before returning. This call catches an AST with
3321 // no module name and no imports.
3322 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3323 return Result;
3324
3325 // Validate input files.
3326 const HeaderSearchOptions &HSOpts =
3327 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3328
3329 // All user input files reside at the index range [0, NumUserInputs), and
3330 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3331 // loaded module files, ignore missing inputs.
3332 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3333 F.Kind != MK_PrebuiltModule) {
3334 bool Complain =
3335 !canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities);
3336
3337 // If we are reading a module, we will create a verification timestamp,
3338 // so we verify all input files. Otherwise, verify only user input
3339 // files.
3340
3341 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3342 F.InputFilesValidationStatus = ValidateSystemInputs
3343 ? InputFilesValidation::AllFiles
3344 : InputFilesValidation::UserFiles;
3345 auto [_, WasValidated] = wasValidatedInBuildSession(MF: F, HSOpts);
3346 if (WasValidated) {
3347 N = ForceValidateUserInputs ? NumUserInputs : 0;
3348 F.InputFilesValidationStatus =
3349 ForceValidateUserInputs
3350 ? InputFilesValidation::UserFiles
3351 : InputFilesValidation::SkippedInBuildSession;
3352 }
3353
3354 if (N != 0)
3355 Diag(DiagID: diag::remark_module_validation)
3356 << N << F.ModuleName << F.FileName;
3357
3358 for (unsigned I = 0; I < N; ++I) {
3359 InputFile IF = getInputFile(F, ID: I+1, Complain);
3360 if (!IF.getFile() || IF.isOutOfDate())
3361 return OutOfDate;
3362 }
3363
3364 // A header added to a directory the module enumerated was never
3365 // recorded as an input file, so the check above misses it.
3366 if (!WasValidated && F.Kind == MK_ImplicitModule &&
3367 HSOpts.ModulesValidateDirectoryDependencies &&
3368 isDirectoryDependencyOutOfDate(F, Complain))
3369 return OutOfDate;
3370 } else {
3371 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3372 }
3373
3374 if (Listener)
3375 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind, DirectlyImported: F.isDirectlyImported());
3376
3377 if (Listener && Listener->needsInputFileVisitation()) {
3378 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3379 : NumUserInputs;
3380 for (unsigned I = 0; I < N; ++I) {
3381 bool IsSystem = I >= NumUserInputs;
3382 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3383 auto FilenameAsRequested = ResolveImportedPath(
3384 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3385 Listener->visitInputFile(
3386 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3387 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3388 }
3389 }
3390
3391 return Success;
3392 }
3393
3394 case llvm::BitstreamEntry::SubBlock:
3395 switch (Entry.ID) {
3396 case INPUT_FILES_BLOCK_ID:
3397 F.InputFilesCursor = Stream;
3398 if (llvm::Error Err = Stream.SkipBlock()) {
3399 Error(Err: std::move(Err));
3400 return Failure;
3401 }
3402 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3403 Error(Msg: "malformed block record in AST file");
3404 return Failure;
3405 }
3406 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3407 continue;
3408
3409 case OPTIONS_BLOCK_ID:
3410 // If we're reading the first module for this group, check its options
3411 // are compatible with ours. For modules it imports, no further checking
3412 // is required, because we checked them when we built it.
3413 if (Listener && !ImportedBy) {
3414 // Should we allow the configuration of the module file to differ from
3415 // the configuration of the current translation unit in a compatible
3416 // way?
3417 //
3418 // FIXME: Allow this for files explicitly specified with -include-pch.
3419 bool AllowCompatibleConfigurationMismatch =
3420 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3421
3422 ASTReadResult Result =
3423 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3424 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3425 SuggestedPredefines);
3426 if (Result == Failure) {
3427 Error(Msg: "malformed block record in AST file");
3428 return Result;
3429 }
3430
3431 if (DisableValidation ||
3432 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3433 Result = Success;
3434
3435 // If we can't load the module, exit early since we likely
3436 // will rebuild the module anyway. The stream may be in the
3437 // middle of a block.
3438 if (Result != Success)
3439 return Result;
3440 } else if (llvm::Error Err = Stream.SkipBlock()) {
3441 Error(Err: std::move(Err));
3442 return Failure;
3443 }
3444 continue;
3445
3446 default:
3447 if (llvm::Error Err = Stream.SkipBlock()) {
3448 Error(Err: std::move(Err));
3449 return Failure;
3450 }
3451 continue;
3452 }
3453
3454 case llvm::BitstreamEntry::Record:
3455 // The interesting case.
3456 break;
3457 }
3458
3459 // Read and process a record.
3460 Record.clear();
3461 StringRef Blob;
3462 Expected<unsigned> MaybeRecordType =
3463 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3464 if (!MaybeRecordType) {
3465 Error(Err: MaybeRecordType.takeError());
3466 return Failure;
3467 }
3468 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3469 case METADATA: {
3470 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3471 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3472 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3473 : diag::err_ast_file_version_too_new)
3474 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3475 return VersionMismatch;
3476 }
3477
3478 bool hasErrors = Record[7];
3479 if (hasErrors && !DisableValidation) {
3480 // If requested by the caller and the module hasn't already been read
3481 // or compiled, mark modules on error as out-of-date.
3482 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3483 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3484 return OutOfDate;
3485
3486 if (!AllowASTWithCompilerErrors) {
3487 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3488 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3489 return HadErrors;
3490 }
3491 }
3492 if (hasErrors) {
3493 Diags.ErrorOccurred = true;
3494 Diags.UncompilableErrorOccurred = true;
3495 Diags.UnrecoverableErrorOccurred = true;
3496 }
3497
3498 F.RelocatablePCH = Record[4];
3499 // Relative paths in a relocatable PCH are relative to our sysroot.
3500 if (F.RelocatablePCH)
3501 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3502
3503 F.StandardCXXModule = Record[5];
3504
3505 F.HasTimestamps = Record[6];
3506
3507 const std::string &CurBranch = getClangFullRepositoryVersion();
3508 StringRef ASTBranch = Blob;
3509 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3510 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3511 Diag(DiagID: diag::err_ast_file_different_branch)
3512 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3513 << CurBranch;
3514 return VersionMismatch;
3515 }
3516 break;
3517 }
3518
3519 case IMPORT: {
3520 // Validate the AST before processing any imports (otherwise, untangling
3521 // them can be error-prone and expensive). A module will have a name and
3522 // will already have been validated, but this catches the PCH case.
3523 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3524 return Result;
3525
3526 unsigned Idx = 0;
3527 // Read information about the AST file.
3528 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3529
3530 // The import location will be the local one for now; we will adjust
3531 // all import locations of module imports after the global source
3532 // location info are setup, in ReadAST.
3533 auto [ImportLoc, ImportModuleFileIndex] =
3534 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3535 // The import location must belong to the current module file itself.
3536 assert(ImportModuleFileIndex == 0);
3537
3538 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3539
3540 bool IsImportingStdCXXModule = Record[Idx++];
3541 bool IsCXX20ModuleOrPrebuiltHeaderUnit = 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 (IsCXX20ModuleOrPrebuiltHeaderUnit && ImportedFile.empty()) {
3562 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3563 return Missing;
3564 }
3565
3566 if (!IsCXX20ModuleOrPrebuiltHeaderUnit) {
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 whether the AST we just read from ImportedFile contains a
3609 // different module than we expected (ImportedName).
3610 //
3611 // This can occur for C++20 Modules (including explicitly mapped header
3612 // units) when given a mismatch in -fmodule-file=<name>=<file>.
3613 if (IsCXX20ModuleOrPrebuiltHeaderUnit) {
3614 if (const auto *Imported =
3615 getModuleManager().lookupByFileName(FileName: ImportedFile);
3616 Imported != nullptr && Imported->ModuleName != ImportedName) {
3617 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3618 Result = Missing;
3619 }
3620 }
3621
3622 // If we diagnosed a problem, produce a backtrace.
3623 bool recompilingFinalized = Result == OutOfDate &&
3624 (Capabilities & ARR_OutOfDate) &&
3625 getModuleManager()
3626 .getModuleCache()
3627 .getInMemoryModuleCache()
3628 .isPCMFinal(Filename: F.FileName);
3629 if (!IgnoreImportedByNote &&
3630 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3631 Diag(DiagID: diag::note_module_file_imported_by)
3632 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3633
3634 switch (Result) {
3635 case Failure: return Failure;
3636 // If we have to ignore the dependency, we'll have to ignore this too.
3637 case Missing:
3638 case OutOfDate: return OutOfDate;
3639 case VersionMismatch: return VersionMismatch;
3640 case ConfigurationMismatch: return ConfigurationMismatch;
3641 case HadErrors: return HadErrors;
3642 case Success: break;
3643 }
3644 break;
3645 }
3646
3647 case ORIGINAL_FILE:
3648 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3649 F.ActualOriginalSourceFileName = std::string(Blob);
3650 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3651 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3652 break;
3653
3654 case ORIGINAL_FILE_ID:
3655 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3656 break;
3657
3658 case MODULE_NAME:
3659 F.ModuleName = std::string(Blob);
3660 Diag(DiagID: diag::remark_module_import)
3661 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3662 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3663 if (Listener)
3664 Listener->ReadModuleName(ModuleName: F.ModuleName);
3665
3666 // Validate the AST as soon as we have a name so we can exit early on
3667 // failure.
3668 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3669 return Result;
3670
3671 break;
3672
3673 case MODULE_DIRECTORY: {
3674 // Save the BaseDirectory as written in the PCM for computing the module
3675 // filename for the ModuleCache.
3676 BaseDirectoryAsWritten = Blob;
3677 assert(!F.ModuleName.empty() &&
3678 "MODULE_DIRECTORY found before MODULE_NAME");
3679 F.BaseDirectory = std::string(Blob);
3680
3681 auto [MaybeM, IgnoreError] =
3682 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3683 if (!MaybeM.has_value())
3684 break;
3685
3686 Module *M = MaybeM.value();
3687 if (!M || !M->Directory)
3688 break;
3689 if (IgnoreError) {
3690 F.BaseDirectory = std::string(M->Directory->getName());
3691 break;
3692 }
3693 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3694 break;
3695
3696 // If we're implicitly loading a module, the base directory can't
3697 // change between the build and use.
3698 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3699 if (BuildDir && (*BuildDir == M->Directory)) {
3700 F.BaseDirectory = std::string(M->Directory->getName());
3701 break;
3702 }
3703 Diag(DiagID: diag::remark_module_relocated)
3704 << F.ModuleName << Blob << M->Directory->getName();
3705
3706 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3707 Diag(DiagID: diag::err_imported_module_relocated)
3708 << F.ModuleName << Blob << M->Directory->getName();
3709 return OutOfDate;
3710 }
3711
3712 case MODULE_MAP_FILE:
3713 if (ASTReadResult Result =
3714 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3715 return Result;
3716 break;
3717
3718 case MODULE_DIRECTORY_DEPENDENCIES:
3719 ReadDirectoryDependencies(Record, F);
3720 break;
3721
3722 case INPUT_FILE_OFFSETS:
3723 NumInputs = Record[0];
3724 NumUserInputs = Record[1];
3725 F.InputFileOffsets =
3726 (const llvm::support::unaligned_uint64_t *)Blob.data();
3727 F.InputFilesLoaded.resize(new_size: NumInputs);
3728 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3729 F.NumUserInputFiles = NumUserInputs;
3730 break;
3731 }
3732 }
3733}
3734
3735llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3736 unsigned ClientLoadCapabilities) {
3737 BitstreamCursor &Stream = F.Stream;
3738
3739 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3740 return Err;
3741 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3742
3743 // Read all of the records and blocks for the AST file.
3744 RecordData Record;
3745 while (true) {
3746 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3747 if (!MaybeEntry)
3748 return MaybeEntry.takeError();
3749 llvm::BitstreamEntry Entry = MaybeEntry.get();
3750
3751 switch (Entry.Kind) {
3752 case llvm::BitstreamEntry::Error:
3753 return llvm::createStringError(
3754 EC: std::errc::illegal_byte_sequence,
3755 Fmt: "error at end of module block in AST file");
3756 case llvm::BitstreamEntry::EndBlock:
3757 // Outside of C++, we do not store a lookup map for the translation unit.
3758 // Instead, mark it as needing a lookup map to be built if this module
3759 // contains any declarations lexically within it (which it always does!).
3760 // This usually has no cost, since we very rarely need the lookup map for
3761 // the translation unit outside C++.
3762 if (ASTContext *Ctx = ContextObj) {
3763 DeclContext *DC = Ctx->getTranslationUnitDecl();
3764 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3765 DC->setMustBuildLookupTable();
3766 }
3767
3768 return llvm::Error::success();
3769 case llvm::BitstreamEntry::SubBlock:
3770 switch (Entry.ID) {
3771 case DECLTYPES_BLOCK_ID:
3772 // We lazily load the decls block, but we want to set up the
3773 // DeclsCursor cursor to point into it. Clone our current bitcode
3774 // cursor to it, enter the block and read the abbrevs in that block.
3775 // With the main cursor, we just skip over it.
3776 F.DeclsCursor = Stream;
3777 if (llvm::Error Err = Stream.SkipBlock())
3778 return Err;
3779 if (llvm::Error Err = ReadBlockAbbrevs(
3780 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3781 return Err;
3782 break;
3783
3784 case PREPROCESSOR_BLOCK_ID:
3785 F.MacroCursor = Stream;
3786 if (!PP.getExternalSource())
3787 PP.setExternalSource(this);
3788
3789 if (llvm::Error Err = Stream.SkipBlock())
3790 return Err;
3791 if (llvm::Error Err =
3792 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3793 return Err;
3794 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3795 break;
3796
3797 case PREPROCESSOR_DETAIL_BLOCK_ID:
3798 F.PreprocessorDetailCursor = Stream;
3799
3800 if (llvm::Error Err = Stream.SkipBlock()) {
3801 return Err;
3802 }
3803 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3804 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3805 return Err;
3806 F.PreprocessorDetailStartOffset
3807 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3808
3809 if (!PP.getPreprocessingRecord())
3810 PP.createPreprocessingRecord();
3811 if (!PP.getPreprocessingRecord()->getExternalSource())
3812 PP.getPreprocessingRecord()->SetExternalSource(*this);
3813 break;
3814
3815 case SOURCE_MANAGER_BLOCK_ID:
3816 if (llvm::Error Err = ReadSourceManagerBlock(F))
3817 return Err;
3818 break;
3819
3820 case SUBMODULE_BLOCK_ID:
3821 F.SubmodulesCursor = Stream;
3822 if (llvm::Error Err = Stream.SkipBlock())
3823 return Err;
3824 if (llvm::Error Err =
3825 ReadBlockAbbrevs(Cursor&: F.SubmodulesCursor, BlockID: SUBMODULE_BLOCK_ID))
3826 return Err;
3827 F.SubmodulesOffsetBase = F.SubmodulesCursor.GetCurrentBitNo();
3828 break;
3829
3830 case COMMENTS_BLOCK_ID: {
3831 BitstreamCursor C = Stream;
3832
3833 if (llvm::Error Err = Stream.SkipBlock())
3834 return Err;
3835 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3836 return Err;
3837 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3838 break;
3839 }
3840
3841 default:
3842 if (llvm::Error Err = Stream.SkipBlock())
3843 return Err;
3844 break;
3845 }
3846 continue;
3847
3848 case llvm::BitstreamEntry::Record:
3849 // The interesting case.
3850 break;
3851 }
3852
3853 // Read and process a record.
3854 Record.clear();
3855 StringRef Blob;
3856 Expected<unsigned> MaybeRecordType =
3857 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3858 if (!MaybeRecordType)
3859 return MaybeRecordType.takeError();
3860 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3861
3862 // If we're not loading an AST context, we don't care about most records.
3863 if (!ContextObj) {
3864 switch (RecordType) {
3865 case IDENTIFIER_TABLE:
3866 case IDENTIFIER_OFFSET:
3867 case INTERESTING_IDENTIFIERS:
3868 case STATISTICS:
3869 case PP_ASSUME_NONNULL_LOC:
3870 case PP_CONDITIONAL_STACK:
3871 case PP_COUNTER_VALUE:
3872 case SOURCE_LOCATION_OFFSETS:
3873 case MODULE_OFFSET_MAP:
3874 case SOURCE_MANAGER_LINE_TABLE:
3875 case PPD_ENTITIES_OFFSETS:
3876 case HEADER_SEARCH_TABLE:
3877 case IMPORTED_MODULES:
3878 case MACRO_OFFSET:
3879 case SUBMODULE_METADATA:
3880 break;
3881 default:
3882 continue;
3883 }
3884 }
3885
3886 switch (RecordType) {
3887 default: // Default behavior: ignore.
3888 break;
3889
3890 case SUBMODULE_METADATA: {
3891 F.BaseSubmoduleID = getTotalNumSubmodules();
3892 F.LocalNumSubmodules = Record[0];
3893 F.LocalBaseSubmoduleID = Record[1];
3894 F.LocalTopLevelSubmoduleID = Record[2];
3895 F.SubmoduleOffsets =
3896 (const llvm::support::unaligned_uint64_t *)Blob.data();
3897 if (F.LocalNumSubmodules > 0) {
3898 // Introduce the global -> local mapping for submodules within this
3899 // module.
3900 GlobalSubmoduleMap.insert(
3901 Val: std::make_pair(x: getTotalNumSubmodules() + 1, y: &F));
3902
3903 // Introduce the local -> global mapping for submodules within this
3904 // module.
3905 F.SubmoduleRemap.insertOrReplace(
3906 Val: std::make_pair(x&: F.LocalBaseSubmoduleID,
3907 y: F.BaseSubmoduleID - F.LocalBaseSubmoduleID));
3908
3909 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
3910 }
3911
3912 auto ReadSubmodule = [&](unsigned LocalID) -> Module * {
3913 return getSubmodule(GlobalID: getGlobalSubmoduleID(M&: F, LocalID));
3914 };
3915
3916 if (PP.getHeaderSearchInfo().getModuleMap().findModule(Name: F.ModuleName)) {
3917 // If we already knew about this module, make sure to bring all
3918 // submodules up to date.
3919 for (unsigned Index = 0; Index != F.LocalNumSubmodules; ++Index) {
3920 unsigned LocalID =
3921 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
3922 ReadSubmodule(LocalID);
3923 }
3924 } else {
3925 // If we didn't know this module, we loaded it transitively. Deserialize
3926 // just the top-level module to register it with ModuleMap, but load the
3927 // rest lazily.
3928 ReadSubmodule(F.LocalTopLevelSubmoduleID);
3929 }
3930
3931 break;
3932 }
3933
3934 case TYPE_OFFSET: {
3935 if (F.LocalNumTypes != 0)
3936 return llvm::createStringError(
3937 EC: std::errc::illegal_byte_sequence,
3938 Fmt: "duplicate TYPE_OFFSET record in AST file");
3939 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3940 F.LocalNumTypes = Record[0];
3941 F.BaseTypeIndex = getTotalNumTypes();
3942
3943 if (F.LocalNumTypes > 0)
3944 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3945
3946 break;
3947 }
3948
3949 case DECL_OFFSET: {
3950 if (F.LocalNumDecls != 0)
3951 return llvm::createStringError(
3952 EC: std::errc::illegal_byte_sequence,
3953 Fmt: "duplicate DECL_OFFSET record in AST file");
3954 F.DeclOffsets = (const DeclOffset *)Blob.data();
3955 F.LocalNumDecls = Record[0];
3956 F.BaseDeclIndex = getTotalNumDecls();
3957
3958 if (F.LocalNumDecls > 0)
3959 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3960
3961 break;
3962 }
3963
3964 case TU_UPDATE_LEXICAL: {
3965 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3966 LexicalContents Contents(
3967 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3968 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3969 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3970 TU->setHasExternalLexicalStorage(true);
3971 break;
3972 }
3973
3974 case UPDATE_VISIBLE: {
3975 unsigned Idx = 0;
3976 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3977 auto *Data = (const unsigned char*)Blob.data();
3978 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3979 // If we've already loaded the decl, perform the updates when we finish
3980 // loading this block.
3981 if (Decl *D = GetExistingDecl(ID))
3982 PendingUpdateRecords.push_back(
3983 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3984 break;
3985 }
3986
3987 case UPDATE_MODULE_LOCAL_VISIBLE: {
3988 unsigned Idx = 0;
3989 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3990 auto *Data = (const unsigned char *)Blob.data();
3991 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3992 // If we've already loaded the decl, perform the updates when we finish
3993 // loading this block.
3994 if (Decl *D = GetExistingDecl(ID))
3995 PendingUpdateRecords.push_back(
3996 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3997 break;
3998 }
3999
4000 case UPDATE_TU_LOCAL_VISIBLE: {
4001 if (F.Kind != MK_MainFile)
4002 break;
4003 unsigned Idx = 0;
4004 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4005 auto *Data = (const unsigned char *)Blob.data();
4006 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4007 // If we've already loaded the decl, perform the updates when we finish
4008 // loading this block.
4009 if (Decl *D = GetExistingDecl(ID))
4010 PendingUpdateRecords.push_back(
4011 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4012 break;
4013 }
4014
4015 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
4016 unsigned Idx = 0;
4017 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4018 auto *Data = (const unsigned char *)Blob.data();
4019 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4020 // If we've already loaded the decl, perform the updates when we finish
4021 // loading this block.
4022 if (Decl *D = GetExistingDecl(ID))
4023 PendingUpdateRecords.push_back(
4024 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4025 break;
4026 }
4027
4028 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
4029 unsigned Idx = 0;
4030 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4031 auto *Data = (const unsigned char *)Blob.data();
4032 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4033 // If we've already loaded the decl, perform the updates when we finish
4034 // loading this block.
4035 if (Decl *D = GetExistingDecl(ID))
4036 PendingUpdateRecords.push_back(
4037 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4038 break;
4039 }
4040
4041 case IDENTIFIER_TABLE:
4042 F.IdentifierTableData =
4043 reinterpret_cast<const unsigned char *>(Blob.data());
4044 if (Record[0]) {
4045 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
4046 Buckets: F.IdentifierTableData + Record[0],
4047 Payload: F.IdentifierTableData + sizeof(uint32_t),
4048 Base: F.IdentifierTableData,
4049 InfoObj: ASTIdentifierLookupTrait(*this, F));
4050
4051 PP.getIdentifierTable().setExternalIdentifierLookup(this);
4052 }
4053 break;
4054
4055 case IDENTIFIER_OFFSET: {
4056 if (F.LocalNumIdentifiers != 0)
4057 return llvm::createStringError(
4058 EC: std::errc::illegal_byte_sequence,
4059 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
4060 F.IdentifierOffsets = (const uint32_t *)Blob.data();
4061 F.LocalNumIdentifiers = Record[0];
4062 F.BaseIdentifierID = getTotalNumIdentifiers();
4063
4064 if (F.LocalNumIdentifiers > 0)
4065 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
4066 + F.LocalNumIdentifiers);
4067 break;
4068 }
4069
4070 case INTERESTING_IDENTIFIERS:
4071 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
4072 break;
4073
4074 case EAGERLY_DESERIALIZED_DECLS:
4075 // FIXME: Skip reading this record if our ASTConsumer doesn't care
4076 // about "interesting" decls (for instance, if we're building a module).
4077 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4078 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4079 break;
4080
4081 case MODULAR_CODEGEN_DECLS:
4082 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
4083 // them (ie: if we're not codegenerating this module).
4084 if (F.Kind == MK_MainFile ||
4085 getContext().getLangOpts().BuildingPCHWithObjectFile)
4086 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4087 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4088 break;
4089
4090 case SPECIAL_TYPES:
4091 if (SpecialTypes.empty()) {
4092 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4093 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
4094 break;
4095 }
4096
4097 if (Record.empty())
4098 break;
4099
4100 if (SpecialTypes.size() != Record.size())
4101 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4102 Fmt: "invalid special-types record");
4103
4104 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4105 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
4106 if (!SpecialTypes[I])
4107 SpecialTypes[I] = ID;
4108 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
4109 // merge step?
4110 }
4111 break;
4112
4113 case STATISTICS:
4114 TotalNumStatements += Record[0];
4115 TotalNumMacros += Record[1];
4116 TotalLexicalDeclContexts += Record[2];
4117 TotalVisibleDeclContexts += Record[3];
4118 TotalModuleLocalVisibleDeclContexts += Record[4];
4119 TotalTULocalVisibleDeclContexts += Record[5];
4120 break;
4121
4122 case UNUSED_FILESCOPED_DECLS:
4123 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4124 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4125 break;
4126
4127 case DELEGATING_CTORS:
4128 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4129 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4130 break;
4131
4132 case WEAK_UNDECLARED_IDENTIFIERS:
4133 if (Record.size() % 3 != 0)
4134 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4135 Fmt: "invalid weak identifiers record");
4136
4137 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4138 // files. This isn't the way to do it :)
4139 WeakUndeclaredIdentifiers.clear();
4140
4141 // Translate the weak, undeclared identifiers into global IDs.
4142 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4143 WeakUndeclaredIdentifiers.push_back(
4144 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4145 WeakUndeclaredIdentifiers.push_back(
4146 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4147 WeakUndeclaredIdentifiers.push_back(
4148 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4149 }
4150 break;
4151
4152 case EXTNAME_UNDECLARED_IDENTIFIERS:
4153 if (Record.size() % 3 != 0)
4154 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4155 Fmt: "invalid extname identifiers record");
4156
4157 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4158 // non-original PCH files. This isn't the way to do it :)
4159 ExtnameUndeclaredIdentifiers.clear();
4160
4161 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4162 // global IDs.
4163 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4164 ExtnameUndeclaredIdentifiers.push_back(
4165 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4166 ExtnameUndeclaredIdentifiers.push_back(
4167 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4168 ExtnameUndeclaredIdentifiers.push_back(
4169 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4170 }
4171 break;
4172
4173 case SELECTOR_OFFSETS: {
4174 F.SelectorOffsets = (const uint32_t *)Blob.data();
4175 F.LocalNumSelectors = Record[0];
4176 unsigned LocalBaseSelectorID = Record[1];
4177 F.BaseSelectorID = getTotalNumSelectors();
4178
4179 if (F.LocalNumSelectors > 0) {
4180 // Introduce the global -> local mapping for selectors within this
4181 // module.
4182 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4183
4184 // Introduce the local -> global mapping for selectors within this
4185 // module.
4186 F.SelectorRemap.insertOrReplace(
4187 Val: std::make_pair(x&: LocalBaseSelectorID,
4188 y: F.BaseSelectorID - LocalBaseSelectorID));
4189
4190 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4191 }
4192 break;
4193 }
4194
4195 case METHOD_POOL:
4196 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4197 if (Record[0])
4198 F.SelectorLookupTable
4199 = ASTSelectorLookupTable::Create(
4200 Buckets: F.SelectorLookupTableData + Record[0],
4201 Base: F.SelectorLookupTableData,
4202 InfoObj: ASTSelectorLookupTrait(*this, F));
4203 TotalNumMethodPoolEntries += Record[1];
4204 break;
4205
4206 case REFERENCED_SELECTOR_POOL:
4207 if (!Record.empty()) {
4208 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4209 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4210 LocalID: Record[Idx++]));
4211 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4212 getRawEncoding());
4213 }
4214 }
4215 break;
4216
4217 case PP_ASSUME_NONNULL_LOC: {
4218 unsigned Idx = 0;
4219 if (!Record.empty())
4220 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4221 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4222 break;
4223 }
4224
4225 case PP_UNSAFE_BUFFER_USAGE: {
4226 if (!Record.empty()) {
4227 SmallVector<SourceLocation, 64> SrcLocs;
4228 unsigned Idx = 0;
4229 while (Idx < Record.size())
4230 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4231 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4232 }
4233 break;
4234 }
4235
4236 case PP_CONDITIONAL_STACK:
4237 if (!Record.empty()) {
4238 unsigned Idx = 0, End = Record.size() - 1;
4239 bool ReachedEOFWhileSkipping = Record[Idx++];
4240 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4241 if (ReachedEOFWhileSkipping) {
4242 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4243 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4244 bool FoundNonSkipPortion = Record[Idx++];
4245 bool FoundElse = Record[Idx++];
4246 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4247 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4248 args&: FoundElse, args&: ElseLoc);
4249 }
4250 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4251 while (Idx < End) {
4252 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4253 bool WasSkipping = Record[Idx++];
4254 bool FoundNonSkip = Record[Idx++];
4255 bool FoundElse = Record[Idx++];
4256 ConditionalStack.push_back(
4257 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4258 }
4259 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4260 }
4261 break;
4262
4263 case PP_COUNTER_VALUE:
4264 if (!Record.empty() && Listener)
4265 Listener->ReadCounter(M: F, Value: Record[0]);
4266 break;
4267
4268 case FILE_SORTED_DECLS:
4269 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4270 F.NumFileSortedDecls = Record[0];
4271 break;
4272
4273 case SOURCE_LOCATION_OFFSETS: {
4274 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4275 F.LocalNumSLocEntries = Record[0];
4276 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4277 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4278 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4279 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4280 TotalSize: SLocSpaceSize);
4281 if (!F.SLocEntryBaseID) {
4282 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4283 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4284 return llvm::createStringError(EC: std::errc::invalid_argument,
4285 Fmt: "ran out of source locations");
4286 }
4287 // Make our entry in the range map. BaseID is negative and growing, so
4288 // we invert it. Because we invert it, though, we need the other end of
4289 // the range.
4290 unsigned RangeStart =
4291 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4292 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4293 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4294
4295 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4296 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4297 GlobalSLocOffsetMap.insert(
4298 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4299 - SLocSpaceSize,y: &F));
4300
4301 TotalNumSLocEntries += F.LocalNumSLocEntries;
4302 break;
4303 }
4304
4305 case MODULE_OFFSET_MAP:
4306 F.ModuleOffsetMap = Blob;
4307 break;
4308
4309 case SOURCE_MANAGER_LINE_TABLE:
4310 ParseLineTable(F, Record);
4311 break;
4312
4313 case EXT_VECTOR_DECLS:
4314 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4315 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4316 break;
4317
4318 case VTABLE_USES:
4319 if (Record.size() % 3 != 0)
4320 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4321 Fmt: "Invalid VTABLE_USES record");
4322
4323 // Later tables overwrite earlier ones.
4324 // FIXME: Modules will have some trouble with this. This is clearly not
4325 // the right way to do this.
4326 VTableUses.clear();
4327
4328 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4329 VTableUses.push_back(
4330 Elt: {.ID: ReadDeclID(F, Record, Idx),
4331 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4332 .Used: (bool)Record[Idx++]});
4333 }
4334 break;
4335
4336 case PENDING_IMPLICIT_INSTANTIATIONS:
4337
4338 if (Record.size() % 2 != 0)
4339 return llvm::createStringError(
4340 EC: std::errc::illegal_byte_sequence,
4341 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4342
4343 // For standard C++20 module, we will only reads the instantiations
4344 // if it is the main file.
4345 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4346 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4347 PendingInstantiations.push_back(
4348 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4349 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4350 }
4351 }
4352 break;
4353
4354 case SEMA_DECL_REFS:
4355 if (Record.size() != 3)
4356 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4357 Fmt: "Invalid SEMA_DECL_REFS block");
4358 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4359 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4360 break;
4361
4362 case PPD_ENTITIES_OFFSETS: {
4363 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4364 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4365 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4366
4367 unsigned StartingID;
4368 if (!PP.getPreprocessingRecord())
4369 PP.createPreprocessingRecord();
4370 if (!PP.getPreprocessingRecord()->getExternalSource())
4371 PP.getPreprocessingRecord()->SetExternalSource(*this);
4372 StartingID
4373 = PP.getPreprocessingRecord()
4374 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4375 F.BasePreprocessedEntityID = StartingID;
4376
4377 if (F.NumPreprocessedEntities > 0) {
4378 // Introduce the global -> local mapping for preprocessed entities in
4379 // this module.
4380 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4381 }
4382
4383 break;
4384 }
4385
4386 case PPD_SKIPPED_RANGES: {
4387 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4388 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4389 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4390
4391 if (!PP.getPreprocessingRecord())
4392 PP.createPreprocessingRecord();
4393 if (!PP.getPreprocessingRecord()->getExternalSource())
4394 PP.getPreprocessingRecord()->SetExternalSource(*this);
4395 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4396 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4397
4398 if (F.NumPreprocessedSkippedRanges > 0)
4399 GlobalSkippedRangeMap.insert(
4400 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4401 break;
4402 }
4403
4404 case DECL_UPDATE_OFFSETS:
4405 if (Record.size() % 2 != 0)
4406 return llvm::createStringError(
4407 EC: std::errc::illegal_byte_sequence,
4408 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4409 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4410 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4411 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4412
4413 // If we've already loaded the decl, perform the updates when we finish
4414 // loading this block.
4415 if (Decl *D = GetExistingDecl(ID))
4416 PendingUpdateRecords.push_back(
4417 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4418 }
4419 break;
4420
4421 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4422 if (Record.size() % 5 != 0)
4423 return llvm::createStringError(
4424 EC: std::errc::illegal_byte_sequence,
4425 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4426 "file");
4427 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4428 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4429
4430 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4431 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4432 uint64_t LocalLexicalOffset = Record[I++];
4433 uint64_t LexicalOffset =
4434 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4435 uint64_t LocalVisibleOffset = Record[I++];
4436 uint64_t VisibleOffset =
4437 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4438 uint64_t LocalModuleLocalOffset = Record[I++];
4439 uint64_t ModuleLocalOffset =
4440 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4441 uint64_t TULocalLocalOffset = Record[I++];
4442 uint64_t TULocalOffset =
4443 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4444
4445 DelayedNamespaceOffsetMap[ID] = {
4446 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4447
4448 assert(!GetExistingDecl(ID) &&
4449 "We shouldn't load the namespace in the front of delayed "
4450 "namespace lexical and visible block");
4451 }
4452 break;
4453 }
4454
4455 case RELATED_DECLS_MAP:
4456 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4457 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4458 auto &RelatedDecls = RelatedDeclsMap[ID];
4459 unsigned NN = Record[I++];
4460 RelatedDecls.reserve(N: NN);
4461 for (unsigned II = 0; II < NN; II++)
4462 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4463 }
4464 break;
4465
4466 case OBJC_CATEGORIES_MAP:
4467 if (F.LocalNumObjCCategoriesInMap != 0)
4468 return llvm::createStringError(
4469 EC: std::errc::illegal_byte_sequence,
4470 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4471
4472 F.LocalNumObjCCategoriesInMap = Record[0];
4473 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4474 break;
4475
4476 case OBJC_CATEGORIES:
4477 F.ObjCCategories.swap(RHS&: Record);
4478 break;
4479
4480 case CUDA_SPECIAL_DECL_REFS:
4481 // Later tables overwrite earlier ones.
4482 // FIXME: Modules will have trouble with this.
4483 CUDASpecialDeclRefs.clear();
4484 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4485 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4486 break;
4487
4488 case HEADER_SEARCH_TABLE:
4489 F.HeaderFileInfoTableData = Blob.data();
4490 F.LocalNumHeaderFileInfos = Record[1];
4491 if (Record[0]) {
4492 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4493 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4494 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4495 InfoObj: HeaderFileInfoTrait(*this, F));
4496
4497 PP.getHeaderSearchInfo().SetExternalSource(this);
4498 if (!PP.getHeaderSearchInfo().getExternalLookup())
4499 PP.getHeaderSearchInfo().SetExternalLookup(this);
4500 }
4501 break;
4502
4503 case FP_PRAGMA_OPTIONS:
4504 // Later tables overwrite earlier ones.
4505 FPPragmaOptions.swap(RHS&: Record);
4506 break;
4507
4508 case DECLS_WITH_EFFECTS_TO_VERIFY:
4509 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4510 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4511 break;
4512
4513 case OMP_REQUIRES_DECLS:
4514 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4515 OpenMPRequiresDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4516 break;
4517
4518 case OPENCL_EXTENSIONS:
4519 for (unsigned I = 0, E = Record.size(); I != E; ) {
4520 auto Name = ReadString(Record, Idx&: I);
4521 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4522 OptInfo.Supported = Record[I++] != 0;
4523 OptInfo.Enabled = Record[I++] != 0;
4524 OptInfo.WithPragma = Record[I++] != 0;
4525 OptInfo.Avail = Record[I++];
4526 OptInfo.Core = Record[I++];
4527 OptInfo.Opt = Record[I++];
4528 }
4529 break;
4530
4531 case TENTATIVE_DEFINITIONS:
4532 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4533 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4534 break;
4535
4536 case KNOWN_NAMESPACES:
4537 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4538 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4539 break;
4540
4541 case UNDEFINED_BUT_USED:
4542 if (Record.size() % 2 != 0)
4543 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4544 Fmt: "invalid undefined-but-used record");
4545 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4546 UndefinedButUsed.push_back(
4547 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4548 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4549 }
4550 break;
4551
4552 case DELETE_EXPRS_TO_ANALYZE:
4553 for (unsigned I = 0, N = Record.size(); I != N;) {
4554 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4555 const uint64_t Count = Record[I++];
4556 DelayedDeleteExprs.push_back(Elt: Count);
4557 for (uint64_t C = 0; C < Count; ++C) {
4558 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4559 bool IsArrayForm = Record[I++] == 1;
4560 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4561 }
4562 }
4563 break;
4564
4565 case VTABLES_TO_EMIT:
4566 if (F.Kind == MK_MainFile ||
4567 getContext().getLangOpts().BuildingPCHWithObjectFile)
4568 for (unsigned I = 0, N = Record.size(); I != N;)
4569 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4570 break;
4571
4572 case IMPORTED_MODULES:
4573 if (!F.isModule()) {
4574 // If we aren't loading a module (which has its own exports), make
4575 // all of the imported modules visible.
4576 // FIXME: Deal with macros-only imports.
4577 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4578 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4579 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4580 if (GlobalID) {
4581 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4582 if (DeserializationListener)
4583 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4584 }
4585 }
4586 }
4587 break;
4588
4589 case MACRO_OFFSET: {
4590 if (F.LocalNumMacros != 0)
4591 return llvm::createStringError(
4592 EC: std::errc::illegal_byte_sequence,
4593 Fmt: "duplicate MACRO_OFFSET record in AST file");
4594 F.MacroOffsets = (const uint32_t *)Blob.data();
4595 F.LocalNumMacros = Record[0];
4596 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4597 F.BaseMacroID = getTotalNumMacros();
4598
4599 if (F.LocalNumMacros > 0)
4600 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4601 break;
4602 }
4603
4604 case LATE_PARSED_TEMPLATE:
4605 LateParsedTemplates.emplace_back(
4606 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4607 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4608 break;
4609
4610 case OPTIMIZE_PRAGMA_OPTIONS:
4611 if (Record.size() != 1)
4612 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4613 Fmt: "invalid pragma optimize record");
4614 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4615 break;
4616
4617 case MSSTRUCT_PRAGMA_OPTIONS:
4618 if (Record.size() != 1)
4619 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4620 Fmt: "invalid pragma ms_struct record");
4621 PragmaMSStructState = Record[0];
4622 break;
4623
4624 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4625 if (Record.size() != 2)
4626 return llvm::createStringError(
4627 EC: std::errc::illegal_byte_sequence,
4628 Fmt: "invalid pragma pointers to members record");
4629 PragmaMSPointersToMembersState = Record[0];
4630 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4631 break;
4632
4633 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4634 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4635 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4636 break;
4637
4638 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4639 if (Record.size() != 1)
4640 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4641 Fmt: "invalid cuda pragma options record");
4642 ForceHostDeviceDepth = Record[0];
4643 break;
4644
4645 case ALIGN_PACK_PRAGMA_OPTIONS: {
4646 if (Record.size() < 3)
4647 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4648 Fmt: "invalid pragma pack record");
4649 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4650 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4651 unsigned NumStackEntries = Record[2];
4652 unsigned Idx = 3;
4653 // Reset the stack when importing a new module.
4654 PragmaAlignPackStack.clear();
4655 for (unsigned I = 0; I < NumStackEntries; ++I) {
4656 PragmaAlignPackStackEntry Entry;
4657 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4658 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4659 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4660 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4661 Entry.SlotLabel = PragmaAlignPackStrings.back();
4662 PragmaAlignPackStack.push_back(Elt: Entry);
4663 }
4664 break;
4665 }
4666
4667 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4668 if (Record.size() < 3)
4669 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4670 Fmt: "invalid pragma float control record");
4671 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4672 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4673 unsigned NumStackEntries = Record[2];
4674 unsigned Idx = 3;
4675 // Reset the stack when importing a new module.
4676 FpPragmaStack.clear();
4677 for (unsigned I = 0; I < NumStackEntries; ++I) {
4678 FpPragmaStackEntry Entry;
4679 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4680 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4681 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4682 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4683 Entry.SlotLabel = FpPragmaStrings.back();
4684 FpPragmaStack.push_back(Elt: Entry);
4685 }
4686 break;
4687 }
4688
4689 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4690 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4691 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4692 break;
4693
4694 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4695 unsigned NumRecords = Record.front();
4696 // Last record which is used to keep number of valid records.
4697 if (Record.size() - 1 != NumRecords)
4698 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4699 Fmt: "invalid rvv intrinsic pragma record");
4700
4701 if (RISCVVecIntrinsicPragma.empty())
4702 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4703 // There might be multiple precompiled modules imported, we need to union
4704 // them all.
4705 for (unsigned i = 0; i < NumRecords; ++i)
4706 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4707 break;
4708 }
4709 }
4710 }
4711}
4712
4713void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4714 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4715
4716 // Additional remapping information.
4717 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4718 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4719 F.ModuleOffsetMap = StringRef();
4720
4721 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4722 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4723 RemapBuilder SelectorRemap(F.SelectorRemap);
4724
4725 auto &ImportedModuleVector = F.TransitiveImports;
4726 assert(ImportedModuleVector.empty());
4727
4728 while (Data < DataEnd) {
4729 // FIXME: Looking up dependency modules by filename is horrible. Let's
4730 // start fixing this with prebuilt, explicit and implicit modules and see
4731 // how it goes...
4732 using namespace llvm::support;
4733 ModuleKind Kind = static_cast<ModuleKind>(
4734 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4735 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4736 StringRef Name = StringRef((const char*)Data, Len);
4737 Data += Len;
4738 ModuleFile *OM =
4739 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4740 Kind == MK_ImplicitModule
4741 ? ModuleMgr.lookupByModuleName(ModName: Name)
4742 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4743 if (!OM)
4744 OM = ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeInMemory(Name));
4745 if (!OM) {
4746 std::string Msg = "refers to unknown module, cannot find ";
4747 Msg.append(str: std::string(Name));
4748 Error(Msg);
4749 return;
4750 }
4751
4752 ImportedModuleVector.push_back(Elt: OM);
4753
4754 uint32_t SubmoduleIDOffset =
4755 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4756 uint32_t SelectorIDOffset =
4757 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4758
4759 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4760 RemapBuilder &Remap) {
4761 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4762 if (Offset != None)
4763 Remap.insert(Val: std::make_pair(x&: Offset,
4764 y: static_cast<int>(BaseOffset - Offset)));
4765 };
4766
4767 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4768 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4769 }
4770}
4771
4772ASTReader::ASTReadResult
4773ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4774 const ModuleFile *ImportedBy,
4775 unsigned ClientLoadCapabilities) {
4776 unsigned Idx = 0;
4777 F.ModuleMapPath = ReadPath(F, Record, Idx);
4778
4779 // Try to resolve ModuleName in the current header search context and
4780 // verify that it is found in the same module map file as we saved. If the
4781 // top-level AST file is a main file, skip this check because there is no
4782 // usable header search context.
4783 assert(!F.ModuleName.empty() &&
4784 "MODULE_NAME should come before MODULE_MAP_FILE");
4785 auto [MaybeM, IgnoreError] =
4786 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4787 if (MaybeM.has_value()) {
4788 // An implicitly-loaded module file should have its module listed in some
4789 // module map file that we've already loaded.
4790 Module *M = MaybeM.value();
4791 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4792 OptionalFileEntryRef ModMap =
4793 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4794 if (!IgnoreError && !ModMap) {
4795 if (M && M->Directory)
4796 Diag(DiagID: diag::remark_module_relocated)
4797 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4798
4799 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4800 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4801 // This module was defined by an imported (explicit) module.
4802 Diag(DiagID: diag::err_module_file_conflict)
4803 << F.ModuleName << F.FileName << *ASTFileName;
4804 // TODO: Add a note with the module map paths if they differ.
4805 } else {
4806 // This module was built with a different module map.
4807 Diag(DiagID: diag::err_imported_module_not_found)
4808 << F.ModuleName << F.FileName
4809 << (ImportedBy ? ImportedBy->FileName.str() : "")
4810 << F.ModuleMapPath << !ImportedBy;
4811 // In case it was imported by a PCH, there's a chance the user is
4812 // just missing to include the search path to the directory containing
4813 // the modulemap.
4814 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4815 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4816 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4817 }
4818 }
4819 return OutOfDate;
4820 }
4821
4822 assert(M && M->Name == F.ModuleName && "found module with different name");
4823
4824 // Check the primary module map file.
4825 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4826 if (!StoredModMap || *StoredModMap != ModMap) {
4827 assert(ModMap && "found module is missing module map file");
4828 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4829 "top-level import should be verified");
4830 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4831 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4832 Diag(DiagID: diag::err_imported_module_modmap_changed)
4833 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4834 << ModMap->getName() << F.ModuleMapPath << NotImported;
4835 return OutOfDate;
4836 }
4837
4838 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4839 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4840 // FIXME: we should use input files rather than storing names.
4841 std::string Filename = ReadPath(F, Record, Idx);
4842 auto SF = FileMgr.getOptionalFileRef(Filename);
4843 if (!SF) {
4844 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4845 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4846 return OutOfDate;
4847 }
4848 AdditionalStoredMaps.insert(V: *SF);
4849 }
4850
4851 // Check any additional module map files (e.g. module.private.modulemap)
4852 // that are not in the pcm.
4853 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4854 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4855 // Remove files that match
4856 // Note: SmallPtrSet::erase is really remove
4857 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4858 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4859 Diag(DiagID: diag::err_module_different_modmap)
4860 << F.ModuleName << /*new*/0 << ModMap.getName();
4861 return OutOfDate;
4862 }
4863 }
4864 }
4865
4866 // Check any additional module map files that are in the pcm, but not
4867 // found in header search. Cases that match are already removed.
4868 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4869 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4870 Diag(DiagID: diag::err_module_different_modmap)
4871 << F.ModuleName << /*not new*/1 << ModMap.getName();
4872 return OutOfDate;
4873 }
4874 }
4875
4876 if (Listener)
4877 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4878 return Success;
4879}
4880
4881void ASTReader::ReadDirectoryDependencies(const RecordData &Record,
4882 ModuleFile &F) {
4883 unsigned Idx = 0;
4884 unsigned N = Record[Idx++];
4885 F.DirectoryDependencies.reserve(n: N);
4886 for (unsigned I = 0; I != N; ++I) {
4887 // The base directory is relative if the module was found through a
4888 // relative search path, and empty with -fmodule-file-home-is-cwd.
4889 SmallString<256> Dir(ReadPath(F, Record, Idx));
4890 ModuleMap::canonicalizeDirectoryDependency(FileMgr, Path&: Dir);
4891 F.DirectoryDependencies.push_back(x: std::string(Dir));
4892 }
4893}
4894
4895bool ASTReader::isDirectoryDependencyOutOfDate(ModuleFile &F, bool Complain) {
4896 llvm::vfs::FileSystem &FS = PP.getFileManager().getVirtualFileSystem();
4897 // Adding or removing an entry updates the modification time of the directory
4898 // holding it, so comparing every directory in the tree against the module
4899 // file catches a header that was added, even one whose own modification time
4900 // is older. A directory that doesn't exist has no listing to depend on.
4901 auto IsNewer = [&](StringRef Dir) {
4902 llvm::ErrorOr<llvm::vfs::Status> Status = FS.status(Path: Dir);
4903 return Status && Status->isDirectory() &&
4904 llvm::sys::toTimeT(TP: Status->getLastModificationTime()) > F.ModTime;
4905 };
4906
4907 ModuleCache &ModCache = getModuleManager().getModuleCache();
4908 const HeaderSearchOptions &HSOpts =
4909 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4910 for (StringRef Dir : F.DirectoryDependencies) {
4911 if (std::optional<bool> Invalidated =
4912 ModCache.isDirectoryInvalidated(Directory: Dir)) {
4913 // The build system reports paths as changed before the build session
4914 // started, so a module built during it is newer than them.
4915 if (!*Invalidated ||
4916 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp)
4917 continue;
4918 Diag(DiagID: diag::remark_module_path_invalidated) << F.ModuleName << Dir;
4919 if (Complain)
4920 Diag(DiagID: diag::err_module_path_invalidated) << F.ModuleName << Dir;
4921 return true;
4922 }
4923
4924 std::optional<std::string> Changed;
4925 if (IsNewer(Dir)) {
4926 Changed = Dir.str();
4927 } else {
4928 std::error_code EC;
4929 for (llvm::vfs::recursive_directory_iterator I(FS, Dir, EC), E;
4930 I != E && !EC; I.increment(EC)) {
4931 if (I->type() == llvm::sys::fs::file_type::directory_file &&
4932 IsNewer(I->path())) {
4933 Changed = I->path().str();
4934 break;
4935 }
4936 }
4937 }
4938 if (!Changed)
4939 continue;
4940 Diag(DiagID: diag::remark_module_directory_dep_changed) << F.ModuleName << *Changed;
4941 if (Complain)
4942 Diag(DiagID: diag::err_module_directory_dep_changed) << F.ModuleName << *Changed;
4943 return true;
4944 }
4945 return false;
4946}
4947
4948/// Move the given method to the back of the global list of methods.
4949static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4950 // Find the entry for this selector in the method pool.
4951 SemaObjC::GlobalMethodPool::iterator Known =
4952 S.ObjC().MethodPool.find(Val: Method->getSelector());
4953 if (Known == S.ObjC().MethodPool.end())
4954 return;
4955
4956 // Retrieve the appropriate method list.
4957 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4958 : Known->second.second;
4959 bool Found = false;
4960 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4961 if (!Found) {
4962 if (List->getMethod() == Method) {
4963 Found = true;
4964 } else {
4965 // Keep searching.
4966 continue;
4967 }
4968 }
4969
4970 if (List->getNext())
4971 List->setMethod(List->getNext()->getMethod());
4972 else
4973 List->setMethod(Method);
4974 }
4975}
4976
4977void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4978 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4979 for (Decl *D : Names) {
4980 bool wasHidden = !D->isUnconditionallyVisible();
4981 D->setVisibleDespiteOwningModule();
4982
4983 if (wasHidden && SemaObj) {
4984 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4985 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4986 }
4987 }
4988 }
4989}
4990
4991void ASTReader::makeModuleVisible(Module *Mod,
4992 Module::NameVisibilityKind NameVisibility,
4993 SourceLocation ImportLoc) {
4994 llvm::SmallPtrSet<Module *, 4> Visited;
4995 SmallVector<Module *, 4> Stack;
4996 Stack.push_back(Elt: Mod);
4997 while (!Stack.empty()) {
4998 Mod = Stack.pop_back_val();
4999
5000 if (NameVisibility <= Mod->NameVisibility) {
5001 // This module already has this level of visibility (or greater), so
5002 // there is nothing more to do.
5003 continue;
5004 }
5005
5006 if (Mod->isUnimportable()) {
5007 // Modules that aren't importable cannot be made visible.
5008 continue;
5009 }
5010
5011 // Update the module's name visibility.
5012 Mod->NameVisibility = NameVisibility;
5013
5014 // If we've already deserialized any names from this module,
5015 // mark them as visible.
5016 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
5017 if (Hidden != HiddenNamesMap.end()) {
5018 auto HiddenNames = std::move(*Hidden);
5019 HiddenNamesMap.erase(I: Hidden);
5020 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
5021 assert(!HiddenNamesMap.contains(Mod) &&
5022 "making names visible added hidden names");
5023 }
5024
5025 // Push any exported modules onto the stack to be marked as visible.
5026 SmallVector<Module *, 16> Exports;
5027 Mod->getExportedModules(Exported&: Exports);
5028 for (SmallVectorImpl<Module *>::iterator
5029 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
5030 Module *Exported = *I;
5031 if (Visited.insert(Ptr: Exported).second)
5032 Stack.push_back(Elt: Exported);
5033 }
5034 }
5035}
5036
5037/// We've merged the definition \p MergedDef into the existing definition
5038/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
5039/// visible.
5040void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
5041 NamedDecl *MergedDef) {
5042 if (!Def->isUnconditionallyVisible()) {
5043 // If MergedDef is visible or becomes visible, make the definition visible.
5044 if (MergedDef->isUnconditionallyVisible())
5045 Def->setVisibleDespiteOwningModule();
5046 else {
5047 getContext().mergeDefinitionIntoModule(
5048 ND: Def, M: MergedDef->getImportedOwningModule(),
5049 /*NotifyListeners*/ false);
5050 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
5051 }
5052 }
5053}
5054
5055bool ASTReader::loadGlobalIndex() {
5056 if (GlobalIndex)
5057 return false;
5058
5059 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
5060 !PP.getLangOpts().Modules)
5061 return true;
5062
5063 // Try to load the global index.
5064 TriedLoadingGlobalIndex = true;
5065 StringRef SpecificModuleCachePath =
5066 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
5067 std::pair<GlobalModuleIndex *, llvm::Error> Result =
5068 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
5069 if (llvm::Error Err = std::move(Result.second)) {
5070 assert(!Result.first);
5071 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5072 return true;
5073 }
5074
5075 GlobalIndex.reset(p: Result.first);
5076 ModuleMgr.setGlobalIndex(GlobalIndex.get());
5077 return false;
5078}
5079
5080bool ASTReader::isGlobalIndexUnavailable() const {
5081 return PP.getLangOpts().Modules && UseGlobalIndex &&
5082 !hasGlobalIndex() && TriedLoadingGlobalIndex;
5083}
5084
5085/// Given a cursor at the start of an AST file, scan ahead and drop the
5086/// cursor into the start of the given block ID, returning false on success and
5087/// true on failure.
5088static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
5089 while (true) {
5090 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
5091 if (!MaybeEntry) {
5092 // FIXME this drops errors on the floor.
5093 consumeError(Err: MaybeEntry.takeError());
5094 return true;
5095 }
5096 llvm::BitstreamEntry Entry = MaybeEntry.get();
5097
5098 switch (Entry.Kind) {
5099 case llvm::BitstreamEntry::Error:
5100 case llvm::BitstreamEntry::EndBlock:
5101 return true;
5102
5103 case llvm::BitstreamEntry::Record:
5104 // Ignore top-level records.
5105 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
5106 break;
5107 else {
5108 // FIXME this drops errors on the floor.
5109 consumeError(Err: Skipped.takeError());
5110 return true;
5111 }
5112
5113 case llvm::BitstreamEntry::SubBlock:
5114 if (Entry.ID == BlockID) {
5115 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
5116 // FIXME this drops the error on the floor.
5117 consumeError(Err: std::move(Err));
5118 return true;
5119 }
5120 // Found it!
5121 return false;
5122 }
5123
5124 if (llvm::Error Err = Cursor.SkipBlock()) {
5125 // FIXME this drops the error on the floor.
5126 consumeError(Err: std::move(Err));
5127 return true;
5128 }
5129 }
5130 }
5131}
5132
5133ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
5134 ModuleKind Type,
5135 SourceLocation ImportLoc,
5136 unsigned ClientLoadCapabilities,
5137 ModuleFile **NewLoadedModuleFile) {
5138 llvm::TimeTraceScope scope("ReadAST", FileName);
5139
5140 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
5141 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
5142 CurrentDeserializingModuleKind, Type);
5143
5144 // Defer any pending actions until we get to the end of reading the AST file.
5145 Deserializing AnASTFile(this);
5146
5147 // Bump the generation number.
5148 unsigned PreviousGeneration = 0;
5149 if (ContextObj)
5150 PreviousGeneration = incrementGeneration(C&: *ContextObj);
5151
5152 unsigned NumModules = ModuleMgr.size();
5153 SmallVector<ImportedModule, 4> Loaded;
5154 if (ASTReadResult ReadResult =
5155 ReadASTCore(FileName, Type, ImportLoc,
5156 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
5157 ClientLoadCapabilities)) {
5158 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
5159
5160 // If we find that any modules are unusable, the global index is going
5161 // to be out-of-date. Just remove it.
5162 GlobalIndex.reset();
5163 ModuleMgr.setGlobalIndex(nullptr);
5164 return ReadResult;
5165 }
5166
5167 if (NewLoadedModuleFile && !Loaded.empty())
5168 *NewLoadedModuleFile = Loaded.back().Mod;
5169
5170 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
5171 // remove modules from this point. Various fields are updated during reading
5172 // the AST block and removing the modules would result in dangling pointers.
5173 // They are generally only incidentally dereferenced, ie. a binary search
5174 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
5175 // be dereferenced but it wouldn't actually be used.
5176
5177 // Load the AST blocks of all of the modules that we loaded. We can still
5178 // hit errors parsing the ASTs at this point.
5179 for (ImportedModule &M : Loaded) {
5180 ModuleFile &F = *M.Mod;
5181 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
5182
5183 // Read the AST block.
5184 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
5185 Error(Err: std::move(Err));
5186 return Failure;
5187 }
5188
5189 // The AST block should always have a definition for the main module.
5190 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5191 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5192 return Failure;
5193 }
5194
5195 // Read the extension blocks.
5196 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5197 if (llvm::Error Err = ReadExtensionBlock(F)) {
5198 Error(Err: std::move(Err));
5199 return Failure;
5200 }
5201 }
5202
5203 // Once read, set the ModuleFile bit base offset and update the size in
5204 // bits of all files we've seen.
5205 F.GlobalBitOffset = TotalModulesSizeInBits;
5206 TotalModulesSizeInBits += F.SizeInBits;
5207 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5208 }
5209
5210 // Preload source locations and interesting indentifiers.
5211 for (ImportedModule &M : Loaded) {
5212 ModuleFile &F = *M.Mod;
5213
5214 // Map the original source file ID into the ID space of the current
5215 // compilation.
5216 if (F.OriginalSourceFileID.isValid())
5217 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5218
5219 for (auto Offset : F.PreloadIdentifierOffsets) {
5220 const unsigned char *Data = F.IdentifierTableData + Offset;
5221
5222 ASTIdentifierLookupTrait Trait(*this, F);
5223 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5224 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5225
5226 IdentifierInfo *II;
5227 if (!PP.getLangOpts().CPlusPlus) {
5228 // Identifiers present in both the module file and the importing
5229 // instance are marked out-of-date so that they can be deserialized
5230 // on next use via ASTReader::updateOutOfDateIdentifier().
5231 // Identifiers present in the module file but not in the importing
5232 // instance are ignored for now, preventing growth of the identifier
5233 // table. They will be deserialized on first use via ASTReader::get().
5234 auto It = PP.getIdentifierTable().find(Name: Key);
5235 if (It == PP.getIdentifierTable().end())
5236 continue;
5237 II = It->second;
5238 } else {
5239 // With C++ modules, not many identifiers are considered interesting.
5240 // All identifiers in the module file can be placed into the identifier
5241 // table of the importing instance and marked as out-of-date. This makes
5242 // ASTReader::get() a no-op, and deserialization will take place on
5243 // first/next use via ASTReader::updateOutOfDateIdentifier().
5244 II = &PP.getIdentifierTable().getOwn(Name: Key);
5245 }
5246
5247 II->setOutOfDate(true);
5248
5249 // Mark this identifier as being from an AST file so that we can track
5250 // whether we need to serialize it.
5251 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5252
5253 // Associate the ID with the identifier so that the writer can reuse it.
5254 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5255 SetIdentifierInfo(ID, II);
5256 }
5257 }
5258
5259 // Builtins and library builtins have already been initialized. Mark all
5260 // identifiers as out-of-date, so that they are deserialized on first use.
5261 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5262 for (auto &Id : PP.getIdentifierTable())
5263 Id.second->setOutOfDate(true);
5264
5265 // Mark selectors as out of date.
5266 for (const auto &Sel : SelectorGeneration)
5267 SelectorOutOfDate[Sel.first] = true;
5268
5269 // Setup the import locations and notify the module manager that we've
5270 // committed to these module files.
5271 for (ImportedModule &M : Loaded) {
5272 ModuleFile &F = *M.Mod;
5273
5274 ModuleMgr.moduleFileAccepted(MF: &F);
5275
5276 // Set the import location.
5277 F.DirectImportLoc = ImportLoc;
5278 // FIXME: We assume that locations from PCH / preamble do not need
5279 // any translation.
5280 if (!M.ImportedBy)
5281 F.ImportLoc = M.ImportLoc;
5282 else
5283 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5284 }
5285
5286 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5287 // Might be unnecessary as use declarations are only used to build the
5288 // module itself.
5289
5290 if (ContextObj)
5291 InitializeContext();
5292
5293 if (SemaObj)
5294 UpdateSema();
5295
5296 if (DeserializationListener)
5297 DeserializationListener->ReaderInitialized(Reader: this);
5298
5299 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5300 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5301 // If this AST file is a precompiled preamble, then set the
5302 // preamble file ID of the source manager to the file source file
5303 // from which the preamble was built.
5304 if (Type == MK_Preamble) {
5305 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5306 } else if (Type == MK_MainFile) {
5307 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5308 }
5309 }
5310
5311 // For any Objective-C class definitions we have already loaded, make sure
5312 // that we load any additional categories.
5313 if (ContextObj) {
5314 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5315 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5316 D: ObjCClassesLoaded[I], PreviousGeneration);
5317 }
5318 }
5319
5320 const HeaderSearchOptions &HSOpts =
5321 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5322 if (HSOpts.ModulesValidateOncePerBuildSession) {
5323 // Now we are certain that the module and all modules it depends on are
5324 // up-to-date. For implicitly-built module files, ensure the corresponding
5325 // timestamp files are up-to-date in this build session.
5326 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5327 ImportedModule &M = Loaded[I];
5328 if (M.Mod->Kind == MK_ImplicitModule &&
5329 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5330 getModuleManager().getModuleCache().updateModuleTimestamp(
5331 ModuleFilename: M.Mod->FileName);
5332 }
5333 }
5334
5335 return Success;
5336}
5337
5338static ASTFileSignature readASTFileSignature(StringRef PCH);
5339
5340/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5341static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5342 // FIXME checking magic headers is done in other places such as
5343 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5344 // always done the same. Unify it all with a helper.
5345 if (!Stream.canSkipToPos(pos: 4))
5346 return llvm::createStringError(
5347 EC: std::errc::illegal_byte_sequence,
5348 Fmt: "file too small to contain precompiled file magic");
5349 for (unsigned C : {'C', 'P', 'C', 'H'})
5350 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5351 if (Res.get() != C)
5352 return llvm::createStringError(
5353 EC: std::errc::illegal_byte_sequence,
5354 Fmt: "file doesn't start with precompiled file magic");
5355 } else
5356 return Res.takeError();
5357 return llvm::Error::success();
5358}
5359
5360static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5361 switch (Kind) {
5362 case MK_PCH:
5363 return 0; // PCH
5364 case MK_ImplicitModule:
5365 case MK_ExplicitModule:
5366 case MK_PrebuiltModule:
5367 return 1; // module
5368 case MK_MainFile:
5369 case MK_Preamble:
5370 return 2; // main source file
5371 }
5372 llvm_unreachable("unknown module kind");
5373}
5374
5375ASTReader::ASTReadResult ASTReader::ReadASTCore(
5376 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5377 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5378 off_t ExpectedSize, time_t ExpectedModTime,
5379 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5380 auto Result = ModuleMgr.addModule(
5381 FileName, Type, ImportLoc, ImportedBy, Generation: getGeneration(), ExpectedSize,
5382 ExpectedModTime, ExpectedSignature, ReadSignature: readASTFileSignature);
5383 ModuleFile *M = Result.getModule();
5384
5385 switch (Result.getKind()) {
5386 case AddModuleResult::AlreadyLoaded: {
5387 Diag(DiagID: diag::remark_module_import)
5388 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5389 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5390 return Success;
5391 }
5392
5393 case AddModuleResult::NewlyLoaded:
5394 // Load module file below.
5395 break;
5396
5397 case AddModuleResult::Missing:
5398 // The module file was missing; if the client can handle that, return
5399 // it.
5400 if (ClientLoadCapabilities & ARR_Missing)
5401 return Missing;
5402
5403 // Otherwise, return an error.
5404 Diag(DiagID: diag::err_ast_file_not_found)
5405 << moduleKindForDiagnostic(Kind: Type) << FileName;
5406 if (!Result.getBufferError().empty())
5407 Diag(DiagID: diag::note_ast_file_buffer_failed) << Result.getBufferError();
5408 return Failure;
5409
5410 case AddModuleResult::OutOfDate:
5411 // We couldn't load the module file because it is out-of-date. If the
5412 // client can handle out-of-date, return it.
5413 if (ClientLoadCapabilities & ARR_OutOfDate)
5414 return OutOfDate;
5415
5416 // Otherwise, return an error.
5417 Diag(DiagID: diag::err_ast_file_out_of_date)
5418 << moduleKindForDiagnostic(Kind: Type) << FileName;
5419 for (const auto &C : Result.getChanges()) {
5420 Diag(DiagID: diag::note_fe_ast_file_modified)
5421 << C.Kind << (C.Old && C.New) << llvm::itostr(X: C.Old.value_or(u: 0))
5422 << llvm::itostr(X: C.New.value_or(u: 0));
5423 }
5424 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
5425 << Result.getValidationStatus();
5426 if (!Result.getSignatureError().empty())
5427 Diag(DiagID: diag::note_ast_file_signature_failed) << Result.getSignatureError();
5428 return Failure;
5429
5430 case AddModuleResult::None:
5431 llvm_unreachable("Unexpected value from adding module.");
5432 }
5433
5434 assert(M && "Missing module file");
5435
5436 bool ShouldFinalizePCM = false;
5437 llvm::scope_exit FinalizeOrDropPCM([&]() {
5438 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5439 if (ShouldFinalizePCM)
5440 MC.finalizePCM(Filename: FileName);
5441 else
5442 MC.tryToDropPCM(Filename: FileName);
5443 });
5444 ModuleFile &F = *M;
5445 BitstreamCursor &Stream = F.Stream;
5446 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5447 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5448
5449 // Sniff for the signature.
5450 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5451 Diag(DiagID: diag::err_ast_file_invalid)
5452 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5453 return Failure;
5454 }
5455
5456 // This is used for compatibility with older PCH formats.
5457 bool HaveReadControlBlock = false;
5458 while (true) {
5459 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5460 if (!MaybeEntry) {
5461 Error(Err: MaybeEntry.takeError());
5462 return Failure;
5463 }
5464 llvm::BitstreamEntry Entry = MaybeEntry.get();
5465
5466 switch (Entry.Kind) {
5467 case llvm::BitstreamEntry::Error:
5468 case llvm::BitstreamEntry::Record:
5469 case llvm::BitstreamEntry::EndBlock:
5470 Error(Msg: "invalid record at top-level of AST file");
5471 return Failure;
5472
5473 case llvm::BitstreamEntry::SubBlock:
5474 break;
5475 }
5476
5477 switch (Entry.ID) {
5478 case CONTROL_BLOCK_ID:
5479 HaveReadControlBlock = true;
5480 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5481 case Success:
5482 // Check that we didn't try to load a non-module AST file as a module.
5483 //
5484 // FIXME: Should we also perform the converse check? Loading a module as
5485 // a PCH file sort of works, but it's a bit wonky.
5486 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5487 Type == MK_PrebuiltModule) &&
5488 F.ModuleName.empty()) {
5489 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5490 if (Result != OutOfDate ||
5491 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5492 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5493 return Result;
5494 }
5495 break;
5496
5497 case Failure: return Failure;
5498 case Missing: return Missing;
5499 case OutOfDate: return OutOfDate;
5500 case VersionMismatch: return VersionMismatch;
5501 case ConfigurationMismatch: return ConfigurationMismatch;
5502 case HadErrors: return HadErrors;
5503 }
5504 break;
5505
5506 case AST_BLOCK_ID:
5507 if (!HaveReadControlBlock) {
5508 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5509 Diag(DiagID: diag::err_ast_file_version_too_old)
5510 << moduleKindForDiagnostic(Kind: Type) << FileName;
5511 return VersionMismatch;
5512 }
5513
5514 // Record that we've loaded this module.
5515 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5516 ShouldFinalizePCM = true;
5517 return Success;
5518
5519 default:
5520 if (llvm::Error Err = Stream.SkipBlock()) {
5521 Error(Err: std::move(Err));
5522 return Failure;
5523 }
5524 break;
5525 }
5526 }
5527
5528 llvm_unreachable("unexpected break; expected return");
5529}
5530
5531ASTReader::ASTReadResult
5532ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5533 unsigned ClientLoadCapabilities) {
5534 const HeaderSearchOptions &HSOpts =
5535 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5536 bool AllowCompatibleConfigurationMismatch =
5537 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5538 bool DisableValidation = shouldDisableValidationForFile(M: F);
5539
5540 ASTReadResult Result = readUnhashedControlBlockImpl(
5541 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5542 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5543 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5544
5545 // If F was directly imported by another module, it's implicitly validated by
5546 // the importing module.
5547 if (DisableValidation || WasImportedBy ||
5548 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5549 return Success;
5550
5551 if (Result == Failure) {
5552 Error(Msg: "malformed block record in AST file");
5553 return Failure;
5554 }
5555
5556 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5557 // If this module has already been finalized in the ModuleCache, we're stuck
5558 // with it; we can only load a single version of each module.
5559 //
5560 // This can happen when a module is imported in two contexts: in one, as a
5561 // user module; in another, as a system module (due to an import from
5562 // another module marked with the [system] flag). It usually indicates a
5563 // bug in the module map: this module should also be marked with [system].
5564 //
5565 // If -Wno-system-headers (the default), and the first import is as a
5566 // system module, then validation will fail during the as-user import,
5567 // since -Werror flags won't have been validated. However, it's reasonable
5568 // to treat this consistently as a system module.
5569 //
5570 // If -Wsystem-headers, the PCM on disk was built with
5571 // -Wno-system-headers, and the first import is as a user module, then
5572 // validation will fail during the as-system import since the PCM on disk
5573 // doesn't guarantee that -Werror was respected. However, the -Werror
5574 // flags were checked during the initial as-user import.
5575 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5576 Filename: F.FileName)) {
5577 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5578 return Success;
5579 }
5580 }
5581
5582 return Result;
5583}
5584
5585ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5586 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5587 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5588 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5589 // Initialize a stream.
5590 BitstreamCursor Stream(StreamData);
5591
5592 // Sniff for the signature.
5593 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5594 // FIXME this drops the error on the floor.
5595 consumeError(Err: std::move(Err));
5596 return Failure;
5597 }
5598
5599 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5600 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5601 return Failure;
5602
5603 // Read all of the records in the options block.
5604 RecordData Record;
5605 ASTReadResult Result = Success;
5606 while (true) {
5607 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5608 if (!MaybeEntry) {
5609 // FIXME this drops the error on the floor.
5610 consumeError(Err: MaybeEntry.takeError());
5611 return Failure;
5612 }
5613 llvm::BitstreamEntry Entry = MaybeEntry.get();
5614
5615 switch (Entry.Kind) {
5616 case llvm::BitstreamEntry::Error:
5617 case llvm::BitstreamEntry::SubBlock:
5618 return Failure;
5619
5620 case llvm::BitstreamEntry::EndBlock:
5621 return Result;
5622
5623 case llvm::BitstreamEntry::Record:
5624 // The interesting case.
5625 break;
5626 }
5627
5628 // Read and process a record.
5629 Record.clear();
5630 StringRef Blob;
5631 Expected<unsigned> MaybeRecordType =
5632 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5633 if (!MaybeRecordType) {
5634 // FIXME this drops the error.
5635 return Failure;
5636 }
5637 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5638 case SIGNATURE:
5639 if (F) {
5640 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5641 assert(F->Signature != ASTFileSignature::createDummy() &&
5642 "Dummy AST file signature not backpatched in ASTWriter.");
5643 }
5644 break;
5645 case AST_BLOCK_HASH:
5646 if (F) {
5647 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5648 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5649 "Dummy AST block hash not backpatched in ASTWriter.");
5650 }
5651 break;
5652 case DIAGNOSTIC_OPTIONS: {
5653 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5654 if (Listener && ValidateDiagnosticOptions &&
5655 !AllowCompatibleConfigurationMismatch &&
5656 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5657 Result = OutOfDate; // Don't return early. Read the signature.
5658 break;
5659 }
5660 case HEADER_SEARCH_PATHS: {
5661 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5662 if (Listener && !AllowCompatibleConfigurationMismatch &&
5663 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5664 Result = ConfigurationMismatch;
5665 break;
5666 }
5667 case DIAG_PRAGMA_MAPPINGS:
5668 if (!F)
5669 break;
5670 if (F->PragmaDiagMappings.empty())
5671 F->PragmaDiagMappings.swap(RHS&: Record);
5672 else
5673 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5674 From: Record.begin(), To: Record.end());
5675 break;
5676 case HEADER_SEARCH_ENTRY_USAGE:
5677 if (F)
5678 F->SearchPathUsage = ReadBitVector(Record, Blob);
5679 break;
5680 case VFS_USAGE:
5681 if (F)
5682 F->VFSUsage = ReadBitVector(Record, Blob);
5683 break;
5684 }
5685 }
5686}
5687
5688/// Parse a record and blob containing module file extension metadata.
5689static bool parseModuleFileExtensionMetadata(
5690 const SmallVectorImpl<uint64_t> &Record,
5691 StringRef Blob,
5692 ModuleFileExtensionMetadata &Metadata) {
5693 if (Record.size() < 4) return true;
5694
5695 Metadata.MajorVersion = Record[0];
5696 Metadata.MinorVersion = Record[1];
5697
5698 unsigned BlockNameLen = Record[2];
5699 unsigned UserInfoLen = Record[3];
5700
5701 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5702
5703 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5704 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5705 Blob.data() + BlockNameLen + UserInfoLen);
5706 return false;
5707}
5708
5709llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5710 BitstreamCursor &Stream = F.Stream;
5711
5712 RecordData Record;
5713 while (true) {
5714 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5715 if (!MaybeEntry)
5716 return MaybeEntry.takeError();
5717 llvm::BitstreamEntry Entry = MaybeEntry.get();
5718
5719 switch (Entry.Kind) {
5720 case llvm::BitstreamEntry::SubBlock:
5721 if (llvm::Error Err = Stream.SkipBlock())
5722 return Err;
5723 continue;
5724 case llvm::BitstreamEntry::EndBlock:
5725 return llvm::Error::success();
5726 case llvm::BitstreamEntry::Error:
5727 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5728 Fmt: "malformed block record in AST file");
5729 case llvm::BitstreamEntry::Record:
5730 break;
5731 }
5732
5733 Record.clear();
5734 StringRef Blob;
5735 Expected<unsigned> MaybeRecCode =
5736 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5737 if (!MaybeRecCode)
5738 return MaybeRecCode.takeError();
5739 switch (MaybeRecCode.get()) {
5740 case EXTENSION_METADATA: {
5741 ModuleFileExtensionMetadata Metadata;
5742 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5743 return llvm::createStringError(
5744 EC: std::errc::illegal_byte_sequence,
5745 Fmt: "malformed EXTENSION_METADATA in AST file");
5746
5747 // Find a module file extension with this block name.
5748 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5749 if (Known == ModuleFileExtensions.end()) break;
5750
5751 // Form a reader.
5752 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5753 Mod&: F, Stream)) {
5754 F.ExtensionReaders.push_back(x: std::move(Reader));
5755 }
5756
5757 break;
5758 }
5759 }
5760 }
5761
5762 llvm_unreachable("ReadExtensionBlock should return from while loop");
5763}
5764
5765void ASTReader::InitializeContext() {
5766 assert(ContextObj && "no context to initialize");
5767 ASTContext &Context = *ContextObj;
5768
5769 // If there's a listener, notify them that we "read" the translation unit.
5770 if (DeserializationListener)
5771 DeserializationListener->DeclRead(
5772 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5773 D: Context.getTranslationUnitDecl());
5774
5775 // FIXME: Find a better way to deal with collisions between these
5776 // built-in types. Right now, we just ignore the problem.
5777
5778 // Load the special types.
5779 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5780 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5781 if (!Context.CFConstantStringTypeDecl)
5782 Context.setCFConstantStringType(GetType(ID: String));
5783 }
5784
5785 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5786 QualType FileType = GetType(ID: File);
5787 if (FileType.isNull()) {
5788 Error(Msg: "FILE type is NULL");
5789 return;
5790 }
5791
5792 if (!Context.FILEDecl) {
5793 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5794 Context.setFILEDecl(Typedef->getDecl());
5795 else {
5796 const TagType *Tag = FileType->getAs<TagType>();
5797 if (!Tag) {
5798 Error(Msg: "Invalid FILE type in AST file");
5799 return;
5800 }
5801 Context.setFILEDecl(Tag->getDecl());
5802 }
5803 }
5804 }
5805
5806 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5807 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5808 if (Jmp_bufType.isNull()) {
5809 Error(Msg: "jmp_buf type is NULL");
5810 return;
5811 }
5812
5813 if (!Context.jmp_bufDecl) {
5814 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5815 Context.setjmp_bufDecl(Typedef->getDecl());
5816 else {
5817 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5818 if (!Tag) {
5819 Error(Msg: "Invalid jmp_buf type in AST file");
5820 return;
5821 }
5822 Context.setjmp_bufDecl(Tag->getDecl());
5823 }
5824 }
5825 }
5826
5827 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5828 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5829 if (Sigjmp_bufType.isNull()) {
5830 Error(Msg: "sigjmp_buf type is NULL");
5831 return;
5832 }
5833
5834 if (!Context.sigjmp_bufDecl) {
5835 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5836 Context.setsigjmp_bufDecl(Typedef->getDecl());
5837 else {
5838 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5839 assert(Tag && "Invalid sigjmp_buf type in AST file");
5840 Context.setsigjmp_bufDecl(Tag->getDecl());
5841 }
5842 }
5843 }
5844
5845 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5846 if (Context.ObjCIdRedefinitionType.isNull())
5847 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5848 }
5849
5850 if (TypeID ObjCClassRedef =
5851 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5852 if (Context.ObjCClassRedefinitionType.isNull())
5853 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5854 }
5855
5856 if (TypeID ObjCSelRedef =
5857 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5858 if (Context.ObjCSelRedefinitionType.isNull())
5859 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5860 }
5861
5862 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5863 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5864 if (Ucontext_tType.isNull()) {
5865 Error(Msg: "ucontext_t type is NULL");
5866 return;
5867 }
5868
5869 if (!Context.ucontext_tDecl) {
5870 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5871 Context.setucontext_tDecl(Typedef->getDecl());
5872 else {
5873 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5874 assert(Tag && "Invalid ucontext_t type in AST file");
5875 Context.setucontext_tDecl(Tag->getDecl());
5876 }
5877 }
5878 }
5879 }
5880
5881 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5882
5883 // If there were any CUDA special declarations, deserialize them.
5884 if (!CUDASpecialDeclRefs.empty()) {
5885 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5886 Context.setcudaConfigureCallDecl(
5887 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5888 Context.setcudaGetParameterBufferDecl(
5889 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5890 Context.setcudaLaunchDeviceDecl(
5891 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5892 }
5893
5894 // Re-export any modules that were imported by a non-module AST file.
5895 // FIXME: This does not make macro-only imports visible again.
5896 for (auto &Import : PendingImportedModules) {
5897 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5898 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5899 /*ImportLoc=*/Import.ImportLoc);
5900 if (Import.ImportLoc.isValid())
5901 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5902 // This updates visibility for Preprocessor only. For Sema, which can be
5903 // nullptr here, we do the same later, in UpdateSema().
5904 }
5905 }
5906
5907 // Hand off these modules to Sema.
5908 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5909 PendingImportedModules.clear();
5910}
5911
5912void ASTReader::finalizeForWriting() {
5913 // Nothing to do for now.
5914}
5915
5916/// Reads and return the signature record from \p PCH's control block, or
5917/// else returns 0.
5918static ASTFileSignature readASTFileSignature(StringRef PCH) {
5919 BitstreamCursor Stream(PCH);
5920 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5921 // FIXME this drops the error on the floor.
5922 consumeError(Err: std::move(Err));
5923 return ASTFileSignature();
5924 }
5925
5926 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5927 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5928 return ASTFileSignature();
5929
5930 // Scan for SIGNATURE inside the diagnostic options block.
5931 ASTReader::RecordData Record;
5932 while (true) {
5933 Expected<llvm::BitstreamEntry> MaybeEntry =
5934 Stream.advanceSkippingSubblocks();
5935 if (!MaybeEntry) {
5936 // FIXME this drops the error on the floor.
5937 consumeError(Err: MaybeEntry.takeError());
5938 return ASTFileSignature();
5939 }
5940 llvm::BitstreamEntry Entry = MaybeEntry.get();
5941
5942 if (Entry.Kind != llvm::BitstreamEntry::Record)
5943 return ASTFileSignature();
5944
5945 Record.clear();
5946 StringRef Blob;
5947 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5948 if (!MaybeRecord) {
5949 // FIXME this drops the error on the floor.
5950 consumeError(Err: MaybeRecord.takeError());
5951 return ASTFileSignature();
5952 }
5953 if (SIGNATURE == MaybeRecord.get()) {
5954 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5955 assert(Signature != ASTFileSignature::createDummy() &&
5956 "Dummy AST file signature not backpatched in ASTWriter.");
5957 return Signature;
5958 }
5959 }
5960}
5961
5962/// Retrieve the name of the original source file name
5963/// directly from the AST file, without actually loading the AST
5964/// file.
5965std::string ASTReader::getOriginalSourceFile(
5966 const std::string &ASTFileName, FileManager &FileMgr,
5967 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5968 // Open the AST file.
5969 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5970 /*RequiresNullTerminator=*/false,
5971 /*MaybeLimit=*/std::nullopt,
5972 /*IsText=*/false);
5973 if (!Buffer) {
5974 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5975 << ASTFileName << Buffer.getError().message();
5976 return std::string();
5977 }
5978
5979 // Initialize the stream
5980 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5981
5982 // Sniff for the signature.
5983 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5984 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5985 return std::string();
5986 }
5987
5988 // Scan for the CONTROL_BLOCK_ID block.
5989 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5990 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5991 return std::string();
5992 }
5993
5994 // Scan for ORIGINAL_FILE inside the control block.
5995 RecordData Record;
5996 while (true) {
5997 Expected<llvm::BitstreamEntry> MaybeEntry =
5998 Stream.advanceSkippingSubblocks();
5999 if (!MaybeEntry) {
6000 // FIXME this drops errors on the floor.
6001 consumeError(Err: MaybeEntry.takeError());
6002 return std::string();
6003 }
6004 llvm::BitstreamEntry Entry = MaybeEntry.get();
6005
6006 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
6007 return std::string();
6008
6009 if (Entry.Kind != llvm::BitstreamEntry::Record) {
6010 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
6011 return std::string();
6012 }
6013
6014 Record.clear();
6015 StringRef Blob;
6016 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6017 if (!MaybeRecord) {
6018 // FIXME this drops the errors on the floor.
6019 consumeError(Err: MaybeRecord.takeError());
6020 return std::string();
6021 }
6022 if (ORIGINAL_FILE == MaybeRecord.get())
6023 return Blob.str();
6024 }
6025}
6026
6027namespace {
6028
6029 class SimplePCHValidator : public ASTReaderListener {
6030 const LangOptions &ExistingLangOpts;
6031 const CodeGenOptions &ExistingCGOpts;
6032 const TargetOptions &ExistingTargetOpts;
6033 const PreprocessorOptions &ExistingPPOpts;
6034 const HeaderSearchOptions &ExistingHSOpts;
6035 std::string ExistingSpecificModuleCachePath;
6036 FileManager &FileMgr;
6037 bool StrictOptionMatches;
6038
6039 public:
6040 SimplePCHValidator(const LangOptions &ExistingLangOpts,
6041 const CodeGenOptions &ExistingCGOpts,
6042 const TargetOptions &ExistingTargetOpts,
6043 const PreprocessorOptions &ExistingPPOpts,
6044 const HeaderSearchOptions &ExistingHSOpts,
6045 StringRef ExistingSpecificModuleCachePath,
6046 FileManager &FileMgr, bool StrictOptionMatches)
6047 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
6048 ExistingTargetOpts(ExistingTargetOpts),
6049 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
6050 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
6051 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
6052
6053 bool ReadLanguageOptions(const LangOptions &LangOpts,
6054 StringRef ModuleFilename, bool Complain,
6055 bool AllowCompatibleDifferences) override {
6056 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
6057 Diags: nullptr, AllowCompatibleDifferences);
6058 }
6059
6060 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
6061 StringRef ModuleFilename, bool Complain,
6062 bool AllowCompatibleDifferences) override {
6063 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
6064 Diags: nullptr, AllowCompatibleDifferences);
6065 }
6066
6067 bool ReadTargetOptions(const TargetOptions &TargetOpts,
6068 StringRef ModuleFilename, bool Complain,
6069 bool AllowCompatibleDifferences) override {
6070 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
6071 Diags: nullptr, AllowCompatibleDifferences);
6072 }
6073
6074 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
6075 StringRef ASTFilename, StringRef ContextHash,
6076 bool Complain) override {
6077 return checkModuleCachePath(
6078 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
6079 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
6080 }
6081
6082 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
6083 StringRef ModuleFilename, bool ReadMacros,
6084 bool Complain,
6085 std::string &SuggestedPredefines) override {
6086 return checkPreprocessorOptions(
6087 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
6088 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
6089 Validation: StrictOptionMatches ? OptionValidateStrictMatches
6090 : OptionValidateContradictions);
6091 }
6092 };
6093
6094} // namespace
6095
6096bool ASTReader::readASTFileControlBlock(
6097 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6098 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
6099 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
6100 unsigned ClientLoadCapabilities) {
6101 // Open the AST file.
6102 off_t Size;
6103 time_t ModTime;
6104 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
6105 llvm::MemoryBuffer *Buffer =
6106 ModCache.getInMemoryModuleCache().lookupPCM(Filename, Size, ModTime);
6107 if (!Buffer) {
6108 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
6109 // read again later, but we do not have the context here to determine if it
6110 // is safe to change the result of InMemoryModuleCache::getPCMState().
6111
6112 // FIXME: This allows use of the VFS; we do not allow use of the
6113 // VFS when actually loading a module.
6114 auto Entry = Filename == "-" ? FileMgr.getSTDIN()
6115 : FileMgr.getFileRef(Filename,
6116 /*OpenFile=*/false,
6117 /*CacheFailure=*/true,
6118 /*IsText=*/false);
6119 if (!Entry) {
6120 llvm::consumeError(Err: Entry.takeError());
6121 return true;
6122 }
6123 auto BufferOrErr =
6124 FileMgr.getBufferForFile(Entry: *Entry,
6125 /*IsVolatile=*/isVolatile: false,
6126 /*RequiresNullTerminator=*/false,
6127 /*MaybeLimit=*/std::nullopt,
6128 /*IsText=*/false);
6129 if (!BufferOrErr)
6130 return true;
6131 OwnedBuffer = std::move(*BufferOrErr);
6132 Buffer = OwnedBuffer.get();
6133 }
6134
6135 // Initialize the stream
6136 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
6137 BitstreamCursor Stream(Bytes);
6138
6139 // Sniff for the signature.
6140 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
6141 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
6142 return true;
6143 }
6144
6145 // Scan for the CONTROL_BLOCK_ID block.
6146 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
6147 return true;
6148
6149 bool NeedsInputFiles = Listener.needsInputFileVisitation();
6150 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
6151 bool NeedsImports = Listener.needsImportVisitation();
6152 BitstreamCursor InputFilesCursor;
6153 uint64_t InputFilesOffsetBase = 0;
6154
6155 RecordData Record;
6156 std::string ModuleDir;
6157 bool DoneWithControlBlock = false;
6158 SmallString<0> PathBuf;
6159 PathBuf.reserve(N: 256);
6160 // Additional path buffer to use when multiple paths need to be resolved.
6161 // For example, when deserializing input files that contains a path that was
6162 // resolved from a vfs overlay and an external location.
6163 SmallString<0> AdditionalPathBuf;
6164 AdditionalPathBuf.reserve(N: 256);
6165 while (!DoneWithControlBlock) {
6166 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6167 if (!MaybeEntry) {
6168 // FIXME this drops the error on the floor.
6169 consumeError(Err: MaybeEntry.takeError());
6170 return true;
6171 }
6172 llvm::BitstreamEntry Entry = MaybeEntry.get();
6173
6174 switch (Entry.Kind) {
6175 case llvm::BitstreamEntry::SubBlock: {
6176 switch (Entry.ID) {
6177 case OPTIONS_BLOCK_ID: {
6178 std::string IgnoredSuggestedPredefines;
6179 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6180 /*AllowCompatibleConfigurationMismatch*/ false,
6181 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6182 return true;
6183 break;
6184 }
6185
6186 case INPUT_FILES_BLOCK_ID:
6187 InputFilesCursor = Stream;
6188 if (llvm::Error Err = Stream.SkipBlock()) {
6189 // FIXME this drops the error on the floor.
6190 consumeError(Err: std::move(Err));
6191 return true;
6192 }
6193 if (NeedsInputFiles &&
6194 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6195 return true;
6196 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6197 break;
6198
6199 default:
6200 if (llvm::Error Err = Stream.SkipBlock()) {
6201 // FIXME this drops the error on the floor.
6202 consumeError(Err: std::move(Err));
6203 return true;
6204 }
6205 break;
6206 }
6207
6208 continue;
6209 }
6210
6211 case llvm::BitstreamEntry::EndBlock:
6212 DoneWithControlBlock = true;
6213 break;
6214
6215 case llvm::BitstreamEntry::Error:
6216 return true;
6217
6218 case llvm::BitstreamEntry::Record:
6219 break;
6220 }
6221
6222 if (DoneWithControlBlock) break;
6223
6224 Record.clear();
6225 StringRef Blob;
6226 Expected<unsigned> MaybeRecCode =
6227 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6228 if (!MaybeRecCode) {
6229 // FIXME this drops the error.
6230 return Failure;
6231 }
6232 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6233 case METADATA:
6234 if (Record[0] != VERSION_MAJOR)
6235 return true;
6236 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6237 return true;
6238 break;
6239 case MODULE_NAME:
6240 Listener.ReadModuleName(ModuleName: Blob);
6241 break;
6242 case MODULE_DIRECTORY:
6243 ModuleDir = std::string(Blob);
6244 break;
6245 case MODULE_MAP_FILE: {
6246 unsigned Idx = 0;
6247 std::string PathStr = ReadString(Record, Idx);
6248 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6249 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6250 break;
6251 }
6252 case INPUT_FILE_OFFSETS: {
6253 if (!NeedsInputFiles)
6254 break;
6255
6256 unsigned NumInputFiles = Record[0];
6257 unsigned NumUserFiles = Record[1];
6258 const llvm::support::unaligned_uint64_t *InputFileOffs =
6259 (const llvm::support::unaligned_uint64_t *)Blob.data();
6260 for (unsigned I = 0; I != NumInputFiles; ++I) {
6261 // Go find this input file.
6262 bool isSystemFile = I >= NumUserFiles;
6263
6264 if (isSystemFile && !NeedsSystemInputFiles)
6265 break; // the rest are system input files
6266
6267 BitstreamCursor &Cursor = InputFilesCursor;
6268 SavedStreamPosition SavedPosition(Cursor);
6269 if (llvm::Error Err =
6270 Cursor.JumpToBit(NewBitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6271 // FIXME this drops errors on the floor.
6272 consumeError(Err: std::move(Err));
6273 }
6274
6275 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6276 if (!MaybeCode) {
6277 // FIXME this drops errors on the floor.
6278 consumeError(Err: MaybeCode.takeError());
6279 }
6280 unsigned Code = MaybeCode.get();
6281
6282 RecordData Record;
6283 StringRef Blob;
6284 bool shouldContinue = false;
6285 Expected<unsigned> MaybeRecordType =
6286 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6287 if (!MaybeRecordType) {
6288 // FIXME this drops errors on the floor.
6289 consumeError(Err: MaybeRecordType.takeError());
6290 }
6291 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6292 case INPUT_FILE_HASH:
6293 break;
6294 case INPUT_FILE:
6295 time_t StoredTime = static_cast<time_t>(Record[2]);
6296 bool Overridden = static_cast<bool>(Record[3]);
6297 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6298 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6299 auto FilenameAsRequestedBuf = ResolveImportedPath(
6300 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6301 StringRef Filename;
6302 if (UnresolvedFilename.empty())
6303 Filename = *FilenameAsRequestedBuf;
6304 else {
6305 auto FilenameBuf = ResolveImportedPath(
6306 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6307 Filename = *FilenameBuf;
6308 }
6309 shouldContinue = Listener.visitInputFileAsRequested(
6310 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6311 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6312 break;
6313 }
6314 if (!shouldContinue)
6315 break;
6316 }
6317 break;
6318 }
6319
6320 case IMPORT: {
6321 if (!NeedsImports)
6322 break;
6323
6324 unsigned Idx = 0;
6325 // Read information about the AST file.
6326
6327 // Skip Kind
6328 Idx++;
6329
6330 // Skip ImportLoc
6331 Idx++;
6332
6333 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6334
6335 ++Idx; // Standard C++ module
6336 bool IsCXX20ModuleOrPrebuiltHeaderUnit = Record[Idx++];
6337
6338 // For C++20 Modules (including explicitly mapped header units),
6339 // we don't record the path to imported modules in the BMI files.
6340 if (IsCXX20ModuleOrPrebuiltHeaderUnit) {
6341 Listener.visitImport(ModuleName, /*Filename=*/"");
6342 continue;
6343 }
6344
6345 // Skip Size, ModTime and ImplicitModuleSuffix.
6346 Idx += 1 + 1 + 1;
6347 // Skip signature.
6348 Blob = Blob.substr(Start: ASTFileSignature::size);
6349
6350 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6351 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6352 Listener.visitImport(ModuleName, Filename: *Filename);
6353 break;
6354 }
6355
6356 default:
6357 // No other validation to perform.
6358 break;
6359 }
6360 }
6361
6362 // Look for module file extension blocks, if requested.
6363 if (FindModuleFileExtensions) {
6364 BitstreamCursor SavedStream = Stream;
6365 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6366 bool DoneWithExtensionBlock = false;
6367 while (!DoneWithExtensionBlock) {
6368 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6369 if (!MaybeEntry) {
6370 // FIXME this drops the error.
6371 return true;
6372 }
6373 llvm::BitstreamEntry Entry = MaybeEntry.get();
6374
6375 switch (Entry.Kind) {
6376 case llvm::BitstreamEntry::SubBlock:
6377 if (llvm::Error Err = Stream.SkipBlock()) {
6378 // FIXME this drops the error on the floor.
6379 consumeError(Err: std::move(Err));
6380 return true;
6381 }
6382 continue;
6383
6384 case llvm::BitstreamEntry::EndBlock:
6385 DoneWithExtensionBlock = true;
6386 continue;
6387
6388 case llvm::BitstreamEntry::Error:
6389 return true;
6390
6391 case llvm::BitstreamEntry::Record:
6392 break;
6393 }
6394
6395 Record.clear();
6396 StringRef Blob;
6397 Expected<unsigned> MaybeRecCode =
6398 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6399 if (!MaybeRecCode) {
6400 // FIXME this drops the error.
6401 return true;
6402 }
6403 switch (MaybeRecCode.get()) {
6404 case EXTENSION_METADATA: {
6405 ModuleFileExtensionMetadata Metadata;
6406 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6407 return true;
6408
6409 Listener.readModuleFileExtension(Metadata);
6410 break;
6411 }
6412 }
6413 }
6414 }
6415 Stream = std::move(SavedStream);
6416 }
6417
6418 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6419 if (readUnhashedControlBlockImpl(
6420 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6421 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6422 ValidateDiagnosticOptions) != Success)
6423 return true;
6424
6425 return false;
6426}
6427
6428bool ASTReader::isAcceptableASTFile(
6429 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6430 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6431 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6432 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6433 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6434 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6435 SpecificModuleCachePath, FileMgr,
6436 RequireStrictOptionMatches);
6437 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6438 /*FindModuleFileExtensions=*/false, Listener&: validator,
6439 /*ValidateDiagnosticOptions=*/true);
6440}
6441
6442Module *ASTReader::getSubmodule(uint32_t GlobalID) {
6443 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6444 assert(GlobalID == 0 && "Unhandled global submodule ID");
6445 return nullptr;
6446 }
6447
6448 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6449 if (GlobalIndex >= SubmodulesLoaded.size()) {
6450 Error(Msg: "submodule ID out of range in AST file");
6451 return nullptr;
6452 }
6453
6454 if (SubmodulesLoaded[GlobalIndex])
6455 return SubmodulesLoaded[GlobalIndex];
6456
6457 GlobalSubmoduleMapType::iterator It = GlobalSubmoduleMap.find(K: GlobalID);
6458 assert(It != GlobalSubmoduleMap.end());
6459 ModuleFile &F = *It->second;
6460 unsigned Index = GlobalID - F.BaseSubmoduleID - NUM_PREDEF_SUBMODULE_IDS;
6461 [[maybe_unused]] unsigned LocalID =
6462 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
6463
6464 BitstreamCursor &Cursor = F.SubmodulesCursor;
6465 SavedStreamPosition SavedPosition(Cursor);
6466 unsigned Offset = F.SubmoduleOffsets[Index];
6467 if (llvm::Error Err = Cursor.JumpToBit(NewBitNo: F.SubmodulesOffsetBase + Offset)) {
6468 Error(Err: std::move(Err));
6469 return nullptr;
6470 }
6471
6472 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6473 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6474 // If we don't know the top-level module, there's no point in doing qualified
6475 // lookup of its submodules; it won't find anything anywhere within this tree.
6476 // Let's skip that and avoid some string lookups.
6477 auto CreateModule = !KnowsTopLevelModule
6478 ? &ModuleMap::createModule
6479 : &ModuleMap::findOrCreateModuleFirst;
6480
6481 Module *CurrentModule = nullptr;
6482 RecordData Record;
6483 while (true) {
6484 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
6485 if (!MaybeEntry) {
6486 Error(Err: MaybeEntry.takeError());
6487 return nullptr;
6488 }
6489 llvm::BitstreamEntry Entry = MaybeEntry.get();
6490
6491 switch (Entry.Kind) {
6492 case llvm::BitstreamEntry::SubBlock:
6493 case llvm::BitstreamEntry::Error:
6494 case llvm::BitstreamEntry::EndBlock: {
6495 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6496 Fmt: "malformed block record in AST file"));
6497 return nullptr;
6498 }
6499 case llvm::BitstreamEntry::Record:
6500 // The interesting case.
6501 break;
6502 }
6503
6504 // Read a record.
6505 StringRef Blob;
6506 Record.clear();
6507 Expected<unsigned> MaybeKind = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6508 if (!MaybeKind) {
6509 Error(Err: MaybeKind.takeError());
6510 return nullptr;
6511 }
6512 auto Kind = static_cast<SubmoduleRecordTypes>(MaybeKind.get());
6513
6514 switch (Kind) {
6515 case SUBMODULE_END:
6516 if (!CurrentModule) {
6517 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6518 Fmt: "malformed module definition"));
6519 return nullptr;
6520 }
6521 return CurrentModule;
6522
6523 case SUBMODULE_DEFINITION: {
6524 if (Record.size() < 13) {
6525 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6526 Fmt: "malformed module definition"));
6527 return nullptr;
6528 }
6529
6530 StringRef Name = Blob;
6531 unsigned Idx = 0;
6532 [[maybe_unused]] unsigned ReadLocalID = Record[Idx++];
6533 assert(LocalID == ReadLocalID);
6534 assert(GlobalID == getGlobalSubmoduleID(F, ReadLocalID));
6535 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6536 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6537 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6538 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6539 bool IsFramework = Record[Idx++];
6540 bool IsExplicit = Record[Idx++];
6541 bool IsSystem = Record[Idx++];
6542 bool IsExternC = Record[Idx++];
6543 bool InferSubmodules = Record[Idx++];
6544 bool InferExplicitSubmodules = Record[Idx++];
6545 bool InferExportWildcard = Record[Idx++];
6546 bool ConfigMacrosExhaustive = Record[Idx++];
6547 bool ModuleMapIsPrivate = Record[Idx++];
6548 bool NamedModuleHasInit = Record[Idx++];
6549
6550 Module *ParentModule = nullptr;
6551 if (Parent) {
6552 ParentModule = getSubmodule(GlobalID: Parent);
6553 if (!ParentModule)
6554 return nullptr;
6555 }
6556
6557 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6558 args&: IsFramework, args&: IsExplicit);
6559
6560 if (!ParentModule) {
6561 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6562 CurrentModule->getASTFileKey()) {
6563 // Don't emit module relocation error if we have -fno-validate-pch
6564 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6565 DisableValidationForModuleKind::Module)) {
6566 assert(*CurFileKey != F.FileKey &&
6567 "ModuleManager did not de-duplicate");
6568
6569 Diag(DiagID: diag::err_module_file_conflict)
6570 << CurrentModule->getTopLevelModuleName()
6571 << *CurrentModule->getASTFileName() << F.FileName;
6572
6573 auto CurModMapFile =
6574 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6575 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6576 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6577 Diag(DiagID: diag::note_module_file_conflict)
6578 << CurModMapFile->getName() << ModMapFile->getName();
6579
6580 return nullptr;
6581 }
6582 }
6583
6584 F.DidReadTopLevelSubmodule = true;
6585 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6586 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6587 }
6588
6589 CurrentModule->Kind = Kind;
6590 // Note that we may be rewriting an existing location and it is important
6591 // to keep doing that. In particular, we would like to prefer a
6592 // `DefinitionLoc` loaded from the module file instead of the location
6593 // created in the current source manager, because it allows the new
6594 // location to be marked as "unaffecting" when writing and avoid creating
6595 // duplicate locations for the same module map file.
6596 CurrentModule->DefinitionLoc = DefinitionLoc;
6597 CurrentModule->Signature = F.Signature;
6598 CurrentModule->IsFromModuleFile = true;
6599 if (InferredAllowedBy.isValid())
6600 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6601 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6602 CurrentModule->IsExternC = IsExternC;
6603 CurrentModule->InferSubmodules = InferSubmodules;
6604 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6605 CurrentModule->InferExportWildcard = InferExportWildcard;
6606 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6607 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6608 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6609
6610 if (!ParentModule && !F.BaseDirectory.empty()) {
6611 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6612 CurrentModule->Directory = *Dir;
6613 } else if (ParentModule && ParentModule->Directory) {
6614 // Submodules inherit the directory from their parent.
6615 CurrentModule->Directory = ParentModule->Directory;
6616 }
6617
6618 if (DeserializationListener)
6619 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6620
6621 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6622
6623 // Clear out data that will be replaced by what is in the module file.
6624 CurrentModule->LinkLibraries.clear();
6625 CurrentModule->ConfigMacros.clear();
6626 CurrentModule->UnresolvedConflicts.clear();
6627 CurrentModule->Conflicts.clear();
6628
6629 // The module is available unless it's missing a requirement; relevant
6630 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6631 // Missing headers that were present when the module was built do not
6632 // make it unavailable -- if we got this far, this must be an explicitly
6633 // imported module file.
6634 CurrentModule->Requirements.clear();
6635 CurrentModule->MissingHeaders.clear();
6636 CurrentModule->IsUnimportable =
6637 ParentModule && ParentModule->IsUnimportable;
6638 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6639 break;
6640 }
6641
6642 case SUBMODULE_UMBRELLA_HEADER: {
6643 SmallString<128> RelativePathName;
6644 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6645 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6646 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6647 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6648 PathRelativeToRootModuleDirectory: RelativePathName);
6649 }
6650 // Note that it's too late at this point to return out of date if the
6651 // name from the PCM doesn't match up with the one in the module map,
6652 // but also quite unlikely since we will have already checked the
6653 // modification time and size of the module map file itself.
6654 }
6655 break;
6656 }
6657
6658 case SUBMODULE_HEADER:
6659 case SUBMODULE_EXCLUDED_HEADER:
6660 case SUBMODULE_PRIVATE_HEADER:
6661 // We lazily associate headers with their modules via the HeaderInfo table.
6662 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6663 // of complete filenames or remove it entirely.
6664 break;
6665
6666 case SUBMODULE_TEXTUAL_HEADER:
6667 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6668 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6669 // them here.
6670 break;
6671
6672 case SUBMODULE_TOPHEADER: {
6673 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6674 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6675 break;
6676 }
6677
6678 case SUBMODULE_UMBRELLA_DIR: {
6679 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6680 if (auto Umbrella =
6681 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6682 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6683 // FIXME: NameAsWritten
6684 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6685 }
6686 }
6687 break;
6688 }
6689
6690 case SUBMODULE_IMPORTS:
6691 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6692 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6693 CurrentModule->Imports.push_back(Elt: ModuleRef(this, GlobalID));
6694 }
6695 break;
6696
6697 case SUBMODULE_AFFECTING_MODULES:
6698 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6699 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6700 CurrentModule->AffectingClangModules.push_back(
6701 Elt: ModuleRef(this, GlobalID));
6702 }
6703 break;
6704
6705 case SUBMODULE_EXPORTS:
6706 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6707 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6708 bool IsWildcard = Record[Idx + 1];
6709 ModuleRef ExportedMod =
6710 GlobalID ? ModuleRef(this, GlobalID) : ModuleRef();
6711 if (ExportedMod || IsWildcard)
6712 CurrentModule->Exports.push_back(Elt: {ExportedMod, IsWildcard});
6713 }
6714
6715 // Once we've loaded the set of exports, there's no reason to keep
6716 // the parsed, unresolved exports around.
6717 CurrentModule->UnresolvedExports.clear();
6718 break;
6719
6720 case SUBMODULE_REQUIRES:
6721 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6722 Target: PP.getTargetInfo());
6723 break;
6724
6725 case SUBMODULE_LINK_LIBRARY:
6726 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6727 CurrentModule->LinkLibraries.push_back(
6728 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6729 break;
6730
6731 case SUBMODULE_CONFIG_MACRO:
6732 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6733 break;
6734
6735 case SUBMODULE_CONFLICT: {
6736 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6737 Module::Conflict Conflict;
6738 Conflict.Other = ModuleRef(this, GlobalID);
6739 Conflict.Message = Blob.str();
6740 CurrentModule->Conflicts.push_back(x: Conflict);
6741 break;
6742 }
6743
6744 case SUBMODULE_INITIALIZERS: {
6745 if (!ContextObj)
6746 break;
6747 // Standard C++ module has its own way to initialize variables.
6748 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6749 SmallVector<GlobalDeclID, 16> Inits;
6750 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6751 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6752 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6753 }
6754 break;
6755 }
6756
6757 case SUBMODULE_EXPORT_AS:
6758 CurrentModule->ExportAsModule = Blob.str();
6759 ModMap.addLinkAsDependency(Mod: CurrentModule);
6760 break;
6761
6762 case SUBMODULE_CHILD: {
6763 // Record a not-yet-loaded direct child for on-demand deserialization.
6764 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6765 CurrentModule->addSubmodule(Name: Blob, ExternalSource: this, SubmoduleID: GlobalID);
6766 break;
6767 }
6768 }
6769 }
6770}
6771
6772/// Parse the record that corresponds to a LangOptions data
6773/// structure.
6774///
6775/// This routine parses the language options from the AST file and then gives
6776/// them to the AST listener if one is set.
6777///
6778/// \returns true if the listener deems the file unacceptable, false otherwise.
6779bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6780 StringRef ModuleFilename, bool Complain,
6781 ASTReaderListener &Listener,
6782 bool AllowCompatibleDifferences) {
6783 LangOptions LangOpts;
6784 unsigned Idx = 0;
6785 LangOpts.LangStd = static_cast<LangStandard::Kind>(Record[Idx++]);
6786#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6787 LangOpts.Name = Record[Idx++];
6788#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6789 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6790#include "clang/Basic/LangOptions.def"
6791#define SANITIZER(NAME, ID) \
6792 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6793#include "clang/Basic/Sanitizers.def"
6794
6795 for (unsigned N = Record[Idx++]; N; --N)
6796 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6797
6798 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6799 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6800 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6801
6802 LangOpts.CurrentModule = ReadString(Record, Idx);
6803
6804 // Comment options.
6805 for (unsigned N = Record[Idx++]; N; --N) {
6806 LangOpts.CommentOpts.BlockCommandNames.push_back(
6807 x: ReadString(Record, Idx));
6808 }
6809 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6810
6811 // OpenMP offloading options.
6812 for (unsigned N = Record[Idx++]; N; --N) {
6813 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6814 }
6815
6816 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6817
6818 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6819 AllowCompatibleDifferences);
6820}
6821
6822bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6823 StringRef ModuleFilename, bool Complain,
6824 ASTReaderListener &Listener,
6825 bool AllowCompatibleDifferences) {
6826 unsigned Idx = 0;
6827 CodeGenOptions CGOpts;
6828 using CK = CodeGenOptions::CompatibilityKind;
6829#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6830 if constexpr (CK::Compatibility != CK::Benign) \
6831 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6832#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6833 if constexpr (CK::Compatibility != CK::Benign) \
6834 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6835#define DEBUGOPT(Name, Bits, Default, Compatibility)
6836#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6837#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6838#include "clang/Basic/CodeGenOptions.def"
6839
6840 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6841 AllowCompatibleDifferences);
6842}
6843
6844bool ASTReader::ParseTargetOptions(const RecordData &Record,
6845 StringRef ModuleFilename, bool Complain,
6846 ASTReaderListener &Listener,
6847 bool AllowCompatibleDifferences) {
6848 unsigned Idx = 0;
6849 TargetOptions TargetOpts;
6850 TargetOpts.Triple = ReadString(Record, Idx);
6851 TargetOpts.CPU = ReadString(Record, Idx);
6852 TargetOpts.TuneCPU = ReadString(Record, Idx);
6853 TargetOpts.ABI = ReadString(Record, Idx);
6854 for (unsigned N = Record[Idx++]; N; --N) {
6855 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6856 }
6857 for (unsigned N = Record[Idx++]; N; --N) {
6858 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6859 }
6860
6861 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6862 AllowCompatibleDifferences);
6863}
6864
6865bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6866 StringRef ModuleFilename, bool Complain,
6867 ASTReaderListener &Listener) {
6868 DiagnosticOptions DiagOpts;
6869 unsigned Idx = 0;
6870#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6871#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6872 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6873#include "clang/Basic/DiagnosticOptions.def"
6874
6875 for (unsigned N = Record[Idx++]; N; --N)
6876 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6877 for (unsigned N = Record[Idx++]; N; --N)
6878 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6879
6880 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6881}
6882
6883bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6884 ASTReaderListener &Listener) {
6885 FileSystemOptions FSOpts;
6886 unsigned Idx = 0;
6887 FSOpts.WorkingDir = ReadString(Record, Idx);
6888 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6889}
6890
6891bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6892 StringRef ModuleFilename,
6893 bool Complain,
6894 ASTReaderListener &Listener) {
6895 HeaderSearchOptions HSOpts;
6896 unsigned Idx = 0;
6897 HSOpts.Sysroot = ReadString(Record, Idx);
6898
6899 HSOpts.ResourceDir = ReadString(Record, Idx);
6900 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6901 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6902 HSOpts.DisableModuleHash = Record[Idx++];
6903 HSOpts.ImplicitModuleMaps = Record[Idx++];
6904 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6905 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6906 HSOpts.UseBuiltinIncludes = Record[Idx++];
6907 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6908 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6909 HSOpts.UseLibcxx = Record[Idx++];
6910 std::string ContextHash = ReadString(Record, Idx);
6911
6912 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6913 Complain);
6914}
6915
6916bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6917 ASTReaderListener &Listener) {
6918 HeaderSearchOptions HSOpts;
6919 unsigned Idx = 0;
6920
6921 // Include entries.
6922 for (unsigned N = Record[Idx++]; N; --N) {
6923 std::string Path = ReadString(Record, Idx);
6924 frontend::IncludeDirGroup Group
6925 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6926 bool IsFramework = Record[Idx++];
6927 bool IgnoreSysRoot = Record[Idx++];
6928 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6929 args&: IgnoreSysRoot);
6930 }
6931
6932 // System header prefixes.
6933 for (unsigned N = Record[Idx++]; N; --N) {
6934 std::string Prefix = ReadString(Record, Idx);
6935 bool IsSystemHeader = Record[Idx++];
6936 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6937 }
6938
6939 // VFS overlay files.
6940 for (unsigned N = Record[Idx++]; N; --N) {
6941 std::string VFSOverlayFile = ReadString(Record, Idx);
6942 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6943 }
6944
6945 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6946}
6947
6948bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6949 StringRef ModuleFilename,
6950 bool Complain,
6951 ASTReaderListener &Listener,
6952 std::string &SuggestedPredefines) {
6953 PreprocessorOptions PPOpts;
6954 unsigned Idx = 0;
6955
6956 // Macro definitions/undefs
6957 bool ReadMacros = Record[Idx++];
6958 if (ReadMacros) {
6959 for (unsigned N = Record[Idx++]; N; --N) {
6960 std::string Macro = ReadString(Record, Idx);
6961 bool IsUndef = Record[Idx++];
6962 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6963 }
6964 }
6965
6966 // Includes
6967 for (unsigned N = Record[Idx++]; N; --N) {
6968 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6969 }
6970
6971 // Macro Includes
6972 for (unsigned N = Record[Idx++]; N; --N) {
6973 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6974 }
6975
6976 PPOpts.UsePredefines = Record[Idx++];
6977 PPOpts.DetailedRecord = Record[Idx++];
6978 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6979 PPOpts.ObjCXXARCStandardLibrary =
6980 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6981 SuggestedPredefines.clear();
6982 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6983 Complain, SuggestedPredefines);
6984}
6985
6986std::pair<ModuleFile *, unsigned>
6987ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6988 GlobalPreprocessedEntityMapType::iterator
6989 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6990 assert(I != GlobalPreprocessedEntityMap.end() &&
6991 "Corrupted global preprocessed entity map");
6992 ModuleFile *M = I->second;
6993 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6994 return std::make_pair(x&: M, y&: LocalIndex);
6995}
6996
6997llvm::iterator_range<PreprocessingRecord::iterator>
6998ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6999 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
7000 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
7001 count: Mod.NumPreprocessedEntities);
7002
7003 return llvm::make_range(x: PreprocessingRecord::iterator(),
7004 y: PreprocessingRecord::iterator());
7005}
7006
7007bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
7008 unsigned int ClientLoadCapabilities) {
7009 return ClientLoadCapabilities & ARR_OutOfDate &&
7010 !getModuleManager()
7011 .getModuleCache()
7012 .getInMemoryModuleCache()
7013 .isPCMFinal(Filename: ModuleFileName);
7014}
7015
7016llvm::iterator_range<ASTReader::ModuleDeclIterator>
7017ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
7018 return llvm::make_range(
7019 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
7020 y: ModuleDeclIterator(this, &Mod,
7021 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
7022}
7023
7024SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
7025 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
7026 assert(I != GlobalSkippedRangeMap.end() &&
7027 "Corrupted global skipped range map");
7028 ModuleFile *M = I->second;
7029 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
7030 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
7031 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
7032 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
7033 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
7034 assert(Range.isValid());
7035 return Range;
7036}
7037
7038unsigned
7039ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
7040 unsigned ModuleFileIndex = ID >> 32;
7041 assert(ModuleFileIndex && "not translating loaded MacroID?");
7042 assert(getModuleManager().size() > ModuleFileIndex - 1);
7043 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
7044
7045 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
7046 return MF.BasePreprocessedEntityID + ID;
7047}
7048
7049PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
7050 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7051 ModuleFile &M = *PPInfo.first;
7052 unsigned LocalIndex = PPInfo.second;
7053 PreprocessedEntityID PPID =
7054 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
7055 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7056
7057 if (!PP.getPreprocessingRecord()) {
7058 Error(Msg: "no preprocessing record");
7059 return nullptr;
7060 }
7061
7062 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
7063 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
7064 NewBitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
7065 Error(Err: std::move(Err));
7066 return nullptr;
7067 }
7068
7069 Expected<llvm::BitstreamEntry> MaybeEntry =
7070 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
7071 if (!MaybeEntry) {
7072 Error(Err: MaybeEntry.takeError());
7073 return nullptr;
7074 }
7075 llvm::BitstreamEntry Entry = MaybeEntry.get();
7076
7077 if (Entry.Kind != llvm::BitstreamEntry::Record)
7078 return nullptr;
7079
7080 // Read the record.
7081 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
7082 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
7083 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
7084 StringRef Blob;
7085 RecordData Record;
7086 Expected<unsigned> MaybeRecType =
7087 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
7088 if (!MaybeRecType) {
7089 Error(Err: MaybeRecType.takeError());
7090 return nullptr;
7091 }
7092 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
7093 case PPD_MACRO_EXPANSION: {
7094 bool isBuiltin = Record[0];
7095 IdentifierInfo *Name = nullptr;
7096 MacroDefinitionRecord *Def = nullptr;
7097 if (isBuiltin)
7098 Name = getLocalIdentifier(M, LocalID: Record[1]);
7099 else {
7100 PreprocessedEntityID GlobalID =
7101 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
7102 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
7103 Def =
7104 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
7105 }
7106
7107 MacroExpansion *ME;
7108 if (isBuiltin)
7109 ME = new (PPRec) MacroExpansion(Name, Range);
7110 else
7111 ME = new (PPRec) MacroExpansion(Def, Range);
7112
7113 return ME;
7114 }
7115
7116 case PPD_MACRO_DEFINITION: {
7117 // Decode the identifier info and then check again; if the macro is
7118 // still defined and associated with the identifier,
7119 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
7120 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
7121
7122 if (DeserializationListener)
7123 DeserializationListener->MacroDefinitionRead(PPID, MD);
7124
7125 return MD;
7126 }
7127
7128 case PPD_INCLUSION_DIRECTIVE: {
7129 const char *FullFileNameStart = Blob.data() + Record[0];
7130 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
7131 OptionalFileEntryRef File;
7132 if (!FullFileName.empty())
7133 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
7134
7135 // FIXME: Stable encoding
7136 InclusionDirective::InclusionKind Kind
7137 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
7138 InclusionDirective *ID
7139 = new (PPRec) InclusionDirective(PPRec, Kind,
7140 StringRef(Blob.data(), Record[0]),
7141 Record[1], Record[3],
7142 File,
7143 Range);
7144 return ID;
7145 }
7146 }
7147
7148 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
7149}
7150
7151/// Find the next module that contains entities and return the ID
7152/// of the first entry.
7153///
7154/// \param SLocMapI points at a chunk of a module that contains no
7155/// preprocessed entities or the entities it contains are not the ones we are
7156/// looking for.
7157unsigned ASTReader::findNextPreprocessedEntity(
7158 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
7159 ++SLocMapI;
7160 for (GlobalSLocOffsetMapType::const_iterator
7161 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
7162 ModuleFile &M = *SLocMapI->second;
7163 if (M.NumPreprocessedEntities)
7164 return M.BasePreprocessedEntityID;
7165 }
7166
7167 return getTotalNumPreprocessedEntities();
7168}
7169
7170namespace {
7171
7172struct PPEntityComp {
7173 const ASTReader &Reader;
7174 ModuleFile &M;
7175
7176 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7177
7178 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7179 SourceLocation LHS = getLoc(PPE: L);
7180 SourceLocation RHS = getLoc(PPE: R);
7181 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7182 }
7183
7184 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7185 SourceLocation LHS = getLoc(PPE: L);
7186 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7187 }
7188
7189 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7190 SourceLocation RHS = getLoc(PPE: R);
7191 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7192 }
7193
7194 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7195 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7196 }
7197};
7198
7199} // namespace
7200
7201unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7202 bool EndsAfter) const {
7203 if (SourceMgr.isLocalSourceLocation(Loc))
7204 return getTotalNumPreprocessedEntities();
7205
7206 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7207 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7208 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7209 "Corrupted global sloc offset map");
7210
7211 if (SLocMapI->second->NumPreprocessedEntities == 0)
7212 return findNextPreprocessedEntity(SLocMapI);
7213
7214 ModuleFile &M = *SLocMapI->second;
7215
7216 using pp_iterator = const PPEntityOffset *;
7217
7218 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7219 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7220
7221 size_t Count = M.NumPreprocessedEntities;
7222 size_t Half;
7223 pp_iterator First = pp_begin;
7224 pp_iterator PPI;
7225
7226 if (EndsAfter) {
7227 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7228 comp: PPEntityComp(*this, M));
7229 } else {
7230 // Do a binary search manually instead of using std::lower_bound because
7231 // The end locations of entities may be unordered (when a macro expansion
7232 // is inside another macro argument), but for this case it is not important
7233 // whether we get the first macro expansion or its containing macro.
7234 while (Count > 0) {
7235 Half = Count / 2;
7236 PPI = First;
7237 std::advance(i&: PPI, n: Half);
7238 if (SourceMgr.isBeforeInTranslationUnit(
7239 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7240 First = PPI;
7241 ++First;
7242 Count = Count - Half - 1;
7243 } else
7244 Count = Half;
7245 }
7246 }
7247
7248 if (PPI == pp_end)
7249 return findNextPreprocessedEntity(SLocMapI);
7250
7251 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7252}
7253
7254/// Returns a pair of [Begin, End) indices of preallocated
7255/// preprocessed entities that \arg Range encompasses.
7256std::pair<unsigned, unsigned>
7257 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7258 if (Range.isInvalid())
7259 return std::make_pair(x: 0,y: 0);
7260 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7261
7262 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7263 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7264 return std::make_pair(x&: BeginID, y&: EndID);
7265}
7266
7267/// Optionally returns true or false if the preallocated preprocessed
7268/// entity with index \arg Index came from file \arg FID.
7269std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7270 FileID FID) {
7271 if (FID.isInvalid())
7272 return false;
7273
7274 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7275 ModuleFile &M = *PPInfo.first;
7276 unsigned LocalIndex = PPInfo.second;
7277 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7278
7279 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7280 if (Loc.isInvalid())
7281 return false;
7282
7283 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7284 return true;
7285 else
7286 return false;
7287}
7288
7289namespace {
7290
7291 /// Visitor used to search for information about a header file.
7292 class HeaderFileInfoVisitor {
7293 FileEntryRef FE;
7294 std::optional<HeaderFileInfo> HFI;
7295
7296 public:
7297 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7298
7299 bool operator()(ModuleFile &M) {
7300 HeaderFileInfoLookupTable *Table
7301 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7302 if (!Table)
7303 return false;
7304
7305 // Look in the on-disk hash table for an entry for this file name.
7306 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7307 if (Pos == Table->end())
7308 return false;
7309
7310 HFI = *Pos;
7311 return true;
7312 }
7313
7314 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7315 };
7316
7317} // namespace
7318
7319HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7320 HeaderFileInfoVisitor Visitor(FE);
7321 ModuleMgr.visit(Visitor);
7322 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7323 return *HFI;
7324
7325 return HeaderFileInfo();
7326}
7327
7328void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7329 using DiagState = DiagnosticsEngine::DiagState;
7330 SmallVector<DiagState *, 32> DiagStates;
7331
7332 for (ModuleFile &F : ModuleMgr) {
7333 unsigned Idx = 0;
7334 auto &Record = F.PragmaDiagMappings;
7335 if (Record.empty())
7336 continue;
7337
7338 DiagStates.clear();
7339
7340 auto ReadDiagState = [&](const DiagState &BasedOn,
7341 bool IncludeNonPragmaStates) {
7342 unsigned BackrefID = Record[Idx++];
7343 if (BackrefID != 0)
7344 return DiagStates[BackrefID - 1];
7345
7346 // A new DiagState was created here.
7347 Diag.DiagStates.push_back(x: BasedOn);
7348 DiagState *NewState = &Diag.DiagStates.back();
7349 DiagStates.push_back(Elt: NewState);
7350 unsigned Size = Record[Idx++];
7351 assert(Idx + Size * 2 <= Record.size() &&
7352 "Invalid data, not enough diag/map pairs");
7353 while (Size--) {
7354 unsigned DiagID = Record[Idx++];
7355 DiagnosticMapping NewMapping =
7356 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7357 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7358 continue;
7359
7360 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7361
7362 // If this mapping was specified as a warning but the severity was
7363 // upgraded due to diagnostic settings, simulate the current diagnostic
7364 // settings (and use a warning).
7365 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7366 NewMapping.setSeverity(diag::Severity::Warning);
7367 NewMapping.setUpgradedFromWarning(false);
7368 }
7369
7370 Mapping = NewMapping;
7371 }
7372 return NewState;
7373 };
7374
7375 // Read the first state.
7376 DiagState *FirstState;
7377 if (F.Kind == MK_ImplicitModule) {
7378 // Implicitly-built modules are reused with different diagnostic
7379 // settings. Use the initial diagnostic state from Diag to simulate this
7380 // compilation's diagnostic settings.
7381 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7382 DiagStates.push_back(Elt: FirstState);
7383
7384 // Skip the initial diagnostic state from the serialized module.
7385 assert(Record[1] == 0 &&
7386 "Invalid data, unexpected backref in initial state");
7387 Idx = 3 + Record[2] * 2;
7388 assert(Idx < Record.size() &&
7389 "Invalid data, not enough state change pairs in initial state");
7390 } else if (F.isModule()) {
7391 // For an explicit module, preserve the flags from the module build
7392 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7393 // -Wblah flags.
7394 unsigned Flags = Record[Idx++];
7395 DiagState Initial(*Diag.getDiagnosticIDs());
7396 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7397 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7398 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7399 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7400 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7401 Initial.ExtBehavior = (diag::Severity)Flags;
7402 FirstState = ReadDiagState(Initial, true);
7403
7404 assert(F.OriginalSourceFileID.isValid());
7405
7406 // Set up the root buffer of the module to start with the initial
7407 // diagnostic state of the module itself, to cover files that contain no
7408 // explicit transitions (for which we did not serialize anything).
7409 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7410 .StateTransitions.push_back(Elt: {FirstState, 0});
7411 } else {
7412 // For prefix ASTs, start with whatever the user configured on the
7413 // command line.
7414 Idx++; // Skip flags.
7415 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7416 }
7417
7418 // Read the state transitions.
7419 unsigned NumLocations = Record[Idx++];
7420 while (NumLocations--) {
7421 assert(Idx < Record.size() &&
7422 "Invalid data, missing pragma diagnostic states");
7423 FileID FID = ReadFileID(F, Record, Idx);
7424 assert(FID.isValid() && "invalid FileID for transition");
7425 unsigned Transitions = Record[Idx++];
7426
7427 // Note that we don't need to set up Parent/ParentOffset here, because
7428 // we won't be changing the diagnostic state within imported FileIDs
7429 // (other than perhaps appending to the main source file, which has no
7430 // parent).
7431 auto &F = Diag.DiagStatesByLoc.Files[FID];
7432 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7433 for (unsigned I = 0; I != Transitions; ++I) {
7434 unsigned Offset = Record[Idx++];
7435 auto *State = ReadDiagState(*FirstState, false);
7436 F.StateTransitions.push_back(Elt: {State, Offset});
7437 }
7438 }
7439
7440 // Read the final state.
7441 assert(Idx < Record.size() &&
7442 "Invalid data, missing final pragma diagnostic state");
7443 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7444 auto *CurState = ReadDiagState(*FirstState, false);
7445
7446 if (!F.isModule()) {
7447 Diag.DiagStatesByLoc.CurDiagState = CurState;
7448 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7449
7450 // Preserve the property that the imaginary root file describes the
7451 // current state.
7452 FileID NullFile;
7453 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7454 if (T.empty())
7455 T.push_back(Elt: {CurState, 0});
7456 else
7457 T[0].State = CurState;
7458 }
7459
7460 // Restore the push stack so that unmatched pushes from a preamble are
7461 // visible when the main file is parsed, allowing the corresponding
7462 // `#pragma diagnostic pop` to succeed.
7463 assert(Idx < Record.size() &&
7464 "Invalid data, missing diagnostic push stack");
7465 unsigned NumPushes = Record[Idx++];
7466 for (unsigned I = 0; I != NumPushes; ++I) {
7467 auto *State = ReadDiagState(*FirstState, false);
7468 if (!F.isModule())
7469 Diag.DiagStateOnPushStack.push_back(x: State);
7470 }
7471
7472 // Don't try to read these mappings again.
7473 Record.clear();
7474 }
7475}
7476
7477/// Get the correct cursor and offset for loading a type.
7478ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7479 auto [M, Index] = translateTypeIDToIndex(ID);
7480 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7481 M->DeclsBlockStartOffset);
7482}
7483
7484static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7485 switch (code) {
7486#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7487 case TYPE_##CODE_ID: return Type::CLASS_ID;
7488#include "clang/Serialization/TypeBitCodes.def"
7489 default:
7490 return std::nullopt;
7491 }
7492}
7493
7494/// Read and return the type with the given index..
7495///
7496/// The index is the type ID, shifted and minus the number of predefs. This
7497/// routine actually reads the record corresponding to the type at the given
7498/// location. It is a helper routine for GetType, which deals with reading type
7499/// IDs.
7500QualType ASTReader::readTypeRecord(TypeID ID) {
7501 assert(ContextObj && "reading type with no AST context");
7502 ASTContext &Context = *ContextObj;
7503 RecordLocation Loc = TypeCursorForIndex(ID);
7504 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7505
7506 // Keep track of where we are in the stream, then jump back there
7507 // after reading this type.
7508 SavedStreamPosition SavedPosition(DeclsCursor);
7509
7510 ReadingKindTracker ReadingKind(Read_Type, *this);
7511
7512 // Note that we are loading a type record.
7513 Deserializing AType(this);
7514
7515 if (llvm::Error Err = DeclsCursor.JumpToBit(NewBitNo: Loc.Offset)) {
7516 Error(Err: std::move(Err));
7517 return QualType();
7518 }
7519 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7520 if (!RawCode) {
7521 Error(Err: RawCode.takeError());
7522 return QualType();
7523 }
7524
7525 ASTRecordReader Record(*this, *Loc.F);
7526 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7527 if (!Code) {
7528 Error(Err: Code.takeError());
7529 return QualType();
7530 }
7531 if (Code.get() == TYPE_EXT_QUAL) {
7532 QualType baseType = Record.readQualType();
7533 Qualifiers quals = Record.readQualifiers();
7534 return Context.getQualifiedType(T: baseType, Qs: quals);
7535 }
7536
7537 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7538 if (!maybeClass) {
7539 Error(Msg: "Unexpected code for type");
7540 return QualType();
7541 }
7542
7543 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7544 return TypeReader.read(kind: *maybeClass);
7545}
7546
7547namespace clang {
7548
7549class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7550 ASTRecordReader &Reader;
7551
7552 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7553 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7554
7555 TypeSourceInfo *GetTypeSourceInfo() {
7556 return Reader.readTypeSourceInfo();
7557 }
7558
7559 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7560 return Reader.readNestedNameSpecifierLoc();
7561 }
7562
7563 Attr *ReadAttr() {
7564 return Reader.readAttr();
7565 }
7566
7567public:
7568 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7569
7570 // We want compile-time assurance that we've enumerated all of
7571 // these, so unfortunately we have to declare them first, then
7572 // define them out-of-line.
7573#define ABSTRACT_TYPELOC(CLASS, PARENT)
7574#define TYPELOC(CLASS, PARENT) \
7575 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7576#include "clang/AST/TypeLocNodes.def"
7577
7578 void VisitFunctionTypeLoc(FunctionTypeLoc);
7579 void VisitArrayTypeLoc(ArrayTypeLoc);
7580 void VisitTagTypeLoc(TagTypeLoc TL);
7581};
7582
7583} // namespace clang
7584
7585void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7586 // nothing to do
7587}
7588
7589void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7590 TL.setBuiltinLoc(readSourceLocation());
7591 if (TL.needsExtraLocalData()) {
7592 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7593 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7594 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7595 TL.setModeAttr(Reader.readInt());
7596 }
7597}
7598
7599void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7600 TL.setNameLoc(readSourceLocation());
7601}
7602
7603void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7604 TL.setStarLoc(readSourceLocation());
7605}
7606
7607void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7608 // nothing to do
7609}
7610
7611void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7612 // nothing to do
7613}
7614
7615void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7616 // nothing to do
7617}
7618
7619void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7620 TL.setExpansionLoc(readSourceLocation());
7621}
7622
7623void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7624 TL.setCaretLoc(readSourceLocation());
7625}
7626
7627void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7628 TL.setAmpLoc(readSourceLocation());
7629}
7630
7631void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7632 TL.setAmpAmpLoc(readSourceLocation());
7633}
7634
7635void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7636 TL.setStarLoc(readSourceLocation());
7637 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7638}
7639
7640void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7641 TL.setLBracketLoc(readSourceLocation());
7642 TL.setRBracketLoc(readSourceLocation());
7643 if (Reader.readBool())
7644 TL.setSizeExpr(Reader.readExpr());
7645 else
7646 TL.setSizeExpr(nullptr);
7647}
7648
7649void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7650 VisitArrayTypeLoc(TL);
7651}
7652
7653void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7654 VisitArrayTypeLoc(TL);
7655}
7656
7657void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7658 VisitArrayTypeLoc(TL);
7659}
7660
7661void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7662 DependentSizedArrayTypeLoc TL) {
7663 VisitArrayTypeLoc(TL);
7664}
7665
7666void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7667 DependentAddressSpaceTypeLoc TL) {
7668
7669 TL.setAttrNameLoc(readSourceLocation());
7670 TL.setAttrOperandParensRange(readSourceRange());
7671 TL.setAttrExprOperand(Reader.readExpr());
7672}
7673
7674void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7675 DependentSizedExtVectorTypeLoc TL) {
7676 TL.setNameLoc(readSourceLocation());
7677 TL.setAttrNameLoc(readSourceLocation());
7678 TL.setSizeExpr(Reader.readExpr());
7679}
7680
7681void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7682 TL.setNameLoc(readSourceLocation());
7683 TL.setAttrNameLoc(readSourceLocation());
7684 TL.setSizeExpr(Reader.readExpr());
7685}
7686
7687void TypeLocReader::VisitDependentVectorTypeLoc(
7688 DependentVectorTypeLoc TL) {
7689 TL.setNameLoc(readSourceLocation());
7690 TL.setAttrNameLoc(readSourceLocation());
7691 TL.setSizeExpr(Reader.readExpr());
7692}
7693
7694void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7695 VisitVectorTypeLoc(TL);
7696}
7697
7698void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7699 TL.setAttrNameLoc(readSourceLocation());
7700 TL.setAttrOperandParensRange(readSourceRange());
7701 TL.setAttrRowOperand(Reader.readExpr());
7702 TL.setAttrColumnOperand(Reader.readExpr());
7703}
7704
7705void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7706 DependentSizedMatrixTypeLoc TL) {
7707 TL.setAttrNameLoc(readSourceLocation());
7708 TL.setAttrOperandParensRange(readSourceRange());
7709 TL.setAttrRowOperand(Reader.readExpr());
7710 TL.setAttrColumnOperand(Reader.readExpr());
7711}
7712
7713void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7714 TL.setLocalRangeBegin(readSourceLocation());
7715 TL.setLParenLoc(readSourceLocation());
7716 TL.setRParenLoc(readSourceLocation());
7717 TL.setExceptionSpecRange(readSourceRange());
7718 TL.setLocalRangeEnd(readSourceLocation());
7719 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7720 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7721 }
7722}
7723
7724void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7725 VisitFunctionTypeLoc(TL);
7726}
7727
7728void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7729 VisitFunctionTypeLoc(TL);
7730}
7731
7732void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7733 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7734 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7735 SourceLocation NameLoc = readSourceLocation();
7736 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7737}
7738
7739void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7740 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7741 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7742 SourceLocation NameLoc = readSourceLocation();
7743 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7744}
7745
7746void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7747 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7748 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7749 SourceLocation NameLoc = readSourceLocation();
7750 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7751}
7752
7753void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7754 TL.setTypeofLoc(readSourceLocation());
7755 TL.setLParenLoc(readSourceLocation());
7756 TL.setRParenLoc(readSourceLocation());
7757}
7758
7759void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7760 TL.setTypeofLoc(readSourceLocation());
7761 TL.setLParenLoc(readSourceLocation());
7762 TL.setRParenLoc(readSourceLocation());
7763 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7764}
7765
7766void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7767 TL.setDecltypeLoc(readSourceLocation());
7768 TL.setRParenLoc(readSourceLocation());
7769}
7770
7771void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7772 TL.setEllipsisLoc(readSourceLocation());
7773}
7774
7775void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7776 TL.setKWLoc(readSourceLocation());
7777 TL.setLParenLoc(readSourceLocation());
7778 TL.setRParenLoc(readSourceLocation());
7779 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7780}
7781
7782ConceptReference *ASTRecordReader::readConceptReference() {
7783 auto NNS = readNestedNameSpecifierLoc();
7784 auto TemplateKWLoc = readSourceLocation();
7785 auto ConceptNameLoc = readDeclarationNameInfo();
7786 auto FoundDecl = readDeclAs<NamedDecl>();
7787 auto NamedConcept = readTemplateName();
7788 auto *CR = ConceptReference::Create(
7789 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7790 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7791 return CR;
7792}
7793
7794void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7795 TL.setNameLoc(readSourceLocation());
7796 if (Reader.readBool())
7797 TL.setConceptReference(Reader.readConceptReference());
7798 if (Reader.readBool())
7799 TL.setRParenLoc(readSourceLocation());
7800}
7801
7802void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7803 DeducedTemplateSpecializationTypeLoc TL) {
7804 TL.setElaboratedKeywordLoc(readSourceLocation());
7805 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7806 TL.setTemplateNameLoc(readSourceLocation());
7807}
7808
7809void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7810 TL.setElaboratedKeywordLoc(readSourceLocation());
7811 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7812 TL.setNameLoc(readSourceLocation());
7813}
7814
7815void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7816 VisitTagTypeLoc(TL);
7817}
7818
7819void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7820 VisitTagTypeLoc(TL);
7821}
7822
7823void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7824
7825void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7826 TL.setAttr(ReadAttr());
7827}
7828
7829void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7830 // Nothing to do
7831}
7832
7833void TypeLocReader::VisitLateParsedAttrTypeLoc(LateParsedAttrTypeLoc TL) {
7834 llvm_unreachable(
7835 "should be replaced with a concrete type before serialization");
7836}
7837
7838void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7839 // Nothing to do.
7840}
7841
7842void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7843 TL.setAttrLoc(readSourceLocation());
7844}
7845
7846void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7847 HLSLAttributedResourceTypeLoc TL) {
7848 // Nothing to do.
7849}
7850
7851void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7852 // Nothing to do.
7853}
7854
7855void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7856 TL.setNameLoc(readSourceLocation());
7857}
7858
7859void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7860 SubstTemplateTypeParmTypeLoc TL) {
7861 TL.setNameLoc(readSourceLocation());
7862}
7863
7864void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7865 SubstTemplateTypeParmPackTypeLoc TL) {
7866 TL.setNameLoc(readSourceLocation());
7867}
7868
7869void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7870 SubstBuiltinTemplatePackTypeLoc TL) {
7871 TL.setNameLoc(readSourceLocation());
7872}
7873
7874void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7875 TemplateSpecializationTypeLoc TL) {
7876 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7877 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7878 SourceLocation TemplateKeywordLoc = readSourceLocation();
7879 SourceLocation NameLoc = readSourceLocation();
7880 SourceLocation LAngleLoc = readSourceLocation();
7881 SourceLocation RAngleLoc = readSourceLocation();
7882 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7883 LAngleLoc, RAngleLoc);
7884 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7885 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7886 Args[I] = Reader.readTemplateArgumentLocInfo(
7887 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7888}
7889
7890void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7891 TL.setLParenLoc(readSourceLocation());
7892 TL.setRParenLoc(readSourceLocation());
7893}
7894
7895void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7896 TL.setElaboratedKeywordLoc(readSourceLocation());
7897 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7898 TL.setNameLoc(readSourceLocation());
7899}
7900
7901void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7902 TL.setEllipsisLoc(readSourceLocation());
7903}
7904
7905void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7906 TL.setNameLoc(readSourceLocation());
7907 TL.setNameEndLoc(readSourceLocation());
7908}
7909
7910void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7911 if (TL.getNumProtocols()) {
7912 TL.setProtocolLAngleLoc(readSourceLocation());
7913 TL.setProtocolRAngleLoc(readSourceLocation());
7914 }
7915 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7916 TL.setProtocolLoc(i, Loc: readSourceLocation());
7917}
7918
7919void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7920 TL.setHasBaseTypeAsWritten(Reader.readBool());
7921 TL.setTypeArgsLAngleLoc(readSourceLocation());
7922 TL.setTypeArgsRAngleLoc(readSourceLocation());
7923 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7924 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7925 TL.setProtocolLAngleLoc(readSourceLocation());
7926 TL.setProtocolRAngleLoc(readSourceLocation());
7927 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7928 TL.setProtocolLoc(i, Loc: readSourceLocation());
7929}
7930
7931void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7932 TL.setStarLoc(readSourceLocation());
7933}
7934
7935void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7936 TL.setKWLoc(readSourceLocation());
7937 TL.setLParenLoc(readSourceLocation());
7938 TL.setRParenLoc(readSourceLocation());
7939}
7940
7941void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7942 TL.setKWLoc(readSourceLocation());
7943}
7944
7945void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7946 TL.setNameLoc(readSourceLocation());
7947}
7948
7949void TypeLocReader::VisitDependentBitIntTypeLoc(
7950 clang::DependentBitIntTypeLoc TL) {
7951 TL.setNameLoc(readSourceLocation());
7952}
7953
7954void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7955 // Nothing to do.
7956}
7957
7958void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7959 TypeLocReader TLR(*this);
7960 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7961 TLR.Visit(TyLoc: TL);
7962}
7963
7964TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7965 QualType InfoTy = readType();
7966 if (InfoTy.isNull())
7967 return nullptr;
7968
7969 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7970 readTypeLoc(TL: TInfo->getTypeLoc());
7971 return TInfo;
7972}
7973
7974static unsigned getIndexForTypeID(serialization::TypeID ID) {
7975 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7976}
7977
7978static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7979 return ID >> 32;
7980}
7981
7982static bool isPredefinedType(serialization::TypeID ID) {
7983 // We don't need to erase the higher bits since if these bits are not 0,
7984 // it must be larger than NUM_PREDEF_TYPE_IDS.
7985 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7986}
7987
7988std::pair<ModuleFile *, unsigned>
7989ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7990 assert(!isPredefinedType(ID) &&
7991 "Predefined type shouldn't be in TypesLoaded");
7992 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7993 assert(ModuleFileIndex && "Untranslated Local Decl?");
7994
7995 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7996 assert(OwningModuleFile &&
7997 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7998
7999 return {OwningModuleFile,
8000 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
8001}
8002
8003QualType ASTReader::GetType(TypeID ID) {
8004 assert(ContextObj && "reading type with no AST context");
8005 ASTContext &Context = *ContextObj;
8006
8007 unsigned FastQuals = ID & Qualifiers::FastMask;
8008
8009 if (isPredefinedType(ID)) {
8010 QualType T;
8011 unsigned Index = getIndexForTypeID(ID);
8012 switch ((PredefinedTypeIDs)Index) {
8013 case PREDEF_TYPE_LAST_ID:
8014 // We should never use this one.
8015 llvm_unreachable("Invalid predefined type");
8016 break;
8017 case PREDEF_TYPE_NULL_ID:
8018 return QualType();
8019 case PREDEF_TYPE_VOID_ID:
8020 T = Context.VoidTy;
8021 break;
8022 case PREDEF_TYPE_BOOL_ID:
8023 T = Context.BoolTy;
8024 break;
8025 case PREDEF_TYPE_CHAR_U_ID:
8026 case PREDEF_TYPE_CHAR_S_ID:
8027 // FIXME: Check that the signedness of CharTy is correct!
8028 T = Context.CharTy;
8029 break;
8030 case PREDEF_TYPE_UCHAR_ID:
8031 T = Context.UnsignedCharTy;
8032 break;
8033 case PREDEF_TYPE_USHORT_ID:
8034 T = Context.UnsignedShortTy;
8035 break;
8036 case PREDEF_TYPE_UINT_ID:
8037 T = Context.UnsignedIntTy;
8038 break;
8039 case PREDEF_TYPE_ULONG_ID:
8040 T = Context.UnsignedLongTy;
8041 break;
8042 case PREDEF_TYPE_ULONGLONG_ID:
8043 T = Context.UnsignedLongLongTy;
8044 break;
8045 case PREDEF_TYPE_UINT128_ID:
8046 T = Context.UnsignedInt128Ty;
8047 break;
8048 case PREDEF_TYPE_SCHAR_ID:
8049 T = Context.SignedCharTy;
8050 break;
8051 case PREDEF_TYPE_WCHAR_ID:
8052 T = Context.WCharTy;
8053 break;
8054 case PREDEF_TYPE_SHORT_ID:
8055 T = Context.ShortTy;
8056 break;
8057 case PREDEF_TYPE_INT_ID:
8058 T = Context.IntTy;
8059 break;
8060 case PREDEF_TYPE_LONG_ID:
8061 T = Context.LongTy;
8062 break;
8063 case PREDEF_TYPE_LONGLONG_ID:
8064 T = Context.LongLongTy;
8065 break;
8066 case PREDEF_TYPE_INT128_ID:
8067 T = Context.Int128Ty;
8068 break;
8069 case PREDEF_TYPE_BFLOAT16_ID:
8070 T = Context.BFloat16Ty;
8071 break;
8072 case PREDEF_TYPE_HALF_ID:
8073 T = Context.HalfTy;
8074 break;
8075 case PREDEF_TYPE_FLOAT_ID:
8076 T = Context.FloatTy;
8077 break;
8078 case PREDEF_TYPE_DOUBLE_ID:
8079 T = Context.DoubleTy;
8080 break;
8081 case PREDEF_TYPE_LONGDOUBLE_ID:
8082 T = Context.LongDoubleTy;
8083 break;
8084 case PREDEF_TYPE_SHORT_ACCUM_ID:
8085 T = Context.ShortAccumTy;
8086 break;
8087 case PREDEF_TYPE_ACCUM_ID:
8088 T = Context.AccumTy;
8089 break;
8090 case PREDEF_TYPE_LONG_ACCUM_ID:
8091 T = Context.LongAccumTy;
8092 break;
8093 case PREDEF_TYPE_USHORT_ACCUM_ID:
8094 T = Context.UnsignedShortAccumTy;
8095 break;
8096 case PREDEF_TYPE_UACCUM_ID:
8097 T = Context.UnsignedAccumTy;
8098 break;
8099 case PREDEF_TYPE_ULONG_ACCUM_ID:
8100 T = Context.UnsignedLongAccumTy;
8101 break;
8102 case PREDEF_TYPE_SHORT_FRACT_ID:
8103 T = Context.ShortFractTy;
8104 break;
8105 case PREDEF_TYPE_FRACT_ID:
8106 T = Context.FractTy;
8107 break;
8108 case PREDEF_TYPE_LONG_FRACT_ID:
8109 T = Context.LongFractTy;
8110 break;
8111 case PREDEF_TYPE_USHORT_FRACT_ID:
8112 T = Context.UnsignedShortFractTy;
8113 break;
8114 case PREDEF_TYPE_UFRACT_ID:
8115 T = Context.UnsignedFractTy;
8116 break;
8117 case PREDEF_TYPE_ULONG_FRACT_ID:
8118 T = Context.UnsignedLongFractTy;
8119 break;
8120 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
8121 T = Context.SatShortAccumTy;
8122 break;
8123 case PREDEF_TYPE_SAT_ACCUM_ID:
8124 T = Context.SatAccumTy;
8125 break;
8126 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
8127 T = Context.SatLongAccumTy;
8128 break;
8129 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
8130 T = Context.SatUnsignedShortAccumTy;
8131 break;
8132 case PREDEF_TYPE_SAT_UACCUM_ID:
8133 T = Context.SatUnsignedAccumTy;
8134 break;
8135 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
8136 T = Context.SatUnsignedLongAccumTy;
8137 break;
8138 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
8139 T = Context.SatShortFractTy;
8140 break;
8141 case PREDEF_TYPE_SAT_FRACT_ID:
8142 T = Context.SatFractTy;
8143 break;
8144 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
8145 T = Context.SatLongFractTy;
8146 break;
8147 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
8148 T = Context.SatUnsignedShortFractTy;
8149 break;
8150 case PREDEF_TYPE_SAT_UFRACT_ID:
8151 T = Context.SatUnsignedFractTy;
8152 break;
8153 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
8154 T = Context.SatUnsignedLongFractTy;
8155 break;
8156 case PREDEF_TYPE_FLOAT16_ID:
8157 T = Context.Float16Ty;
8158 break;
8159 case PREDEF_TYPE_FLOAT128_ID:
8160 T = Context.Float128Ty;
8161 break;
8162 case PREDEF_TYPE_IBM128_ID:
8163 T = Context.Ibm128Ty;
8164 break;
8165 case PREDEF_TYPE_OVERLOAD_ID:
8166 T = Context.OverloadTy;
8167 break;
8168 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
8169 T = Context.UnresolvedTemplateTy;
8170 break;
8171 case PREDEF_TYPE_BOUND_MEMBER:
8172 T = Context.BoundMemberTy;
8173 break;
8174 case PREDEF_TYPE_PSEUDO_OBJECT:
8175 T = Context.PseudoObjectTy;
8176 break;
8177 case PREDEF_TYPE_DEPENDENT_ID:
8178 T = Context.DependentTy;
8179 break;
8180 case PREDEF_TYPE_UNKNOWN_ANY:
8181 T = Context.UnknownAnyTy;
8182 break;
8183 case PREDEF_TYPE_NULLPTR_ID:
8184 T = Context.NullPtrTy;
8185 break;
8186 case PREDEF_TYPE_META_INFO_ID:
8187 T = Context.MetaInfoTy;
8188 break;
8189 case PREDEF_TYPE_CHAR8_ID:
8190 T = Context.Char8Ty;
8191 break;
8192 case PREDEF_TYPE_CHAR16_ID:
8193 T = Context.Char16Ty;
8194 break;
8195 case PREDEF_TYPE_CHAR32_ID:
8196 T = Context.Char32Ty;
8197 break;
8198 case PREDEF_TYPE_OBJC_ID:
8199 T = Context.ObjCBuiltinIdTy;
8200 break;
8201 case PREDEF_TYPE_OBJC_CLASS:
8202 T = Context.ObjCBuiltinClassTy;
8203 break;
8204 case PREDEF_TYPE_OBJC_SEL:
8205 T = Context.ObjCBuiltinSelTy;
8206 break;
8207#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8208 case PREDEF_TYPE_##Id##_ID: \
8209 T = Context.SingletonId; \
8210 break;
8211#include "clang/Basic/OpenCLImageTypes.def"
8212#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8213 case PREDEF_TYPE_##Id##_ID: \
8214 T = Context.Id##Ty; \
8215 break;
8216#include "clang/Basic/OpenCLExtensionTypes.def"
8217 case PREDEF_TYPE_SAMPLER_ID:
8218 T = Context.OCLSamplerTy;
8219 break;
8220 case PREDEF_TYPE_EVENT_ID:
8221 T = Context.OCLEventTy;
8222 break;
8223 case PREDEF_TYPE_CLK_EVENT_ID:
8224 T = Context.OCLClkEventTy;
8225 break;
8226 case PREDEF_TYPE_QUEUE_ID:
8227 T = Context.OCLQueueTy;
8228 break;
8229 case PREDEF_TYPE_RESERVE_ID_ID:
8230 T = Context.OCLReserveIDTy;
8231 break;
8232 case PREDEF_TYPE_AUTO_DEDUCT:
8233 T = Context.getAutoDeductType();
8234 break;
8235 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8236 T = Context.getAutoRRefDeductType();
8237 break;
8238 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8239 T = Context.ARCUnbridgedCastTy;
8240 break;
8241 case PREDEF_TYPE_BUILTIN_FN:
8242 T = Context.BuiltinFnTy;
8243 break;
8244 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8245 T = Context.IncompleteMatrixIdxTy;
8246 break;
8247 case PREDEF_TYPE_ARRAY_SECTION:
8248 T = Context.ArraySectionTy;
8249 break;
8250 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8251 T = Context.OMPArrayShapingTy;
8252 break;
8253 case PREDEF_TYPE_OMP_ITERATOR:
8254 T = Context.OMPIteratorTy;
8255 break;
8256#define SVE_TYPE(Name, Id, SingletonId) \
8257 case PREDEF_TYPE_##Id##_ID: \
8258 T = Context.SingletonId; \
8259 break;
8260#include "clang/Basic/AArch64ACLETypes.def"
8261#define PPC_VECTOR_TYPE(Name, Id, Size) \
8262 case PREDEF_TYPE_##Id##_ID: \
8263 T = Context.Id##Ty; \
8264 break;
8265#include "clang/Basic/PPCTypes.def"
8266#define RVV_TYPE(Name, Id, SingletonId) \
8267 case PREDEF_TYPE_##Id##_ID: \
8268 T = Context.SingletonId; \
8269 break;
8270#include "clang/Basic/RISCVVTypes.def"
8271#define WASM_TYPE(Name, Id, SingletonId) \
8272 case PREDEF_TYPE_##Id##_ID: \
8273 T = Context.SingletonId; \
8274 break;
8275#include "clang/Basic/WebAssemblyReferenceTypes.def"
8276#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8277 case PREDEF_TYPE_##Id##_ID: \
8278 T = Context.SingletonId; \
8279 break;
8280#include "clang/Basic/AMDGPUTypes.def"
8281#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8282 case PREDEF_TYPE_##Id##_ID: \
8283 T = Context.SingletonId; \
8284 break;
8285#include "clang/Basic/HLSLIntangibleTypes.def"
8286#define HLSL_PACKED_TYPE(Name, Id, SingletonId) \
8287 case PREDEF_TYPE_##Id##_ID: \
8288 T = Context.SingletonId; \
8289 break;
8290#include "clang/Basic/HLSLPackedTypes.def"
8291#define SPIRV_TYPE(Name, Id, SingletonId) \
8292 case PREDEF_TYPE_##Id##_ID: \
8293 T = Context.SingletonId; \
8294 break;
8295#include "clang/Basic/SPIRVTypes.def"
8296 }
8297
8298 assert(!T.isNull() && "Unknown predefined type");
8299 return T.withFastQualifiers(TQs: FastQuals);
8300 }
8301
8302 unsigned Index = translateTypeIDToIndex(ID).second;
8303
8304 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8305 if (TypesLoaded[Index].isNull()) {
8306 TypesLoaded[Index] = readTypeRecord(ID);
8307 if (TypesLoaded[Index].isNull())
8308 return QualType();
8309
8310 TypesLoaded[Index]->setFromAST();
8311 if (DeserializationListener)
8312 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8313 T: TypesLoaded[Index]);
8314 }
8315
8316 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8317}
8318
8319QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8320 return GetType(ID: getGlobalTypeID(F, LocalID));
8321}
8322
8323serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8324 LocalTypeID LocalID) const {
8325 if (isPredefinedType(ID: LocalID))
8326 return LocalID;
8327
8328 if (!F.ModuleOffsetMap.empty())
8329 ReadModuleOffsetMap(F);
8330
8331 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8332 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8333
8334 if (ModuleFileIndex == 0)
8335 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8336
8337 ModuleFile &MF =
8338 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8339 ModuleFileIndex = MF.Index + 1;
8340 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8341}
8342
8343TemplateArgumentLocInfo
8344ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8345 switch (Kind) {
8346 case TemplateArgument::Expression:
8347 return readExpr();
8348 case TemplateArgument::Type:
8349 return readTypeSourceInfo();
8350 case TemplateArgument::Template:
8351 case TemplateArgument::TemplateExpansion: {
8352 SourceLocation TemplateKWLoc = readSourceLocation();
8353 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8354 SourceLocation TemplateNameLoc = readSourceLocation();
8355 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8356 ? readSourceLocation()
8357 : SourceLocation();
8358 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8359 TemplateNameLoc, EllipsisLoc);
8360 }
8361 case TemplateArgument::Null:
8362 case TemplateArgument::Integral:
8363 case TemplateArgument::Declaration:
8364 case TemplateArgument::NullPtr:
8365 case TemplateArgument::StructuralValue:
8366 case TemplateArgument::Pack:
8367 // FIXME: Is this right?
8368 return TemplateArgumentLocInfo();
8369 }
8370 llvm_unreachable("unexpected template argument loc");
8371}
8372
8373TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8374 TemplateArgument Arg = readTemplateArgument();
8375
8376 if (Arg.getKind() == TemplateArgument::Expression) {
8377 if (readBool()) // bool InfoHasSameExpr.
8378 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8379 }
8380 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8381}
8382
8383void ASTRecordReader::readTemplateArgumentListInfo(
8384 TemplateArgumentListInfo &Result) {
8385 Result.setLAngleLoc(readSourceLocation());
8386 Result.setRAngleLoc(readSourceLocation());
8387 unsigned NumArgsAsWritten = readInt();
8388 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8389 Result.addArgument(Loc: readTemplateArgumentLoc());
8390}
8391
8392const ASTTemplateArgumentListInfo *
8393ASTRecordReader::readASTTemplateArgumentListInfo() {
8394 TemplateArgumentListInfo Result;
8395 readTemplateArgumentListInfo(Result);
8396 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8397}
8398
8399Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8400
8401void ASTReader::CompleteRedeclChain(const Decl *D) {
8402 if (NumCurrentElementsDeserializing) {
8403 // We arrange to not care about the complete redeclaration chain while we're
8404 // deserializing. Just remember that the AST has marked this one as complete
8405 // but that it's not actually complete yet, so we know we still need to
8406 // complete it later.
8407 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8408 return;
8409 }
8410
8411 if (!D->getDeclContext()) {
8412 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8413 return;
8414 }
8415
8416 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8417
8418 // If this is a named declaration, complete it by looking it up
8419 // within its context.
8420 //
8421 // FIXME: Merging a function definition should merge
8422 // all mergeable entities within it.
8423 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8424 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8425 if (!getContext().getLangOpts().CPlusPlus &&
8426 isa<TranslationUnitDecl>(Val: DC)) {
8427 // Outside of C++, we don't have a lookup table for the TU, so update
8428 // the identifier instead. (For C++ modules, we don't store decls
8429 // in the serialized identifier table, so we do the lookup in the TU.)
8430 auto *II = Name.getAsIdentifierInfo();
8431 assert(II && "non-identifier name in C?");
8432 if (II->isOutOfDate())
8433 updateOutOfDateIdentifier(II: *II);
8434 } else
8435 DC->lookup(Name);
8436 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8437 // Find all declarations of this kind from the relevant context.
8438 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8439 auto *DC = cast<DeclContext>(Val: DCDecl);
8440 SmallVector<Decl*, 8> Decls;
8441 FindExternalLexicalDecls(
8442 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8443 }
8444 }
8445 }
8446
8447 RedeclarableTemplateDecl *Template = nullptr;
8448 ArrayRef<TemplateArgument> Args;
8449 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8450 Template = CTSD->getSpecializedTemplate();
8451 Args = CTSD->getTemplateArgs().asArray();
8452 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8453 Template = VTSD->getSpecializedTemplate();
8454 Args = VTSD->getTemplateArgs().asArray();
8455 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8456 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8457 Template = Tmplt;
8458 Args = FD->getTemplateSpecializationArgs()->asArray();
8459 }
8460 }
8461
8462 if (Template)
8463 Template->loadLazySpecializationsImpl(Args);
8464}
8465
8466CXXCtorInitializer **
8467ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8468 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8469 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8470 SavedStreamPosition SavedPosition(Cursor);
8471 if (llvm::Error Err = Cursor.JumpToBit(NewBitNo: Loc.Offset)) {
8472 Error(Err: std::move(Err));
8473 return nullptr;
8474 }
8475 ReadingKindTracker ReadingKind(Read_Decl, *this);
8476 Deserializing D(this);
8477
8478 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8479 if (!MaybeCode) {
8480 Error(Err: MaybeCode.takeError());
8481 return nullptr;
8482 }
8483 unsigned Code = MaybeCode.get();
8484
8485 ASTRecordReader Record(*this, *Loc.F);
8486 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8487 if (!MaybeRecCode) {
8488 Error(Err: MaybeRecCode.takeError());
8489 return nullptr;
8490 }
8491 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8492 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8493 return nullptr;
8494 }
8495
8496 return Record.readCXXCtorInitializers();
8497}
8498
8499CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8500 assert(ContextObj && "reading base specifiers with no AST context");
8501 ASTContext &Context = *ContextObj;
8502
8503 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8504 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8505 SavedStreamPosition SavedPosition(Cursor);
8506 if (llvm::Error Err = Cursor.JumpToBit(NewBitNo: Loc.Offset)) {
8507 Error(Err: std::move(Err));
8508 return nullptr;
8509 }
8510 ReadingKindTracker ReadingKind(Read_Decl, *this);
8511 Deserializing D(this);
8512
8513 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8514 if (!MaybeCode) {
8515 Error(Err: MaybeCode.takeError());
8516 return nullptr;
8517 }
8518 unsigned Code = MaybeCode.get();
8519
8520 ASTRecordReader Record(*this, *Loc.F);
8521 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8522 if (!MaybeRecCode) {
8523 Error(Err: MaybeCode.takeError());
8524 return nullptr;
8525 }
8526 unsigned RecCode = MaybeRecCode.get();
8527
8528 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8529 Error(Msg: "malformed AST file: missing C++ base specifiers");
8530 return nullptr;
8531 }
8532
8533 unsigned NumBases = Record.readInt();
8534 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8535 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8536 for (unsigned I = 0; I != NumBases; ++I)
8537 Bases[I] = Record.readCXXBaseSpecifier();
8538 return Bases;
8539}
8540
8541GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8542 LocalDeclID LocalID) const {
8543 if (LocalID < NUM_PREDEF_DECL_IDS)
8544 return GlobalDeclID(LocalID.getRawValue());
8545
8546 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8547 DeclID ID = LocalID.getLocalDeclIndex();
8548
8549 if (!F.ModuleOffsetMap.empty())
8550 ReadModuleOffsetMap(F);
8551
8552 ModuleFile *OwningModuleFile =
8553 OwningModuleFileIndex == 0
8554 ? &F
8555 : F.TransitiveImports[OwningModuleFileIndex - 1];
8556
8557 if (OwningModuleFileIndex == 0)
8558 ID -= NUM_PREDEF_DECL_IDS;
8559
8560 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8561 return GlobalDeclID(NewModuleFileIndex, ID);
8562}
8563
8564bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8565 // Predefined decls aren't from any module.
8566 if (ID < NUM_PREDEF_DECL_IDS)
8567 return false;
8568
8569 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8570 return M.Index == ModuleFileIndex - 1;
8571}
8572
8573ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8574 // Predefined decls aren't from any module.
8575 if (ID < NUM_PREDEF_DECL_IDS)
8576 return nullptr;
8577
8578 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8579 assert(ModuleFileIndex && "Untranslated Local Decl?");
8580
8581 return &getModuleManager()[ModuleFileIndex - 1];
8582}
8583
8584ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8585 if (!D->isFromASTFile())
8586 return nullptr;
8587
8588 return getOwningModuleFile(ID: D->getGlobalID());
8589}
8590
8591SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8592 if (ID < NUM_PREDEF_DECL_IDS)
8593 return SourceLocation();
8594
8595 if (Decl *D = GetExistingDecl(ID))
8596 return D->getLocation();
8597
8598 SourceLocation Loc;
8599 DeclCursorForID(ID, Location&: Loc);
8600 return Loc;
8601}
8602
8603Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8604 assert(ContextObj && "reading predefined decl without AST context");
8605 ASTContext &Context = *ContextObj;
8606 Decl *NewLoaded = nullptr;
8607 switch (ID) {
8608 case PREDEF_DECL_NULL_ID:
8609 return nullptr;
8610
8611 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8612 return Context.getTranslationUnitDecl();
8613
8614 case PREDEF_DECL_OBJC_ID_ID:
8615 if (Context.ObjCIdDecl)
8616 return Context.ObjCIdDecl;
8617 NewLoaded = Context.getObjCIdDecl();
8618 break;
8619
8620 case PREDEF_DECL_OBJC_SEL_ID:
8621 if (Context.ObjCSelDecl)
8622 return Context.ObjCSelDecl;
8623 NewLoaded = Context.getObjCSelDecl();
8624 break;
8625
8626 case PREDEF_DECL_OBJC_CLASS_ID:
8627 if (Context.ObjCClassDecl)
8628 return Context.ObjCClassDecl;
8629 NewLoaded = Context.getObjCClassDecl();
8630 break;
8631
8632 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8633 if (Context.ObjCProtocolClassDecl)
8634 return Context.ObjCProtocolClassDecl;
8635 NewLoaded = Context.getObjCProtocolDecl();
8636 break;
8637
8638 case PREDEF_DECL_INT_128_ID:
8639 if (Context.Int128Decl)
8640 return Context.Int128Decl;
8641 NewLoaded = Context.getInt128Decl();
8642 break;
8643
8644 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8645 if (Context.UInt128Decl)
8646 return Context.UInt128Decl;
8647 NewLoaded = Context.getUInt128Decl();
8648 break;
8649
8650 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8651 if (Context.ObjCInstanceTypeDecl)
8652 return Context.ObjCInstanceTypeDecl;
8653 NewLoaded = Context.getObjCInstanceTypeDecl();
8654 break;
8655
8656 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8657 if (Context.BuiltinVaListDecl)
8658 return Context.BuiltinVaListDecl;
8659 NewLoaded = Context.getBuiltinVaListDecl();
8660 break;
8661
8662 case PREDEF_DECL_VA_LIST_TAG:
8663 if (Context.VaListTagDecl)
8664 return Context.VaListTagDecl;
8665 NewLoaded = Context.getVaListTagDecl();
8666 break;
8667
8668 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8669 if (Context.BuiltinMSVaListDecl)
8670 return Context.BuiltinMSVaListDecl;
8671 NewLoaded = Context.getBuiltinMSVaListDecl();
8672 break;
8673
8674 case PREDEF_DECL_BUILTIN_ZOS_VA_LIST_ID:
8675 if (Context.BuiltinZOSVaListDecl)
8676 return Context.BuiltinZOSVaListDecl;
8677 NewLoaded = Context.getBuiltinZOSVaListDecl();
8678 break;
8679
8680 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8681 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8682 return Context.getMSGuidTagDecl();
8683
8684 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8685 if (Context.ExternCContext)
8686 return Context.ExternCContext;
8687 NewLoaded = Context.getExternCContextDecl();
8688 break;
8689
8690 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8691 if (Context.CFConstantStringTypeDecl)
8692 return Context.CFConstantStringTypeDecl;
8693 NewLoaded = Context.getCFConstantStringDecl();
8694 break;
8695
8696 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8697 if (Context.CFConstantStringTagDecl)
8698 return Context.CFConstantStringTagDecl;
8699 NewLoaded = Context.getCFConstantStringTagDecl();
8700 break;
8701
8702 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8703 return Context.getMSTypeInfoTagDecl();
8704
8705#define BuiltinTemplate(BTName) \
8706 case PREDEF_DECL##BTName##_ID: \
8707 if (Context.Decl##BTName) \
8708 return Context.Decl##BTName; \
8709 NewLoaded = Context.get##BTName##Decl(); \
8710 break;
8711#include "clang/Basic/BuiltinTemplates.inc"
8712
8713 case NUM_PREDEF_DECL_IDS:
8714 llvm_unreachable("Invalid decl ID");
8715 break;
8716 }
8717
8718 assert(NewLoaded && "Failed to load predefined decl?");
8719
8720 if (DeserializationListener)
8721 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8722
8723 return NewLoaded;
8724}
8725
8726unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8727 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8728 if (!OwningModuleFile) {
8729 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8730 return GlobalID.getRawValue();
8731 }
8732
8733 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8734}
8735
8736Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8737 assert(ContextObj && "reading decl with no AST context");
8738
8739 if (ID < NUM_PREDEF_DECL_IDS) {
8740 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8741 if (D) {
8742 // Track that we have merged the declaration with ID \p ID into the
8743 // pre-existing predefined declaration \p D.
8744 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8745 if (Merged.empty())
8746 Merged.push_back(Elt: ID);
8747 }
8748 return D;
8749 }
8750
8751 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8752
8753 if (Index >= DeclsLoaded.size()) {
8754 assert(0 && "declaration ID out-of-range for AST file");
8755 Error(Msg: "declaration ID out-of-range for AST file");
8756 return nullptr;
8757 }
8758
8759 return DeclsLoaded[Index];
8760}
8761
8762Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8763 if (ID < NUM_PREDEF_DECL_IDS)
8764 return GetExistingDecl(ID);
8765
8766 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8767
8768 if (Index >= DeclsLoaded.size()) {
8769 assert(0 && "declaration ID out-of-range for AST file");
8770 Error(Msg: "declaration ID out-of-range for AST file");
8771 return nullptr;
8772 }
8773
8774 if (!DeclsLoaded[Index]) {
8775 ReadDeclRecord(ID);
8776 if (DeserializationListener)
8777 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8778 }
8779
8780 return DeclsLoaded[Index];
8781}
8782
8783LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8784 GlobalDeclID GlobalID) {
8785 if (GlobalID < NUM_PREDEF_DECL_IDS)
8786 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8787
8788 if (!M.ModuleOffsetMap.empty())
8789 ReadModuleOffsetMap(F&: M);
8790
8791 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8792 DeclID ID = GlobalID.getLocalDeclIndex();
8793
8794 if (Owner == &M) {
8795 ID += NUM_PREDEF_DECL_IDS;
8796 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8797 }
8798
8799 uint64_t OrignalModuleFileIndex = 0;
8800 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8801 if (M.TransitiveImports[I] == Owner) {
8802 OrignalModuleFileIndex = I + 1;
8803 break;
8804 }
8805
8806 if (!OrignalModuleFileIndex)
8807 return LocalDeclID();
8808
8809 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8810}
8811
8812GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8813 unsigned &Idx) {
8814 if (Idx >= Record.size()) {
8815 Error(Msg: "Corrupted AST file");
8816 return GlobalDeclID(0);
8817 }
8818
8819 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8820}
8821
8822/// Resolve the offset of a statement into a statement.
8823///
8824/// This operation will read a new statement from the external
8825/// source each time it is called, and is meant to be used via a
8826/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8827Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8828 // Switch case IDs are per Decl.
8829 ClearSwitchCaseIDs();
8830
8831 // Offset here is a global offset across the entire chain.
8832 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8833 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(NewBitNo: Loc.Offset)) {
8834 Error(Err: std::move(Err));
8835 return nullptr;
8836 }
8837 assert(NumCurrentElementsDeserializing == 0 &&
8838 "should not be called while already deserializing");
8839 Deserializing D(this);
8840 return ReadStmtFromStream(F&: *Loc.F);
8841}
8842
8843bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8844 const Decl *D) {
8845 assert(D);
8846
8847 auto It = SpecLookups.find(Val: D);
8848 if (It == SpecLookups.end())
8849 return false;
8850
8851 // Get Decl may violate the iterator from SpecializationsLookups so we store
8852 // the DeclIDs in ahead.
8853 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8854 It->second.Table.findAll();
8855
8856 // Since we've loaded all the specializations, we can erase it from
8857 // the lookup table.
8858 SpecLookups.erase(I: It);
8859
8860 bool NewSpecsFound = false;
8861 Deserializing LookupResults(this);
8862 for (auto &Info : Infos) {
8863 if (GetExistingDecl(ID: Info))
8864 continue;
8865 NewSpecsFound = true;
8866 GetDecl(ID: Info);
8867 }
8868
8869 return NewSpecsFound;
8870}
8871
8872bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8873 assert(D);
8874
8875 CompleteRedeclChain(D);
8876 bool NewSpecsFound =
8877 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8878 if (OnlyPartial)
8879 return NewSpecsFound;
8880
8881 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8882 return NewSpecsFound;
8883}
8884
8885bool ASTReader::LoadExternalSpecializationsImpl(
8886 SpecLookupTableTy &SpecLookups, const Decl *D,
8887 ArrayRef<TemplateArgument> TemplateArgs) {
8888 assert(D);
8889
8890 auto It = SpecLookups.find(Val: D);
8891 if (It == SpecLookups.end())
8892 return false;
8893
8894 Deserializing LookupResults(this);
8895 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8896
8897 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8898 It->second.Table.find(EKey: HashValue);
8899
8900 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8901 std::string Name;
8902 llvm::raw_string_ostream OS(Name);
8903 auto *ND = cast<NamedDecl>(Val: D);
8904 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8905 /*Qualified=*/true);
8906 return Name;
8907 });
8908
8909 bool NewSpecsFound = false;
8910 for (auto &Info : Infos) {
8911 if (GetExistingDecl(ID: Info))
8912 continue;
8913 NewSpecsFound = true;
8914 GetDecl(ID: Info);
8915 }
8916
8917 return NewSpecsFound;
8918}
8919
8920bool ASTReader::LoadExternalSpecializations(
8921 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8922 assert(D);
8923
8924 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8925 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8926 NewDeclsFound |=
8927 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8928
8929 return NewDeclsFound;
8930}
8931
8932void ASTReader::FindExternalLexicalDecls(
8933 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8934 SmallVectorImpl<Decl *> &Decls) {
8935 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8936
8937 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8938 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8939 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8940 auto K = (Decl::Kind)+LexicalDecls[I];
8941 if (!IsKindWeWant(K))
8942 continue;
8943
8944 auto ID = (DeclID) + LexicalDecls[I + 1];
8945
8946 // Don't add predefined declarations to the lexical context more
8947 // than once.
8948 if (ID < NUM_PREDEF_DECL_IDS) {
8949 if (PredefsVisited[ID])
8950 continue;
8951
8952 PredefsVisited[ID] = true;
8953 }
8954
8955 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8956 assert(D->getKind() == K && "wrong kind for lexical decl");
8957 if (!DC->isDeclInLexicalTraversal(D))
8958 Decls.push_back(Elt: D);
8959 }
8960 }
8961 };
8962
8963 if (isa<TranslationUnitDecl>(Val: DC)) {
8964 for (const auto &Lexical : TULexicalDecls)
8965 Visit(Lexical.first, Lexical.second);
8966 } else {
8967 auto I = LexicalDecls.find(Val: DC);
8968 if (I != LexicalDecls.end())
8969 Visit(I->second.first, I->second.second);
8970 }
8971
8972 ++NumLexicalDeclContextsRead;
8973}
8974
8975namespace {
8976
8977class UnalignedDeclIDComp {
8978 ASTReader &Reader;
8979 ModuleFile &Mod;
8980
8981public:
8982 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8983 : Reader(Reader), Mod(M) {}
8984
8985 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8986 SourceLocation LHS = getLocation(ID: L);
8987 SourceLocation RHS = getLocation(ID: R);
8988 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8989 }
8990
8991 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8992 SourceLocation RHS = getLocation(ID: R);
8993 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8994 }
8995
8996 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8997 SourceLocation LHS = getLocation(ID: L);
8998 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8999 }
9000
9001 SourceLocation getLocation(unaligned_decl_id_t ID) const {
9002 return Reader.getSourceManager().getFileLoc(
9003 Loc: Reader.getSourceLocationForDeclID(
9004 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
9005 }
9006};
9007
9008} // namespace
9009
9010void ASTReader::FindFileRegionDecls(FileID File,
9011 unsigned Offset, unsigned Length,
9012 SmallVectorImpl<Decl *> &Decls) {
9013 SourceManager &SM = getSourceManager();
9014
9015 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
9016 if (I == FileDeclIDs.end())
9017 return;
9018
9019 FileDeclsInfo &DInfo = I->second;
9020 if (DInfo.Decls.empty())
9021 return;
9022
9023 SourceLocation
9024 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
9025 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
9026
9027 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
9028 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
9029 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
9030 if (BeginIt != DInfo.Decls.begin())
9031 --BeginIt;
9032
9033 // If we are pointing at a top-level decl inside an objc container, we need
9034 // to backtrack until we find it otherwise we will fail to report that the
9035 // region overlaps with an objc container.
9036 while (BeginIt != DInfo.Decls.begin() &&
9037 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
9038 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
9039 ->isTopLevelDeclInObjCContainer())
9040 --BeginIt;
9041
9042 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
9043 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
9044 if (EndIt != DInfo.Decls.end())
9045 ++EndIt;
9046
9047 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
9048 ++DIt)
9049 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
9050 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
9051}
9052
9053bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
9054 DeclarationName Name,
9055 const DeclContext *OriginalDC) {
9056 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
9057 "DeclContext has no visible decls in storage");
9058 if (!Name)
9059 return false;
9060
9061 // Load the list of declarations.
9062 DeclsSet DS;
9063
9064 auto Find = [&, this](auto &&Table, auto &&Key) {
9065 for (GlobalDeclID ID : Table.find(Key)) {
9066 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9067 if (ND->getDeclName() != Name)
9068 continue;
9069 // Special case for namespaces: There can be a lot of redeclarations of
9070 // some namespaces, and we import a "key declaration" per imported module.
9071 // Since all declarations of a namespace are essentially interchangeable,
9072 // we can optimize namespace look-up by only storing the key declaration
9073 // of the current TU, rather than storing N key declarations where N is
9074 // the # of imported modules that declare that namespace.
9075 // TODO: Try to generalize this optimization to other redeclarable decls.
9076 if (isa<NamespaceDecl>(Val: ND))
9077 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9078 DS.insert(ND);
9079 }
9080 };
9081
9082 Deserializing LookupResults(this);
9083
9084 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
9085 // available.
9086 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
9087 ++NumVisibleDeclContextsRead;
9088 Find(It->second.Table, Name);
9089 }
9090
9091 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
9092 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
9093 ++NumModuleLocalVisibleDeclContexts;
9094 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
9095 }
9096 };
9097 if (auto *NamedModule =
9098 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
9099 : nullptr)
9100 FindModuleLocalLookup(NamedModule);
9101 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
9102 // We're going to find a decl but the decl context of the lookup is
9103 // unspecified. In this case, the OriginalDC may be the decl context in other
9104 // module.
9105 if (ContextObj && ContextObj->getCurrentNamedModule())
9106 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
9107
9108 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
9109 ++NumTULocalVisibleDeclContexts;
9110 Find(It->second.Table, Name);
9111 }
9112
9113 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9114 return !DS.empty();
9115}
9116
9117void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
9118 if (!DC->hasExternalVisibleStorage())
9119 return;
9120
9121 DeclsMap Decls;
9122
9123 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
9124 auto It = LookupTables.find(DC);
9125 if (It == LookupTables.end())
9126 return;
9127
9128 NumRead++;
9129
9130 for (GlobalDeclID ID : It->second.Table.findAll()) {
9131 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9132 // Special case for namespaces: There can be a lot of redeclarations of
9133 // some namespaces, and we import a "key declaration" per imported module.
9134 // Since all declarations of a namespace are essentially interchangeable,
9135 // we can optimize namespace look-up by only storing the key declaration
9136 // of the current TU, rather than storing N key declarations where N is
9137 // the # of imported modules that declare that namespace.
9138 // TODO: Try to generalize this optimization to other redeclarable decls.
9139 if (isa<NamespaceDecl>(Val: ND))
9140 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9141 Decls[ND->getDeclName()].insert(ND);
9142 }
9143
9144 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
9145 };
9146
9147 findAll(Lookups, NumVisibleDeclContextsRead);
9148 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
9149 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
9150
9151 for (auto &[Name, DS] : Decls)
9152 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9153
9154 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
9155}
9156
9157const serialization::reader::DeclContextLookupTable *
9158ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
9159 auto I = Lookups.find(Val: Primary);
9160 return I == Lookups.end() ? nullptr : &I->second;
9161}
9162
9163const serialization::reader::ModuleLocalLookupTable *
9164ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
9165 auto I = ModuleLocalLookups.find(Val: Primary);
9166 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
9167}
9168
9169const serialization::reader::DeclContextLookupTable *
9170ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
9171 auto I = TULocalLookups.find(Val: Primary);
9172 return I == TULocalLookups.end() ? nullptr : &I->second;
9173}
9174
9175serialization::reader::LazySpecializationInfoLookupTable *
9176ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
9177 assert(D->isCanonicalDecl());
9178 auto &LookupTable =
9179 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
9180 auto I = LookupTable.find(Val: D);
9181 return I == LookupTable.end() ? nullptr : &I->second;
9182}
9183
9184bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
9185 assert(D->isCanonicalDecl());
9186 return PartialSpecializationsLookups.contains(Val: D) ||
9187 SpecializationsLookups.contains(Val: D);
9188}
9189
9190/// Under non-PCH compilation the consumer receives the objc methods
9191/// before receiving the implementation, and codegen depends on this.
9192/// We simulate this by deserializing and passing to consumer the methods of the
9193/// implementation before passing the deserialized implementation decl.
9194static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
9195 ASTConsumer *Consumer) {
9196 assert(ImplD && Consumer);
9197
9198 for (auto *I : ImplD->methods())
9199 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
9200
9201 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
9202}
9203
9204void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
9205 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
9206 PassObjCImplDeclToConsumer(ImplD, Consumer);
9207 else
9208 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9209}
9210
9211void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9212 Consumer->HandleVTable(RD);
9213}
9214
9215void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9216 this->Consumer = Consumer;
9217
9218 if (Consumer)
9219 PassInterestingDeclsToConsumer();
9220
9221 if (DeserializationListener)
9222 DeserializationListener->ReaderInitialized(Reader: this);
9223}
9224
9225void ASTReader::PrintStats() {
9226 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9227
9228 unsigned NumTypesLoaded =
9229 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9230 unsigned NumDeclsLoaded =
9231 DeclsLoaded.size() -
9232 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9233 unsigned NumIdentifiersLoaded =
9234 IdentifiersLoaded.size() -
9235 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9236 unsigned NumMacrosLoaded =
9237 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9238 unsigned NumSelectorsLoaded =
9239 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9240
9241 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9242 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9243 NumSLocEntriesRead, TotalNumSLocEntries,
9244 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9245 if (!TypesLoaded.empty())
9246 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9247 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9248 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9249 if (!DeclsLoaded.empty())
9250 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9251 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9252 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9253 if (!IdentifiersLoaded.empty())
9254 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9255 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9256 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9257 if (!MacrosLoaded.empty())
9258 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9259 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9260 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9261 if (!SelectorsLoaded.empty())
9262 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9263 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9264 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9265 if (TotalNumStatements)
9266 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9267 NumStatementsRead, TotalNumStatements,
9268 ((float)NumStatementsRead/TotalNumStatements * 100));
9269 if (TotalNumMacros)
9270 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9271 NumMacrosRead, TotalNumMacros,
9272 ((float)NumMacrosRead/TotalNumMacros * 100));
9273 if (TotalLexicalDeclContexts)
9274 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9275 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9276 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9277 * 100));
9278 if (TotalVisibleDeclContexts)
9279 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9280 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9281 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9282 * 100));
9283 if (TotalModuleLocalVisibleDeclContexts)
9284 std::fprintf(
9285 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9286 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9287 ((float)NumModuleLocalVisibleDeclContexts /
9288 TotalModuleLocalVisibleDeclContexts * 100));
9289 if (TotalTULocalVisibleDeclContexts)
9290 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9291 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9292 ((float)NumTULocalVisibleDeclContexts /
9293 TotalTULocalVisibleDeclContexts * 100));
9294 if (TotalNumMethodPoolEntries)
9295 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9296 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9297 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9298 * 100));
9299 if (NumMethodPoolLookups)
9300 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9301 NumMethodPoolHits, NumMethodPoolLookups,
9302 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9303 if (NumMethodPoolTableLookups)
9304 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9305 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9306 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9307 * 100.0));
9308 if (NumIdentifierLookupHits)
9309 std::fprintf(stderr,
9310 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9311 NumIdentifierLookupHits, NumIdentifierLookups,
9312 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9313
9314 if (GlobalIndex) {
9315 std::fprintf(stderr, format: "\n");
9316 GlobalIndex->printStats();
9317 }
9318
9319 std::fprintf(stderr, format: "\n");
9320 dump();
9321 std::fprintf(stderr, format: "\n");
9322}
9323
9324template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9325LLVM_DUMP_METHOD static void
9326dumpModuleIDMap(StringRef Name,
9327 const ContinuousRangeMap<Key, ModuleFile *,
9328 InitialCapacity> &Map) {
9329 if (Map.begin() == Map.end())
9330 return;
9331
9332 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9333
9334 llvm::errs() << Name << ":\n";
9335 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9336 I != IEnd; ++I)
9337 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9338 << "\n";
9339}
9340
9341LLVM_DUMP_METHOD void ASTReader::dump() {
9342 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9343 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9344 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9345 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9346 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9347 dumpModuleIDMap(Name: "Global preprocessed entity map",
9348 Map: GlobalPreprocessedEntityMap);
9349
9350 llvm::errs() << "\n*** PCH/Modules Loaded:";
9351 for (ModuleFile &M : ModuleMgr)
9352 M.dump();
9353}
9354
9355/// Return the amount of memory used by memory buffers, breaking down
9356/// by heap-backed versus mmap'ed memory.
9357void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9358 for (ModuleFile &I : ModuleMgr) {
9359 if (llvm::MemoryBuffer *buf = I.Buffer) {
9360 size_t bytes = buf->getBufferSize();
9361 switch (buf->getBufferKind()) {
9362 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9363 sizes.malloc_bytes += bytes;
9364 break;
9365 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9366 sizes.mmap_bytes += bytes;
9367 break;
9368 }
9369 }
9370 }
9371}
9372
9373void ASTReader::InitializeSema(Sema &S) {
9374 SemaObj = &S;
9375 S.addExternalSource(E: this);
9376
9377 // Makes sure any declarations that were deserialized "too early"
9378 // still get added to the identifier's declaration chains.
9379 for (GlobalDeclID ID : PreloadedDeclIDs) {
9380 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9381 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9382 }
9383 PreloadedDeclIDs.clear();
9384
9385 // FIXME: What happens if these are changed by a module import?
9386 if (!FPPragmaOptions.empty()) {
9387 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9388 FPOptionsOverride NewOverrides =
9389 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9390 SemaObj->CurFPFeatures =
9391 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9392 }
9393
9394 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9395 Decl *D = GetDecl(ID);
9396 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9397 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9398 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9399 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9400 else
9401 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9402 }
9403 DeclsWithEffectsToVerify.clear();
9404
9405 SemaObj->OpenCLFeatures = OpenCLExtensions;
9406
9407 UpdateSema();
9408}
9409
9410void ASTReader::UpdateSema() {
9411 assert(SemaObj && "no Sema to update");
9412
9413 // UpdateSema() runs after each AST file is loaded, not only the first, so a
9414 // 'requires' directive from a module is registered too.
9415 for (GlobalDeclID ID : OpenMPRequiresDecls)
9416 SemaObj->OpenMP().addRequiresDecl(D: cast<OMPRequiresDecl>(Val: GetDecl(ID)));
9417 OpenMPRequiresDecls.clear();
9418
9419 // Load the offsets of the declarations that Sema references.
9420 // They will be lazily deserialized when needed.
9421 if (!SemaDeclRefs.empty()) {
9422 assert(SemaDeclRefs.size() % 3 == 0);
9423 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9424 if (!SemaObj->StdNamespace)
9425 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9426 if (!SemaObj->StdBadAlloc)
9427 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9428 if (!SemaObj->StdAlignValT)
9429 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9430 }
9431 SemaDeclRefs.clear();
9432 }
9433
9434 // Update the state of pragmas. Use the same API as if we had encountered the
9435 // pragma in the source.
9436 if(OptimizeOffPragmaLocation.isValid())
9437 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9438 if (PragmaMSStructState != -1)
9439 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9440 if (PointersToMembersPragmaLocation.isValid()) {
9441 SemaObj->ActOnPragmaMSPointersToMembers(
9442 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9443 PragmaMSPointersToMembersState,
9444 PragmaLoc: PointersToMembersPragmaLocation);
9445 }
9446 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9447 if (!RISCVVecIntrinsicPragma.empty()) {
9448 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9449 "Wrong number of RISCVVecIntrinsicPragma");
9450 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9451 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9452 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9453 }
9454
9455 if (PragmaAlignPackCurrentValue) {
9456 // The bottom of the stack might have a default value. It must be adjusted
9457 // to the current value to ensure that the packing state is preserved after
9458 // popping entries that were included/imported from a PCH/module.
9459 bool DropFirst = false;
9460 if (!PragmaAlignPackStack.empty() &&
9461 PragmaAlignPackStack.front().Location.isInvalid()) {
9462 assert(PragmaAlignPackStack.front().Value ==
9463 SemaObj->AlignPackStack.DefaultValue &&
9464 "Expected a default alignment value");
9465 SemaObj->AlignPackStack.Stack.emplace_back(
9466 Args&: PragmaAlignPackStack.front().SlotLabel,
9467 Args&: SemaObj->AlignPackStack.CurrentValue,
9468 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9469 Args&: PragmaAlignPackStack.front().PushLocation);
9470 DropFirst = true;
9471 }
9472 for (const auto &Entry :
9473 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9474 SemaObj->AlignPackStack.Stack.emplace_back(
9475 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9476 }
9477 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9478 assert(*PragmaAlignPackCurrentValue ==
9479 SemaObj->AlignPackStack.DefaultValue &&
9480 "Expected a default align and pack value");
9481 // Keep the current values.
9482 } else {
9483 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9484 SemaObj->AlignPackStack.CurrentPragmaLocation =
9485 PragmaAlignPackCurrentLocation;
9486 }
9487 }
9488 if (FpPragmaCurrentValue) {
9489 // The bottom of the stack might have a default value. It must be adjusted
9490 // to the current value to ensure that fp-pragma state is preserved after
9491 // popping entries that were included/imported from a PCH/module.
9492 bool DropFirst = false;
9493 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9494 assert(FpPragmaStack.front().Value ==
9495 SemaObj->FpPragmaStack.DefaultValue &&
9496 "Expected a default pragma float_control value");
9497 SemaObj->FpPragmaStack.Stack.emplace_back(
9498 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9499 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9500 Args&: FpPragmaStack.front().PushLocation);
9501 DropFirst = true;
9502 }
9503 for (const auto &Entry :
9504 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9505 SemaObj->FpPragmaStack.Stack.emplace_back(
9506 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9507 if (FpPragmaCurrentLocation.isInvalid()) {
9508 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9509 "Expected a default pragma float_control value");
9510 // Keep the current values.
9511 } else {
9512 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9513 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9514 }
9515 }
9516
9517 // For non-modular AST files, restore visiblity of modules.
9518 for (auto &Import : PendingImportedModulesSema) {
9519 if (Import.ImportLoc.isInvalid())
9520 continue;
9521 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9522 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9523 }
9524 }
9525 PendingImportedModulesSema.clear();
9526}
9527
9528IdentifierInfo *ASTReader::get(StringRef Name) {
9529 // Note that we are loading an identifier.
9530 Deserializing AnIdentifier(this);
9531
9532 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9533 NumIdentifierLookups,
9534 NumIdentifierLookupHits);
9535
9536 // We don't need to do identifier table lookups in C++ modules (we preload
9537 // all interesting declarations, and don't need to use the scope for name
9538 // lookups). Perform the lookup in PCH files, though, since we don't build
9539 // a complete initial identifier table if we're carrying on from a PCH.
9540 if (PP.getLangOpts().CPlusPlus) {
9541 for (auto *F : ModuleMgr.pch_modules())
9542 if (Visitor(*F))
9543 break;
9544 } else {
9545 // If there is a global index, look there first to determine which modules
9546 // provably do not have any results for this identifier.
9547 GlobalModuleIndex::HitSet Hits;
9548 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9549 if (!loadGlobalIndex()) {
9550 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9551 HitsPtr = &Hits;
9552 }
9553 }
9554
9555 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9556 }
9557
9558 IdentifierInfo *II = Visitor.getIdentifierInfo();
9559 markIdentifierUpToDate(II);
9560 return II;
9561}
9562
9563namespace clang {
9564
9565 /// An identifier-lookup iterator that enumerates all of the
9566 /// identifiers stored within a set of AST files.
9567 class ASTIdentifierIterator : public IdentifierIterator {
9568 /// The AST reader whose identifiers are being enumerated.
9569 const ASTReader &Reader;
9570
9571 /// The current index into the chain of AST files stored in
9572 /// the AST reader.
9573 unsigned Index;
9574
9575 /// The current position within the identifier lookup table
9576 /// of the current AST file.
9577 ASTIdentifierLookupTable::key_iterator Current;
9578
9579 /// The end position within the identifier lookup table of
9580 /// the current AST file.
9581 ASTIdentifierLookupTable::key_iterator End;
9582
9583 /// Whether to skip any modules in the ASTReader.
9584 bool SkipModules;
9585
9586 public:
9587 explicit ASTIdentifierIterator(const ASTReader &Reader,
9588 bool SkipModules = false);
9589
9590 StringRef Next() override;
9591 };
9592
9593} // namespace clang
9594
9595ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9596 bool SkipModules)
9597 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9598}
9599
9600StringRef ASTIdentifierIterator::Next() {
9601 while (Current == End) {
9602 // If we have exhausted all of our AST files, we're done.
9603 if (Index == 0)
9604 return StringRef();
9605
9606 --Index;
9607 ModuleFile &F = Reader.ModuleMgr[Index];
9608 if (SkipModules && F.isModule())
9609 continue;
9610
9611 ASTIdentifierLookupTable *IdTable =
9612 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9613 Current = IdTable->key_begin();
9614 End = IdTable->key_end();
9615 }
9616
9617 // We have any identifiers remaining in the current AST file; return
9618 // the next one.
9619 StringRef Result = *Current;
9620 ++Current;
9621 return Result;
9622}
9623
9624namespace {
9625
9626/// A utility for appending two IdentifierIterators.
9627class ChainedIdentifierIterator : public IdentifierIterator {
9628 std::unique_ptr<IdentifierIterator> Current;
9629 std::unique_ptr<IdentifierIterator> Queued;
9630
9631public:
9632 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9633 std::unique_ptr<IdentifierIterator> Second)
9634 : Current(std::move(First)), Queued(std::move(Second)) {}
9635
9636 StringRef Next() override {
9637 if (!Current)
9638 return StringRef();
9639
9640 StringRef result = Current->Next();
9641 if (!result.empty())
9642 return result;
9643
9644 // Try the queued iterator, which may itself be empty.
9645 Current.reset();
9646 std::swap(x&: Current, y&: Queued);
9647 return Next();
9648 }
9649};
9650
9651} // namespace
9652
9653IdentifierIterator *ASTReader::getIdentifiers() {
9654 if (!loadGlobalIndex()) {
9655 std::unique_ptr<IdentifierIterator> ReaderIter(
9656 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9657 std::unique_ptr<IdentifierIterator> ModulesIter(
9658 GlobalIndex->createIdentifierIterator());
9659 return new ChainedIdentifierIterator(std::move(ReaderIter),
9660 std::move(ModulesIter));
9661 }
9662
9663 return new ASTIdentifierIterator(*this);
9664}
9665
9666namespace clang {
9667namespace serialization {
9668
9669 class ReadMethodPoolVisitor {
9670 ASTReader &Reader;
9671 Selector Sel;
9672 unsigned PriorGeneration;
9673 unsigned InstanceBits = 0;
9674 unsigned FactoryBits = 0;
9675 bool InstanceHasMoreThanOneDecl = false;
9676 bool FactoryHasMoreThanOneDecl = false;
9677 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9678 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9679
9680 public:
9681 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9682 unsigned PriorGeneration)
9683 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9684
9685 bool operator()(ModuleFile &M) {
9686 if (!M.SelectorLookupTable)
9687 return false;
9688
9689 // If we've already searched this module file, skip it now.
9690 if (M.Generation <= PriorGeneration)
9691 return true;
9692
9693 ++Reader.NumMethodPoolTableLookups;
9694 ASTSelectorLookupTable *PoolTable
9695 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9696 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9697 if (Pos == PoolTable->end())
9698 return false;
9699
9700 ++Reader.NumMethodPoolTableHits;
9701 ++Reader.NumSelectorsRead;
9702 // FIXME: Not quite happy with the statistics here. We probably should
9703 // disable this tracking when called via LoadSelector.
9704 // Also, should entries without methods count as misses?
9705 ++Reader.NumMethodPoolEntriesRead;
9706 ASTSelectorLookupTrait::data_type Data = *Pos;
9707 if (Reader.DeserializationListener)
9708 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9709
9710 // Append methods in the reverse order, so that later we can process them
9711 // in the order they appear in the source code by iterating through
9712 // the vector in the reverse order.
9713 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9714 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9715 InstanceBits = Data.InstanceBits;
9716 FactoryBits = Data.FactoryBits;
9717 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9718 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9719 return false;
9720 }
9721
9722 /// Retrieve the instance methods found by this visitor.
9723 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9724 return InstanceMethods;
9725 }
9726
9727 /// Retrieve the instance methods found by this visitor.
9728 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9729 return FactoryMethods;
9730 }
9731
9732 unsigned getInstanceBits() const { return InstanceBits; }
9733 unsigned getFactoryBits() const { return FactoryBits; }
9734
9735 bool instanceHasMoreThanOneDecl() const {
9736 return InstanceHasMoreThanOneDecl;
9737 }
9738
9739 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9740 };
9741
9742} // namespace serialization
9743} // namespace clang
9744
9745/// Add the given set of methods to the method list.
9746static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9747 ObjCMethodList &List) {
9748 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9749 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9750}
9751
9752void ASTReader::ReadMethodPool(Selector Sel) {
9753 // Get the selector generation and update it to the current generation.
9754 unsigned &Generation = SelectorGeneration[Sel];
9755 unsigned PriorGeneration = Generation;
9756 Generation = getGeneration();
9757 SelectorOutOfDate[Sel] = false;
9758
9759 // Search for methods defined with this selector.
9760 ++NumMethodPoolLookups;
9761 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9762 ModuleMgr.visit(Visitor);
9763
9764 if (Visitor.getInstanceMethods().empty() &&
9765 Visitor.getFactoryMethods().empty())
9766 return;
9767
9768 ++NumMethodPoolHits;
9769
9770 if (!getSema())
9771 return;
9772
9773 Sema &S = *getSema();
9774 auto &Methods = S.ObjC().MethodPool[Sel];
9775
9776 Methods.first.setBits(Visitor.getInstanceBits());
9777 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9778 Methods.second.setBits(Visitor.getFactoryBits());
9779 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9780
9781 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9782 // when building a module we keep every method individually and may need to
9783 // update hasMoreThanOneDecl as we add the methods.
9784 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9785 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9786}
9787
9788void ASTReader::updateOutOfDateSelector(Selector Sel) {
9789 if (SelectorOutOfDate[Sel])
9790 ReadMethodPool(Sel);
9791}
9792
9793void ASTReader::ReadKnownNamespaces(
9794 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9795 Namespaces.clear();
9796
9797 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9798 if (NamespaceDecl *Namespace
9799 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9800 Namespaces.push_back(Elt: Namespace);
9801 }
9802}
9803
9804void ASTReader::ReadUndefinedButUsed(
9805 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9806 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9807 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9808 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9809 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9810 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9811 }
9812 UndefinedButUsed.clear();
9813}
9814
9815void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9816 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9817 Exprs) {
9818 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9819 FieldDecl *FD =
9820 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9821 uint64_t Count = DelayedDeleteExprs[Idx++];
9822 for (uint64_t C = 0; C < Count; ++C) {
9823 SourceLocation DeleteLoc =
9824 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9825 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9826 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9827 }
9828 }
9829}
9830
9831void ASTReader::ReadTentativeDefinitions(
9832 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9833 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9834 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9835 if (Var)
9836 TentativeDefs.push_back(Elt: Var);
9837 }
9838 TentativeDefinitions.clear();
9839}
9840
9841void ASTReader::ReadUnusedFileScopedDecls(
9842 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9843 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9844 DeclaratorDecl *D
9845 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9846 if (D)
9847 Decls.push_back(Elt: D);
9848 }
9849 UnusedFileScopedDecls.clear();
9850}
9851
9852void ASTReader::ReadDelegatingConstructors(
9853 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9854 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9855 CXXConstructorDecl *D
9856 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9857 if (D)
9858 Decls.push_back(Elt: D);
9859 }
9860 DelegatingCtorDecls.clear();
9861}
9862
9863void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9864 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9865 TypedefNameDecl *D
9866 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9867 if (D)
9868 Decls.push_back(Elt: D);
9869 }
9870 ExtVectorDecls.clear();
9871}
9872
9873void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9874 llvm::SmallPtrSetImpl<const TypedefNameDecl *> &Decls) {
9875 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9876 ++I) {
9877 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9878 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9879 if (D)
9880 Decls.insert(Ptr: D);
9881 }
9882 UnusedLocalTypedefNameCandidates.clear();
9883}
9884
9885void ASTReader::ReadDeclsToCheckForDeferredDiags(
9886 llvm::SmallSetVector<Decl *, 4> &Decls) {
9887 for (auto I : DeclsToCheckForDeferredDiags) {
9888 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9889 if (D)
9890 Decls.insert(X: D);
9891 }
9892 DeclsToCheckForDeferredDiags.clear();
9893}
9894
9895void ASTReader::ReadReferencedSelectors(
9896 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9897 if (ReferencedSelectorsData.empty())
9898 return;
9899
9900 // If there are @selector references added them to its pool. This is for
9901 // implementation of -Wselector.
9902 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9903 unsigned I = 0;
9904 while (I < DataSize) {
9905 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9906 SourceLocation SelLoc
9907 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9908 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9909 }
9910 ReferencedSelectorsData.clear();
9911}
9912
9913void ASTReader::ReadWeakUndeclaredIdentifiers(
9914 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9915 if (WeakUndeclaredIdentifiers.empty())
9916 return;
9917
9918 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9919 IdentifierInfo *WeakId
9920 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9921 IdentifierInfo *AliasId
9922 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9923 SourceLocation Loc =
9924 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9925 WeakInfo WI(AliasId, Loc);
9926 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9927 }
9928 WeakUndeclaredIdentifiers.clear();
9929}
9930
9931void ASTReader::ReadExtnameUndeclaredIdentifiers(
9932 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9933 if (ExtnameUndeclaredIdentifiers.empty())
9934 return;
9935
9936 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9937 IdentifierInfo *NameId =
9938 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9939 IdentifierInfo *ExtnameId =
9940 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I + 1]);
9941 SourceLocation Loc =
9942 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I + 2]);
9943 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9944 Ctx&: getContext(), Label: ExtnameId->getName(),
9945 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9946 AttributeCommonInfo::Form::Pragma()));
9947 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9948 }
9949 ExtnameUndeclaredIdentifiers.clear();
9950}
9951
9952void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9953 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9954 ExternalVTableUse VT;
9955 VTableUse &TableInfo = VTableUses[Idx++];
9956 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9957 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9958 VT.DefinitionRequired = TableInfo.Used;
9959 VTables.push_back(Elt: VT);
9960 }
9961
9962 VTableUses.clear();
9963}
9964
9965void ASTReader::ReadPendingInstantiations(
9966 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9967 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9968 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9969 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9970 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9971
9972 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9973 }
9974 PendingInstantiations.clear();
9975}
9976
9977void ASTReader::ReadLateParsedTemplates(
9978 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9979 &LPTMap) {
9980 for (auto &LPT : LateParsedTemplates) {
9981 ModuleFile *FMod = LPT.first;
9982 RecordDataImpl &LateParsed = LPT.second;
9983 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9984 /* In loop */) {
9985 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9986
9987 auto LT = std::make_unique<LateParsedTemplate>();
9988 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9989 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9990
9991 ModuleFile *F = getOwningModuleFile(D: LT->D);
9992 assert(F && "No module");
9993
9994 unsigned TokN = LateParsed[Idx++];
9995 LT->Toks.reserve(N: TokN);
9996 for (unsigned T = 0; T < TokN; ++T)
9997 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9998
9999 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
10000 }
10001 }
10002
10003 LateParsedTemplates.clear();
10004}
10005
10006void ASTReader::LoadSelector(Selector Sel) {
10007 // It would be complicated to avoid reading the methods anyway. So don't.
10008 ReadMethodPool(Sel);
10009}
10010
10011void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
10012 assert(ID && "Non-zero identifier ID required");
10013 unsigned Index = translateIdentifierIDToIndex(ID).second;
10014 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
10015 IdentifiersLoaded[Index] = II;
10016 if (DeserializationListener)
10017 DeserializationListener->IdentifierRead(ID, II);
10018}
10019
10020/// Set the globally-visible declarations associated with the given
10021/// identifier.
10022///
10023/// If the AST reader is currently in a state where the given declaration IDs
10024/// cannot safely be resolved, they are queued until it is safe to resolve
10025/// them.
10026///
10027/// \param II an IdentifierInfo that refers to one or more globally-visible
10028/// declarations.
10029///
10030/// \param DeclIDs the set of declaration IDs with the name @p II that are
10031/// visible at global scope.
10032///
10033/// \param Decls if non-null, this vector will be populated with the set of
10034/// deserialized declarations. These declarations will not be pushed into
10035/// scope.
10036void ASTReader::SetGloballyVisibleDecls(
10037 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
10038 SmallVectorImpl<Decl *> *Decls) {
10039 if (NumCurrentElementsDeserializing && !Decls) {
10040 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
10041 return;
10042 }
10043
10044 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
10045 if (!SemaObj) {
10046 // Queue this declaration so that it will be added to the
10047 // translation unit scope and identifier's declaration chain
10048 // once a Sema object is known.
10049 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
10050 continue;
10051 }
10052
10053 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
10054
10055 // If we're simply supposed to record the declarations, do so now.
10056 if (Decls) {
10057 Decls->push_back(Elt: D);
10058 continue;
10059 }
10060
10061 // Introduce this declaration into the translation-unit scope
10062 // and add it to the declaration chain for this identifier, so
10063 // that (unqualified) name lookup will find it.
10064 pushExternalDeclIntoScope(D, Name: II);
10065 }
10066}
10067
10068std::pair<ModuleFile *, unsigned>
10069ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
10070 if (ID == 0)
10071 return {nullptr, 0};
10072
10073 unsigned ModuleFileIndex = ID >> 32;
10074 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
10075
10076 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
10077 assert(getModuleManager().size() > ModuleFileIndex - 1);
10078
10079 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10080 assert(LocalID < MF.LocalNumIdentifiers);
10081 return {&MF, MF.BaseIdentifierID + LocalID};
10082}
10083
10084IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
10085 if (ID == 0)
10086 return nullptr;
10087
10088 if (IdentifiersLoaded.empty()) {
10089 Error(Msg: "no identifier table in AST file");
10090 return nullptr;
10091 }
10092
10093 auto [M, Index] = translateIdentifierIDToIndex(ID);
10094 if (!IdentifiersLoaded[Index]) {
10095 assert(M != nullptr && "Untranslated Identifier ID?");
10096 assert(Index >= M->BaseIdentifierID);
10097 unsigned LocalIndex = Index - M->BaseIdentifierID;
10098 const unsigned char *Data =
10099 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
10100
10101 ASTIdentifierLookupTrait Trait(*this, *M);
10102 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
10103 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
10104 auto &II = PP.getIdentifierTable().get(Name: Key);
10105 IdentifiersLoaded[Index] = &II;
10106 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
10107 markIdentifierFromAST(Reader&: *this, II, IsModule);
10108 if (DeserializationListener)
10109 DeserializationListener->IdentifierRead(ID, II: &II);
10110 }
10111
10112 return IdentifiersLoaded[Index];
10113}
10114
10115IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
10116 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
10117}
10118
10119IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
10120 if (LocalID < NUM_PREDEF_IDENT_IDS)
10121 return LocalID;
10122
10123 if (!M.ModuleOffsetMap.empty())
10124 ReadModuleOffsetMap(F&: M);
10125
10126 unsigned ModuleFileIndex = LocalID >> 32;
10127 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
10128 ModuleFile *MF =
10129 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10130 assert(MF && "malformed identifier ID encoding?");
10131
10132 if (!ModuleFileIndex)
10133 LocalID -= NUM_PREDEF_IDENT_IDS;
10134
10135 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
10136}
10137
10138std::pair<ModuleFile *, unsigned>
10139ASTReader::translateMacroIDToIndex(MacroID ID) const {
10140 if (ID == 0)
10141 return {nullptr, 0};
10142
10143 unsigned ModuleFileIndex = ID >> 32;
10144 assert(ModuleFileIndex && "not translating loaded MacroID?");
10145 assert(getModuleManager().size() > ModuleFileIndex - 1);
10146 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10147
10148 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
10149 assert(LocalID < MF.LocalNumMacros);
10150 return {&MF, MF.BaseMacroID + LocalID};
10151}
10152
10153MacroInfo *ASTReader::getMacro(MacroID ID) {
10154 if (ID == 0)
10155 return nullptr;
10156
10157 if (MacrosLoaded.empty()) {
10158 Error(Msg: "no macro table in AST file");
10159 return nullptr;
10160 }
10161
10162 auto [M, Index] = translateMacroIDToIndex(ID);
10163 if (!MacrosLoaded[Index]) {
10164 assert(M != nullptr && "Untranslated Macro ID?");
10165 assert(Index >= M->BaseMacroID);
10166 unsigned LocalIndex = Index - M->BaseMacroID;
10167 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
10168 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
10169
10170 if (DeserializationListener)
10171 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
10172 }
10173
10174 return MacrosLoaded[Index];
10175}
10176
10177MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
10178 if (LocalID < NUM_PREDEF_MACRO_IDS)
10179 return LocalID;
10180
10181 if (!M.ModuleOffsetMap.empty())
10182 ReadModuleOffsetMap(F&: M);
10183
10184 unsigned ModuleFileIndex = LocalID >> 32;
10185 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
10186 ModuleFile *MF =
10187 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10188 assert(MF && "malformed identifier ID encoding?");
10189
10190 if (!ModuleFileIndex) {
10191 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10192 LocalID -= NUM_PREDEF_MACRO_IDS;
10193 }
10194
10195 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10196}
10197
10198serialization::SubmoduleID
10199ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10200 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10201 return LocalID;
10202
10203 if (!M.ModuleOffsetMap.empty())
10204 ReadModuleOffsetMap(F&: M);
10205
10206 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10207 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10208 assert(I != M.SubmoduleRemap.end()
10209 && "Invalid index into submodule index remap");
10210
10211 return LocalID + I->second;
10212}
10213
10214Module *ASTReader::getModule(unsigned ID) {
10215 return getSubmodule(GlobalID: ID);
10216}
10217
10218ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10219 if (ID & 1) {
10220 // It's a module, look it up by submodule ID.
10221 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10222 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10223 } else {
10224 // It's a prefix (preamble, PCH, ...). Look it up by index.
10225 int IndexFromEnd = static_cast<int>(ID >> 1);
10226 assert(IndexFromEnd && "got reference to unknown module file");
10227 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10228 }
10229}
10230
10231unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10232 if (!M)
10233 return 1;
10234
10235 // For a file representing a module, use the submodule ID of the top-level
10236 // module as the file ID. For any other kind of file, the number of such
10237 // files loaded beforehand will be the same on reload.
10238 // FIXME: Is this true even if we have an explicit module file and a PCH?
10239 if (M->isModule())
10240 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10241
10242 auto PCHModules = getModuleManager().pch_modules();
10243 auto I = llvm::find(Range&: PCHModules, Val: M);
10244 assert(I != PCHModules.end() && "emitting reference to unknown file");
10245 return std::distance(first: I, last: PCHModules.end()) << 1;
10246}
10247
10248std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10249 if (Module *M = getSubmodule(GlobalID: ID))
10250 return ASTSourceDescriptor(*M);
10251
10252 // If there is only a single PCH, return it instead.
10253 // Chained PCH are not supported.
10254 const auto &PCHChain = ModuleMgr.pch_modules();
10255 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10256 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10257 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10258 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10259 return ASTSourceDescriptor(ModuleName,
10260 llvm::sys::path::parent_path(path: MF.FileName),
10261 FileName, MF.Signature);
10262 }
10263 return std::nullopt;
10264}
10265
10266ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10267 auto I = DefinitionSource.find(Val: FD);
10268 if (I == DefinitionSource.end())
10269 return EK_ReplyHazy;
10270 return I->second ? EK_Never : EK_Always;
10271}
10272
10273bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10274 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10275}
10276
10277Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10278 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10279}
10280
10281Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10282 if (ID == 0)
10283 return Selector();
10284
10285 if (ID > SelectorsLoaded.size()) {
10286 Error(Msg: "selector ID out of range in AST file");
10287 return Selector();
10288 }
10289
10290 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10291 // Load this selector from the selector table.
10292 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10293 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10294 ModuleFile &M = *I->second;
10295 ASTSelectorLookupTrait Trait(*this, M);
10296 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10297 SelectorsLoaded[ID - 1] =
10298 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10299 if (DeserializationListener)
10300 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10301 }
10302
10303 return SelectorsLoaded[ID - 1];
10304}
10305
10306Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10307 return DecodeSelector(ID);
10308}
10309
10310uint32_t ASTReader::GetNumExternalSelectors() {
10311 // ID 0 (the null selector) is considered an external selector.
10312 return getTotalNumSelectors() + 1;
10313}
10314
10315serialization::SelectorID
10316ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10317 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10318 return LocalID;
10319
10320 if (!M.ModuleOffsetMap.empty())
10321 ReadModuleOffsetMap(F&: M);
10322
10323 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10324 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10325 assert(I != M.SelectorRemap.end()
10326 && "Invalid index into selector index remap");
10327
10328 return LocalID + I->second;
10329}
10330
10331DeclarationNameLoc
10332ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10333 switch (Name.getNameKind()) {
10334 case DeclarationName::CXXConstructorName:
10335 case DeclarationName::CXXDestructorName:
10336 case DeclarationName::CXXConversionFunctionName:
10337 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10338
10339 case DeclarationName::CXXOperatorName:
10340 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10341
10342 case DeclarationName::CXXLiteralOperatorName:
10343 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10344 Loc: readSourceLocation());
10345
10346 case DeclarationName::Identifier:
10347 case DeclarationName::ObjCZeroArgSelector:
10348 case DeclarationName::ObjCOneArgSelector:
10349 case DeclarationName::ObjCMultiArgSelector:
10350 case DeclarationName::CXXUsingDirective:
10351 case DeclarationName::CXXDeductionGuideName:
10352 break;
10353 }
10354 return DeclarationNameLoc();
10355}
10356
10357DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10358 DeclarationNameInfo NameInfo;
10359 NameInfo.setName(readDeclarationName());
10360 NameInfo.setLoc(readSourceLocation());
10361 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10362 return NameInfo;
10363}
10364
10365TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10366 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10367}
10368
10369SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10370 auto Kind = readInt();
10371 auto ResultType = readQualType();
10372 auto Value = readAPInt();
10373 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10374 assert(Op.isValid());
10375 return Op;
10376}
10377
10378void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10379 Info.QualifierLoc = readNestedNameSpecifierLoc();
10380 unsigned NumTPLists = readInt();
10381 Info.NumTemplParamLists = NumTPLists;
10382 if (NumTPLists) {
10383 Info.TemplParamLists =
10384 new (getContext()) TemplateParameterList *[NumTPLists];
10385 for (unsigned i = 0; i != NumTPLists; ++i)
10386 Info.TemplParamLists[i] = readTemplateParameterList();
10387 }
10388}
10389
10390TemplateParameterList *
10391ASTRecordReader::readTemplateParameterList() {
10392 SourceLocation TemplateLoc = readSourceLocation();
10393 SourceLocation LAngleLoc = readSourceLocation();
10394 SourceLocation RAngleLoc = readSourceLocation();
10395
10396 unsigned NumParams = readInt();
10397 SmallVector<NamedDecl *, 16> Params;
10398 Params.reserve(N: NumParams);
10399 while (NumParams--)
10400 Params.push_back(Elt: readDeclAs<NamedDecl>());
10401
10402 bool HasRequiresClause = readBool();
10403 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10404
10405 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10406 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10407 return TemplateParams;
10408}
10409
10410void ASTRecordReader::readTemplateArgumentList(
10411 SmallVectorImpl<TemplateArgument> &TemplArgs,
10412 bool Canonicalize) {
10413 unsigned NumTemplateArgs = readInt();
10414 TemplArgs.reserve(N: NumTemplateArgs);
10415 while (NumTemplateArgs--)
10416 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10417}
10418
10419/// Read a UnresolvedSet structure.
10420void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10421 unsigned NumDecls = readInt();
10422 Set.reserve(C&: getContext(), N: NumDecls);
10423 while (NumDecls--) {
10424 GlobalDeclID ID = readDeclID();
10425 AccessSpecifier AS = (AccessSpecifier) readInt();
10426 Set.addLazyDecl(C&: getContext(), ID, AS);
10427 }
10428}
10429
10430CXXBaseSpecifier
10431ASTRecordReader::readCXXBaseSpecifier() {
10432 bool isVirtual = readBool();
10433 bool isBaseOfClass = readBool();
10434 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10435 bool inheritConstructors = readBool();
10436 TypeSourceInfo *TInfo = readTypeSourceInfo();
10437 SourceRange Range = readSourceRange();
10438 SourceLocation EllipsisLoc = readSourceLocation();
10439 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10440 EllipsisLoc);
10441 Result.setInheritConstructors(inheritConstructors);
10442 return Result;
10443}
10444
10445CXXCtorInitializer **
10446ASTRecordReader::readCXXCtorInitializers() {
10447 ASTContext &Context = getContext();
10448 unsigned NumInitializers = readInt();
10449 assert(NumInitializers && "wrote ctor initializers but have no inits");
10450 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10451 for (unsigned i = 0; i != NumInitializers; ++i) {
10452 TypeSourceInfo *TInfo = nullptr;
10453 bool IsBaseVirtual = false;
10454 FieldDecl *Member = nullptr;
10455 IndirectFieldDecl *IndirectMember = nullptr;
10456
10457 CtorInitializerType Type = (CtorInitializerType) readInt();
10458 switch (Type) {
10459 case CTOR_INITIALIZER_BASE:
10460 TInfo = readTypeSourceInfo();
10461 IsBaseVirtual = readBool();
10462 break;
10463
10464 case CTOR_INITIALIZER_DELEGATING:
10465 TInfo = readTypeSourceInfo();
10466 break;
10467
10468 case CTOR_INITIALIZER_MEMBER:
10469 Member = readDeclAs<FieldDecl>();
10470 break;
10471
10472 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10473 IndirectMember = readDeclAs<IndirectFieldDecl>();
10474 break;
10475 }
10476
10477 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10478 Expr *Init = readExpr();
10479 SourceLocation LParenLoc = readSourceLocation();
10480 SourceLocation RParenLoc = readSourceLocation();
10481
10482 CXXCtorInitializer *BOMInit;
10483 if (Type == CTOR_INITIALIZER_BASE)
10484 BOMInit = new (Context)
10485 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10486 RParenLoc, MemberOrEllipsisLoc);
10487 else if (Type == CTOR_INITIALIZER_DELEGATING)
10488 BOMInit = new (Context)
10489 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10490 else if (Member)
10491 BOMInit = new (Context)
10492 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10493 Init, RParenLoc);
10494 else
10495 BOMInit = new (Context)
10496 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10497 LParenLoc, Init, RParenLoc);
10498
10499 if (/*IsWritten*/readBool()) {
10500 unsigned SourceOrder = readInt();
10501 BOMInit->setSourceOrder(SourceOrder);
10502 }
10503
10504 CtorInitializers[i] = BOMInit;
10505 }
10506
10507 return CtorInitializers;
10508}
10509
10510NestedNameSpecifierLoc
10511ASTRecordReader::readNestedNameSpecifierLoc() {
10512 ASTContext &Context = getContext();
10513 unsigned N = readInt();
10514 NestedNameSpecifierLocBuilder Builder;
10515 for (unsigned I = 0; I != N; ++I) {
10516 auto Kind = readNestedNameSpecifierKind();
10517 switch (Kind) {
10518 case NestedNameSpecifier::Kind::Namespace: {
10519 auto *NS = readDeclAs<NamespaceBaseDecl>();
10520 SourceRange Range = readSourceRange();
10521 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10522 break;
10523 }
10524
10525 case NestedNameSpecifier::Kind::Type: {
10526 TypeSourceInfo *T = readTypeSourceInfo();
10527 if (!T)
10528 return NestedNameSpecifierLoc();
10529 SourceLocation ColonColonLoc = readSourceLocation();
10530 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10531 break;
10532 }
10533
10534 case NestedNameSpecifier::Kind::Global: {
10535 SourceLocation ColonColonLoc = readSourceLocation();
10536 Builder.MakeGlobal(Context, ColonColonLoc);
10537 break;
10538 }
10539
10540 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10541 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10542 SourceRange Range = readSourceRange();
10543 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10544 break;
10545 }
10546
10547 case NestedNameSpecifier::Kind::Null:
10548 llvm_unreachable("unexpected null nested name specifier");
10549 }
10550 }
10551
10552 return Builder.getWithLocInContext(Context);
10553}
10554
10555SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10556 unsigned &Idx) {
10557 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10558 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10559 return SourceRange(beg, end);
10560}
10561
10562llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10563 const StringRef Blob) {
10564 unsigned Count = Record[0];
10565 const char *Byte = Blob.data();
10566 llvm::BitVector Ret = llvm::BitVector(Count, false);
10567 for (unsigned I = 0; I < Count; ++Byte)
10568 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10569 if (*Byte & (1 << Bit))
10570 Ret[I] = true;
10571 return Ret;
10572}
10573
10574/// Read a floating-point value
10575llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10576 return llvm::APFloat(Sem, readAPInt());
10577}
10578
10579// Read a string
10580std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10581 unsigned Len = Record[Idx++];
10582 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10583 Idx += Len;
10584 return Result;
10585}
10586
10587StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10588 StringRef &Blob) {
10589 unsigned Len = Record[Idx++];
10590 StringRef Result = Blob.substr(Start: 0, N: Len);
10591 Blob = Blob.substr(Start: Len);
10592 return Result;
10593}
10594
10595std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10596 unsigned &Idx) {
10597 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10598}
10599
10600std::string ASTReader::ReadPath(StringRef BaseDirectory,
10601 const RecordData &Record, unsigned &Idx) {
10602 std::string Filename = ReadString(Record, Idx);
10603 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10604}
10605
10606std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10607 const RecordData &Record, unsigned &Idx,
10608 StringRef &Blob) {
10609 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10610 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10611}
10612
10613VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10614 unsigned &Idx) {
10615 unsigned Major = Record[Idx++];
10616 unsigned Minor = Record[Idx++];
10617 unsigned Subminor = Record[Idx++];
10618 if (Minor == 0)
10619 return VersionTuple(Major);
10620 if (Subminor == 0)
10621 return VersionTuple(Major, Minor - 1);
10622 return VersionTuple(Major, Minor - 1, Subminor - 1);
10623}
10624
10625CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10626 const RecordData &Record,
10627 unsigned &Idx) {
10628 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10629 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10630}
10631
10632DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10633 return Diag(Loc: CurrentImportLoc, DiagID);
10634}
10635
10636DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10637 return Diags.Report(Loc, DiagID);
10638}
10639
10640void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10641 llvm::function_ref<void()> Fn) {
10642 // When Sema is available, avoid duplicate errors.
10643 if (SemaObj) {
10644 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10645 return;
10646 }
10647
10648 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10649}
10650
10651/// Retrieve the identifier table associated with the
10652/// preprocessor.
10653IdentifierTable &ASTReader::getIdentifierTable() {
10654 return PP.getIdentifierTable();
10655}
10656
10657/// Record that the given ID maps to the given switch-case
10658/// statement.
10659void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10660 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10661 "Already have a SwitchCase with this ID");
10662 (*CurrSwitchCaseStmts)[ID] = SC;
10663}
10664
10665/// Retrieve the switch-case statement with the given ID.
10666SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10667 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10668 return (*CurrSwitchCaseStmts)[ID];
10669}
10670
10671void ASTReader::ClearSwitchCaseIDs() {
10672 CurrSwitchCaseStmts->clear();
10673}
10674
10675void ASTReader::ReadComments() {
10676 ASTContext &Context = getContext();
10677 std::vector<RawComment *> Comments;
10678 for (SmallVectorImpl<std::pair<BitstreamCursor,
10679 serialization::ModuleFile *>>::iterator
10680 I = CommentsCursors.begin(),
10681 E = CommentsCursors.end();
10682 I != E; ++I) {
10683 Comments.clear();
10684 BitstreamCursor &Cursor = I->first;
10685 serialization::ModuleFile &F = *I->second;
10686 SavedStreamPosition SavedPosition(Cursor);
10687
10688 RecordData Record;
10689 while (true) {
10690 Expected<llvm::BitstreamEntry> MaybeEntry =
10691 Cursor.advanceSkippingSubblocks(
10692 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10693 if (!MaybeEntry) {
10694 Error(Err: MaybeEntry.takeError());
10695 return;
10696 }
10697 llvm::BitstreamEntry Entry = MaybeEntry.get();
10698
10699 switch (Entry.Kind) {
10700 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10701 case llvm::BitstreamEntry::Error:
10702 Error(Msg: "malformed block record in AST file");
10703 return;
10704 case llvm::BitstreamEntry::EndBlock:
10705 goto NextCursor;
10706 case llvm::BitstreamEntry::Record:
10707 // The interesting case.
10708 break;
10709 }
10710
10711 // Read a record.
10712 Record.clear();
10713 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10714 if (!MaybeComment) {
10715 Error(Err: MaybeComment.takeError());
10716 return;
10717 }
10718 switch ((CommentRecordTypes)MaybeComment.get()) {
10719 case COMMENTS_RAW_COMMENT: {
10720 unsigned Idx = 0;
10721 SourceRange SR = ReadSourceRange(F, Record, Idx);
10722 RawComment::CommentKind Kind =
10723 (RawComment::CommentKind) Record[Idx++];
10724 bool IsTrailingComment = Record[Idx++];
10725 bool IsAlmostTrailingComment = Record[Idx++];
10726 Comments.push_back(x: new (Context) RawComment(
10727 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10728 break;
10729 }
10730 }
10731 }
10732 NextCursor:
10733 for (RawComment *C : Comments) {
10734 SourceLocation CommentLoc = C->getBeginLoc();
10735 if (CommentLoc.isValid()) {
10736 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10737 if (Loc.first.isValid())
10738 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10739 }
10740 }
10741 }
10742}
10743
10744void ASTReader::visitInputFileInfos(
10745 serialization::ModuleFile &MF, bool IncludeSystem,
10746 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10747 bool IsSystem)>
10748 Visitor) {
10749 unsigned NumUserInputs = MF.NumUserInputFiles;
10750 unsigned NumInputs = MF.InputFilesLoaded.size();
10751 assert(NumUserInputs <= NumInputs);
10752 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10753 for (unsigned I = 0; I < N; ++I) {
10754 bool IsSystem = I >= NumUserInputs;
10755 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10756 Visitor(IFI, IsSystem);
10757 }
10758}
10759
10760void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10761 bool IncludeSystem, bool Complain,
10762 llvm::function_ref<void(const serialization::InputFile &IF,
10763 bool isSystem)> Visitor) {
10764 unsigned NumUserInputs = MF.NumUserInputFiles;
10765 unsigned NumInputs = MF.InputFilesLoaded.size();
10766 assert(NumUserInputs <= NumInputs);
10767 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10768 for (unsigned I = 0; I < N; ++I) {
10769 bool IsSystem = I >= NumUserInputs;
10770 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10771 Visitor(IF, IsSystem);
10772 }
10773}
10774
10775void ASTReader::visitTopLevelModuleMaps(
10776 serialization::ModuleFile &MF,
10777 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10778 unsigned NumInputs = MF.InputFilesLoaded.size();
10779 for (unsigned I = 0; I < NumInputs; ++I) {
10780 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10781 if (IFI.TopLevel && IFI.ModuleMap)
10782 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10783 Visitor(*FE);
10784 }
10785}
10786
10787void ASTReader::finishPendingActions() {
10788 while (!PendingIdentifierInfos.empty() ||
10789 !PendingDeducedFunctionTypes.empty() ||
10790 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10791 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10792 !PendingUpdateRecords.empty() ||
10793 !PendingObjCExtensionIvarRedeclarations.empty()) {
10794 // If any identifiers with corresponding top-level declarations have
10795 // been loaded, load those declarations now.
10796 using TopLevelDeclsMap =
10797 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10798 TopLevelDeclsMap TopLevelDecls;
10799
10800 while (!PendingIdentifierInfos.empty()) {
10801 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10802 SmallVector<GlobalDeclID, 4> DeclIDs =
10803 std::move(PendingIdentifierInfos.back().second);
10804 PendingIdentifierInfos.pop_back();
10805
10806 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10807 }
10808
10809 // Load each function type that we deferred loading because it was a
10810 // deduced type that might refer to a local type declared within itself.
10811 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10812 auto *FD = PendingDeducedFunctionTypes[I].first;
10813 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10814
10815 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10816 // If we gave a function a deduced return type, remember that we need to
10817 // propagate that along the redeclaration chain.
10818 if (DT->isDeduced()) {
10819 PendingDeducedTypeUpdates.insert(
10820 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10821 continue;
10822 }
10823
10824 // The function has undeduced DeduceType return type. We hope we can
10825 // find the deduced type by iterating the redecls in other modules
10826 // later.
10827 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10828 continue;
10829 }
10830 }
10831 PendingDeducedFunctionTypes.clear();
10832
10833 // Load each variable type that we deferred loading because it was a
10834 // deduced type that might refer to a local type declared within itself.
10835 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10836 auto *VD = PendingDeducedVarTypes[I].first;
10837 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10838 }
10839 PendingDeducedVarTypes.clear();
10840
10841 // Load pending declaration chains.
10842 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10843 loadPendingDeclChain(D: PendingDeclChains[I].first,
10844 LocalOffset: PendingDeclChains[I].second);
10845 PendingDeclChains.clear();
10846
10847 // Make the most recent of the top-level declarations visible.
10848 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10849 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10850 IdentifierInfo *II = TLD->first;
10851 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10852 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10853 }
10854 }
10855
10856 // Load any pending macro definitions.
10857 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10858 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10859 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10860 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10861 // Initialize the macro history from chained-PCHs ahead of module imports.
10862 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10863 ++IDIdx) {
10864 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10865 if (!Info.M->isModule())
10866 resolvePendingMacro(II, PMInfo: Info);
10867 }
10868 // Handle module imports.
10869 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10870 ++IDIdx) {
10871 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10872 if (Info.M->isModule())
10873 resolvePendingMacro(II, PMInfo: Info);
10874 }
10875 }
10876 PendingMacroIDs.clear();
10877
10878 // Wire up the DeclContexts for Decls that we delayed setting until
10879 // recursive loading is completed.
10880 while (!PendingDeclContextInfos.empty()) {
10881 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10882 PendingDeclContextInfos.pop_front();
10883 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10884 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10885 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10886 }
10887
10888 // Perform any pending declaration updates.
10889 while (!PendingUpdateRecords.empty()) {
10890 auto Update = PendingUpdateRecords.pop_back_val();
10891 ReadingKindTracker ReadingKind(Read_Decl, *this);
10892 loadDeclUpdateRecords(Record&: Update);
10893 }
10894
10895 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10896 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10897 auto DuplicateIvars =
10898 PendingObjCExtensionIvarRedeclarations.back().second;
10899 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10900 StructuralEquivalenceContext Ctx(
10901 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10902 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10903 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10904 /*Complain =*/false,
10905 /*ErrorOnTagTypeMismatch =*/true);
10906 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10907 // Merge redeclared ivars with their predecessors.
10908 for (auto IvarPair : DuplicateIvars) {
10909 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10910 // Change semantic DeclContext but keep the lexical one.
10911 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10912 LexicalDC: Ivar->getLexicalDeclContext(),
10913 Ctx&: getContext());
10914 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10915 }
10916 // Invalidate duplicate extension and the cached ivar list.
10917 ExtensionsPair.first->setInvalidDecl();
10918 ExtensionsPair.second->getClassInterface()
10919 ->getDefinition()
10920 ->setIvarList(nullptr);
10921 } else {
10922 for (auto IvarPair : DuplicateIvars) {
10923 Diag(Loc: IvarPair.first->getLocation(),
10924 DiagID: diag::err_duplicate_ivar_declaration)
10925 << IvarPair.first->getIdentifier();
10926 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10927 }
10928 }
10929 PendingObjCExtensionIvarRedeclarations.pop_back();
10930 }
10931 }
10932
10933 // At this point, all update records for loaded decls are in place, so any
10934 // fake class definitions should have become real.
10935 assert(PendingFakeDefinitionData.empty() &&
10936 "faked up a class definition but never saw the real one");
10937
10938 // If we deserialized any C++ or Objective-C class definitions, any
10939 // Objective-C protocol definitions, or any redeclarable templates, make sure
10940 // that all redeclarations point to the definitions. Note that this can only
10941 // happen now, after the redeclaration chains have been fully wired.
10942 for (Decl *D : PendingDefinitions) {
10943 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10944 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10945 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10946 R = R->getPreviousDecl()) {
10947 assert((R == D) ==
10948 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10949 "declaration thinks it's the definition but it isn't");
10950 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10951 }
10952 }
10953
10954 continue;
10955 }
10956
10957 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10958 // Make sure that the ObjCInterfaceType points at the definition.
10959 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10960 ->Decl = ID;
10961
10962 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10963 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10964
10965 continue;
10966 }
10967
10968 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10969 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10970 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10971
10972 continue;
10973 }
10974
10975 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10976 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10977 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10978 }
10979 PendingDefinitions.clear();
10980
10981 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10982 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10983 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10984 return VD->isThisDeclarationADefinition() ||
10985 VD->isThisDeclarationADemotedDefinition();
10986
10987 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10988 return TD->isThisDeclarationADefinition() ||
10989 TD->isThisDeclarationADemotedDefinition();
10990
10991 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10992 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10993
10994 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10995 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10996
10997 // Conservatively return false here.
10998 return false;
10999 };
11000
11001 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
11002 return hasDefinitionImpl(D, hasDefinitionImpl);
11003 };
11004
11005 // It is not good to prevent multiple declarations since the forward
11006 // declaration is common. Let's try to avoid duplicated definitions
11007 // only.
11008 if (!hasDefinition(D) || !hasDefinition(Previous))
11009 continue;
11010
11011 Module *PM = Previous->getOwningModule();
11012 Module *DM = D->getOwningModule();
11013 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
11014 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
11015 << (DM ? DM->getTopLevelModuleName() : "global module");
11016 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
11017 }
11018 PendingWarningForDuplicatedDefsInModuleUnits.clear();
11019
11020 // Load the bodies of any functions or methods we've encountered. We do
11021 // this now (delayed) so that we can be sure that the declaration chains
11022 // have been fully wired up (hasBody relies on this).
11023 // FIXME: We shouldn't require complete redeclaration chains here.
11024 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
11025 PBEnd = PendingBodies.end();
11026 PB != PBEnd; ++PB) {
11027 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
11028 // FIXME: Check for =delete/=default?
11029 const FunctionDecl *Defn = nullptr;
11030 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
11031 FD->setLazyBody(PB->second);
11032 } else {
11033 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
11034 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
11035
11036 if (!FD->isLateTemplateParsed() &&
11037 !NonConstDefn->isLateTemplateParsed() &&
11038 // We only perform ODR checks for decls not in the explicit
11039 // global module fragment.
11040 !shouldSkipCheckingODR(D: FD) &&
11041 !shouldSkipCheckingODR(D: NonConstDefn) &&
11042 FD->getODRHash() != NonConstDefn->getODRHash()) {
11043 if (!isa<CXXMethodDecl>(Val: FD)) {
11044 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
11045 } else if (FD->getLexicalParent()->isFileContext() &&
11046 NonConstDefn->getLexicalParent()->isFileContext()) {
11047 // Only diagnose out-of-line method definitions. If they are
11048 // in class definitions, then an error will be generated when
11049 // processing the class bodies.
11050 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
11051 }
11052 }
11053 }
11054 continue;
11055 }
11056
11057 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
11058 if (!getContext().getLangOpts().Modules || !MD->hasBody())
11059 MD->setLazyBody(PB->second);
11060 }
11061 PendingBodies.clear();
11062
11063 // Inform any classes that had members added that they now have more members.
11064 for (auto [RD, MD] : PendingAddedClassMembers) {
11065 // The module we just imported added MD to RD so we should do the same, but
11066 // only RD doesn't already contain the same member. This can happen when two
11067 // modules independent add and serialize the same implicit member.
11068
11069 // Fast path: we can check for the presence of implicit special members
11070 // (default/copy/move constructors + copy/move assignments + destructors)
11071 // with RD's needsImplicit*() methods (which are constant time).
11072 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: MD)) {
11073 if (!Ctor->isInheritingConstructor()) {
11074 if (Ctor->isDefaultConstructor()) {
11075 if (RD->needsImplicitDefaultConstructor())
11076 RD->addHiddenDecl(D: MD);
11077 continue;
11078 }
11079 if (Ctor->isCopyConstructor()) {
11080 if (RD->needsImplicitCopyConstructor())
11081 RD->addHiddenDecl(D: MD);
11082 continue;
11083 }
11084 if (Ctor->isMoveConstructor()) {
11085 if (RD->needsImplicitMoveConstructor())
11086 RD->addHiddenDecl(D: MD);
11087 continue;
11088 }
11089 }
11090 } else if (const auto *Mthd = dyn_cast<CXXMethodDecl>(Val: MD)) {
11091 if (isa<CXXDestructorDecl>(Val: Mthd)) {
11092 if (RD->needsImplicitDestructor())
11093 RD->addHiddenDecl(D: MD);
11094 continue;
11095 }
11096 if (Mthd->isCopyAssignmentOperator()) {
11097 if (RD->needsImplicitCopyAssignment())
11098 RD->addHiddenDecl(D: MD);
11099 continue;
11100 }
11101 if (Mthd->isMoveAssignmentOperator()) {
11102 if (RD->needsImplicitMoveAssignment())
11103 RD->addHiddenDecl(D: MD);
11104 continue;
11105 }
11106 }
11107
11108 if (auto *FD = dyn_cast<FunctionDecl>(Val: MD)) {
11109 // Slow path: this is some other implicit added member (e.g., inherited
11110 // constructor). We need iterate through all of RD's members and compare
11111 // their ODR hash against MD's.
11112 auto needToAddMD = true;
11113 for (auto *RDD : RD->noload_decls()) {
11114 if (auto *RDFD = dyn_cast<FunctionDecl>(Val: RDD)) {
11115 if (FD->getODRHash() == RDFD->getODRHash()) {
11116 needToAddMD = false;
11117 break;
11118 }
11119 }
11120 }
11121 if (needToAddMD)
11122 RD->addHiddenDecl(D: MD);
11123 continue;
11124 }
11125
11126 // This branch is only reachable if MD is not a function decl (which means
11127 // it doesn't have an ODR hash to compare). Call addedMember() to preserve
11128 // historical behavior.
11129 RD->addedMember(D: MD);
11130 }
11131 PendingAddedClassMembers.clear();
11132
11133 // Do some cleanup.
11134 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
11135 getContext().deduplicateMergedDefinitionsFor(ND);
11136 PendingMergedDefinitionsToDeduplicate.clear();
11137
11138 // For each decl chain that we wanted to complete while deserializing, mark
11139 // it as "still needs to be completed".
11140 for (Decl *D : PendingIncompleteDeclChains)
11141 markIncompleteDeclChain(D);
11142 PendingIncompleteDeclChains.clear();
11143
11144 assert(PendingIdentifierInfos.empty() &&
11145 "Should be empty at the end of finishPendingActions");
11146 assert(PendingDeducedFunctionTypes.empty() &&
11147 "Should be empty at the end of finishPendingActions");
11148 assert(PendingDeducedVarTypes.empty() &&
11149 "Should be empty at the end of finishPendingActions");
11150 assert(PendingDeclChains.empty() &&
11151 "Should be empty at the end of finishPendingActions");
11152 assert(PendingMacroIDs.empty() &&
11153 "Should be empty at the end of finishPendingActions");
11154 assert(PendingDeclContextInfos.empty() &&
11155 "Should be empty at the end of finishPendingActions");
11156 assert(PendingUpdateRecords.empty() &&
11157 "Should be empty at the end of finishPendingActions");
11158 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
11159 "Should be empty at the end of finishPendingActions");
11160 assert(PendingFakeDefinitionData.empty() &&
11161 "Should be empty at the end of finishPendingActions");
11162 assert(PendingDefinitions.empty() &&
11163 "Should be empty at the end of finishPendingActions");
11164 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
11165 "Should be empty at the end of finishPendingActions");
11166 assert(PendingBodies.empty() &&
11167 "Should be empty at the end of finishPendingActions");
11168 assert(PendingAddedClassMembers.empty() &&
11169 "Should be empty at the end of finishPendingActions");
11170 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
11171 "Should be empty at the end of finishPendingActions");
11172 assert(PendingIncompleteDeclChains.empty() &&
11173 "Should be empty at the end of finishPendingActions");
11174}
11175
11176void ASTReader::diagnoseOdrViolations() {
11177 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
11178 PendingRecordOdrMergeFailures.empty() &&
11179 PendingFunctionOdrMergeFailures.empty() &&
11180 PendingEnumOdrMergeFailures.empty() &&
11181 PendingObjCInterfaceOdrMergeFailures.empty() &&
11182 PendingObjCProtocolOdrMergeFailures.empty())
11183 return;
11184
11185 // Trigger the import of the full definition of each class that had any
11186 // odr-merging problems, so we can produce better diagnostics for them.
11187 // These updates may in turn find and diagnose some ODR failures, so take
11188 // ownership of the set first.
11189 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
11190 PendingOdrMergeFailures.clear();
11191 for (auto &Merge : OdrMergeFailures) {
11192 Merge.first->buildLookup();
11193 Merge.first->decls_begin();
11194 Merge.first->bases_begin();
11195 Merge.first->vbases_begin();
11196 for (auto &RecordPair : Merge.second) {
11197 auto *RD = RecordPair.first;
11198 RD->decls_begin();
11199 RD->bases_begin();
11200 RD->vbases_begin();
11201 }
11202 }
11203
11204 // Trigger the import of the full definition of each record in C/ObjC.
11205 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
11206 PendingRecordOdrMergeFailures.clear();
11207 for (auto &Merge : RecordOdrMergeFailures) {
11208 Merge.first->decls_begin();
11209 for (auto &D : Merge.second)
11210 D->decls_begin();
11211 }
11212
11213 // Trigger the import of the full interface definition.
11214 auto ObjCInterfaceOdrMergeFailures =
11215 std::move(PendingObjCInterfaceOdrMergeFailures);
11216 PendingObjCInterfaceOdrMergeFailures.clear();
11217 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11218 Merge.first->decls_begin();
11219 for (auto &InterfacePair : Merge.second)
11220 InterfacePair.first->decls_begin();
11221 }
11222
11223 // Trigger the import of functions.
11224 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
11225 PendingFunctionOdrMergeFailures.clear();
11226 for (auto &Merge : FunctionOdrMergeFailures) {
11227 Merge.first->buildLookup();
11228 Merge.first->decls_begin();
11229 Merge.first->getBody();
11230 for (auto &FD : Merge.second) {
11231 FD->buildLookup();
11232 FD->decls_begin();
11233 FD->getBody();
11234 }
11235 }
11236
11237 // Trigger the import of enums.
11238 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
11239 PendingEnumOdrMergeFailures.clear();
11240 for (auto &Merge : EnumOdrMergeFailures) {
11241 Merge.first->decls_begin();
11242 for (auto &Enum : Merge.second) {
11243 Enum->decls_begin();
11244 }
11245 }
11246
11247 // Trigger the import of the full protocol definition.
11248 auto ObjCProtocolOdrMergeFailures =
11249 std::move(PendingObjCProtocolOdrMergeFailures);
11250 PendingObjCProtocolOdrMergeFailures.clear();
11251 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11252 Merge.first->decls_begin();
11253 for (auto &ProtocolPair : Merge.second)
11254 ProtocolPair.first->decls_begin();
11255 }
11256
11257 // For each declaration from a merged context, check that the canonical
11258 // definition of that context also contains a declaration of the same
11259 // entity.
11260 //
11261 // Caution: this loop does things that might invalidate iterators into
11262 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11263 while (!PendingOdrMergeChecks.empty()) {
11264 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11265
11266 // FIXME: Skip over implicit declarations for now. This matters for things
11267 // like implicitly-declared special member functions. This isn't entirely
11268 // correct; we can end up with multiple unmerged declarations of the same
11269 // implicit entity.
11270 if (D->isImplicit())
11271 continue;
11272
11273 DeclContext *CanonDef = D->getDeclContext();
11274
11275 bool Found = false;
11276 const Decl *DCanon = D->getCanonicalDecl();
11277
11278 for (auto *RI : D->redecls()) {
11279 if (RI->getLexicalDeclContext() == CanonDef) {
11280 Found = true;
11281 break;
11282 }
11283 }
11284 if (Found)
11285 continue;
11286
11287 // Quick check failed, time to do the slow thing. Note, we can't just
11288 // look up the name of D in CanonDef here, because the member that is
11289 // in CanonDef might not be found by name lookup (it might have been
11290 // replaced by a more recent declaration in the lookup table), and we
11291 // can't necessarily find it in the redeclaration chain because it might
11292 // be merely mergeable, not redeclarable.
11293 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11294 for (auto *CanonMember : CanonDef->decls()) {
11295 if (CanonMember->getCanonicalDecl() == DCanon) {
11296 // This can happen if the declaration is merely mergeable and not
11297 // actually redeclarable (we looked for redeclarations earlier).
11298 //
11299 // FIXME: We should be able to detect this more efficiently, without
11300 // pulling in all of the members of CanonDef.
11301 Found = true;
11302 break;
11303 }
11304 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11305 if (ND->getDeclName() == D->getDeclName())
11306 Candidates.push_back(Elt: ND);
11307 }
11308
11309 if (!Found) {
11310 // The AST doesn't like TagDecls becoming invalid after they've been
11311 // completed. We only really need to mark FieldDecls as invalid here.
11312 if (!isa<TagDecl>(Val: D))
11313 D->setInvalidDecl();
11314
11315 // Ensure we don't accidentally recursively enter deserialization while
11316 // we're producing our diagnostic.
11317 Deserializing RecursionGuard(this);
11318
11319 std::string CanonDefModule =
11320 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11321 D: cast<Decl>(Val: CanonDef));
11322 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11323 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11324 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11325
11326 if (Candidates.empty())
11327 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11328 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11329 else {
11330 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11331 Diag(Loc: Candidates[I]->getLocation(),
11332 DiagID: diag::note_module_odr_violation_possible_decl)
11333 << Candidates[I];
11334 }
11335
11336 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11337 }
11338 }
11339
11340 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11341 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11342 ObjCInterfaceOdrMergeFailures.empty() &&
11343 ObjCProtocolOdrMergeFailures.empty())
11344 return;
11345
11346 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11347 getPreprocessor().getLangOpts());
11348
11349 // Issue any pending ODR-failure diagnostics.
11350 for (auto &Merge : OdrMergeFailures) {
11351 // If we've already pointed out a specific problem with this class, don't
11352 // bother issuing a general "something's different" diagnostic.
11353 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11354 continue;
11355
11356 bool Diagnosed = false;
11357 CXXRecordDecl *FirstRecord = Merge.first;
11358 for (auto &RecordPair : Merge.second) {
11359 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11360 SecondDD: RecordPair.second)) {
11361 Diagnosed = true;
11362 break;
11363 }
11364 }
11365
11366 if (!Diagnosed) {
11367 // All definitions are updates to the same declaration. This happens if a
11368 // module instantiates the declaration of a class template specialization
11369 // and two or more other modules instantiate its definition.
11370 //
11371 // FIXME: Indicate which modules had instantiations of this definition.
11372 // FIXME: How can this even happen?
11373 Diag(Loc: Merge.first->getLocation(),
11374 DiagID: diag::err_module_odr_violation_different_instantiations)
11375 << Merge.first;
11376 }
11377 }
11378
11379 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11380 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11381 for (auto &Merge : RecordOdrMergeFailures) {
11382 // If we've already pointed out a specific problem with this class, don't
11383 // bother issuing a general "something's different" diagnostic.
11384 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11385 continue;
11386
11387 RecordDecl *FirstRecord = Merge.first;
11388 bool Diagnosed = false;
11389 for (auto *SecondRecord : Merge.second) {
11390 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11391 Diagnosed = true;
11392 break;
11393 }
11394 }
11395 (void)Diagnosed;
11396 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11397 }
11398
11399 // Issue ODR failures diagnostics for functions.
11400 for (auto &Merge : FunctionOdrMergeFailures) {
11401 FunctionDecl *FirstFunction = Merge.first;
11402 bool Diagnosed = false;
11403 for (auto &SecondFunction : Merge.second) {
11404 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11405 Diagnosed = true;
11406 break;
11407 }
11408 }
11409 (void)Diagnosed;
11410 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11411 }
11412
11413 // Issue ODR failures diagnostics for enums.
11414 for (auto &Merge : EnumOdrMergeFailures) {
11415 // If we've already pointed out a specific problem with this enum, don't
11416 // bother issuing a general "something's different" diagnostic.
11417 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11418 continue;
11419
11420 EnumDecl *FirstEnum = Merge.first;
11421 bool Diagnosed = false;
11422 for (auto &SecondEnum : Merge.second) {
11423 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11424 Diagnosed = true;
11425 break;
11426 }
11427 }
11428 (void)Diagnosed;
11429 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11430 }
11431
11432 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11433 // If we've already pointed out a specific problem with this interface,
11434 // don't bother issuing a general "something's different" diagnostic.
11435 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11436 continue;
11437
11438 bool Diagnosed = false;
11439 ObjCInterfaceDecl *FirstID = Merge.first;
11440 for (auto &InterfacePair : Merge.second) {
11441 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11442 SecondDD: InterfacePair.second)) {
11443 Diagnosed = true;
11444 break;
11445 }
11446 }
11447 (void)Diagnosed;
11448 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11449 }
11450
11451 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11452 // If we've already pointed out a specific problem with this protocol,
11453 // don't bother issuing a general "something's different" diagnostic.
11454 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11455 continue;
11456
11457 ObjCProtocolDecl *FirstProtocol = Merge.first;
11458 bool Diagnosed = false;
11459 for (auto &ProtocolPair : Merge.second) {
11460 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11461 SecondDD: ProtocolPair.second)) {
11462 Diagnosed = true;
11463 break;
11464 }
11465 }
11466 (void)Diagnosed;
11467 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11468 }
11469}
11470
11471void ASTReader::StartedDeserializing() {
11472 if (llvm::Timer *T = ReadTimer.get();
11473 ++NumCurrentElementsDeserializing == 1 && T)
11474 ReadTimeRegion.emplace(args&: T);
11475}
11476
11477void ASTReader::FinishedDeserializing() {
11478 assert(NumCurrentElementsDeserializing &&
11479 "FinishedDeserializing not paired with StartedDeserializing");
11480 if (NumCurrentElementsDeserializing == 1) {
11481 // We decrease NumCurrentElementsDeserializing only after pending actions
11482 // are finished, to avoid recursively re-calling finishPendingActions().
11483 finishPendingActions();
11484 }
11485 --NumCurrentElementsDeserializing;
11486
11487 if (NumCurrentElementsDeserializing == 0) {
11488 {
11489 // Guard variable to avoid recursively entering the process of passing
11490 // decls to consumer.
11491 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11492 /*NewValue=*/false);
11493
11494 // Propagate exception specification and deduced type updates along
11495 // redeclaration chains.
11496 //
11497 // We do this now rather than in finishPendingActions because we want to
11498 // be able to walk the complete redeclaration chains of the updated decls.
11499 while (!PendingExceptionSpecUpdates.empty() ||
11500 !PendingDeducedTypeUpdates.empty() ||
11501 !PendingUndeducedFunctionDecls.empty()) {
11502 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11503 PendingExceptionSpecUpdates.clear();
11504 for (auto Update : ESUpdates) {
11505 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11506 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11507 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11508 if (auto *Listener = getContext().getASTMutationListener())
11509 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11510 for (auto *Redecl : Update.second->redecls())
11511 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11512 }
11513
11514 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11515 PendingDeducedTypeUpdates.clear();
11516 for (auto Update : DTUpdates) {
11517 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11518 // FIXME: If the return type is already deduced, check that it
11519 // matches.
11520 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11521 ResultType: Update.second);
11522 }
11523
11524 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11525 PendingUndeducedFunctionDecls.clear();
11526 // We hope we can find the deduced type for the functions by iterating
11527 // redeclarations in other modules.
11528 for (FunctionDecl *UndeducedFD : UDTUpdates)
11529 (void)UndeducedFD->getMostRecentDecl();
11530 }
11531
11532 ReadTimeRegion.reset();
11533
11534 diagnoseOdrViolations();
11535 }
11536
11537 // We are not in recursive loading, so it's safe to pass the "interesting"
11538 // decls to the consumer.
11539 if (Consumer)
11540 PassInterestingDeclsToConsumer();
11541 }
11542}
11543
11544void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11545 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11546 // Remove any fake results before adding any real ones.
11547 auto It = PendingFakeLookupResults.find(Key: II);
11548 if (It != PendingFakeLookupResults.end()) {
11549 for (auto *ND : It->second)
11550 SemaObj->IdResolver.RemoveDecl(D: ND);
11551 // FIXME: this works around module+PCH performance issue.
11552 // Rather than erase the result from the map, which is O(n), just clear
11553 // the vector of NamedDecls.
11554 It->second.clear();
11555 }
11556 }
11557
11558 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11559 SemaObj->TUScope->AddDecl(D);
11560 } else if (SemaObj->TUScope) {
11561 // Adding the decl to IdResolver may have failed because it was already in
11562 // (even though it was not added in scope). If it is already in, make sure
11563 // it gets in the scope as well.
11564 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11565 SemaObj->TUScope->AddDecl(D);
11566 }
11567}
11568
11569ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11570 ASTContext *Context,
11571 const PCHContainerReader &PCHContainerRdr,
11572 const CodeGenOptions &CodeGenOpts,
11573 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11574 StringRef isysroot,
11575 DisableValidationForModuleKind DisableValidationKind,
11576 bool AllowASTWithCompilerErrors,
11577 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11578 bool ForceValidateUserInputs,
11579 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11580 std::unique_ptr<llvm::Timer> ReadTimer)
11581 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11582 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11583 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11584 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11585 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11586 StackHandler(Diags), PP(PP), ContextObj(Context),
11587 CodeGenOpts(CodeGenOpts),
11588 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11589 PP.getHeaderSearchInfo()),
11590 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11591 DisableValidationKind(DisableValidationKind),
11592 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11593 AllowConfigurationMismatch(AllowConfigurationMismatch),
11594 ValidateSystemInputs(ValidateSystemInputs),
11595 ForceValidateUserInputs(ForceValidateUserInputs),
11596 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11597 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11598 SourceMgr.setExternalSLocEntrySource(this);
11599
11600 PathBuf.reserve(N: 256);
11601
11602 for (const auto &Ext : Extensions) {
11603 auto BlockName = Ext->getExtensionMetadata().BlockName;
11604 auto Known = ModuleFileExtensions.find(Key: BlockName);
11605 if (Known != ModuleFileExtensions.end()) {
11606 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11607 << BlockName;
11608 continue;
11609 }
11610
11611 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11612 }
11613}
11614
11615ASTReader::~ASTReader() {
11616 if (OwnsDeserializationListener)
11617 delete DeserializationListener;
11618}
11619
11620IdentifierResolver &ASTReader::getIdResolver() {
11621 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11622}
11623
11624Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11625 unsigned AbbrevID) {
11626 Idx = 0;
11627 Record.clear();
11628 return Cursor.readRecord(AbbrevID, Vals&: Record);
11629}
11630//===----------------------------------------------------------------------===//
11631//// OMPClauseReader implementation
11632////===----------------------------------------------------------------------===//
11633
11634// This has to be in namespace clang because it's friended by all
11635// of the OMP clauses.
11636namespace clang {
11637
11638class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11639 ASTRecordReader &Record;
11640 ASTContext &Context;
11641
11642public:
11643 OMPClauseReader(ASTRecordReader &Record)
11644 : Record(Record), Context(Record.getContext()) {}
11645#define GEN_CLANG_CLAUSE_CLASS
11646#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11647#include "llvm/Frontend/OpenMP/OMP.inc"
11648 OMPClause *readClause();
11649 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11650 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11651};
11652
11653} // end namespace clang
11654
11655OMPClause *ASTRecordReader::readOMPClause() {
11656 return OMPClauseReader(*this).readClause();
11657}
11658
11659OMPClause *OMPClauseReader::readClause() {
11660 OMPClause *C = nullptr;
11661 switch (llvm::omp::Clause(Record.readInt())) {
11662 case llvm::omp::OMPC_if:
11663 C = new (Context) OMPIfClause();
11664 break;
11665 case llvm::omp::OMPC_final:
11666 C = new (Context) OMPFinalClause();
11667 break;
11668 case llvm::omp::OMPC_num_threads:
11669 C = OMPNumThreadsClause::CreateEmpty(C: Context, N: Record.readInt());
11670 break;
11671 case llvm::omp::OMPC_safelen:
11672 C = new (Context) OMPSafelenClause();
11673 break;
11674 case llvm::omp::OMPC_simdlen:
11675 C = new (Context) OMPSimdlenClause();
11676 break;
11677 case llvm::omp::OMPC_sizes: {
11678 unsigned NumSizes = Record.readInt();
11679 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11680 break;
11681 }
11682 case llvm::omp::OMPC_counts: {
11683 unsigned NumCounts = Record.readInt();
11684 C = OMPCountsClause::CreateEmpty(C: Context, NumCounts);
11685 break;
11686 }
11687 case llvm::omp::OMPC_permutation: {
11688 unsigned NumLoops = Record.readInt();
11689 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11690 break;
11691 }
11692 case llvm::omp::OMPC_full:
11693 C = new (Context) OMPFullClause();
11694 break;
11695 case llvm::omp::OMPC_partial:
11696 C = new (Context) OMPPartialClause();
11697 break;
11698 case llvm::omp::OMPC_depth:
11699 C = new (Context) OMPDepthClause();
11700 break;
11701 case llvm::omp::OMPC_looprange:
11702 C = new (Context) OMPLoopRangeClause();
11703 break;
11704 case llvm::omp::OMPC_allocator:
11705 C = new (Context) OMPAllocatorClause();
11706 break;
11707 case llvm::omp::OMPC_collapse:
11708 C = new (Context) OMPCollapseClause();
11709 break;
11710 case llvm::omp::OMPC_default:
11711 C = new (Context) OMPDefaultClause();
11712 break;
11713 case llvm::omp::OMPC_proc_bind:
11714 C = new (Context) OMPProcBindClause();
11715 break;
11716 case llvm::omp::OMPC_schedule:
11717 C = new (Context) OMPScheduleClause();
11718 break;
11719 case llvm::omp::OMPC_ordered:
11720 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11721 break;
11722 case llvm::omp::OMPC_nowait:
11723 C = new (Context) OMPNowaitClause();
11724 break;
11725 case llvm::omp::OMPC_untied:
11726 C = new (Context) OMPUntiedClause();
11727 break;
11728 case llvm::omp::OMPC_mergeable:
11729 C = new (Context) OMPMergeableClause();
11730 break;
11731 case llvm::omp::OMPC_threadset:
11732 C = new (Context) OMPThreadsetClause();
11733 break;
11734 case llvm::omp::OMPC_transparent:
11735 C = new (Context) OMPTransparentClause();
11736 break;
11737 case llvm::omp::OMPC_read:
11738 C = new (Context) OMPReadClause();
11739 break;
11740 case llvm::omp::OMPC_write:
11741 C = new (Context) OMPWriteClause();
11742 break;
11743 case llvm::omp::OMPC_update:
11744 C = new (Context) OMPUpdateClause();
11745 break;
11746 case llvm::omp::OMPC_update_depend_objects:
11747 C = OMPUpdateDependObjectsClause::CreateEmpty(C: Context);
11748 break;
11749 case llvm::omp::OMPC_capture:
11750 C = new (Context) OMPCaptureClause();
11751 break;
11752 case llvm::omp::OMPC_compare:
11753 C = new (Context) OMPCompareClause();
11754 break;
11755 case llvm::omp::OMPC_fail:
11756 C = new (Context) OMPFailClause();
11757 break;
11758 case llvm::omp::OMPC_seq_cst:
11759 C = new (Context) OMPSeqCstClause();
11760 break;
11761 case llvm::omp::OMPC_acq_rel:
11762 C = new (Context) OMPAcqRelClause();
11763 break;
11764 case llvm::omp::OMPC_absent: {
11765 unsigned NumKinds = Record.readInt();
11766 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11767 break;
11768 }
11769 case llvm::omp::OMPC_holds:
11770 C = new (Context) OMPHoldsClause();
11771 break;
11772 case llvm::omp::OMPC_contains: {
11773 unsigned NumKinds = Record.readInt();
11774 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11775 break;
11776 }
11777 case llvm::omp::OMPC_no_openmp:
11778 C = new (Context) OMPNoOpenMPClause();
11779 break;
11780 case llvm::omp::OMPC_no_openmp_routines:
11781 C = new (Context) OMPNoOpenMPRoutinesClause();
11782 break;
11783 case llvm::omp::OMPC_no_openmp_constructs:
11784 C = new (Context) OMPNoOpenMPConstructsClause();
11785 break;
11786 case llvm::omp::OMPC_no_parallelism:
11787 C = new (Context) OMPNoParallelismClause();
11788 break;
11789 case llvm::omp::OMPC_acquire:
11790 C = new (Context) OMPAcquireClause();
11791 break;
11792 case llvm::omp::OMPC_release:
11793 C = new (Context) OMPReleaseClause();
11794 break;
11795 case llvm::omp::OMPC_relaxed:
11796 C = new (Context) OMPRelaxedClause();
11797 break;
11798 case llvm::omp::OMPC_weak:
11799 C = new (Context) OMPWeakClause();
11800 break;
11801 case llvm::omp::OMPC_threads:
11802 C = new (Context) OMPThreadsClause();
11803 break;
11804 case llvm::omp::OMPC_simd:
11805 C = new (Context) OMPSIMDClause();
11806 break;
11807 case llvm::omp::OMPC_nogroup:
11808 C = new (Context) OMPNogroupClause();
11809 break;
11810 case llvm::omp::OMPC_unified_address:
11811 C = new (Context) OMPUnifiedAddressClause();
11812 break;
11813 case llvm::omp::OMPC_unified_shared_memory:
11814 C = new (Context) OMPUnifiedSharedMemoryClause();
11815 break;
11816 case llvm::omp::OMPC_reverse_offload:
11817 C = new (Context) OMPReverseOffloadClause();
11818 break;
11819 case llvm::omp::OMPC_dynamic_allocators:
11820 C = new (Context) OMPDynamicAllocatorsClause();
11821 break;
11822 case llvm::omp::OMPC_atomic_default_mem_order:
11823 C = new (Context) OMPAtomicDefaultMemOrderClause();
11824 break;
11825 case llvm::omp::OMPC_self_maps:
11826 C = new (Context) OMPSelfMapsClause();
11827 break;
11828 case llvm::omp::OMPC_at:
11829 C = new (Context) OMPAtClause();
11830 break;
11831 case llvm::omp::OMPC_severity:
11832 C = new (Context) OMPSeverityClause();
11833 break;
11834 case llvm::omp::OMPC_message:
11835 C = new (Context) OMPMessageClause();
11836 break;
11837 case llvm::omp::OMPC_private:
11838 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11839 break;
11840 case llvm::omp::OMPC_firstprivate:
11841 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11842 break;
11843 case llvm::omp::OMPC_lastprivate:
11844 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11845 break;
11846 case llvm::omp::OMPC_shared:
11847 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11848 break;
11849 case llvm::omp::OMPC_reduction: {
11850 unsigned N = Record.readInt();
11851 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11852 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11853 break;
11854 }
11855 case llvm::omp::OMPC_task_reduction:
11856 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11857 break;
11858 case llvm::omp::OMPC_in_reduction:
11859 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11860 break;
11861 case llvm::omp::OMPC_linear:
11862 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11863 break;
11864 case llvm::omp::OMPC_aligned:
11865 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11866 break;
11867 case llvm::omp::OMPC_copyin:
11868 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11869 break;
11870 case llvm::omp::OMPC_copyprivate:
11871 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11872 break;
11873 case llvm::omp::OMPC_flush:
11874 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11875 break;
11876 case llvm::omp::OMPC_depobj:
11877 C = new (Context) OMPDepobjClause();
11878 break;
11879 case llvm::omp::OMPC_depend: {
11880 unsigned NumVars = Record.readInt();
11881 unsigned NumLoops = Record.readInt();
11882 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11883 break;
11884 }
11885 case llvm::omp::OMPC_device:
11886 C = new (Context) OMPDeviceClause();
11887 break;
11888 case llvm::omp::OMPC_map: {
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 = OMPMapClause::CreateEmpty(C: Context, Sizes);
11895 break;
11896 }
11897 case llvm::omp::OMPC_num_teams:
11898 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11899 break;
11900 case llvm::omp::OMPC_thread_limit:
11901 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11902 break;
11903 case llvm::omp::OMPC_priority:
11904 C = new (Context) OMPPriorityClause();
11905 break;
11906 case llvm::omp::OMPC_grainsize:
11907 C = new (Context) OMPGrainsizeClause();
11908 break;
11909 case llvm::omp::OMPC_num_tasks:
11910 C = new (Context) OMPNumTasksClause();
11911 break;
11912 case llvm::omp::OMPC_hint:
11913 C = new (Context) OMPHintClause();
11914 break;
11915 case llvm::omp::OMPC_dist_schedule:
11916 C = new (Context) OMPDistScheduleClause();
11917 break;
11918 case llvm::omp::OMPC_defaultmap:
11919 C = new (Context) OMPDefaultmapClause();
11920 break;
11921 case llvm::omp::OMPC_to: {
11922 OMPMappableExprListSizeTy Sizes;
11923 Sizes.NumVars = Record.readInt();
11924 Sizes.NumUniqueDeclarations = Record.readInt();
11925 Sizes.NumComponentLists = Record.readInt();
11926 Sizes.NumComponents = Record.readInt();
11927 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11928 break;
11929 }
11930 case llvm::omp::OMPC_from: {
11931 OMPMappableExprListSizeTy Sizes;
11932 Sizes.NumVars = Record.readInt();
11933 Sizes.NumUniqueDeclarations = Record.readInt();
11934 Sizes.NumComponentLists = Record.readInt();
11935 Sizes.NumComponents = Record.readInt();
11936 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11937 break;
11938 }
11939 case llvm::omp::OMPC_use_device_ptr: {
11940 OMPMappableExprListSizeTy Sizes;
11941 Sizes.NumVars = Record.readInt();
11942 Sizes.NumUniqueDeclarations = Record.readInt();
11943 Sizes.NumComponentLists = Record.readInt();
11944 Sizes.NumComponents = Record.readInt();
11945 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11946 break;
11947 }
11948 case llvm::omp::OMPC_use_device_addr: {
11949 OMPMappableExprListSizeTy Sizes;
11950 Sizes.NumVars = Record.readInt();
11951 Sizes.NumUniqueDeclarations = Record.readInt();
11952 Sizes.NumComponentLists = Record.readInt();
11953 Sizes.NumComponents = Record.readInt();
11954 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11955 break;
11956 }
11957 case llvm::omp::OMPC_is_device_ptr: {
11958 OMPMappableExprListSizeTy Sizes;
11959 Sizes.NumVars = Record.readInt();
11960 Sizes.NumUniqueDeclarations = Record.readInt();
11961 Sizes.NumComponentLists = Record.readInt();
11962 Sizes.NumComponents = Record.readInt();
11963 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11964 break;
11965 }
11966 case llvm::omp::OMPC_has_device_addr: {
11967 OMPMappableExprListSizeTy Sizes;
11968 Sizes.NumVars = Record.readInt();
11969 Sizes.NumUniqueDeclarations = Record.readInt();
11970 Sizes.NumComponentLists = Record.readInt();
11971 Sizes.NumComponents = Record.readInt();
11972 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11973 break;
11974 }
11975 case llvm::omp::OMPC_allocate:
11976 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11977 break;
11978 case llvm::omp::OMPC_nontemporal:
11979 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11980 break;
11981 case llvm::omp::OMPC_inclusive:
11982 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11983 break;
11984 case llvm::omp::OMPC_exclusive:
11985 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11986 break;
11987 case llvm::omp::OMPC_order:
11988 C = new (Context) OMPOrderClause();
11989 break;
11990 case llvm::omp::OMPC_init: {
11991 unsigned VarListSize = Record.readInt();
11992 unsigned NumAttrs = Record.readInt();
11993 C = OMPInitClause::CreateEmpty(C: Context, /*NumPrefs=*/VarListSize - 1,
11994 NumAttrs);
11995 break;
11996 }
11997 case llvm::omp::OMPC_use:
11998 C = new (Context) OMPUseClause();
11999 break;
12000 case llvm::omp::OMPC_destroy:
12001 C = new (Context) OMPDestroyClause();
12002 break;
12003 case llvm::omp::OMPC_novariants:
12004 C = new (Context) OMPNovariantsClause();
12005 break;
12006 case llvm::omp::OMPC_nocontext:
12007 C = new (Context) OMPNocontextClause();
12008 break;
12009 case llvm::omp::OMPC_detach:
12010 C = new (Context) OMPDetachClause();
12011 break;
12012 case llvm::omp::OMPC_uses_allocators:
12013 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
12014 break;
12015 case llvm::omp::OMPC_affinity:
12016 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
12017 break;
12018 case llvm::omp::OMPC_filter:
12019 C = new (Context) OMPFilterClause();
12020 break;
12021 case llvm::omp::OMPC_bind:
12022 C = new (Context) OMPBindClause();
12023 break;
12024 case llvm::omp::OMPC_align:
12025 C = new (Context) OMPAlignClause();
12026 break;
12027 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
12028 C = new (Context) OMPXDynCGroupMemClause();
12029 break;
12030 case llvm::omp::OMPC_dyn_groupprivate:
12031 C = new (Context) OMPDynGroupprivateClause();
12032 break;
12033 case llvm::omp::OMPC_doacross: {
12034 unsigned NumVars = Record.readInt();
12035 unsigned NumLoops = Record.readInt();
12036 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
12037 break;
12038 }
12039 case llvm::omp::OMPC_ompx_attribute:
12040 C = new (Context) OMPXAttributeClause();
12041 break;
12042 case llvm::omp::OMPC_ompx_bare:
12043 C = new (Context) OMPXBareClause();
12044 break;
12045#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
12046 case llvm::omp::Enum: \
12047 break;
12048#include "llvm/Frontend/OpenMP/OMPKinds.def"
12049 default:
12050 break;
12051 }
12052 assert(C && "Unknown OMPClause type");
12053
12054 Visit(S: C);
12055 C->setLocStart(Record.readSourceLocation());
12056 C->setLocEnd(Record.readSourceLocation());
12057
12058 return C;
12059}
12060
12061void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12062 C->setPreInitStmt(S: Record.readSubStmt(),
12063 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
12064}
12065
12066void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12067 VisitOMPClauseWithPreInit(C);
12068 C->setPostUpdateExpr(Record.readSubExpr());
12069}
12070
12071void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12072 VisitOMPClauseWithPreInit(C);
12073 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12074 C->setNameModifierLoc(Record.readSourceLocation());
12075 C->setColonLoc(Record.readSourceLocation());
12076 C->setCondition(Record.readSubExpr());
12077 C->setLParenLoc(Record.readSourceLocation());
12078}
12079
12080void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12081 VisitOMPClauseWithPreInit(C);
12082 C->setCondition(Record.readSubExpr());
12083 C->setLParenLoc(Record.readSourceLocation());
12084}
12085
12086void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12087 C->setPrescriptivenessModifier(
12088 Record.readEnum<OpenMPNumThreadsClauseModifier>());
12089 C->setPrescriptivenessModifierLoc(Record.readSourceLocation());
12090 C->setDimsModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
12091 C->setDimsModifierLoc(Record.readSourceLocation());
12092 C->setDimsModifierExpr(Record.readSubExpr());
12093 VisitOMPClauseWithPreInit(C);
12094 C->setLParenLoc(Record.readSourceLocation());
12095 unsigned NumVars = C->varlist_size();
12096 SmallVector<Expr *, 16> Vars;
12097 Vars.reserve(N: NumVars);
12098 for (unsigned I = 0; I != NumVars; ++I)
12099 Vars.push_back(Elt: Record.readSubExpr());
12100 C->setVarRefs(Vars);
12101}
12102
12103void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12104 C->setSafelen(Record.readSubExpr());
12105 C->setLParenLoc(Record.readSourceLocation());
12106}
12107
12108void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12109 C->setSimdlen(Record.readSubExpr());
12110 C->setLParenLoc(Record.readSourceLocation());
12111}
12112
12113void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12114 for (Expr *&E : C->getSizesRefs())
12115 E = Record.readSubExpr();
12116 C->setLParenLoc(Record.readSourceLocation());
12117}
12118
12119void OMPClauseReader::VisitOMPCountsClause(OMPCountsClause *C) {
12120 bool HasFill = Record.readBool();
12121 if (HasFill)
12122 C->setOmpFillIndex(Record.readInt());
12123 C->setOmpFillLoc(Record.readSourceLocation());
12124 for (Expr *&E : C->getCountsRefs())
12125 E = Record.readSubExpr();
12126 C->setLParenLoc(Record.readSourceLocation());
12127}
12128
12129void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
12130 for (Expr *&E : C->getArgsRefs())
12131 E = Record.readSubExpr();
12132 C->setLParenLoc(Record.readSourceLocation());
12133}
12134
12135void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12136
12137void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12138 C->setFactor(Record.readSubExpr());
12139 C->setLParenLoc(Record.readSourceLocation());
12140}
12141
12142void OMPClauseReader::VisitOMPDepthClause(OMPDepthClause *C) {
12143 C->setDepth(Record.readSubExpr());
12144 C->setLParenLoc(Record.readSourceLocation());
12145}
12146
12147void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
12148 C->setFirst(Record.readSubExpr());
12149 C->setCount(Record.readSubExpr());
12150 C->setLParenLoc(Record.readSourceLocation());
12151 C->setFirstLoc(Record.readSourceLocation());
12152 C->setCountLoc(Record.readSourceLocation());
12153}
12154
12155void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12156 C->setAllocator(Record.readExpr());
12157 C->setLParenLoc(Record.readSourceLocation());
12158}
12159
12160void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12161 C->setNumForLoops(Record.readSubExpr());
12162 C->setLParenLoc(Record.readSourceLocation());
12163}
12164
12165void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12166 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12167 C->setLParenLoc(Record.readSourceLocation());
12168 C->setDefaultKindKwLoc(Record.readSourceLocation());
12169 C->setDefaultVariableCategory(
12170 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
12171 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
12172}
12173
12174// Read the parameter of threadset clause. This will have been saved when
12175// OMPClauseWriter is called.
12176void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
12177 C->setLParenLoc(Record.readSourceLocation());
12178 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
12179 C->setThreadsetKindLoc(ThreadsetKindLoc);
12180 OpenMPThreadsetKind TKind =
12181 static_cast<OpenMPThreadsetKind>(Record.readInt());
12182 C->setThreadsetKind(TKind);
12183}
12184
12185void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
12186 C->setLParenLoc(Record.readSourceLocation());
12187 C->setImpexTypeKind(Record.readSubExpr());
12188}
12189
12190void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12191 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12192 C->setLParenLoc(Record.readSourceLocation());
12193 C->setProcBindKindKwLoc(Record.readSourceLocation());
12194}
12195
12196void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12197 VisitOMPClauseWithPreInit(C);
12198 C->setScheduleKind(
12199 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12200 C->setFirstScheduleModifier(
12201 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12202 C->setSecondScheduleModifier(
12203 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12204 C->setChunkSize(Record.readSubExpr());
12205 C->setLParenLoc(Record.readSourceLocation());
12206 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12207 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12208 C->setScheduleKindLoc(Record.readSourceLocation());
12209 C->setCommaLoc(Record.readSourceLocation());
12210}
12211
12212void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12213 C->setNumForLoops(Record.readSubExpr());
12214 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12215 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
12216 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12217 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
12218 C->setLParenLoc(Record.readSourceLocation());
12219}
12220
12221void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12222 C->setEventHandler(Record.readSubExpr());
12223 C->setLParenLoc(Record.readSourceLocation());
12224}
12225
12226void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
12227 C->setCondition(Record.readSubExpr());
12228 C->setLParenLoc(Record.readSourceLocation());
12229}
12230
12231void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12232
12233void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12234
12235void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12236
12237void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12238
12239void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12240
12241void OMPClauseReader::VisitOMPUpdateDependObjectsClause(
12242 OMPUpdateDependObjectsClause *C) {
12243 C->setLParenLoc(Record.readSourceLocation());
12244 C->setArgumentLoc(Record.readSourceLocation());
12245 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12246}
12247
12248void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12249
12250void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12251
12252// Read the parameter of fail clause. This will have been saved when
12253// OMPClauseWriter is called.
12254void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
12255 C->setLParenLoc(Record.readSourceLocation());
12256 SourceLocation FailParameterLoc = Record.readSourceLocation();
12257 C->setFailParameterLoc(FailParameterLoc);
12258 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
12259 C->setFailParameter(CKind);
12260}
12261
12262void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
12263 unsigned Count = C->getDirectiveKinds().size();
12264 C->setLParenLoc(Record.readSourceLocation());
12265 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12266 DKVec.reserve(N: Count);
12267 for (unsigned I = 0; I < Count; I++) {
12268 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12269 }
12270 C->setDirectiveKinds(DKVec);
12271}
12272
12273void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
12274 C->setExpr(Record.readExpr());
12275 C->setLParenLoc(Record.readSourceLocation());
12276}
12277
12278void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
12279 unsigned Count = C->getDirectiveKinds().size();
12280 C->setLParenLoc(Record.readSourceLocation());
12281 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12282 DKVec.reserve(N: Count);
12283 for (unsigned I = 0; I < Count; I++) {
12284 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12285 }
12286 C->setDirectiveKinds(DKVec);
12287}
12288
12289void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12290
12291void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12292 OMPNoOpenMPRoutinesClause *) {}
12293
12294void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12295 OMPNoOpenMPConstructsClause *) {}
12296
12297void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12298
12299void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12300
12301void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12302
12303void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12304
12305void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12306
12307void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12308
12309void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12310
12311void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12312
12313void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12314
12315void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12316
12317void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12318 unsigned NumVars = C->varlist_size();
12319 SmallVector<Expr *, 16> Vars;
12320 Vars.reserve(N: NumVars);
12321 for (unsigned I = 0; I != NumVars; ++I)
12322 Vars.push_back(Elt: Record.readSubExpr());
12323 C->setVarRefs(Vars);
12324 C->setIsTarget(Record.readBool());
12325 C->setIsTargetSync(Record.readBool());
12326 C->setHasPreferAttrs(Record.readBool());
12327
12328 unsigned NumPrefs = C->varlist_size() - 1;
12329 SmallVector<unsigned, 4> Counts;
12330 SmallVector<Expr *, 8> Attrs;
12331 Counts.reserve(N: NumPrefs);
12332 for (unsigned I = 0; I < NumPrefs; ++I) {
12333 unsigned NA = Record.readInt();
12334 Counts.push_back(Elt: NA);
12335 for (unsigned J = 0; J < NA; ++J)
12336 Attrs.push_back(Elt: Record.readSubExpr());
12337 }
12338 C->setAttrs(Counts, Attrs);
12339
12340 C->setLParenLoc(Record.readSourceLocation());
12341 C->setVarLoc(Record.readSourceLocation());
12342}
12343
12344void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12345 C->setInteropVar(Record.readSubExpr());
12346 C->setLParenLoc(Record.readSourceLocation());
12347 C->setVarLoc(Record.readSourceLocation());
12348}
12349
12350void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12351 C->setInteropVar(Record.readSubExpr());
12352 C->setLParenLoc(Record.readSourceLocation());
12353 C->setVarLoc(Record.readSourceLocation());
12354}
12355
12356void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12357 VisitOMPClauseWithPreInit(C);
12358 C->setCondition(Record.readSubExpr());
12359 C->setLParenLoc(Record.readSourceLocation());
12360}
12361
12362void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12363 VisitOMPClauseWithPreInit(C);
12364 C->setCondition(Record.readSubExpr());
12365 C->setLParenLoc(Record.readSourceLocation());
12366}
12367
12368void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12369
12370void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12371 OMPUnifiedSharedMemoryClause *) {}
12372
12373void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12374
12375void
12376OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12377}
12378
12379void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12380 OMPAtomicDefaultMemOrderClause *C) {
12381 C->setAtomicDefaultMemOrderKind(
12382 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12383 C->setLParenLoc(Record.readSourceLocation());
12384 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12385}
12386
12387void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12388
12389void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12390 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12391 C->setLParenLoc(Record.readSourceLocation());
12392 C->setAtKindKwLoc(Record.readSourceLocation());
12393}
12394
12395void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12396 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12397 C->setLParenLoc(Record.readSourceLocation());
12398 C->setSeverityKindKwLoc(Record.readSourceLocation());
12399}
12400
12401void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12402 VisitOMPClauseWithPreInit(C);
12403 C->setMessageString(Record.readSubExpr());
12404 C->setLParenLoc(Record.readSourceLocation());
12405}
12406
12407void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12408 C->setLParenLoc(Record.readSourceLocation());
12409 unsigned NumVars = C->varlist_size();
12410 SmallVector<Expr *, 16> Vars;
12411 Vars.reserve(N: NumVars);
12412 for (unsigned i = 0; i != NumVars; ++i)
12413 Vars.push_back(Elt: Record.readSubExpr());
12414 C->setVarRefs(Vars);
12415 Vars.clear();
12416 for (unsigned i = 0; i != NumVars; ++i)
12417 Vars.push_back(Elt: Record.readSubExpr());
12418 C->setPrivateCopies(Vars);
12419}
12420
12421void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12422 VisitOMPClauseWithPreInit(C);
12423 C->setLParenLoc(Record.readSourceLocation());
12424 unsigned NumVars = C->varlist_size();
12425 SmallVector<Expr *, 16> Vars;
12426 Vars.reserve(N: NumVars);
12427 for (unsigned i = 0; i != NumVars; ++i)
12428 Vars.push_back(Elt: Record.readSubExpr());
12429 C->setVarRefs(Vars);
12430 Vars.clear();
12431 for (unsigned i = 0; i != NumVars; ++i)
12432 Vars.push_back(Elt: Record.readSubExpr());
12433 C->setPrivateCopies(Vars);
12434 Vars.clear();
12435 for (unsigned i = 0; i != NumVars; ++i)
12436 Vars.push_back(Elt: Record.readSubExpr());
12437 C->setInits(Vars);
12438}
12439
12440void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12441 VisitOMPClauseWithPostUpdate(C);
12442 C->setLParenLoc(Record.readSourceLocation());
12443 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12444 C->setKindLoc(Record.readSourceLocation());
12445 C->setColonLoc(Record.readSourceLocation());
12446 unsigned NumVars = C->varlist_size();
12447 SmallVector<Expr *, 16> Vars;
12448 Vars.reserve(N: NumVars);
12449 for (unsigned i = 0; i != NumVars; ++i)
12450 Vars.push_back(Elt: Record.readSubExpr());
12451 C->setVarRefs(Vars);
12452 Vars.clear();
12453 for (unsigned i = 0; i != NumVars; ++i)
12454 Vars.push_back(Elt: Record.readSubExpr());
12455 C->setPrivateCopies(Vars);
12456 Vars.clear();
12457 for (unsigned i = 0; i != NumVars; ++i)
12458 Vars.push_back(Elt: Record.readSubExpr());
12459 C->setSourceExprs(Vars);
12460 Vars.clear();
12461 for (unsigned i = 0; i != NumVars; ++i)
12462 Vars.push_back(Elt: Record.readSubExpr());
12463 C->setDestinationExprs(Vars);
12464 Vars.clear();
12465 for (unsigned i = 0; i != NumVars; ++i)
12466 Vars.push_back(Elt: Record.readSubExpr());
12467 C->setAssignmentOps(Vars);
12468}
12469
12470void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12471 C->setLParenLoc(Record.readSourceLocation());
12472 unsigned NumVars = C->varlist_size();
12473 SmallVector<Expr *, 16> Vars;
12474 Vars.reserve(N: NumVars);
12475 for (unsigned i = 0; i != NumVars; ++i)
12476 Vars.push_back(Elt: Record.readSubExpr());
12477 C->setVarRefs(Vars);
12478}
12479
12480void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12481 VisitOMPClauseWithPostUpdate(C);
12482 C->setLParenLoc(Record.readSourceLocation());
12483 C->setModifierLoc(Record.readSourceLocation());
12484 C->setColonLoc(Record.readSourceLocation());
12485 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12486 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12487 C->setQualifierLoc(NNSL);
12488 C->setNameInfo(DNI);
12489
12490 unsigned NumVars = C->varlist_size();
12491 SmallVector<Expr *, 16> Vars;
12492 Vars.reserve(N: NumVars);
12493 for (unsigned i = 0; i != NumVars; ++i)
12494 Vars.push_back(Elt: Record.readSubExpr());
12495 C->setVarRefs(Vars);
12496 Vars.clear();
12497 for (unsigned i = 0; i != NumVars; ++i)
12498 Vars.push_back(Elt: Record.readSubExpr());
12499 C->setPrivates(Vars);
12500 Vars.clear();
12501 for (unsigned i = 0; i != NumVars; ++i)
12502 Vars.push_back(Elt: Record.readSubExpr());
12503 C->setLHSExprs(Vars);
12504 Vars.clear();
12505 for (unsigned i = 0; i != NumVars; ++i)
12506 Vars.push_back(Elt: Record.readSubExpr());
12507 C->setRHSExprs(Vars);
12508 Vars.clear();
12509 for (unsigned i = 0; i != NumVars; ++i)
12510 Vars.push_back(Elt: Record.readSubExpr());
12511 C->setReductionOps(Vars);
12512 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12513 Vars.clear();
12514 for (unsigned i = 0; i != NumVars; ++i)
12515 Vars.push_back(Elt: Record.readSubExpr());
12516 C->setInscanCopyOps(Vars);
12517 Vars.clear();
12518 for (unsigned i = 0; i != NumVars; ++i)
12519 Vars.push_back(Elt: Record.readSubExpr());
12520 C->setInscanCopyArrayTemps(Vars);
12521 Vars.clear();
12522 for (unsigned i = 0; i != NumVars; ++i)
12523 Vars.push_back(Elt: Record.readSubExpr());
12524 C->setInscanCopyArrayElems(Vars);
12525 }
12526 unsigned NumFlags = Record.readInt();
12527 SmallVector<bool, 16> Flags;
12528 Flags.reserve(N: NumFlags);
12529 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12530 Flags.push_back(Elt: Record.readInt());
12531 C->setPrivateVariableReductionFlags(Flags);
12532}
12533
12534void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12535 VisitOMPClauseWithPostUpdate(C);
12536 C->setLParenLoc(Record.readSourceLocation());
12537 C->setColonLoc(Record.readSourceLocation());
12538 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12539 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12540 C->setQualifierLoc(NNSL);
12541 C->setNameInfo(DNI);
12542
12543 unsigned NumVars = C->varlist_size();
12544 SmallVector<Expr *, 16> Vars;
12545 Vars.reserve(N: NumVars);
12546 for (unsigned I = 0; I != NumVars; ++I)
12547 Vars.push_back(Elt: Record.readSubExpr());
12548 C->setVarRefs(Vars);
12549 Vars.clear();
12550 for (unsigned I = 0; I != NumVars; ++I)
12551 Vars.push_back(Elt: Record.readSubExpr());
12552 C->setPrivates(Vars);
12553 Vars.clear();
12554 for (unsigned I = 0; I != NumVars; ++I)
12555 Vars.push_back(Elt: Record.readSubExpr());
12556 C->setLHSExprs(Vars);
12557 Vars.clear();
12558 for (unsigned I = 0; I != NumVars; ++I)
12559 Vars.push_back(Elt: Record.readSubExpr());
12560 C->setRHSExprs(Vars);
12561 Vars.clear();
12562 for (unsigned I = 0; I != NumVars; ++I)
12563 Vars.push_back(Elt: Record.readSubExpr());
12564 C->setReductionOps(Vars);
12565}
12566
12567void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12568 VisitOMPClauseWithPostUpdate(C);
12569 C->setLParenLoc(Record.readSourceLocation());
12570 C->setColonLoc(Record.readSourceLocation());
12571 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12572 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12573 C->setQualifierLoc(NNSL);
12574 C->setNameInfo(DNI);
12575
12576 unsigned NumVars = C->varlist_size();
12577 SmallVector<Expr *, 16> Vars;
12578 Vars.reserve(N: NumVars);
12579 for (unsigned I = 0; I != NumVars; ++I)
12580 Vars.push_back(Elt: Record.readSubExpr());
12581 C->setVarRefs(Vars);
12582 Vars.clear();
12583 for (unsigned I = 0; I != NumVars; ++I)
12584 Vars.push_back(Elt: Record.readSubExpr());
12585 C->setPrivates(Vars);
12586 Vars.clear();
12587 for (unsigned I = 0; I != NumVars; ++I)
12588 Vars.push_back(Elt: Record.readSubExpr());
12589 C->setLHSExprs(Vars);
12590 Vars.clear();
12591 for (unsigned I = 0; I != NumVars; ++I)
12592 Vars.push_back(Elt: Record.readSubExpr());
12593 C->setRHSExprs(Vars);
12594 Vars.clear();
12595 for (unsigned I = 0; I != NumVars; ++I)
12596 Vars.push_back(Elt: Record.readSubExpr());
12597 C->setReductionOps(Vars);
12598 Vars.clear();
12599 for (unsigned I = 0; I != NumVars; ++I)
12600 Vars.push_back(Elt: Record.readSubExpr());
12601 C->setTaskgroupDescriptors(Vars);
12602}
12603
12604void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12605 VisitOMPClauseWithPostUpdate(C);
12606 C->setLParenLoc(Record.readSourceLocation());
12607 C->setColonLoc(Record.readSourceLocation());
12608 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12609 C->setModifierLoc(Record.readSourceLocation());
12610 unsigned NumVars = C->varlist_size();
12611 SmallVector<Expr *, 16> Vars;
12612 Vars.reserve(N: NumVars);
12613 for (unsigned i = 0; i != NumVars; ++i)
12614 Vars.push_back(Elt: Record.readSubExpr());
12615 C->setVarRefs(Vars);
12616 Vars.clear();
12617 for (unsigned i = 0; i != NumVars; ++i)
12618 Vars.push_back(Elt: Record.readSubExpr());
12619 C->setPrivates(Vars);
12620 Vars.clear();
12621 for (unsigned i = 0; i != NumVars; ++i)
12622 Vars.push_back(Elt: Record.readSubExpr());
12623 C->setInits(Vars);
12624 Vars.clear();
12625 for (unsigned i = 0; i != NumVars; ++i)
12626 Vars.push_back(Elt: Record.readSubExpr());
12627 C->setUpdates(Vars);
12628 Vars.clear();
12629 for (unsigned i = 0; i != NumVars; ++i)
12630 Vars.push_back(Elt: Record.readSubExpr());
12631 C->setFinals(Vars);
12632 C->setStep(Record.readSubExpr());
12633 C->setCalcStep(Record.readSubExpr());
12634 Vars.clear();
12635 for (unsigned I = 0; I != NumVars + 1; ++I)
12636 Vars.push_back(Elt: Record.readSubExpr());
12637 C->setUsedExprs(Vars);
12638}
12639
12640void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12641 C->setLParenLoc(Record.readSourceLocation());
12642 C->setColonLoc(Record.readSourceLocation());
12643 unsigned NumVars = C->varlist_size();
12644 SmallVector<Expr *, 16> Vars;
12645 Vars.reserve(N: NumVars);
12646 for (unsigned i = 0; i != NumVars; ++i)
12647 Vars.push_back(Elt: Record.readSubExpr());
12648 C->setVarRefs(Vars);
12649 C->setAlignment(Record.readSubExpr());
12650}
12651
12652void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12653 C->setLParenLoc(Record.readSourceLocation());
12654 unsigned NumVars = C->varlist_size();
12655 SmallVector<Expr *, 16> Exprs;
12656 Exprs.reserve(N: NumVars);
12657 for (unsigned i = 0; i != NumVars; ++i)
12658 Exprs.push_back(Elt: Record.readSubExpr());
12659 C->setVarRefs(Exprs);
12660 Exprs.clear();
12661 for (unsigned i = 0; i != NumVars; ++i)
12662 Exprs.push_back(Elt: Record.readSubExpr());
12663 C->setSourceExprs(Exprs);
12664 Exprs.clear();
12665 for (unsigned i = 0; i != NumVars; ++i)
12666 Exprs.push_back(Elt: Record.readSubExpr());
12667 C->setDestinationExprs(Exprs);
12668 Exprs.clear();
12669 for (unsigned i = 0; i != NumVars; ++i)
12670 Exprs.push_back(Elt: Record.readSubExpr());
12671 C->setAssignmentOps(Exprs);
12672}
12673
12674void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12675 C->setLParenLoc(Record.readSourceLocation());
12676 unsigned NumVars = C->varlist_size();
12677 SmallVector<Expr *, 16> Exprs;
12678 Exprs.reserve(N: NumVars);
12679 for (unsigned i = 0; i != NumVars; ++i)
12680 Exprs.push_back(Elt: Record.readSubExpr());
12681 C->setVarRefs(Exprs);
12682 Exprs.clear();
12683 for (unsigned i = 0; i != NumVars; ++i)
12684 Exprs.push_back(Elt: Record.readSubExpr());
12685 C->setSourceExprs(Exprs);
12686 Exprs.clear();
12687 for (unsigned i = 0; i != NumVars; ++i)
12688 Exprs.push_back(Elt: Record.readSubExpr());
12689 C->setDestinationExprs(Exprs);
12690 Exprs.clear();
12691 for (unsigned i = 0; i != NumVars; ++i)
12692 Exprs.push_back(Elt: Record.readSubExpr());
12693 C->setAssignmentOps(Exprs);
12694}
12695
12696void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12697 C->setLParenLoc(Record.readSourceLocation());
12698 unsigned NumVars = C->varlist_size();
12699 SmallVector<Expr *, 16> Vars;
12700 Vars.reserve(N: NumVars);
12701 for (unsigned i = 0; i != NumVars; ++i)
12702 Vars.push_back(Elt: Record.readSubExpr());
12703 C->setVarRefs(Vars);
12704}
12705
12706void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12707 C->setDepobj(Record.readSubExpr());
12708 C->setLParenLoc(Record.readSourceLocation());
12709}
12710
12711void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12712 C->setLParenLoc(Record.readSourceLocation());
12713 C->setModifier(Record.readSubExpr());
12714 C->setDependencyKind(
12715 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12716 C->setDependencyLoc(Record.readSourceLocation());
12717 C->setColonLoc(Record.readSourceLocation());
12718 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12719 unsigned NumVars = C->varlist_size();
12720 SmallVector<Expr *, 16> Vars;
12721 Vars.reserve(N: NumVars);
12722 for (unsigned I = 0; I != NumVars; ++I)
12723 Vars.push_back(Elt: Record.readSubExpr());
12724 C->setVarRefs(Vars);
12725 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12726 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12727}
12728
12729void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12730 VisitOMPClauseWithPreInit(C);
12731 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12732 C->setDevice(Record.readSubExpr());
12733 C->setModifierLoc(Record.readSourceLocation());
12734 C->setLParenLoc(Record.readSourceLocation());
12735}
12736
12737void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12738 C->setLParenLoc(Record.readSourceLocation());
12739 bool HasIteratorModifier = false;
12740 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12741 C->setMapTypeModifier(
12742 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12743 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12744 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12745 HasIteratorModifier = true;
12746 }
12747 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12748 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12749 C->setMapType(
12750 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12751 C->setMapLoc(Record.readSourceLocation());
12752 C->setColonLoc(Record.readSourceLocation());
12753 auto NumVars = C->varlist_size();
12754 auto UniqueDecls = C->getUniqueDeclarationsNum();
12755 auto TotalLists = C->getTotalComponentListNum();
12756 auto TotalComponents = C->getTotalComponentsNum();
12757
12758 SmallVector<Expr *, 16> Vars;
12759 Vars.reserve(N: NumVars);
12760 for (unsigned i = 0; i != NumVars; ++i)
12761 Vars.push_back(Elt: Record.readExpr());
12762 C->setVarRefs(Vars);
12763
12764 SmallVector<Expr *, 16> UDMappers;
12765 UDMappers.reserve(N: NumVars);
12766 for (unsigned I = 0; I < NumVars; ++I)
12767 UDMappers.push_back(Elt: Record.readExpr());
12768 C->setUDMapperRefs(UDMappers);
12769
12770 if (HasIteratorModifier)
12771 C->setIteratorModifier(Record.readExpr());
12772
12773 SmallVector<ValueDecl *, 16> Decls;
12774 Decls.reserve(N: UniqueDecls);
12775 for (unsigned i = 0; i < UniqueDecls; ++i)
12776 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12777 C->setUniqueDecls(Decls);
12778
12779 SmallVector<unsigned, 16> ListsPerDecl;
12780 ListsPerDecl.reserve(N: UniqueDecls);
12781 for (unsigned i = 0; i < UniqueDecls; ++i)
12782 ListsPerDecl.push_back(Elt: Record.readInt());
12783 C->setDeclNumLists(ListsPerDecl);
12784
12785 SmallVector<unsigned, 32> ListSizes;
12786 ListSizes.reserve(N: TotalLists);
12787 for (unsigned i = 0; i < TotalLists; ++i)
12788 ListSizes.push_back(Elt: Record.readInt());
12789 C->setComponentListSizes(ListSizes);
12790
12791 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12792 Components.reserve(N: TotalComponents);
12793 for (unsigned i = 0; i < TotalComponents; ++i) {
12794 Expr *AssociatedExprPr = Record.readExpr();
12795 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12796 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12797 /*IsNonContiguous=*/Args: false);
12798 }
12799 C->setComponents(Components, CLSs: ListSizes);
12800}
12801
12802void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12803 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12804 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12805 C->setLParenLoc(Record.readSourceLocation());
12806 C->setColonLoc(Record.readSourceLocation());
12807 C->setAllocator(Record.readSubExpr());
12808 C->setAlignment(Record.readSubExpr());
12809 unsigned NumVars = C->varlist_size();
12810 SmallVector<Expr *, 16> Vars;
12811 Vars.reserve(N: NumVars);
12812 for (unsigned i = 0; i != NumVars; ++i)
12813 Vars.push_back(Elt: Record.readSubExpr());
12814 C->setVarRefs(Vars);
12815}
12816
12817void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12818 C->setModifier(Record.readEnum<OpenMPNumTeamsClauseModifier>());
12819 C->setModifierLoc(Record.readSourceLocation());
12820 C->setModifierExpr(Record.readSubExpr());
12821 VisitOMPClauseWithPreInit(C);
12822 C->setLParenLoc(Record.readSourceLocation());
12823 unsigned NumVars = C->varlist_size();
12824 SmallVector<Expr *, 16> Vars;
12825 Vars.reserve(N: NumVars);
12826 for (unsigned I = 0; I != NumVars; ++I)
12827 Vars.push_back(Elt: Record.readSubExpr());
12828 C->setVarRefs(Vars);
12829}
12830
12831void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12832 C->setModifier(Record.readEnum<OpenMPThreadLimitClauseModifier>());
12833 C->setModifierLoc(Record.readSourceLocation());
12834 C->setModifierExpr(Record.readSubExpr());
12835 VisitOMPClauseWithPreInit(C);
12836 C->setLParenLoc(Record.readSourceLocation());
12837 unsigned NumVars = C->varlist_size();
12838 SmallVector<Expr *, 16> Vars;
12839 Vars.reserve(N: NumVars);
12840 for (unsigned I = 0; I != NumVars; ++I)
12841 Vars.push_back(Elt: Record.readSubExpr());
12842 C->setVarRefs(Vars);
12843}
12844
12845void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12846 VisitOMPClauseWithPreInit(C);
12847 C->setPriority(Record.readSubExpr());
12848 C->setLParenLoc(Record.readSourceLocation());
12849}
12850
12851void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12852 VisitOMPClauseWithPreInit(C);
12853 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12854 C->setGrainsize(Record.readSubExpr());
12855 C->setModifierLoc(Record.readSourceLocation());
12856 C->setLParenLoc(Record.readSourceLocation());
12857}
12858
12859void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12860 VisitOMPClauseWithPreInit(C);
12861 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12862 C->setNumTasks(Record.readSubExpr());
12863 C->setModifierLoc(Record.readSourceLocation());
12864 C->setLParenLoc(Record.readSourceLocation());
12865}
12866
12867void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12868 C->setHint(Record.readSubExpr());
12869 C->setLParenLoc(Record.readSourceLocation());
12870}
12871
12872void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12873 VisitOMPClauseWithPreInit(C);
12874 C->setDistScheduleKind(
12875 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12876 C->setChunkSize(Record.readSubExpr());
12877 C->setLParenLoc(Record.readSourceLocation());
12878 C->setDistScheduleKindLoc(Record.readSourceLocation());
12879 C->setCommaLoc(Record.readSourceLocation());
12880}
12881
12882void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12883 C->setDefaultmapKind(
12884 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12885 C->setDefaultmapModifier(
12886 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12887 C->setLParenLoc(Record.readSourceLocation());
12888 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12889 C->setDefaultmapKindLoc(Record.readSourceLocation());
12890}
12891
12892void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12893 C->setLParenLoc(Record.readSourceLocation());
12894 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12895 C->setMotionModifier(
12896 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12897 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12898 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12899 C->setIteratorModifier(Record.readExpr());
12900 }
12901 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12902 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12903 C->setColonLoc(Record.readSourceLocation());
12904 auto NumVars = C->varlist_size();
12905 auto UniqueDecls = C->getUniqueDeclarationsNum();
12906 auto TotalLists = C->getTotalComponentListNum();
12907 auto TotalComponents = C->getTotalComponentsNum();
12908
12909 SmallVector<Expr *, 16> Vars;
12910 Vars.reserve(N: NumVars);
12911 for (unsigned i = 0; i != NumVars; ++i)
12912 Vars.push_back(Elt: Record.readSubExpr());
12913 C->setVarRefs(Vars);
12914
12915 SmallVector<Expr *, 16> UDMappers;
12916 UDMappers.reserve(N: NumVars);
12917 for (unsigned I = 0; I < NumVars; ++I)
12918 UDMappers.push_back(Elt: Record.readSubExpr());
12919 C->setUDMapperRefs(UDMappers);
12920
12921 SmallVector<ValueDecl *, 16> Decls;
12922 Decls.reserve(N: UniqueDecls);
12923 for (unsigned i = 0; i < UniqueDecls; ++i)
12924 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12925 C->setUniqueDecls(Decls);
12926
12927 SmallVector<unsigned, 16> ListsPerDecl;
12928 ListsPerDecl.reserve(N: UniqueDecls);
12929 for (unsigned i = 0; i < UniqueDecls; ++i)
12930 ListsPerDecl.push_back(Elt: Record.readInt());
12931 C->setDeclNumLists(ListsPerDecl);
12932
12933 SmallVector<unsigned, 32> ListSizes;
12934 ListSizes.reserve(N: TotalLists);
12935 for (unsigned i = 0; i < TotalLists; ++i)
12936 ListSizes.push_back(Elt: Record.readInt());
12937 C->setComponentListSizes(ListSizes);
12938
12939 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12940 Components.reserve(N: TotalComponents);
12941 for (unsigned i = 0; i < TotalComponents; ++i) {
12942 Expr *AssociatedExprPr = Record.readSubExpr();
12943 bool IsNonContiguous = Record.readBool();
12944 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12945 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12946 }
12947 C->setComponents(Components, CLSs: ListSizes);
12948}
12949
12950void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12951 C->setLParenLoc(Record.readSourceLocation());
12952 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12953 C->setMotionModifier(
12954 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12955 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12956 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12957 C->setIteratorModifier(Record.readExpr());
12958 }
12959 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12960 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12961 C->setColonLoc(Record.readSourceLocation());
12962 auto NumVars = C->varlist_size();
12963 auto UniqueDecls = C->getUniqueDeclarationsNum();
12964 auto TotalLists = C->getTotalComponentListNum();
12965 auto TotalComponents = C->getTotalComponentsNum();
12966
12967 SmallVector<Expr *, 16> Vars;
12968 Vars.reserve(N: NumVars);
12969 for (unsigned i = 0; i != NumVars; ++i)
12970 Vars.push_back(Elt: Record.readSubExpr());
12971 C->setVarRefs(Vars);
12972
12973 SmallVector<Expr *, 16> UDMappers;
12974 UDMappers.reserve(N: NumVars);
12975 for (unsigned I = 0; I < NumVars; ++I)
12976 UDMappers.push_back(Elt: Record.readSubExpr());
12977 C->setUDMapperRefs(UDMappers);
12978
12979 SmallVector<ValueDecl *, 16> Decls;
12980 Decls.reserve(N: UniqueDecls);
12981 for (unsigned i = 0; i < UniqueDecls; ++i)
12982 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12983 C->setUniqueDecls(Decls);
12984
12985 SmallVector<unsigned, 16> ListsPerDecl;
12986 ListsPerDecl.reserve(N: UniqueDecls);
12987 for (unsigned i = 0; i < UniqueDecls; ++i)
12988 ListsPerDecl.push_back(Elt: Record.readInt());
12989 C->setDeclNumLists(ListsPerDecl);
12990
12991 SmallVector<unsigned, 32> ListSizes;
12992 ListSizes.reserve(N: TotalLists);
12993 for (unsigned i = 0; i < TotalLists; ++i)
12994 ListSizes.push_back(Elt: Record.readInt());
12995 C->setComponentListSizes(ListSizes);
12996
12997 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12998 Components.reserve(N: TotalComponents);
12999 for (unsigned i = 0; i < TotalComponents; ++i) {
13000 Expr *AssociatedExprPr = Record.readSubExpr();
13001 bool IsNonContiguous = Record.readBool();
13002 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13003 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
13004 }
13005 C->setComponents(Components, CLSs: ListSizes);
13006}
13007
13008void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13009 C->setLParenLoc(Record.readSourceLocation());
13010 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
13011 C->setFallbackModifierLoc(Record.readSourceLocation());
13012 auto NumVars = C->varlist_size();
13013 auto UniqueDecls = C->getUniqueDeclarationsNum();
13014 auto TotalLists = C->getTotalComponentListNum();
13015 auto TotalComponents = C->getTotalComponentsNum();
13016
13017 SmallVector<Expr *, 16> Vars;
13018 Vars.reserve(N: NumVars);
13019 for (unsigned i = 0; i != NumVars; ++i)
13020 Vars.push_back(Elt: Record.readSubExpr());
13021 C->setVarRefs(Vars);
13022 Vars.clear();
13023 for (unsigned i = 0; i != NumVars; ++i)
13024 Vars.push_back(Elt: Record.readSubExpr());
13025 C->setPrivateCopies(Vars);
13026 Vars.clear();
13027 for (unsigned i = 0; i != NumVars; ++i)
13028 Vars.push_back(Elt: Record.readSubExpr());
13029 C->setInits(Vars);
13030
13031 SmallVector<ValueDecl *, 16> Decls;
13032 Decls.reserve(N: UniqueDecls);
13033 for (unsigned i = 0; i < UniqueDecls; ++i)
13034 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13035 C->setUniqueDecls(Decls);
13036
13037 SmallVector<unsigned, 16> ListsPerDecl;
13038 ListsPerDecl.reserve(N: UniqueDecls);
13039 for (unsigned i = 0; i < UniqueDecls; ++i)
13040 ListsPerDecl.push_back(Elt: Record.readInt());
13041 C->setDeclNumLists(ListsPerDecl);
13042
13043 SmallVector<unsigned, 32> ListSizes;
13044 ListSizes.reserve(N: TotalLists);
13045 for (unsigned i = 0; i < TotalLists; ++i)
13046 ListSizes.push_back(Elt: Record.readInt());
13047 C->setComponentListSizes(ListSizes);
13048
13049 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13050 Components.reserve(N: TotalComponents);
13051 for (unsigned i = 0; i < TotalComponents; ++i) {
13052 auto *AssociatedExprPr = Record.readSubExpr();
13053 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13054 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
13055 /*IsNonContiguous=*/Args: false);
13056 }
13057 C->setComponents(Components, CLSs: ListSizes);
13058}
13059
13060void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
13061 C->setLParenLoc(Record.readSourceLocation());
13062 auto NumVars = C->varlist_size();
13063 auto UniqueDecls = C->getUniqueDeclarationsNum();
13064 auto TotalLists = C->getTotalComponentListNum();
13065 auto TotalComponents = C->getTotalComponentsNum();
13066
13067 SmallVector<Expr *, 16> Vars;
13068 Vars.reserve(N: NumVars);
13069 for (unsigned i = 0; i != NumVars; ++i)
13070 Vars.push_back(Elt: Record.readSubExpr());
13071 C->setVarRefs(Vars);
13072
13073 SmallVector<ValueDecl *, 16> Decls;
13074 Decls.reserve(N: UniqueDecls);
13075 for (unsigned i = 0; i < UniqueDecls; ++i)
13076 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13077 C->setUniqueDecls(Decls);
13078
13079 SmallVector<unsigned, 16> ListsPerDecl;
13080 ListsPerDecl.reserve(N: UniqueDecls);
13081 for (unsigned i = 0; i < UniqueDecls; ++i)
13082 ListsPerDecl.push_back(Elt: Record.readInt());
13083 C->setDeclNumLists(ListsPerDecl);
13084
13085 SmallVector<unsigned, 32> ListSizes;
13086 ListSizes.reserve(N: TotalLists);
13087 for (unsigned i = 0; i < TotalLists; ++i)
13088 ListSizes.push_back(Elt: Record.readInt());
13089 C->setComponentListSizes(ListSizes);
13090
13091 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13092 Components.reserve(N: TotalComponents);
13093 for (unsigned i = 0; i < TotalComponents; ++i) {
13094 Expr *AssociatedExpr = Record.readSubExpr();
13095 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13096 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13097 /*IsNonContiguous*/ Args: false);
13098 }
13099 C->setComponents(Components, CLSs: ListSizes);
13100}
13101
13102void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13103 C->setLParenLoc(Record.readSourceLocation());
13104 auto NumVars = C->varlist_size();
13105 auto UniqueDecls = C->getUniqueDeclarationsNum();
13106 auto TotalLists = C->getTotalComponentListNum();
13107 auto TotalComponents = C->getTotalComponentsNum();
13108
13109 SmallVector<Expr *, 16> Vars;
13110 Vars.reserve(N: NumVars);
13111 for (unsigned i = 0; i != NumVars; ++i)
13112 Vars.push_back(Elt: Record.readSubExpr());
13113 C->setVarRefs(Vars);
13114 Vars.clear();
13115
13116 SmallVector<ValueDecl *, 16> Decls;
13117 Decls.reserve(N: UniqueDecls);
13118 for (unsigned i = 0; i < UniqueDecls; ++i)
13119 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13120 C->setUniqueDecls(Decls);
13121
13122 SmallVector<unsigned, 16> ListsPerDecl;
13123 ListsPerDecl.reserve(N: UniqueDecls);
13124 for (unsigned i = 0; i < UniqueDecls; ++i)
13125 ListsPerDecl.push_back(Elt: Record.readInt());
13126 C->setDeclNumLists(ListsPerDecl);
13127
13128 SmallVector<unsigned, 32> ListSizes;
13129 ListSizes.reserve(N: TotalLists);
13130 for (unsigned i = 0; i < TotalLists; ++i)
13131 ListSizes.push_back(Elt: Record.readInt());
13132 C->setComponentListSizes(ListSizes);
13133
13134 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13135 Components.reserve(N: TotalComponents);
13136 for (unsigned i = 0; i < TotalComponents; ++i) {
13137 Expr *AssociatedExpr = Record.readSubExpr();
13138 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13139 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13140 /*IsNonContiguous=*/Args: false);
13141 }
13142 C->setComponents(Components, CLSs: ListSizes);
13143}
13144
13145void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
13146 C->setLParenLoc(Record.readSourceLocation());
13147 auto NumVars = C->varlist_size();
13148 auto UniqueDecls = C->getUniqueDeclarationsNum();
13149 auto TotalLists = C->getTotalComponentListNum();
13150 auto TotalComponents = C->getTotalComponentsNum();
13151
13152 SmallVector<Expr *, 16> Vars;
13153 Vars.reserve(N: NumVars);
13154 for (unsigned I = 0; I != NumVars; ++I)
13155 Vars.push_back(Elt: Record.readSubExpr());
13156 C->setVarRefs(Vars);
13157 Vars.clear();
13158
13159 SmallVector<ValueDecl *, 16> Decls;
13160 Decls.reserve(N: UniqueDecls);
13161 for (unsigned I = 0; I < UniqueDecls; ++I)
13162 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13163 C->setUniqueDecls(Decls);
13164
13165 SmallVector<unsigned, 16> ListsPerDecl;
13166 ListsPerDecl.reserve(N: UniqueDecls);
13167 for (unsigned I = 0; I < UniqueDecls; ++I)
13168 ListsPerDecl.push_back(Elt: Record.readInt());
13169 C->setDeclNumLists(ListsPerDecl);
13170
13171 SmallVector<unsigned, 32> ListSizes;
13172 ListSizes.reserve(N: TotalLists);
13173 for (unsigned i = 0; i < TotalLists; ++i)
13174 ListSizes.push_back(Elt: Record.readInt());
13175 C->setComponentListSizes(ListSizes);
13176
13177 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13178 Components.reserve(N: TotalComponents);
13179 for (unsigned I = 0; I < TotalComponents; ++I) {
13180 Expr *AssociatedExpr = Record.readSubExpr();
13181 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13182 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13183 /*IsNonContiguous=*/Args: false);
13184 }
13185 C->setComponents(Components, CLSs: ListSizes);
13186}
13187
13188void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
13189 C->setLParenLoc(Record.readSourceLocation());
13190 unsigned NumVars = C->varlist_size();
13191 SmallVector<Expr *, 16> Vars;
13192 Vars.reserve(N: NumVars);
13193 for (unsigned i = 0; i != NumVars; ++i)
13194 Vars.push_back(Elt: Record.readSubExpr());
13195 C->setVarRefs(Vars);
13196 Vars.clear();
13197 Vars.reserve(N: NumVars);
13198 for (unsigned i = 0; i != NumVars; ++i)
13199 Vars.push_back(Elt: Record.readSubExpr());
13200 C->setPrivateRefs(Vars);
13201}
13202
13203void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
13204 C->setLParenLoc(Record.readSourceLocation());
13205 unsigned NumVars = C->varlist_size();
13206 SmallVector<Expr *, 16> Vars;
13207 Vars.reserve(N: NumVars);
13208 for (unsigned i = 0; i != NumVars; ++i)
13209 Vars.push_back(Elt: Record.readSubExpr());
13210 C->setVarRefs(Vars);
13211}
13212
13213void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
13214 C->setLParenLoc(Record.readSourceLocation());
13215 unsigned NumVars = C->varlist_size();
13216 SmallVector<Expr *, 16> Vars;
13217 Vars.reserve(N: NumVars);
13218 for (unsigned i = 0; i != NumVars; ++i)
13219 Vars.push_back(Elt: Record.readSubExpr());
13220 C->setVarRefs(Vars);
13221}
13222
13223void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13224 C->setLParenLoc(Record.readSourceLocation());
13225 unsigned NumOfAllocators = C->getNumberOfAllocators();
13226 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13227 Data.reserve(N: NumOfAllocators);
13228 for (unsigned I = 0; I != NumOfAllocators; ++I) {
13229 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13230 D.Allocator = Record.readSubExpr();
13231 D.AllocatorTraits = Record.readSubExpr();
13232 D.LParenLoc = Record.readSourceLocation();
13233 D.RParenLoc = Record.readSourceLocation();
13234 }
13235 C->setAllocatorsData(Data);
13236}
13237
13238void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13239 C->setLParenLoc(Record.readSourceLocation());
13240 C->setModifier(Record.readSubExpr());
13241 C->setColonLoc(Record.readSourceLocation());
13242 unsigned NumOfLocators = C->varlist_size();
13243 SmallVector<Expr *, 4> Locators;
13244 Locators.reserve(N: NumOfLocators);
13245 for (unsigned I = 0; I != NumOfLocators; ++I)
13246 Locators.push_back(Elt: Record.readSubExpr());
13247 C->setVarRefs(Locators);
13248}
13249
13250void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13251 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13252 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
13253 C->setLParenLoc(Record.readSourceLocation());
13254 C->setKindKwLoc(Record.readSourceLocation());
13255 C->setModifierKwLoc(Record.readSourceLocation());
13256}
13257
13258void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
13259 VisitOMPClauseWithPreInit(C);
13260 C->setThreadID(Record.readSubExpr());
13261 C->setLParenLoc(Record.readSourceLocation());
13262}
13263
13264void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13265 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13266 C->setLParenLoc(Record.readSourceLocation());
13267 C->setBindKindLoc(Record.readSourceLocation());
13268}
13269
13270void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13271 C->setAlignment(Record.readExpr());
13272 C->setLParenLoc(Record.readSourceLocation());
13273}
13274
13275void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
13276 VisitOMPClauseWithPreInit(C);
13277 C->setSize(Record.readSubExpr());
13278 C->setLParenLoc(Record.readSourceLocation());
13279}
13280
13281void OMPClauseReader::VisitOMPDynGroupprivateClause(
13282 OMPDynGroupprivateClause *C) {
13283 VisitOMPClauseWithPreInit(C);
13284 C->setDynGroupprivateModifier(
13285 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
13286 C->setDynGroupprivateFallbackModifier(
13287 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
13288 C->setSize(Record.readSubExpr());
13289 C->setLParenLoc(Record.readSourceLocation());
13290 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
13291 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
13292}
13293
13294void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
13295 C->setLParenLoc(Record.readSourceLocation());
13296 C->setDependenceType(
13297 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
13298 C->setDependenceLoc(Record.readSourceLocation());
13299 C->setColonLoc(Record.readSourceLocation());
13300 unsigned NumVars = C->varlist_size();
13301 SmallVector<Expr *, 16> Vars;
13302 Vars.reserve(N: NumVars);
13303 for (unsigned I = 0; I != NumVars; ++I)
13304 Vars.push_back(Elt: Record.readSubExpr());
13305 C->setVarRefs(Vars);
13306 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13307 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13308}
13309
13310void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13311 AttrVec Attrs;
13312 Record.readAttributes(Attrs);
13313 C->setAttrs(Attrs);
13314 C->setLocStart(Record.readSourceLocation());
13315 C->setLParenLoc(Record.readSourceLocation());
13316 C->setLocEnd(Record.readSourceLocation());
13317}
13318
13319void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13320
13321OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13322 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13323 TI.Sets.resize(N: readUInt32());
13324 for (auto &Set : TI.Sets) {
13325 Set.Kind = readEnum<llvm::omp::TraitSet>();
13326 Set.Selectors.resize(N: readUInt32());
13327 for (auto &Selector : Set.Selectors) {
13328 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13329 Selector.ScoreOrCondition = nullptr;
13330 if (readBool())
13331 Selector.ScoreOrCondition = readExprRef();
13332 Selector.Properties.resize(N: readUInt32());
13333 for (auto &Property : Selector.Properties)
13334 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13335 }
13336 }
13337 return &TI;
13338}
13339
13340void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13341 if (!Data)
13342 return;
13343 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13344 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13345 skipInts(N: 3);
13346 }
13347 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13348 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13349 Clauses[I] = readOMPClause();
13350 Data->setClauses(Clauses);
13351 if (Data->hasAssociatedStmt())
13352 Data->setAssociatedStmt(readStmt());
13353 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13354 Data->getChildren()[I] = readStmt();
13355}
13356
13357SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13358 unsigned NumVars = readInt();
13359 llvm::SmallVector<Expr *> VarList;
13360 for (unsigned I = 0; I < NumVars; ++I)
13361 VarList.push_back(Elt: readExpr());
13362 return VarList;
13363}
13364
13365SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13366 unsigned NumExprs = readInt();
13367 llvm::SmallVector<Expr *> ExprList;
13368 for (unsigned I = 0; I < NumExprs; ++I)
13369 ExprList.push_back(Elt: readSubExpr());
13370 return ExprList;
13371}
13372
13373OpenACCClause *ASTRecordReader::readOpenACCClause() {
13374 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13375 SourceLocation BeginLoc = readSourceLocation();
13376 SourceLocation EndLoc = readSourceLocation();
13377
13378 switch (ClauseKind) {
13379 case OpenACCClauseKind::Default: {
13380 SourceLocation LParenLoc = readSourceLocation();
13381 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13382 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13383 EndLoc);
13384 }
13385 case OpenACCClauseKind::If: {
13386 SourceLocation LParenLoc = readSourceLocation();
13387 Expr *CondExpr = readSubExpr();
13388 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13389 EndLoc);
13390 }
13391 case OpenACCClauseKind::Self: {
13392 SourceLocation LParenLoc = readSourceLocation();
13393 bool isConditionExprClause = readBool();
13394 if (isConditionExprClause) {
13395 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13396 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13397 ConditionExpr: CondExpr, EndLoc);
13398 }
13399 unsigned NumVars = readInt();
13400 llvm::SmallVector<Expr *> VarList;
13401 for (unsigned I = 0; I < NumVars; ++I)
13402 VarList.push_back(Elt: readSubExpr());
13403 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13404 EndLoc);
13405 }
13406 case OpenACCClauseKind::NumGangs: {
13407 SourceLocation LParenLoc = readSourceLocation();
13408 unsigned NumClauses = readInt();
13409 llvm::SmallVector<Expr *> IntExprs;
13410 for (unsigned I = 0; I < NumClauses; ++I)
13411 IntExprs.push_back(Elt: readSubExpr());
13412 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13413 IntExprs, EndLoc);
13414 }
13415 case OpenACCClauseKind::NumWorkers: {
13416 SourceLocation LParenLoc = readSourceLocation();
13417 Expr *IntExpr = readSubExpr();
13418 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13419 IntExpr, EndLoc);
13420 }
13421 case OpenACCClauseKind::DeviceNum: {
13422 SourceLocation LParenLoc = readSourceLocation();
13423 Expr *IntExpr = readSubExpr();
13424 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13425 IntExpr, EndLoc);
13426 }
13427 case OpenACCClauseKind::DefaultAsync: {
13428 SourceLocation LParenLoc = readSourceLocation();
13429 Expr *IntExpr = readSubExpr();
13430 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13431 IntExpr, EndLoc);
13432 }
13433 case OpenACCClauseKind::VectorLength: {
13434 SourceLocation LParenLoc = readSourceLocation();
13435 Expr *IntExpr = readSubExpr();
13436 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13437 IntExpr, EndLoc);
13438 }
13439 case OpenACCClauseKind::Private: {
13440 SourceLocation LParenLoc = readSourceLocation();
13441 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13442
13443 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13444 for (unsigned I = 0; I < VarList.size(); ++I) {
13445 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13446 VarDecl *Alloca = readDeclAs<VarDecl>();
13447 RecipeList.push_back(Elt: {Alloca});
13448 }
13449
13450 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13451 VarList, InitRecipes: RecipeList, EndLoc);
13452 }
13453 case OpenACCClauseKind::Host: {
13454 SourceLocation LParenLoc = readSourceLocation();
13455 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13456 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13457 EndLoc);
13458 }
13459 case OpenACCClauseKind::Device: {
13460 SourceLocation LParenLoc = readSourceLocation();
13461 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13462 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13463 VarList, EndLoc);
13464 }
13465 case OpenACCClauseKind::FirstPrivate: {
13466 SourceLocation LParenLoc = readSourceLocation();
13467 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13468 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13469 for (unsigned I = 0; I < VarList.size(); ++I) {
13470 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13471 VarDecl *Recipe = readDeclAs<VarDecl>();
13472 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13473 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13474 }
13475
13476 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13477 VarList, InitRecipes: RecipeList, EndLoc);
13478 }
13479 case OpenACCClauseKind::Attach: {
13480 SourceLocation LParenLoc = readSourceLocation();
13481 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13482 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13483 VarList, EndLoc);
13484 }
13485 case OpenACCClauseKind::Detach: {
13486 SourceLocation LParenLoc = readSourceLocation();
13487 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13488 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13489 VarList, EndLoc);
13490 }
13491 case OpenACCClauseKind::Delete: {
13492 SourceLocation LParenLoc = readSourceLocation();
13493 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13494 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13495 VarList, EndLoc);
13496 }
13497 case OpenACCClauseKind::UseDevice: {
13498 SourceLocation LParenLoc = readSourceLocation();
13499 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13500 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13501 VarList, EndLoc);
13502 }
13503 case OpenACCClauseKind::DevicePtr: {
13504 SourceLocation LParenLoc = readSourceLocation();
13505 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13506 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13507 VarList, EndLoc);
13508 }
13509 case OpenACCClauseKind::NoCreate: {
13510 SourceLocation LParenLoc = readSourceLocation();
13511 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13512 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13513 VarList, EndLoc);
13514 }
13515 case OpenACCClauseKind::Present: {
13516 SourceLocation LParenLoc = readSourceLocation();
13517 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13518 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13519 VarList, EndLoc);
13520 }
13521 case OpenACCClauseKind::PCopy:
13522 case OpenACCClauseKind::PresentOrCopy:
13523 case OpenACCClauseKind::Copy: {
13524 SourceLocation LParenLoc = readSourceLocation();
13525 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13526 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13527 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13528 LParenLoc, Mods: ModList, VarList, EndLoc);
13529 }
13530 case OpenACCClauseKind::CopyIn:
13531 case OpenACCClauseKind::PCopyIn:
13532 case OpenACCClauseKind::PresentOrCopyIn: {
13533 SourceLocation LParenLoc = readSourceLocation();
13534 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13535 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13536 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13537 LParenLoc, Mods: ModList, VarList, EndLoc);
13538 }
13539 case OpenACCClauseKind::CopyOut:
13540 case OpenACCClauseKind::PCopyOut:
13541 case OpenACCClauseKind::PresentOrCopyOut: {
13542 SourceLocation LParenLoc = readSourceLocation();
13543 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13544 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13545 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13546 LParenLoc, Mods: ModList, VarList, EndLoc);
13547 }
13548 case OpenACCClauseKind::Create:
13549 case OpenACCClauseKind::PCreate:
13550 case OpenACCClauseKind::PresentOrCreate: {
13551 SourceLocation LParenLoc = readSourceLocation();
13552 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13553 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13554 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13555 LParenLoc, Mods: ModList, VarList, EndLoc);
13556 }
13557 case OpenACCClauseKind::Async: {
13558 SourceLocation LParenLoc = readSourceLocation();
13559 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13560 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13561 IntExpr: AsyncExpr, EndLoc);
13562 }
13563 case OpenACCClauseKind::Wait: {
13564 SourceLocation LParenLoc = readSourceLocation();
13565 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13566 SourceLocation QueuesLoc = readSourceLocation();
13567 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13568 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13569 DevNumExpr, QueuesLoc, QueueIdExprs,
13570 EndLoc);
13571 }
13572 case OpenACCClauseKind::DeviceType:
13573 case OpenACCClauseKind::DType: {
13574 SourceLocation LParenLoc = readSourceLocation();
13575 llvm::SmallVector<DeviceTypeArgument> Archs;
13576 unsigned NumArchs = readInt();
13577
13578 for (unsigned I = 0; I < NumArchs; ++I) {
13579 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13580 SourceLocation Loc = readSourceLocation();
13581 Archs.emplace_back(Args&: Loc, Args&: Ident);
13582 }
13583
13584 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13585 LParenLoc, Archs, EndLoc);
13586 }
13587 case OpenACCClauseKind::Reduction: {
13588 SourceLocation LParenLoc = readSourceLocation();
13589 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13590 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13591 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13592
13593 for (unsigned I = 0; I < VarList.size(); ++I) {
13594 VarDecl *Recipe = readDeclAs<VarDecl>();
13595
13596 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13597 3 * sizeof(int *));
13598
13599 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13600 unsigned NumCombiners = readInt();
13601 for (unsigned I = 0; I < NumCombiners; ++I) {
13602 VarDecl *LHS = readDeclAs<VarDecl>();
13603 VarDecl *RHS = readDeclAs<VarDecl>();
13604 Expr *Op = readExpr();
13605
13606 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13607 }
13608
13609 RecipeList.push_back(Elt: {Recipe, Combiners});
13610 }
13611
13612 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13613 VarList, Recipes: RecipeList, EndLoc);
13614 }
13615 case OpenACCClauseKind::Seq:
13616 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13617 case OpenACCClauseKind::NoHost:
13618 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13619 case OpenACCClauseKind::Finalize:
13620 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13621 case OpenACCClauseKind::IfPresent:
13622 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13623 case OpenACCClauseKind::Independent:
13624 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13625 case OpenACCClauseKind::Auto:
13626 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13627 case OpenACCClauseKind::Collapse: {
13628 SourceLocation LParenLoc = readSourceLocation();
13629 bool HasForce = readBool();
13630 Expr *LoopCount = readSubExpr();
13631 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13632 HasForce, LoopCount, EndLoc);
13633 }
13634 case OpenACCClauseKind::Tile: {
13635 SourceLocation LParenLoc = readSourceLocation();
13636 unsigned NumClauses = readInt();
13637 llvm::SmallVector<Expr *> SizeExprs;
13638 for (unsigned I = 0; I < NumClauses; ++I)
13639 SizeExprs.push_back(Elt: readSubExpr());
13640 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13641 SizeExprs, EndLoc);
13642 }
13643 case OpenACCClauseKind::Gang: {
13644 SourceLocation LParenLoc = readSourceLocation();
13645 unsigned NumExprs = readInt();
13646 llvm::SmallVector<OpenACCGangKind> GangKinds;
13647 llvm::SmallVector<Expr *> Exprs;
13648 for (unsigned I = 0; I < NumExprs; ++I) {
13649 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13650 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13651 Exprs.push_back(Elt: readExpr());
13652 }
13653 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13654 GangKinds, IntExprs: Exprs, EndLoc);
13655 }
13656 case OpenACCClauseKind::Worker: {
13657 SourceLocation LParenLoc = readSourceLocation();
13658 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13659 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13660 IntExpr: WorkerExpr, EndLoc);
13661 }
13662 case OpenACCClauseKind::Vector: {
13663 SourceLocation LParenLoc = readSourceLocation();
13664 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13665 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13666 IntExpr: VectorExpr, EndLoc);
13667 }
13668 case OpenACCClauseKind::Link: {
13669 SourceLocation LParenLoc = readSourceLocation();
13670 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13671 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13672 EndLoc);
13673 }
13674 case OpenACCClauseKind::DeviceResident: {
13675 SourceLocation LParenLoc = readSourceLocation();
13676 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13677 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13678 LParenLoc, VarList, EndLoc);
13679 }
13680
13681 case OpenACCClauseKind::Bind: {
13682 SourceLocation LParenLoc = readSourceLocation();
13683 bool IsString = readBool();
13684 if (IsString)
13685 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13686 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13687 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13688 ID: readIdentifier(), EndLoc);
13689 }
13690 case OpenACCClauseKind::Shortloop:
13691 case OpenACCClauseKind::Invalid:
13692 llvm_unreachable("Clause serialization not yet implemented");
13693 }
13694 llvm_unreachable("Invalid Clause Kind");
13695}
13696
13697void ASTRecordReader::readOpenACCClauseList(
13698 MutableArrayRef<const OpenACCClause *> Clauses) {
13699 for (unsigned I = 0; I < Clauses.size(); ++I)
13700 Clauses[I] = readOpenACCClause();
13701}
13702
13703void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13704 unsigned NumVars = readInt();
13705 A->Clauses.resize(N: NumVars);
13706 readOpenACCClauseList(Clauses: A->Clauses);
13707}
13708
13709static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13710 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13711 // module name?
13712 llvm::FoldingSetNodeID ID;
13713 ID.AddString(String: PrimaryModuleName);
13714 return ID.computeStableHash();
13715}
13716
13717UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13718 if (!M)
13719 return std::nullopt;
13720
13721 if (M->isHeaderLikeModule())
13722 return std::nullopt;
13723
13724 if (M->isGlobalModule())
13725 return std::nullopt;
13726
13727 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13728 return getStableHashForModuleName(PrimaryModuleName);
13729}
13730